/** C O N S T A N T S  **/

var PNG_TRANSPARENT_GIF_IMAGE_PATH = 'globalImages/transparent.gif';

var LOAD_CIRCLE_ROTATION_AMOUNT_DEG = 10;
var LOAD_CIRCLE_ROTATION_TIMEOUT_MS = 50;

var FORMBACKEND_LOCATION = 'includes/form_backend.php';

var DEBUG_WINDOW_ELEMENT_ID = "debug_window";

/** G L O B A L S **/

/** gHasRuntimeStyle - not a function, but checks to see how we will handle PNG files  **/
var gHasRuntimeStyle = false;
if(document.body.runtimeStyle) {
	gHasRuntimeStyle = true;
}

var gLoadCirclePosition = 350;
var gLoadCircleTimer;

var gOldOnLoad = window.onload;
//window.onload = function(){ gOldOnLoad(); loadCirclePreLoad(); }

var gLoadCircleImageArray = new Array();

function loadCirclePreLoad() {
	for(var count = 0; count < 360; count += LOAD_CIRCLE_ROTATION_AMOUNT_DEG) {
		gLoadCircleImageArray[count] = new Image();
		if(gHasRuntimeStyle) {
			gLoadCircleImageArray[count].src = 'globalImages/transparent.gif';
			gLoadCircleImageArray[count].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/load_circle/load_circle_" + count + ".png', sizingMethod='image')";
		}
		else {
			gLoadCircleImageArray[count].src = 'globalImages/load_circle/load_circle_' + count + '.png';
		}
	}
}

/** F U N C T I O N S  **/

function swapDisplay(id) {
	if (document.getElementById(id).style.display == "none")
        	document.getElementById(id).style.display = "";
	else
        	document.getElementById(id).style.display = "none";
}

function checkSpouse(selectValue) {
	if (selectValue == "Married")
        	document.getElementById('spouseInformation').style.display = "";
	else
        	document.getElementById('spouseInformation').style.display = "none";
}

function ge(elementid) {
	return getElement(elementid);
}

function createLoadCircle() {
	var fnLoadCircleBackground = document.createElement("div");
	fnLoadCircleBackground.style["position"] = "absolute";
	fnLoadCircleBackground.style["top"] = "0";
	fnLoadCircleBackground.style["left"] = "0";
	fnLoadCircleBackground.style["width"] = document.body.scrollWidth;
	fnLoadCircleBackground.style["height"] = document.body.scrollHeight;
	if(gHasRuntimeStyle) {
		fnLoadCircleBackground.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/other/black_60op.png', sizingMethod='scale')";
	}
	else {
		fnLoadCircleBackground.style["background"] = "url(globalImages/other/black_60op.png)";
	}
	
	var fnLoadCircleShell = document.createElement("div");
	fnLoadCircleShell.style["border"] = "none";
	fnLoadCircleShell.style["position"] = "absolute";
	fnLoadCircleShell.style["width"] = "100px";
	fnLoadCircleShell.style["height"] = "100px";
	fnLoadCircleShell.style["left"] = parseInt(parseInt(parseInt(getWindowSize()[0]) / 2) + parseInt(getScrollXY()[0]) - parseInt(parseInt(fnLoadCircleShell.style["width"]) / 2)) + "px";
	fnLoadCircleShell.style["top"] = parseInt(parseInt(parseInt(getWindowSize()[1]) / 2) + parseInt(getScrollXY()[1]) - parseInt(parseInt(fnLoadCircleShell.style["height"]) / 2)) + "px";
	
	fnLoadCircleBackground.appendChild(fnLoadCircleShell);
	
	var fnLoadCircleImageContainer = document.createElement("div");
	fnLoadCircleImageContainer.id = "load_circle_image_container";

	fnLoadCircleShell.appendChild(fnLoadCircleImageContainer);
	
	var fnLoadCircle = document.createElement("img");
	if(gHasRuntimeStyle) {
		fnLoadCircle.src = "globalImages/transparent.gif";
		fnLoadCircle.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/load_circle/load_circle_0.png', sizingMethod='image')";
	}
	else {
		fnLoadCircle.src = "globalImages/load_circle/load_circle_0.png";
	}

	fnLoadCircleImageContainer.appendChild(fnLoadCircle);
	
	var fnSavingImage = document.createElement("img");
	if(gHasRuntimeStyle) {
		fnSavingImage.src = "globalImages/transparent.gif";
		fnSavingImage.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/load_circle/saving.png', sizingMethod='image')";
	}
	else {
		fnSavingImage.src = "globalImages/load_circle/saving.png";
	}
	fnSavingImage.style["margin"] = "12 0 0 0px";
	
	fnLoadCircleShell.appendChild(fnSavingImage);
	
	document.body.appendChild(fnLoadCircleBackground);
	
	return fnLoadCircleBackground;
}	

