// General Javascript Utility Functions for Invizage

function showPopup(htmlFile, width, height, windowName) 
{
	var left = (screen.width-width)/2;
	var top = ((screen.height-height)/2)-20;
	//var windowName = '';
	
	if(typeof(windowName)=="undefined"){
		 windowName='Invizage'; }
	
	window.open(htmlFile, windowName,'top=' + top + ',left=' + left + ',resizeable=no,scrollbars=yes,status=no,width=' + width + ',height=' + height + ',hotkeys=0');
}	

function showPopupNoScroll(htmlFile, width, height, windowName) 
{
	var left = (screen.width-width)/2;
	var top = ((screen.height-height)/2)-20;
	//var windowName = '';
	
	if(typeof(windowName)=="undefined"){
		 windowName='Invizage'; }
	
	window.open(htmlFile, windowName,'top=' + top + ',left=' + left + ',resizeable=no,scrollbars=no,status=no,width=' + width + ',height=' + height + ',hotkeys=0');
}	

function showPrintPopup(htmlFile, width, height) 
{
	var left = (screen.width-width)/2;
	var top = ((screen.height-height)/2)-20;
	
	window.open(htmlFile, 'Invizage','top=' + top + ',left=' + left + ',resizeable=yes,scrollbars=yes,menubar=yes,status=yes,width=' + width + ',height=' + height + ',hotkeys=0');
}	

function printpage() 
{
	window.print();
}

function preload() 
{
 if (!document.images) return;
 var ar = new Array();
 var arguments = preload.arguments;
 var maxWidth = arguments[0];
 for (var i = 1; i < arguments.length; i++) {
   ar[i] = new Image();
   ar[i].src = arguments[i];
   scaleImage(ar[i],maxWidth);
   //ar[i].width = 300;
 }
}

function scaleImage(imgObject,maxSize)
//This function scales images so you can restrict the images height and width without
//using fixed values.  Stops image distortion
//Call this function on the onLoad event of an img tag and use the object 'this' as the Image object
//e.g <img src="test.jpg" onLoad="scaleImage(this,120)">

{
	var temp = imgObject.width;
	
    if (imgObject.height > imgObject.width)	
	{
		imgRatio = imgObject.width/imgObject.height;
		imgObject.height = maxSize;
	    imgObject.width = imgObject.width * imgRatio;
		//alert(imgObject.width)
	}
}

function roundOff(value, precision)
{
    value = "" + value //convert value to string
    precision = parseInt(precision);
 
    var whole = "" + Math.round(value * Math.pow(10, precision));
 
    var decPoint = whole.length - precision;
 
    if(decPoint > 0)
    {
       result = whole.substring(0, decPoint);
       result += "."; result += whole.substring(decPoint, whole.length);
    }
    else
    {
    result = whole / 100;
    } return result;
 }
 
function FormatCurrency(amount) 
{
	for (var i = 0; i < amount.length; i++)
	{
		var textFormat = "";
		var t=0;
		var str=""; 
		for (var j = 0; j < amount.length ; j++) 
		{
		amount=amount.replace(',','');
		}
	
		str=amount; 
		
		if (str.length != 0)
		{
			textFormat = str.substr(str.length-3,3);
			
			for (var k = str.length-4; k>=0 ; k--)
			{
				t++;
				if (t % 3 == 0)
				{ 
					textFormat = "," + str.substr(k,1) + textFormat;
				} 
				else 
				{
					textFormat =  str.substr(k,1) + textFormat;
				}
			} 
		
			if (textFormat.substr(0,1) == ",")
			{ 
				return textFormat.substr(1,textFormat.length-1);
			} 
			else 
			{ 
				return textFormat;
			}
		}
	} 
}

function trimString(str) 
// This function removes all whitespace in a string using regular expressions.
// Can be easily modifies to only remove leading and/or trailing spaces.
{
  str = this != window? this : str;
  return str.replace(/\s+/g, '').replace(/\s+/g, '');
}

function textCounter(field, countfield, maxlimit) 
//This function limits the number of characters entered into a text box
//and displays the number of characters left
{
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}


function val_radio(names, form_name, displayname)
{
	var radio_elements = names.split(':');

	
	for(var i = 0; i < radio_elements.length; i++)
	{
		var valid = false;
		
		for(var j = 0; j < eval("document." + form_name + "." + radio_elements[i] + ".length"); j++)
		{			
			if (eval("document." + form_name + "." + radio_elements[i] + "[j].checked")) 
			{
				valid = true;
				break;
			}
		}
		
		if (!valid) 
		{
			alert (displayname + " has not been answered.\nPlease answer it.");
			return false;
		}
		else
		{
			return true;
		}
		

	}
}
