function getXMLHTTP() { //function to return the xml http object
	var xmlhttp=false;
	try {
		xmlhttp=new XMLHttpRequest();
	}
	catch(e) {
		try {
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e) {
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1) {
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}

function updateInnerHTML(strURL,divElement,params,method,async) {
	var req = getXMLHTTP();
	if (req) {
		if (async) {
			// asynchronous mode
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {
						document.getElementById(divElement).innerHTML=req.responseText;
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText + " (" + req.status + ")");
					} // end if req.status == 200
				} // end if req.readyState == 4
			} // end function
			req.open(method, strURL, async);
			req.send(params);
		} else {
			// synchronous mode
			req.open(method, strURL, async);
			req.send(params);
			if (req.status == 200) {
				document.getElementById(divElement).innerHTML=req.responseText;
			} else {
				alert("There was a problem while using XMLHTTP:\n" + req.statusText + " (" + req.status + ")");
			} // end if req.status == 200
		} // end if else
	} // end if req
} // end function

function setStateOptions(country,state,addressType,className,formType) {
	//If Country is set to nothng, would like to reset the State/Prov Field to the default.
	//if (country == "") {
	//	var stateSpan = addressType+"State";
	//	document.getElementById(stateSpan).innerHTML= "<option value='' selected='selected'>Select a country first</option>";
	
	//} else {
		updateInnerHTML('/includes/getRegionType.php?country='+country+'&addressType='+addressType+'&class='+className+'&formType='+formType,addressType+'StateLabelSpan','','GET',true);
		updateInnerHTML('/includes/getRegionList.php?country='+country+'&field='+addressType+'State&state='+state,addressType+'StateSpan','','GET',true);
	//}
}

function setCountryCodes(country,workhome) {
	if (workhome == 'work' || workhome == 'both') {
		document.getElementById('WTC').value = countryCodes[country];
		document.getElementById('WFC').value = countryCodes[country];
	}
	if (workhome == 'home' || workhome == 'both') {
		document.getElementById('HTC').value = countryCodes[country];
		document.getElementById('HFC').value = countryCodes[country];
		document.getElementById('HCC').value = countryCodes[country];
	}
}




function toggleValue (inputElementName,labelElementName,currentValue) {
	/* input vars:
	inputElementName: name of an <input> or <select> element to be changed
	labelElementName: name of a <label> element whose class is to be changed
	currentValue: the value of the text that should be written or the <option> that should be selected
	*/
	// TODO: Figure out if it's better to use getElementById or getElementsByName or if it matters - use only one method
	var sourceElement = parent.pending.document.getElementById(inputElementName);
	var targetElement = document.getElementById(inputElementName);
	var labelElement = document.getElementById(labelElementName);
	if (targetElement.type == 'text') {
		if (labelElement.className == 'rejectedChange') {
			// action is to accept pending change
			setTextPending(sourceElement,targetElement,labelElement);
		} else if (labelElement.className == 'acceptedChange') {
			// action is to reject pending change, revert to existing value
			setTextCurrent(currentValue,targetElement,labelElement);
		} else {
			// should never get here?
		}
	} else if (targetElement.type == 'select-one') {
		if (labelElement.className == 'rejectedChange') {
			// action is to accept pending change
			setSelectPending(sourceElement,targetElement,labelElement);
		} else if (labelElement.className == 'acceptedChange') {
			// action is to reject pending change, revert to existing value
			setSelectCurrent(currentValue,targetElement,labelElement);
		} else {
			// should never get here?
		}
	} else {
		// should never get here?
	}
}