function rotateLoadCircleStart() {
	gLoadCirclePosition -= LOAD_CIRCLE_ROTATION_AMOUNT_DEG;
	
	if(gLoadCirclePosition < 0)
		gLoadCirclePosition = 350;
	
	getElement('load_circle_image_container').innerHTML = "";
	getElement('load_circle_image_container').appendChild(gLoadCircleImageArray[gLoadCirclePosition]);
	
	gLoadCircleTimer = setTimeout('rotateLoadCircleStart()', LOAD_CIRCLE_ROTATION_TIMEOUT_MS);
}

function rotateLoadCircleStop() {
	clearTimeout(gLoadCircleTimer);
}

function roundedPopupWindowCreate( aEvent, aWidth, aHeight, aContentElement ) {
	if(!aContentElement) {
		aContentElement = document.createElement("div");
		aContentElement.style["display"] = "none";
	}
	
	fnMousePositionArray = getMousePosition(aEvent);

	var fnRoundedPopupWIndow = document.createElement("div");
	fnRoundedPopupWIndow.className = "rounded_popup_window";
	fnRoundedPopupWIndow.style["width"] = aWidth + "px";
	fnRoundedPopupWIndow.style["height"] = aHeight + "px";
	fnRoundedPopupWIndow.style["left"] = parseInt(fnMousePositionArray[0]) - (parseInt(fnRoundedPopupWIndow.style["width"]) / 2) + "px";
	fnRoundedPopupWIndow.style["top"] = parseInt(fnMousePositionArray[1]) - (parseInt(fnRoundedPopupWIndow.style["height"]) / 2) + "px";
	
	var fnRoundedPopupWindowCornerUL = document.createElement("div");
	fnRoundedPopupWindowCornerUL.className = "rounded_popup_window_corn_ul";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowCornerUL);
	
	var fnRoundedPopupWindowCornerULImage = document.createElement("img");
	if(gHasRuntimeStyle) {
		fnRoundedPopupWindowCornerULImage.src = 'globalImages/transparent.gif';
		fnRoundedPopupWindowCornerULImage.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/roundBox/round_box_c_ul.png', sizingMethod='image')";
	}
	else {
		fnRoundedPopupWindowCornerULImage.src = "globalImages/roundBox/round_box_c_ul.png";
	}
	fnRoundedPopupWindowCornerULImage.width = "3";
	fnRoundedPopupWindowCornerULImage.height = "3";
	fnRoundedPopupWindowCornerULImage.border = "0";
	fnRoundedPopupWindowCornerUL.appendChild(fnRoundedPopupWindowCornerULImage);
	
	var fnRoundedPopupWindowCornerUR = document.createElement("div");
	fnRoundedPopupWindowCornerUR.className = "rounded_popup_window_corn_ur";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowCornerUR);
	
	var fnRoundedPopupWindowCornerURImage = document.createElement("img");
	if(gHasRuntimeStyle) {
		fnRoundedPopupWindowCornerURImage.src = 'globalImages/transparent.gif';
		fnRoundedPopupWindowCornerURImage.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/roundBox/round_box_c_ur.png', sizingMethod='image')";
	}
	else {
		fnRoundedPopupWindowCornerURImage.src = "globalImages/roundBox/round_box_c_ur.png";
	}
	fnRoundedPopupWindowCornerURImage.width = "3";
	fnRoundedPopupWindowCornerURImage.height = "3";
	fnRoundedPopupWindowCornerURImage.border = "0";
	fnRoundedPopupWindowCornerUR.appendChild(fnRoundedPopupWindowCornerURImage);
	
	var fnRoundedPopupWindowCornerLR = document.createElement("div");
	fnRoundedPopupWindowCornerLR.className = "rounded_popup_window_corn_lr";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowCornerLR);
	
	var fnRoundedPopupWindowCornerLRImage = document.createElement("img");
	if(gHasRuntimeStyle) {
		fnRoundedPopupWindowCornerLRImage.src = 'globalImages/transparent.gif';
		fnRoundedPopupWindowCornerLRImage.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/roundBox/round_box_c_lr.png', sizingMethod='image')";
	}
	else {
		fnRoundedPopupWindowCornerLRImage.src = "globalImages/roundBox/round_box_c_lr.png";
	}
	fnRoundedPopupWindowCornerLRImage.width = "3";
	fnRoundedPopupWindowCornerLRImage.height = "3";
	fnRoundedPopupWindowCornerLRImage.border = "0";
	fnRoundedPopupWindowCornerLR.appendChild(fnRoundedPopupWindowCornerLRImage);
	
	var fnRoundedPopupWindowCornerLL = document.createElement("div");
	fnRoundedPopupWindowCornerLL.className = "rounded_popup_window_corn_ll";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowCornerLL);
	
	var fnRoundedPopupWindowCornerLLImage = document.createElement("img");
	if(gHasRuntimeStyle) {
		fnRoundedPopupWindowCornerLLImage.src = 'globalImages/transparent.gif';
		fnRoundedPopupWindowCornerLLImage.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/roundBox/round_box_c_ll.png', sizingMethod='image')";
	}
	else {
		fnRoundedPopupWindowCornerLLImage.src = "globalImages/roundBox/round_box_c_ll.png";
	}
	fnRoundedPopupWindowCornerLLImage.width = "3";
	fnRoundedPopupWindowCornerLLImage.height = "3";
	fnRoundedPopupWindowCornerLLImage.border = "0";
	fnRoundedPopupWindowCornerLL.appendChild(fnRoundedPopupWindowCornerLLImage);
	
	var fnRoundedPopupWindowBorderT = document.createElement("div");
	fnRoundedPopupWindowBorderT.className = "rounded_popup_window_bord_t";
	fnRoundedPopupWindowBorderT.style["width"] = parseInt( parseInt(aWidth) - parseInt(6) ) + "px";
	fnRoundedPopupWindowBorderT.style["height"] = "3px";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowBorderT);

	var fnRoundedPopupWindowBorderR = document.createElement("div");
	fnRoundedPopupWindowBorderR.className = "rounded_popup_window_bord_r";
	fnRoundedPopupWindowBorderR.style["width"] = "3px";
	fnRoundedPopupWindowBorderR.style["height"] = parseInt( parseInt(aHeight) - parseInt(6) ) + "px";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowBorderR);
	
	var fnRoundedPopupWindowBorderB = document.createElement("div");
	fnRoundedPopupWindowBorderB.className = "rounded_popup_window_bord_b";
	fnRoundedPopupWindowBorderB.style["width"] = parseInt( parseInt(aWidth) - parseInt(6) ) + "px";
	fnRoundedPopupWindowBorderB.style["height"] = "3px";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowBorderB);
	
	var fnRoundedPopupWindowBorderL = document.createElement("div");
	fnRoundedPopupWindowBorderL.className = "rounded_popup_window_bord_l";
	fnRoundedPopupWindowBorderL.style["width"] = "3px";
	fnRoundedPopupWindowBorderL.style["height"] = parseInt( parseInt(aHeight) - parseInt(6) ) + "px";
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowBorderL);

	var fnRoundedPopupWindowCont = document.createElement("div");
	fnRoundedPopupWindowCont.className = "rounded_popup_window_cont";
	fnRoundedPopupWindowCont.style["width"] = parseInt( parseInt(aWidth) - parseInt(6) ) + "px";
	fnRoundedPopupWindowCont.style["height"] = parseInt( parseInt(aHeight) - parseInt(6) ) + "px";
	fnRoundedPopupWindowCont.appendChild(aContentElement);
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowCont);
	
	var fnRoundedPopupWindowCloseContainer = document.createElement("div");
	fnRoundedPopupWindowCloseContainer.className = "rounded_popup_window_close_container";
	fnRoundedPopupWindowCloseContainer.onclick = function(){this.parentNode.parentNode.removeChild(this.parentNode);}
	fnRoundedPopupWIndow.appendChild(fnRoundedPopupWindowCloseContainer);
	
	var fnRoundedPopupWindowCloseX = document.createElement("div");
	fnRoundedPopupWindowCloseX.className = "rounded_popup_window_close_x";
	fnRoundedPopupWindowCloseContainer.appendChild(fnRoundedPopupWindowCloseX);
	
	var fnRoundedPopupWindowCloseXImage = document.createElement("img");
	if(gHasRuntimeStyle) {
		fnRoundedPopupWindowCloseXImage.src = 'globalImages/transparent.gif';
		fnRoundedPopupWindowCloseXImage.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/close_x.png', sizingMethod='image')";
	}
	else {
		fnRoundedPopupWindowCloseXImage.src = "globalImages/close_x.png";
	}		
	fnRoundedPopupWindowCloseXImage.width = "9";
	fnRoundedPopupWindowCloseXImage.height = "9";
	fnRoundedPopupWindowCloseXImage.border = "0";
	fnRoundedPopupWindowCloseX.appendChild(fnRoundedPopupWindowCloseXImage);
			
	document.body.appendChild(fnRoundedPopupWIndow);
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return [myWidth,myHeight];
}

