var url = window.location.href;
url = url.substring(0,(url.lastIndexOf('/'))); 
document.write("<style>");
document.write("#moButton,#yrButton{");
document.write("background:transparent url("+url+"/images/menubar.jpg) no-repeat left top;");
document.write("}");
document.write("</style>");

function checkForm(){
	var error="0";

	if (document.dataForm.mo.selectedIndex<0 || document.dataForm.yr.selectedIndex<0 || document.dataForm.fn.value == "First Name" || document.dataForm.ln.value == "Last Name"){
		document.getElementById("errorMessage").innerHTML = "Please fill out all required fields.";
		error="1";
	}
	
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);

	if (document.dataForm.em.value == "Email"){
		document.getElementById("errorMessage").innerHTML = "Please type a correct email address.";
		error="1";
	} else if (! regex.test((document.dataForm.em.value).trim())) {
		document.getElementById("errorMessage").innerHTML = "Please type a correct email address.";
		error="1";
	}
	
	var usValue="", caValue="", mxValue="";
	for(var i=0; i<document.dataForm.us.length; i++){
		if(document.dataForm.us[i].checked){
			usValue += document.dataForm.us[i].value + ",";
		}
	}
	if(usValue.length>0){
		usValue = usValue.substring(0,usValue.length-1);
	}
	
	for(var j=0; j<document.dataForm.ca.length; j++){
		if(document.dataForm.ca[j].checked){
			caValue += document.dataForm.ca[j].value + ",";
		}
	}
	if(caValue.length>0){
		caValue = caValue.substring(0,caValue.length-1);
	}
	
	for(var k=0; k<document.dataForm.mx.length; k++){
		if(document.dataForm.mx[k].checked){
			mxValue += document.dataForm.mx[k].value + ",";
		}
	}
	if(mxValue.length>0){
		mxValue = mxValue.substring(0,mxValue.length-1);
	}
	
	if(usValue=="" && caValue=="" && mxValue==""){
		document.getElementById("errorMessage").innerHTML = "Please select at least one location.";
		error="1";
	}
	
	if(error=="0"){
		var moValue = document.dataForm.mo.options[document.dataForm.mo.selectedIndex].value;
		var yrValue = document.dataForm.yr.options[document.dataForm.yr.selectedIndex].value; 
		var phoneValue = (document.dataForm.ph.value).trim();
		if(document.dataForm.MonthlyEmail.checked) document.dataForm.MonthlyEmail.value = "1";
		if(phoneValue=="Phone (Optional)") phoneValue = "";
		
		var query = location.search.substring(1);
		var args = query.split('&');
		var stValue = "";
		for(var m=0; m < args.length; m++){
			var temp = args[m].substring(0,3);
			if(temp == "st="){
				stValue = args[m].substring(3,args[m].length);
			}
		}
		if(stValue!="01"&&stValue!="02"&&stValue!="03"){
			stValue = "";
		}
		//var ifr = document.getElementById("iframeForm");
		//var iwin = ifr.contentWindow || ifr.contentDocument;
		//var iform = iwin.document.iframeData;
		var iform = window.frames['iframeForm'].document.iframeData;
		
		iform.mo.value = moValue;
		iform.yr.value = yrValue;
		iform.uc.value = usValue;
		iform.cc.value = caValue;
		iform.mc.value = mxValue;
		iform.em.value = (document.dataForm.em.value).trim();
		iform.fn.value = (document.dataForm.fn.value).trim();
		iform.ln.value = (document.dataForm.ln.value).trim();
		iform.ph.value = phoneValue;
		iform.MonthlyEmail.value = document.dataForm.MonthlyEmail.value;
		iform.st.value = stValue;
		iform.submit();
		hide("formContent"); show("thankContent");
		initForm();
		
	}
}

function initForm(){
	document.dataForm.em.value = "Email";
	document.dataForm.fn.value = "First Name";
	document.dataForm.ln.value = "Last Name";
	document.dataForm.ph.value = "Phone (Optional)";
	document.dataForm.MonthlyEmail.checked = false;
	document.dataForm.mo.selectedIndex = -1;
	document.dataForm.yr.selectedIndex = -1;
	for(var i=0; i<document.dataForm.us.length; i++){
		document.dataForm.us[i].checked = false;
	}
	for(var j=0; j<document.dataForm.ca.length; j++){
		document.dataForm.ca[j].checked = false;
	}
	for(var k=0; k<document.dataForm.mx.length; k++){
		document.dataForm.mx[k].checked = false;
	}
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

function assignValue(element, v){
	document.getElementById(element).innerHTML = "<span>"+v+"</span>";
}
function show(element){
	document.getElementById(element).style.display = "block";
}
function hide(element){
	document.getElementById(element).style.display = "none";
}