function toggleValueCountryState (addressType,currentValueCountry,currentValueState,formType) {
	/* input vars:
	addressType: type of address, either Work or Home
	currentValueCountry: the value of the <option> that should be selected
	currentValueState: the value of the text that should be written or the <option> that should be selected
	formType: type of form (from formType defined in PHP)
	*/
	// TODO: Figure out if it's better to use getElementById or getElementsByName or if it matters - use only one method
	var sourceElementCountry = parent.pending.document.getElementById(addressType+'Country');
	var targetElementCountry = document.getElementById(addressType+'Country');
	var labelElementCountry = document.getElementById(addressType+'CountryLabel');
	var sourceElementState = parent.pending.document.getElementById(addressType+'State');
	var targetElementState = document.getElementById(addressType+'State');
	var labelElementState = document.getElementById(addressType+'StateLabel');
	// country is always a select-one type, so change that first
	if (labelElementCountry.className == 'rejectedChange') {
		// action is to accept pending change
		setSelectPending(sourceElementCountry,targetElementCountry,labelElementCountry);
		if (!sourceElementState){//Certain countrys have NO state field (e.g. Hong Kong)
			//alert("NUll 2a");
			setStateOptions(sourceElementCountry.options[sourceElementCountry.selectedIndex].value,'',addressType,'a', formType);
			
		} else if (!targetElementState || (targetElementState.type == 'select-one' && sourceElementState.type == 'select-one')) {
			// both options are dropdowns, update state list
			setStateOptions(sourceElementCountry.options[sourceElementCountry.selectedIndex].value,sourceElementState.options[sourceElementState.selectedIndex].value,addressType,'a', formType);
			if (formType == 'C') {
				setCountryCodes(currentValueCountry,'Both');
			} else {
				setCountryCodes(currentValueCountry,addressType);
			}
		} else if (targetElementState.type == 'text' && sourceElementState.type == 'text') {
			//application and target have a text field.
			setTextPending(sourceElementState,targetElementState,labelElementState);
		} else if (targetElementState.type == 'text' && sourceElementState.type == 'select-one') {
			alert("Option 1a - Target text, source select-one");
		} else if (targetElementState.type == 'select-one' && sourceElementState.type == 'text') {
			setStateOptions(sourceElementCountry.options[sourceElementCountry.selectedIndex].value,sourceElementState.value,addressType,'a', formType);
		} else {
			//should never get here?	
			alert("ERROR: Could not define target or source element");
		}
		
	} else if (labelElementCountry.className == 'acceptedChange') {
		// action is to reject pending change, revert to existing value
		setSelectCurrent(currentValueCountry,targetElementCountry,labelElementCountry);
		// update state list
		setStateOptions(currentValueCountry,currentValueState,addressType,'r', formType);
		if (formType == 'C') {
			setCountryCodes(currentValueCountry,'Both');
		} else {
			setCountryCodes(currentValueCountry,addressType);
		}
		
	// The next two cases cover if country stays the same, but state is changed.
	} else if (labelElementState.className == 'rejectedChange') { //same country, different state
		// action is to accept pending change
		if (targetElementState.type == 'text') {
			setTextPending(sourceElementState,targetElementState,labelElementState);
		} else if (targetElementState.type == 'select-one') {
			setSelectPending(sourceElementState,targetElementState,labelElementState);
		} else {
			// should never get here?
		}
	} else if (labelElementState.className == 'acceptedChange') { //same country different state
		// action is to reject pending change, revert to existing value
		if (targetElementState.type == 'text') {
			setTextCurrent(currentValueState,targetElementState,labelElementState);
		} else if (targetElementState.type == 'select-one') {
			setSelectCurrent(currentValueState,targetElementState,labelElementState);
		} else {
			// should never get here?
		}
	} else {
		// should never get here?
		alert('ERROR: Unable to determine class of label element');
	}
}