function getMousePosition(e)
{
	var returnArray = new Array(2);
	
	var mouseX = 0;
	var mouseY = 0;

	if (!e) var e = window.event;
	
	if (e.pageX || e.pageY)
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		mouseX = e.clientX + document.body.scrollLeft;
		mouseY = e.clientY + document.body.scrollTop;
	}
	
	return [mouseX, mouseY];
}

function numericPadSet(aTargetElement, aDisplayValue) {
	aTargetElement.value = aDisplayValue;
	
	if( aTargetElement.type == "hidden" ) {
		getElement(aTargetElement.id + "_display").innerHTML = aTargetElement.value;
	}
}


function numericPadCreate(aTargetElement, aMaxLength, aIsPassword) {

	function numericPadClose(aTargetElement, aNumericPad) {
		var aDisplayValue = aNumericPad.childNodes[0].innerHTML;

		aTargetElement.value = aDisplayValue;
		if( aTargetElement.type == "hidden" ) {
			getElement(aTargetElement.id + "_display").innerHTML = aTargetElement.value;
		}
		
		if(aNumericPad) {
			aNumericPad.parentNode.parentNode.parentNode.removeChild(aNumericPad.parentNode.parentNode);
		}
	}

	if(!aMaxLength) {
		aMaxLength = 0;
	}
	
	var fnNumericPad = document.createElement("div");
	fnNumericPad.className = "numeric_pad";
	
	var fnNumericPadDisplay = document.createElement("div");
	fnNumericPadDisplay.className = "numeric_pad_display";
	fnNumericPad.appendChild(fnNumericPadDisplay);
	
	for( var countN = 3; countN > 0; countN-- ) {
		for( var countM = 0; countM < 3; countM++ ) {
			var fnNumericPadNumber = document.createElement("img");		
			fnNumericPadNumber.currentNumber = parseInt(-2 + (3 * countN) + countM);
			fnNumericPadNumber.className = "numeric_pad_number";
			fnNumericPadNumber.src = "globalImages/numericPad/numeric_pad_" + fnNumericPadNumber.currentNumber + ".png";
			
			if(gHasRuntimeStyle) {
				fnNumericPadNumber.src = 'globalImages/transparent.gif';
				fnNumericPadNumber.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/numericPad/numeric_pad_" + fnNumericPadNumber.currentNumber + ".png', sizingMethod='image')";
			}
			else {
				fnNumericPadNumber.src = "globalImages/numericPad/numeric_pad_" + fnNumericPadNumber.currentNumber + ".png";
			}		
			
			fnNumericPadNumber.border = 0;
			fnNumericPadNumber.width = 37;
			fnNumericPadNumber.height = 32;
			fnNumericPadNumber.onclick = function(){
				this.parentNode.childNodes[0].innerHTML += this.currentNumber;
				if( (aMaxLength > 0) && (this.parentNode.childNodes[0].innerHTML.length >= aMaxLength) ) {
					numericPadClose( aTargetElement, fnNumericPad );
				}
			}
			
			fnNumericPad.appendChild(fnNumericPadNumber);
		}
	}

	var fnNumericPadNumber = document.createElement("img");			
	fnNumericPadNumber.className = "numeric_pad_number";
	if(gHasRuntimeStyle) {
		fnNumericPadNumber.src = 'globalImages/transparent.gif';
		fnNumericPadNumber.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/numericPad/numeric_pad_0.png', sizingMethod='image')";
	}
	else {
		fnNumericPadNumber.src = "globalImages/numericPad/numeric_pad_0.png";
	}		
	fnNumericPadNumber.border = 0;
	fnNumericPadNumber.width = 37;
	fnNumericPadNumber.height = 32;
	fnNumericPadNumber.onclick = function(){
		this.parentNode.childNodes[0].innerHTML += "0";
		if( (aMaxLength > 0) && (this.parentNode.childNodes[0].innerHTML.length >= aMaxLength) ) {
			numericPadClose( aTargetElement, fnNumericPad );
		}
	}
	fnNumericPad.appendChild(fnNumericPadNumber);

	var fnNumericPadNumber = document.createElement("img");		
	fnNumericPadNumber.className = "numeric_pad_number";
	if(gHasRuntimeStyle) {
		fnNumericPadNumber.src = 'globalImages/transparent.gif';
		fnNumericPadNumber.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/numericPad/numeric_pad_c.png', sizingMethod='image')";
	}
	else {
		fnNumericPadNumber.src = "globalImages/numericPad/numeric_pad_c.png";
	}		
	fnNumericPadNumber.border = 0;
	fnNumericPadNumber.width = 37;
	fnNumericPadNumber.height = 32;
	fnNumericPadNumber.onclick = function(){ fnNumericPadNumber.parentNode.childNodes[0].innerHTML = ""; }
	fnNumericPad.appendChild(fnNumericPadNumber);
	
	var fnNumericPadNumber = document.createElement("img");		
	fnNumericPadNumber.className = "numeric_pad_number";
	if(gHasRuntimeStyle) {
		fnNumericPadNumber.src = 'globalImages/transparent.gif';
		fnNumericPadNumber.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='globalImages/numericPad/numeric_pad_e.png', sizingMethod='image')";
	}
	else {
		fnNumericPadNumber.src = "globalImages/numericPad/numeric_pad_e.png";
	}		
	fnNumericPadNumber.border = 0;
	fnNumericPadNumber.width = 37;
	fnNumericPadNumber.height = 32;
	fnNumericPadNumber.onclick = function(){ numericPadClose( aTargetElement, fnNumericPad ); }
	fnNumericPad.appendChild(fnNumericPadNumber);
	
	return fnNumericPad;
}

