// JavaScript Document

//tabs phone numbers
function autotab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
	destination.focus()
}
//copies address
function BillingAddress() {
	if (document.form1.SameAdd.checked) {
		document.form1.sub_bill_name.value = document.form1.sub_first.value + " " + document.form1.sub_last.value;
		document.form1.sub_bill_org.value = document.form1.sub_org.value;
		document.form1.sub_bill_email.value = document.form1.sub_email.value;
		document.form1.sub_bill_address.value = document.form1.sub_address.value;
		document.form1.sub_bill_city.value = document.form1.sub_city.value;
		document.form1.sub_bill_zip.value = document.form1.sub_zip.value;
		document.form1.sub_bill_state.value = document.form1.sub_state.value;

	}
	if (!document.form1.SameAdd.checked) {
		document.form1.sub_bill_name.disabled = false;
		document.form1.sub_bill_org.disabled = false;
		document.form1.sub_bill_email.disabled = false;
		document.form1.sub_bill_address.disabled = false;
		document.form1.sub_bill_city.disabled = false;
		document.form1.sub_bill_zip.disabled = false;
		document.form1.sub_bill_state.disabled = false;
	}
}
//updates Billing Address on address changes
function updateBilling(){
	if (document.form1.SameAdd.checked) {
		BillingAddress()
	}else{
		return false;
	}
	
}

//checks length on keystroke
function checkPassLength(){
	if(document.form1.sub_password.value.length>=5){
		document.getElementById('passOK').style.display = 'inline';
		}else{
		document.getElementById('passOK').style.display = 'none';
		}
}
// Checks length of password after finished
function checkPassFullLength(){
	var checkPass=0;
	if(document.form1.sub_password.value.length<5){checkPass=1;}
	if(document.form1.sub_password.value.length>10){checkPass=1;}
	if(checkPass==1){
		document.getElementById('passOK').style.display = 'none';
		document.form1.sub_password.value="";
		document.form1.sub_password.focus()
		alert("Your password must be 5-10 characters");
		
	}
	if(document.form1.sub_match_password.value.length>0){checkPassMatch()}
}

//Checks password matches
function checkPassMatch(){
	if(document.form1.sub_match_password.value!=document.form1.sub_password.value){
		document.form1.sub_match_password.value="";
		document.form1.sub_match_password.focus()
		document.getElementById('pass2OK').style.display = 'none';
		alert("Your passwords did not match, please try again.");
		return false;
		}
	if(document.form1.sub_match_password.value==document.form1.sub_password.value){
		document.getElementById('pass2OK').style.display = 'inline';
		return true;
	}
}

//Simple email validation - lets almost anything fly must have an "@" and a "."
function checkEmail(){
	if ((document.form1.sub_email.value.indexOf(".") > 2) && (document.form1.sub_email.value.indexOf("@") > 0)){
		document.getElementById('emailOK').style.display = 'inline';
		return true;
	}else{
		document.getElementById('emailOK').style.display = 'none';
		alert("Your email address does not appear to be valid.");
		form1.sub_email.focus()
	}
}


function submform() {	

	var themessage = "You are required to complete the following fields: ";
	var focusOn='';
	
	if (document.form1.sub_first.value=="") {
		if(!focusOn){document.form1.sub_first.focus(); focusOn='Y';}
		themessage = themessage + " - First Name";
		}
	if (document.form1.sub_last.value=="") {
		if(!focusOn){document.form1.sub_last.focus(); focusOn='Y';}
		themessage = themessage + " - Last Name";
		}
	if (document.form1.sub_email.value=="") {
		if(!focusOn){document.form1.sub_email.focus(); focusOn='Y';}
		themessage = themessage + " -  Email Address";
		}
	if (document.form1.sub_password.value=="") {
		if(!focusOn){document.form1.sub_password.focus(); focusOn='Y';}
		themessage = themessage + " - Password";
		}
	if (document.form1.sub_match_password.value=="") {
		if(!focusOn){document.form1.sub_match_password.focus(); focusOn='Y';}
		themessage = themessage + " - Re-Enter Password";
		}
	if (document.form1.reminder_question.value=="") {
		if(!focusOn){document.form1.reminder_question.focus(); focusOn='Y';}
		themessage = themessage + " - Reminder Question";
		}	
	if (document.form1.reminder_answer.value=="") {
		if(!focusOn){document.form1.reminder_answer.focus(); focusOn='Y';}
		themessage = themessage + " - Reminder Answer";
		}			
	if (document.form1.sub_org.value=="") {
		if(!focusOn){document.form1.sub_org.focus(); focusOn='Y';}
		themessage = themessage + " - Organization";
	}
	if (document.form1.sub_service_code.value=="") {
		if(!focusOn){document.form1.sub_service_code.focus(); focusOn='Y';}
		themessage = themessage + " - Service Area";
	}
	if (document.form1.sub_address.value=="") {
		if(!focusOn){document.form1.sub_address.focus(); focusOn='Y';}
		themessage = themessage + " - Address";
	}
	if (document.form1.sub_city.value=="") {
		if(!focusOn){document.form1.sub_city.focus(); focusOn='Y';}
		themessage = themessage + " - City";
	}
	if (document.form1.sub_state.value=="") {
		if(!focusOn){document.form1.sub_state.focus(); focusOn='Y';}
		themessage = themessage + " - State";
	}
	if (document.form1.sub_zip.value=="") {
			if(!focusOn){document.form1.sub_zip.focus(); focusOn='Y';}
			themessage = themessage + " - Zip";
	}

	if (document.form1.sub_bill_name.value=="") {
		if(!focusOn){document.form1.sub_bill_name.focus(); focusOn='Y';}
		themessage = themessage + " - Billing Name";
		}	
	if (document.form1.sub_bill_org.value=="") {
		if(!focusOn){document.form1.sub_bill_org.focus(); focusOn='Y';}
		themessage = themessage + " - Billing Organization";
	}
	if (document.form1.sub_bill_email.value=="") {
		if(!focusOn){document.form1.sub_bill_email.focus(); focusOn='Y';}
		themessage = themessage + " -  Billing Email Address";
		}	
	if (document.form1.sub_bill_address.value=="") {
		if(!focusOn){document.form1.sub_bill_address.focus(); focusOn='Y';}
		themessage = themessage + " - Billing Address";
	}
	if (document.form1.sub_bill_city.value=="") {
		if(!focusOn){document.form1.sub_bill_city.focus(); focusOn='Y';}
		themessage = themessage + " - Billing City";
	}
	if (document.form1.sub_bill_state.value=="") {
		if(!focusOn){document.form1.sub_state.focus(); focusOn='Y';}
		themessage = themessage + " - Billing State";
	}
	if (document.form1.sub_bill_zip.value=="") {
			if(!focusOn){document.form1.sub_zip.focus(); focusOn='Y';}
			themessage = themessage + " - Billing Zip";
	}
	
	
//alert if fields are empty and cancel form submit
	if (themessage == "You are required to complete the following fields: ") {
		document.form1.submit();
		}
	else {
		alert(themessage);
		false;
   }
}
