function changeCountry(oForm, sCountry)
{
	var oUSCA = oForm.state;
	var oOth = oForm.StateOther;
	var oCountry = oForm.country;

	switch (sCountry)
	{
		case 'United States':
		case 'Canada':
			oOth.disabled = true;
			oOth.style.backgroundColor = "#E2E2E2";
			oCountry.disabled = false;
			oCountry.style.backgroundColor = "#E2E2E2";
			break;

		case '':
		default:
			sCountry = 'other'
			oOth.disabled = false;
			oCountry.disabled = false;
			oCountry.style.backgroundColor = "#FFFFFF";
			oOth.style.backgroundColor = "#FFFFFF";
			break;
	}	
	
	if(!(oUSCA.options[oUSCA.selectedIndex].id == sCountry)) {
		for(x=0; x < oUSCA.length; x++) {
			if(oUSCA.options[x].id == sCountry) {
				oUSCA.selectedIndex = x;
				break;
			}
		}
	}
}

function changeStateProv(oForm, oUSCA)
{
	var oOth = oForm.StateOther;
	var oCountry = oForm.country;
	var sCountry = oCountry.options[oCountry.selectedIndex].value;
	var sUSCA = oUSCA.options[oUSCA.selectedIndex].id;

	switch (sUSCA)
	{
		case 'other':
			oOth.disabled = false;
			oOth.style.backgroundColor = "#FFFFFF";
			oCountry.disabled = false;
			oCountry.style.backgroundColor = "#FFFFFF";
			
			if(sCountry == 'United States' || sCountry == 'Canada') { oCountry.selectedIndex = 0; }
			break;

		case 'United States':
		case 'Canada':
			oOth.disabled = true;
			oOth.style.backgroundColor = "#E2E2E2";
			oCountry.disabled = false;
			oCountry.style.backgroundColor = "#E2E2E2";

			for(x=0; x < oCountry.length; x++) {
				if(oCountry.options[x].value == sUSCA) {
					oCountry.selectedIndex = x;
					break;
				}
			}
			break;
		case '':
		default:
			// do nothing
	}
}

function countryStateCheck()
{
	var oForm = document.forms[0];

	var oUSCA = oForm.state;

	changeStateProv(oForm, oUSCA);

	var oCountry = oForm.country;
	var sCountry = oCountry.options[oCountry.selectedIndex].value;

	changeCountry(oForm, sCountry);
}



function MM_findObj(n, d) { //v4.01
	var p, i, x;
	if(!d) 
		d=document; 
	if((p=n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
	if( !(x=d[n]) && d.all)
		x = d.all[n]; 
	for(i=0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
	for(i=0; !x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n, d.layers[i].document);
	if(!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

function MM_preloadImages() { //v3.0
	var d = document; 
	if(d.images){ 
	  	if(!d.MM_p) {
	  		d.MM_p = new Array();
	  	}
		var i;
		var j = d.MM_p.length;
		var a = MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++) {
			if (a[i].indexOf("#") != 0) { 
				d.MM_p[j] = new Image;
				d.MM_p[j++].src=a[i];
			}
		}
	}
	return true;
}

function MM_swapImgRestore() { //v3.0
	var i, x;
	var a = document.MM_sr; 
	for(i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) {
		x.src=x.oSrc;
	}
	return true;
}

function MM_swapImage() { //v3.0
	var i, x;
	var j = 0;
	var a = MM_swapImage.arguments; 
	document.MM_sr = new Array(); 
	for(i = 0; i < (a.length-2); i+=3) {
		if ((x=MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if(!x.oSrc) {
				x.oSrc=x.src;
			}
			x.src = a[i+2];
		}
	}
	return true;
}

function PopUp(url) {
	newwindow = window.open(url, 'anew', config='height=500,width=750,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=no');
}

function PopUpResize(url) {
	newwindow = window.open(url, 'anew', config='height=600,width=800,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
}

function PopUpReport(url) {
	newwindow = window.open(url, 'newWin', config='height=355,width=235,screenX=350,screenY=760,top=350,left=750,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
}

function PopUpSized(url, w, h, iResize, iScroll) {
	// date used for a unique window name so that all new windows open up seperately
	var d = new Date()
	
	// center the window in the screen
	var L = (screen.availWidth - w) / 2;
	var T = (screen.availHeight - h) / 2;

	newwindow = window.open(url, 'nw'+d.getTime(), config='height=' + h + ',width=' + w + ',top=' + T + ',left=' + L + ',toolbar=no,menubar=no,scrollbars=' + iScroll + ',resizable=' + iResize + ',location=no,directories=no,status=no');
	newwindow.focus();
}


function ValidateEmail(sEmail, iList)
{
	var bRet = true;
	var aEmail = new Array();
	
	if(iList == 1){
		// split the list of email addresses into an array
		if(sEmail.indexOf(';') >= 0) {
		
			alert("The list of email address contains an invalid semi-colon character. Please only separate email addresses with a comma.");

			return false;
		}

		aEmail = sEmail.split(',');
	} else {
		
		aEmail[0] = sEmail;
	
	}

	for(x=0; x < aEmail.length; x++) {
		sEmail = trim(aEmail[x]);
		if(!isValidEmail(sEmail))
		{
			bRet = false;
			alert("Email address (" + sEmail + ") is invalid");
			break;
		} else {
			bRet = true;
			//alert("Email address (" + trim(sEmail) + ") is valid!");
		}
	}
	
	return bRet;

}

function trim(s) {
	return String(s).replace(/^\s+|\s+$/g,"").replace(/\s+/g," ");
}

function isValidEmail(sEmail)
{
	//-- Using Fancy Handy Dandy Regular Expressions to validate email addresses
	/*
	The name portion of the email (before the @) allows for all alphanumeric characters
	as well as some special characters with the exception of these:
		! @ # $ % ! ) ( ` , : ; > | < \ ] [ space
	
	There must be a @ character, and it cannot be immediately preceeded or followed by a dot.
	
	The address portion of the email (after the @) allows for all characters (in order to support IDNA).
	The Top Level Domain (.com, .net, etc) must exist at the end and must be alphabetical 
	characters with a minimum length of 2 characters.
	*/
	
	bRet = false;
	
	if(window.RegExp) {
		// regular expression validation of email address
		// it's complicated but takes all into account
	
		var filter = /^([^!@#$%!)(`,:;>|<\\\]\[\s]*)+@[^\.][^@]*\.([a-z]|[A-Z]){2,}$/;
	
		if(sEmail.match(filter)) {
			bRet = true;
		}
	} else {
		// for browser backwards compatibility. basic check: 
		// 1-		@ exists and is not first character
		// 2-		@ is in the email only once
		// 3-		. (dot) exists after the @
		// 4-		no . (dot) immediately adjacent to the @ character
		// 5-		TLD is at least two characters (dot is at least three from end)
		
		if(	sEmail.indexOf("@") > 0 
			&& sEmail.indexOf("@") == sEmail.lastIndexOf("@")
			&& sEmail.charAt(sEmail.indexOf("@")+1) != "."
			&& sEmail.charAt(sEmail.indexOf("@")-1) != "."
			&& sEmail.lastIndexOf(".") > sEmail.indexOf("@") 
			&& (sEmail.lastIndexOf(".")) <= (sEmail.length-3)
		) bRet = true;
	}
	
	return bRet;
	
}
