//window.onload = regChecked;

var xmlHttp;

String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
    }
// ***********************************************************
// *************** Check Promo Code Using AJAX ***************
// ***********************************************************

function checkPromoCode(){ 
	var promoCode = document.getElementById("promoCode").value;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="includes/getpromo.php";
	url=url+"?q="+promoCode;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById("promoTxt").innerHTML=xmlHttp.responseText;
	}
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

// ***************************************************************************************
// *************** Check courses selected and calculate subtotal and total ***************
// ***************************************************************************************

function regChecked(regArray) {
	var sum = 0;
	var courseCount = 0;
	var percDol = document.getElementById("promoType").value;
	var amount = document.getElementById("promoAmount").value;
//	var regArray2=regArray;
	for (i=0; i<regArray.length; i++){
		var courseID = document.getElementById("course_" + regArray[i]).value;
		if (document.getElementById("course_" + regArray[i]).checked==true){
			sum=costSum(sum, courseID);
			courseCount ++;
		}
	}
	var total = sum;
	document.getElementById("qtyCoursesSelected").value = courseCount;
	document.getElementById("subtotal").innerHTML = "<input type='text' readonly='readonly' name='stprice' value='$" + sum + "' id='stprice' />";
	if (percDol==0){
		total=Math.round(sum*(1-(amount/100)));
		if (total < 0){total = 0;}
	}
	if (percDol==1){
		total=Math.round(sum-amount);
		if (total < 0){total = 0;}
	}
	validateCousesSelected();
	document.getElementById("totalPrice").value="$"+total;
}

function costSum(sum, courseID){
	var currentCost = document.getElementById(courseID+"_price").innerHTML;
	currentCost = parseFloat(currentCost.split("$")[1]);
	sum = sum + currentCost;
	return sum;
}


function validateForm(regArray){
	var allGood=true;
	regChecked(regArray);
	if (!valFirstName()){allGood=false;}
	if (!valLastName()){allGood=false;}
	if (!validateAddress()){allGood=false;}
	if (!validateCousesSelected()){allGood=false;}
	if (!validateCity()){allGood=false;}
	if (!validateState()){allGood=false;}
	if (!validateZip()){allGood=false;}
	if (!emailConfirm()){allGood=false;}
	if (!validateDaytimePhone()){allGood=false;}
	if (!validateCancellationPolicy()){allGood=false;}
	if (!allGood){ alert ("Please Fix Highlighted Problems Before Proceeding"); }
	regChecked(regArray);
	return allGood;
}

function validateCousesSelected(){
	qtyCoursesSelected = document.getElementById("qtyCoursesSelected").value;
	if (qtyCoursesSelected == 0){
		document.getElementById("coursesTable").style.borderTop = "dashed";
		document.getElementById("coursesTable").style.borderTopColor = "darkred";
		document.getElementById("coursesTable").style.borderBottom = "dashed";
		document.getElementById("coursesTable").style.borderBottomColor = "darkred";
		document.getElementById("coursesTable").style.borderLeft = "dashed";
		document.getElementById("coursesTable").style.borderLeftColor = "darkred";
		document.getElementById("coursesTable").style.borderRight = "dashed";
		document.getElementById("coursesTable").style.borderRightColor = "darkred";
		return false;
	}else{
		document.getElementById("coursesTable").style.borderTop = "";
		document.getElementById("coursesTable").style.borderTopColor = "";
		document.getElementById("coursesTable").style.borderBottom = "";
		document.getElementById("coursesTable").style.borderBottomColor = "";
		document.getElementById("coursesTable").style.borderLeft = "";
		document.getElementById("coursesTable").style.borderLeftColor = "";
		document.getElementById("coursesTable").style.borderRight = "";
		document.getElementById("coursesTable").style.borderRightColor = "";
		return true;
	}
}

function valFirstName(){
	firstName = String(document.getElementById("firstName").value);
	firstName = firstName.trim();
	if (firstName == ""){
		document.getElementById("firstName").style.borderColor = "darkred";
		document.getElementById("firstName").style.backgroundColor = "pink";
		document.getElementById("name").className = "notValid";
		return false;
	}else{
		document.getElementById("firstName").style.backgroundColor = "";
		document.getElementById("firstName").style.borderColor = "";
		checkNameSet();
		return true;
	}
}