function rotateOptionBoxClick(aBaseID) {
	var currentIndex = parseInt(getElement(aBaseID + "_current_index").value);
	var maxIndex = getElement(aBaseID + "_max_index").value;
	
	currentIndex++;
	
	if(currentIndex > maxIndex)
		currentIndex = 0;

	rotateOptionBoxSet(aBaseID, currentIndex);
}

function rotateOptionBoxSet(aBaseID, aIndex) {
	getElement(aBaseID).value = gRotateOptionBox[aBaseID].children[aIndex];
	
	getElement(aBaseID + "_current_index").value = aIndex;
	
	getElement(aBaseID + "_display").innerHTML = gRotateOptionBox[aBaseID].children[aIndex];	
}

function rotateOptionBoxGroupChange(aClassName, aValue) {
	/* get all the input elements */
	fnInputElements = document.getElementsByTagName('input');
	
	/* loop through them all */
	for( var count = 0; count < fnInputElements.length; count++ ) {

		/* find only the rotate option box elements ( "rotate_option_box_data" ) and aClassName are in the className */
		if( (fnInputElements[count].className.indexOf("rotate_option_box_data") >= 0) && (fnInputElements[count].className.indexOf(aClassName) >= 0) ) {
	
			/* loop through the children */
			for( var inner_count = 0; inner_count < gRotateOptionBox[fnInputElements[count].id].children.length; inner_count++ ) {
				
				/* find the index of the value we are working with */
				if( gRotateOptionBox[fnInputElements[count].id].children[inner_count] == aValue ) {
				
					/* set rotate option box */
					rotateOptionBoxSet(fnInputElements[count].id, inner_count);
				}
			}
		}
	}
}