/*
 * 	ExpertInsure - Client-side Javascript Functions
 *		
 *		include with: 	<script type="text/javascript" language="javascript" src="../ClientScripts/clientScripts.js"></script>
 *
 */


// Prevent Back Button Clicks //
history.forward();

// Call with:  radioButtonValue(document.form1.FieldName);
function getRadioButtonValue(objButtonName) {
    var intCnt = -1;
    for (var i=objButtonName.length-1; i > -1; i--) {
        if (objButtonName[i].checked) {intCnt = i; i = -1;}
    }
	
    if (intCnt > -1) {
		return objButtonName[intCnt].value;
	} else {
		return null;
	}
}
 
function CopyToClipboard(objFormElement) {
   objFormElement.focus();
   objFormElement.select();
   CopiedTxt = document.selection.createRange();
   CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(objFormElement) { 
   objFormElement.focus();
   PastedText = objFormElementea.createTextRange();
   PastedText.execCommand("Paste");
} 

function pausecomp(Amount) {
	d = new Date() 						//today's date
	while (1)
	{
		mill=new Date() 				// Date Now
		diff = mill-d 					//difference in milliseconds
		if( diff > Amount ) {break;}
	}
}

function OpenPopupCentered(strURL,intWidth,intHeight,blnResizable) {
	//only use left half for 2048 (rdp users)
	if (window.screen.availWidth == 2048) {
		intAvailWidth = 1024
	} else {
		intAvailWidth = window.screen.availWidth
	}
	
	if (blnResizable == true) {
		window.open(strURL,'','width=' + intWidth + ',height=' + intHeight + ',left=' + ((intAvailWidth / 2) - (intWidth / 2)) + ',top=' + ((window.screen.availHeight / 2) - (intHeight / 2)) + ',toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes')
	} else {
		window.open(strURL,'','width=' + intWidth + ',height=' + intHeight + ',left=' + ((intAvailWidth / 2) - (intWidth / 2)) + ',top=' + ((window.screen.availHeight / 2) - (intHeight / 2)) + ',toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no')
	}
}

function OpenDocumentCentered(strURL,blnResizable) {
	//only use left half for 2048 (rdp users)
	if (window.screen.availWidth == 2048) {
		intAvailWidth = 1024
	} else {
		intAvailWidth = window.screen.availWidth
	}
		
	if (blnResizable == true) {	
		window.open(strURL,'','width=' + (intAvailWidth - 100) + ',height=' + (window.screen.availHeight - 100) + ',left=50,top=25,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,resizable=yes')
	} else {
		window.open(strURL,'','width=' + (intAvailWidth - 100) + ',height=' + (window.screen.availHeight - 100) + ',left=50,top=25,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,resizable=no')
	}
}

function ResizeMe(intWidth,intHeight)
{
	//only use left half for 2048 (rdp users)
	if (window.screen.availWidth == 2048) {
		intAvailWidth = 1024
	} else {
		intAvailWidth = window.screen.availWidth
	}
	
	window.resizeTo(intWidth,intHeight);
	window.moveTo((intAvailWidth / 2)-(intWidth / 2),(screen.availHeight / 2)-(intHeight / 2));
}

function CurrencyFormatted(amount)
{
	try
	{
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return s;
	}
	catch(err)
	{
		alert(err.description + "\n\n    amount: " + amount);
	}
}

function NumberFormatted(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}



function enableFormFields() {
	try {
		for (i=0; i < document.forms.length; i++) {
			for (j=0; j < document.forms[i].elements.length; j++) {
				document.forms[i].elements[j].disabled = false;
				document.forms[i].elements[j].readonly = false;
			}
		}
	} catch (ex) {
		alert(ex.message);
	}
}