function toggleValuePhone (inputElementPrefix,labelElementName,currentValueC,currentValueR,currentValueL,currentValueE) {
	/* input vars:
	inputElementPrefix: two-letter prefix of a set of <input> elements for a phone number
	labelElementName: name of a <label> element whose class is to be changed
	currentValueC: the value of the text that should be written into the Country field
	currentValueR: the value of the text that should be written into the Region field
	currentValueL: the value of the text that should be written into the Local field
	currentValueE: the value of the text that should be written into the Extension field
	*/
	// TODO: Figure out if it's better to use getElementById or getElementsByName or if it matters - use only one method
	var sourceElementC = parent.pending.document.getElementById(inputElementPrefix+'C');
	var targetElementC = document.getElementById(inputElementPrefix+'C');
	var sourceElementR = parent.pending.document.getElementById(inputElementPrefix+'R');
	var targetElementR = document.getElementById(inputElementPrefix+'R');
	var sourceElementL = parent.pending.document.getElementById(inputElementPrefix+'L');
	var targetElementL = document.getElementById(inputElementPrefix+'L');
	if (inputElementPrefix == 'WT') {
		var sourceElementE = parent.pending.document.getElementById(inputElementPrefix+'E');
		var targetElementE = document.getElementById(inputElementPrefix+'E');
	}
	var labelElement = document.getElementById(labelElementName);
	if (labelElement.className == 'rejectedChange') {
		// action is to accept pending change
		setTextPending(sourceElementC,targetElementC,labelElement);
		setTextPending(sourceElementR,targetElementR,labelElement);
		setTextPending(sourceElementL,targetElementL,labelElement);
		if (inputElementPrefix == 'WT') {
			setTextPending(sourceElementE,targetElementE,labelElement);
		}
	} else if (labelElement.className == 'acceptedChange') {
		// action is to reject pending change, revert to existing value
		setTextCurrent(currentValueC,targetElementC,labelElement);
		setTextCurrent(currentValueR,targetElementR,labelElement);
		setTextCurrent(currentValueL,targetElementL,labelElement);
		if (inputElementPrefix == 'WT') {
			setTextCurrent(currentValueE,targetElementE,labelElement);
		}
	} else {
		// should never get here?
	}
}

function toggleValueChapter (labelElementName) {
	/* input vars:
	labelElementName: name of a <label> element whose class is to be changed
	*/
	// TODO: Figure out if it's better to use getElementById or getElementsByName or if it matters - use only one method
	var labelElement = document.getElementById(labelElementName);
	if (labelElement.className == 'rejectedChange') {
		// action is to accept pending change
		labelElement.className = 'acceptedChange';
	} else if (labelElement.className == 'acceptedChange') {
		// action is to reject pending change, revert to existing value
		labelElement.className = 'rejectedChange';
	} else if (labelElement.className == 'standSelected') {
		// action is to reject pending change, revert to existing value
		labelElement.className = 'standbySelected';
	} else if (labelElement.className == 'standbySelected') {
		// 
		labelElement.className = 'standSelected';
	} else if (labelElement.className == '') {
		// If no class, make bold so you can tell what is being toggled.
		labelElement.className = 'standSelected';
	} else {
		// should never get here?
	}
}

function setTextPending (sourceElement,targetElement,labelElement) {
	// sets a text field's value to that from the pending record
	targetElement.value = sourceElement.value;
	labelElement.className = "acceptedChange";
}

function setTextCurrent (currentValue,targetElement,labelElement) {
	// sets a text field's value to that from the current record
	targetElement.value = currentValue;
	labelElement.className = "rejectedChange";
}

function setSelectPending (sourceElement,targetElement,labelElement) {
	// sets a select field's option to that from the pending record
	targetElement.selectedIndex = sourceElement.selectedIndex;
	labelElement.className = "acceptedChange";
}

function setSelectCurrent (currentValue,targetElement,labelElement) {
	// sets a select field's option to that from the current record
	setSelectByValue(targetElement,currentValue);
	labelElement.className = "rejectedChange";
}

function setSelectByValue (element,value) {
	/* input vars:
	element: a <select> element to be changed
	value: the value of the <option> that should be selected
	*/
	for (var i=0; i < element.length; i++) {
		if (element[i].value == value) {
			element.selectedIndex = i;
			break;
		}
	}
}