function valLastName(){
	lastName = String(document.getElementById("lastName").value);
	lastName = lastName.trim();	
	if (lastName == ""){
		document.getElementById("lastName").style.borderColor = "darkred";
		document.getElementById("lastName").style.backgroundColor = "pink";
		document.getElementById("name").className = "notValid";
		return false;
	}else{
		document.getElementById("lastName").style.backgroundColor = "";
		document.getElementById("lastName").style.borderColor = "";
		checkNameSet();
		return true;		
	}

}

function checkNameSet(){
	firstName = String(document.getElementById("firstName").value);
	firstName = firstName.trim();
	lastName = String(document.getElementById("lastName").value);
	lastName = lastName.trim();	
	if (firstName != "" && lastName != ""){
		document.getElementById("name").className = "";
	}
}

// ******************************************************
// *************** Validate Address Input ***************
// ******************************************************

function validateAddress(){
	address = String(document.getElementById("address1").value);
	address = address.trim();
	if (address == ""){
		document.getElementById("address1").style.borderColor = "darkred";
		document.getElementById("address1").style.backgroundColor = "pink";
		document.getElementById("addressRow").className = "notValid";
		return false;
	}else{
		document.getElementById("address1").style.borderColor = "";
		document.getElementById("address1").style.backgroundColor = "";
		document.getElementById("addressRow").className = "";
		return true;
	}
}

// ***************************************************************************************************************************
// *************** Validate City State and Zip, If they are all good change CSZ class to "" if not leave as is ***************
// ***************************************************************************************************************************

function validateCity(){
	city = String(document.getElementById("city").value);
	city = city.trim();
	if (city == ""){
		document.getElementById("city").style.borderColor = "darkred";
		document.getElementById("city").style.backgroundColor = "pink";
		document.getElementById("CSZ").className = "notValid";
		return false;
	}else{
		document.getElementById("city").style.borderColor = "";
		document.getElementById("city").style.backgroundColor = "";
		checkCSZSet()
		return true;
	}
}
function validateState(){
	state = String(document.getElementById("state").value);
	state = state.trim();
	if (state == ""){
		document.getElementById("state").style.borderColor = "darkred";
		document.getElementById("state").style.backgroundColor = "pink";
		document.getElementById("CSZ").className = "notValid";
		return false;
	}else{
		document.getElementById("state").style.borderColor = "";
		document.getElementById("state").style.backgroundColor = "";
		checkCSZSet()
		return true;
	}
}
function validateZip(){
	zip = String(document.getElementById("zip").value);
	zip = zip.trim();
	if (zip == ""){
		document.getElementById("zip").style.borderColor = "darkred";
		document.getElementById("zip").style.backgroundColor = "pink";
		document.getElementById("CSZ").className = "notValid";
		return false;
	}else{
		document.getElementById("zip").style.borderColor = "";
		document.getElementById("zip").style.backgroundColor = "";
		checkCSZSet()
		return true;
	}
}

function checkCSZSet(){
	city = String(document.getElementById("city").value);
	city = city.trim();
	state = String(document.getElementById("state").value);
	state = state.trim();
	zip = String(document.getElementById("zip").value);
	zip = zip.trim();
	if (city != "" && state != "" && zip != ""){
		document.getElementById("CSZ").className = "";
	}
}

// *********************************************************************************
// *************** Validate Email and cross check with email confirm ***************
// *********************************************************************************

function emailConfirm(){
	email = String(document.getElementById("email").value);
	email = email.trim()
	confirmEmail = String(document.getElementById("confirmEmail").value);
	confirmEmail = confirmEmail.trim()
	if (email == confirmEmail){
		allGood = validateEmail();
		if (!allGood){emailConfirmNoGood();}else{clearEmailStyle();}
	}else{
		emailConfirmNoGood();
		allGood = false;
	}
	return allGood;
}

function validateEmail() {
	email = String(document.getElementById("email").value);
	email = email.trim()
	var allGood = true;
	var invalidChars = " /:,;";
	if (email == "") {
		allGood = emailNoGood();
	}
	for (var k=0; k<invalidChars.length; k++) {
		var badChar = invalidChars.charAt(k);
		if (email.indexOf(badChar) > -1) {
			allGood = emailNoGood();
		}
	}
	var atPos = email.indexOf("@",1);
	if (atPos == -1) {
		allGood = emailNoGood();
	}
	if (email.indexOf("@",atPos+1) != -1) {
		allGood = emailNoGood();
	}
	var periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {	
		allGood = emailNoGood();
	}
	if (periodPos+3 > email.length)	{
		allGood = emailNoGood();
	}
	if (allGood == true){
		document.getElementById("email").style.borderColor = "";
		document.getElementById("email").style.backgroundColor = "";
		document.getElementById("emailRow").className = "";
	}
	return allGood;
}

function emailNoGood(){
	document.getElementById("email").style.borderColor = "darkred";
	document.getElementById("email").style.backgroundColor = "pink";
	document.getElementById("emailRow").className = "notValid";
	return false;
}

function emailConfirmNoGood(){
	document.getElementById("email").style.borderColor = "darkred";
	document.getElementById("email").style.backgroundColor = "pink";
	document.getElementById("emailRow").className = "notValid";
	document.getElementById("confirmEmail").style.borderColor = "darkred";
	document.getElementById("confirmEmail").style.backgroundColor = "pink";
	document.getElementById("confirmEmailRow").className = "notValid";
}

function clearEmailStyle(){
	document.getElementById("email").style.borderColor = "";
	document.getElementById("email").style.backgroundColor = "";
	document.getElementById("emailRow").className = "";
	document.getElementById("confirmEmail").style.borderColor = "";
	document.getElementById("confirmEmail").style.backgroundColor = "";
	document.getElementById("confirmEmailRow").className = "";
}

// ******************************************************
// *************** Validate Phone Numbers ***************
// ******************************************************

function validateDaytimePhone(){
	phNum = String(document.getElementById("phoneDaytime").value);
	phNum = phNum.trim();
	error = validatePhoneFormat(phNum);
	if (error == 0){
		document.getElementById("phoneDaytime").style.borderColor = "";
		document.getElementById("phoneDaytime").style.backgroundColor = "";
		document.getElementById("phone").className = "";
		document.getElementById("formatDayPhone").innerHTML = "";
		return true;
	}else{
		document.getElementById("phoneDaytime").style.borderColor = "darkred";
		document.getElementById("phoneDaytime").style.backgroundColor = "pink";
		document.getElementById("phone").className = "notValid";
		document.getElementById("formatDayPhone").innerHTML = error;
	}
}

function validateEveningPhone(){
	phNum = String(document.getElementById("phoneEvening").value);
	phNum = phNum.trim();
	error = validatePhoneFormat(phNum);
	if (error == 0 || error == " You didn't enter a phone number."){
		document.getElementById("phoneEvening").style.borderColor = "";
		document.getElementById("phoneEvening").style.backgroundColor = "";
		document.getElementById("phoneEveningRow").className = "";
		document.getElementById("formatEveningPhone").innerHTML = "";
		return true;
	}else{
		document.getElementById("phoneEvening").style.borderColor = "darkred";
		document.getElementById("phoneEvening").style.backgroundColor = "pink";
		document.getElementById("phoneEveningRow").className = "notValid";
		document.getElementById("formatEveningPhone").innerHTML = error;
	}
}

function validateFax(){
	phNum = String(document.getElementById("fax").value);
	phNum = phNum.trim();
	error = validatePhoneFormat(phNum);
	if (error == 0 || error == " You didn't enter a phone number."){
		document.getElementById("fax").style.borderColor = "";
		document.getElementById("fax").style.backgroundColor = "";
		document.getElementById("faxRow").className = "";
		document.getElementById("formatFax").innerHTML = "";
		return true;
	}else{
		document.getElementById("fax").style.borderColor = "darkred";
		document.getElementById("fax").style.backgroundColor = "pink";
		document.getElementById("faxRow").className = "notValid";
		document.getElementById("formatFax").innerHTML = error;
	}
}

function validatePhoneFormat(fld) {
	var error = 0;
	var stripped = fld.replace(/[\(\)\.\-\ ]/g, ''); 
	if (stripped == "") {
		error = " You didn't enter a phone number.";
	}else if (isNaN(stripped)) {
		error = " The phone number contains illegal characters use format xxx-xxx-xxxx.";
	}else if(!(stripped.length == 10)) {
		error = " The phone number is the wrong length. Make sure you included an area code.";
	}
	return error;
}

// ***************

function validateCancellationPolicy(){
	policyGood = true;
	if(!document.getElementById("cancellationPolicy").checked){
		//not checked
		document.getElementById("cancellationPolicyRow").className = "notValid";
		policyGood = false;
	} else {
		document.getElementById("cancellationPolicyRow").className = "";
		policyGood = true;
	}
	return policyGood;
}
