function setFocus() {
	document.forms[0].elements[0].focus();
}
sfFocus = function() {
	var sfEls = document.getElementsByTagName("INPUT");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function() {
			this.className+=" sffocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
		}
	}
}

//country selection to remove required notation for zipcode
function countrySelection(){
	if (document.address_info.bill_country.value == "USA")
		{
		document.getElementById("zrequired").style.display="inline";
		document.getElementById("bsrequired").style.display="inline";
		} else {
		document.getElementById("stpaErr").style.display="none";
		document.getElementById("bsrequired").style.display="none";
		document.getElementById("zrequired").style.display="none";
		document.getElementById("zipErr").style.display="none";
		document.getElementById("bill_zipcode").style.background="#fff";
		}
	if (document.address_info.ship_country.value == "USA")
		{
		document.getElementById("szrequired").style.display="inline";
		document.getElementById("ssrequired").style.display="inline";
		} else {
		document.getElementById("sstpaErr").style.display="none";
		document.getElementById("ssrequired").style.display="none";	
		document.getElementById("szrequired").style.display="none";
		document.getElementById("szipErr").style.display="none";
		document.getElementById("ship_zipcode").style.background="#fff";
		}
}


//form validation for bill address

function validateBillInfo(theForm){
var isValid = "";


	isValid += validateContact(theForm.bill_firstname);
	isValid += validateLastName(theForm.bill_lastname);
	isValid += validateAddress(theForm.bill_address1);
	isValid += validateCity(theForm.bill_city);
	if (document.address_info.bill_country.value == "USA"){
		isValid += validateZipCode(theForm.bill_zipcode);
		isValid += validateStPa(theForm.bill_state);
	}
	isValid += validatePhone(theForm.bill_phone);
	isValid += validateEmail(theForm.email_address);
	
	//validateShipInfo(theForm);
  
 	//setFocus();  
  if (isValid != "") {
  	
    return false;
	
  }

  return true;
  
}

//validate ship address 
function validateShipInfo(theForm){
var isValid = "";


	isValid += validateShipContact(theForm.ship_firstname);
	isValid += validateShipLastName(theForm.ship_lastname);
	isValid += validateShipAddress(theForm.ship_address1);
	isValid += validateShipCity(theForm.ship_city);
	if (document.address_info.ship_country.value == "USA"){
		isValid += validateShipZipCode(theForm.ship_zipcode);
		isValid += validateShipStPa(theForm.ship_state);
	}
	isValid += validateShipPhone(theForm.ship_phone);
  
 	//setFocus();  
  if (isValid != "") {
    return false;
	
  }

  return true;
}


function validateCard(theForm){
var isValid = "";


	isValid += validateCreditCard(theForm.CCNumber);
	isValid += validateExpirationDate(theForm.CCExpMonth,theForm.CCExpYear);
  
 	//setFocus();  
  if (isValid != "") {
  	
    return false;
	
  }

  return true;
  
}


//validation donation form
function validateDonation(theForm) {
var isValid = "";

  isValid += validateEmail(theForm.email);
  isValid += validateCharity(theForm.charity);
  isValid += validateContact(theForm.name);
  isValid += validateBikesExp(theForm.bikesexp);
  isValid += validateBikesExpType(theForm.bikesexptype);
  //isValid += validateDate(theForm.month,theForm.day,theForm.year);
      
  if (isValid != "") {
    return false;
  }

  return true;
}



// field validation functions 
function validateContact(fld) {
    var error = "";
 
    if (fld.value.length < 2) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("nameErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("nameErr").style.display="none";
    }
    return error;  
}

function validateLastName(fld) {
    var error = "";
 
    if (fld.value.length < 1 ) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("lnameErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("lnameErr").style.display="none";
    }
    return error;  
}

function validateAddress(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("addressErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("addressErr").style.display="none";
    }
    return error;  
}


function validateCity(fld) {
    var error = "";
 
    if (fld.value.length < 1) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("cityErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("cityErr").style.display="none";
    }
    return error;  
}

function validateStPa(fld) {
    var error = "";
 
    if (fld.value.length < 1) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("stpaErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("stpaErr").style.display="none";
    }
    return error;  
}

function validateZipCode(fld) {
    var error = "";
	var zip = document.address_info.bill_zipcode.value;
	var validzip = /\d{5}/;
	if (fld.value.length < 5 || zip.match(validzip) == null) {
        	fld.style.background = '#ffffbb';
			fld.style.border = '1 px solid red'; 
        	document.getElementById("zipErr").style.display="inline";
			//wrtie in cf variable
			error = "The required field has not been filled in";
  	 	} else {
        	fld.style.background = '#fff';
			document.getElementById("zipErr").style.display="none";
	}
   return error;  
}


/*
function validateDate(month,day,year) {
    
	var error = "";
	var eventdate = new Date;
	
	eventdate.setDate(day);
	eventdate.setMonth(month); // January = 0
	eventdate.setFullYear(year); 

	var today = new Date;
	
	if (eventdate < today) 
	{
	fld.style.background = '#f5d77b'; 
    document.getElementById("dateErr").style.display="inline";
	//wrtie in cf variable
	error = "The required field has not been filled in";
	} else {
      fld.style.background = '#fff';
	  document.getElementById("dateErr").style.display="none";
   }
    return error;  
}
*/

function validateBikesExp(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffffbb'; 
        document.getElementById("bikesexpErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("bikesexpErr").style.display="none";
    }
    return error;  
}

function validateBikesExpType(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffffbb'; 
        document.getElementById("bikesexptypeErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("bikesexptypeErr").style.display="none";
    }
    return error;  
}

function validateCharity(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffffbb'; 
        document.getElementById("charityErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("charityErr").style.display="none";
    }

    return error;  
}


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);  // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#ffffbb';
        //set error message in cf variable
		error = "You didn't enter an email address";
		document.getElementById("emailErr").style.display="inline";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ffffbb';
        //set error message in cf variable
		error = "Please enter a valid email address.";
		document.getElementById("emailErr").style.display="inline";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ffffbb';
        //set error message in cf variable
		error = "The email address contains illegal characters";
		document.getElementById("emailErr").style.display="inline";
    } else {
        fld.style.background = '#fff';
		document.getElementById("emailErr").style.display="none";
    }
    return error;
}





function validatePhone(fld) {
    var error = "";
	var phone = document.address_info.bill_phone.value;
	var validphone = /[0-9]*/;
   if (fld.value.length < 10 || phone.match(validphone) == null) {
        //set error message in cf variable
		error = "You didn't enter a phone number.";
        fld.style.background = '#ffffbb';
		document.getElementById("phoneErr").style.display="inline";
    }
	else{
	fld.style.background = 'White';
	document.getElementById("phoneErr").style.display="none";
	} 
    return error;
}

//same as billing
function sameAsBilling(){

     if(document.address_info.same_as_billing.checked){
     document.address_info.ship_firstname.value = document.address_info.bill_firstname.value;
	 document.address_info.ship_lastname.value = document.address_info.bill_lastname.value;
	 document.address_info.ship_company.value = document.address_info.bill_company.value;
	 document.address_info.ship_address1.value = document.address_info.bill_address1.value;
	 document.address_info.ship_address2.value =  document.address_info.bill_address2.value;
	 document.address_info.ship_city.value =  document.address_info.bill_city.value;
	 document.address_info.ship_state.value = document.address_info.bill_state.value;	 
	 document.address_info.ship_zipcode.value = document.address_info.bill_zipcode.value;
	 document.address_info.ship_country.value = document.address_info.bill_country.value;
	 document.address_info.ship_phone.value = document.address_info.bill_phone.value;
	 document.address_info.same_as_billing.value = 1;
     }
	 else{
	 document.address_info.ship_firstname.value = "";
	 document.address_info.ship_lastname.value = "";
	 document.address_info.ship_company.value = "";
	 document.address_info.ship_address1.value = "";
	 document.address_info.ship_address2.value = "";
	 document.address_info.ship_city.value = "";
	 document.address_info.ship_state.value = "";	 
	 document.address_info.ship_zipcode.value = "";
	 document.address_info.ship_country.value = "";
	 document.address_info.ship_phone.value = "";
	 document.address_info.same_as_billing.value = 0;
	 }
}
//ship info
function validateShipContact(fld) {
    var error = "";
 
    if (fld.value.length < 2) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("snameErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("snameErr").style.display="none";
    }
    return error;  
}

function validateShipLastName(fld) {
    var error = "";
 
    if (fld.value.length < 1 ) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("slnameErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("slnameErr").style.display="none";
    }
    return error;  
}

function validateShipAddress(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("saddressErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("saddressErr").style.display="none";
    }
    return error;  
}


function validateShipCity(fld) {
    var error = "";
 
    if (fld.value.length < 1) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("scityErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("scityErr").style.display="none";
    }
    return error;  
}

function validateShipStPa(fld) {
    var error = "";
 
    if (fld.value.length < 1) {
        fld.style.background = '#ffffbb';
		fld.style.border = '1 px solid red'; 
        document.getElementById("sstpaErr").style.display="inline";
		//wrtie in cf variable
		error = "The required field has not been filled in";
    } else {
        fld.style.background = '#fff';
		document.getElementById("sstpaErr").style.display="none";
    }
    return error;  
}

function validateShipZipCode(fld) {
    var error = "";
	var zip = document.address_info.bill_zipcode.value;
	var validzip = /\d{5}/;
	if (fld.value.length < 5 || zip.match(validzip) == null) {
        	fld.style.background = '#ffffbb';
			fld.style.border = '1 px solid red'; 
        	document.getElementById("szipErr").style.display="inline";
			//wrtie in cf variable
			error = "The required field has not been filled in";
  	 	} else {
        	fld.style.background = '#fff';
			document.getElementById("szipErr").style.display="none";
	}
   return error;  
}
function validateShipPhone(fld) {
    var error = "";
	var phone = document.address_info.ship_phone.value;
	var validphone = /[0-9]*/;
   if (fld.value.length < 10  || phone.match(validphone) == null) {
        //set error message in cf variable
		error = "You didn't enter a phone number.";
        fld.style.background = '#ffffbb';
		document.getElementById("sphoneErr").style.display="inline";
    }
	else{
	fld.style.background = 'White';
	document.getElementById("sphoneErr").style.display="none";
	} 
    return error;
}
//credit card validation
function validateCreditCard(fld) {
	var error = "";
	var card = document.checkout.CCNumber.value;
	var validcard = /[0-9]/;
	if (fld.value.length == 0 || card.match(validcard) == null) {
        	//fld.style.background = '#ffffbb';
			//fld.style.border = '1 px solid red'; 
        	document.getElementById("credErr").style.display="inline";
			//wrtie in cf variable
			error = "The required field has not been filled in";
	}
	if (fld.value.length < 13 || fld.value.length > 20){
			//fld.style.background = '#ffffbb';
			//fld.style.border = '1 px solid red'; 
      	document.getElementById("credErr").style.display="inline";
			//wrtie in cf variable
			error = "The required field has not been filled in";
	} 
	 else {
        	//fld.style.background = '#fff';
			document.getElementById("credErr").style.display="none";
			}
    return error; 
}

function validateExpirationDate(month,year){
	var error = "";
	var date=new Date();
	var currMonth=date.getMonth()+1, currYear=date.getFullYear();
	var expYear = 20+year.value;

	if (month.value.length == 0 && year.value.length == 0) {
			//fld.style.background = '#ffffbb';
			//fld.style.border = '1 px solid red'; 
        	document.getElementById("dateErr").style.display="inline";
			//wrtie in cf variable
			error = "The required field has not been filled in";
			//alert(currMonth +" "+ currYear);
	}
	else {
        //	fld.style.background = '#fff';
			document.getElementById("dateErr").style.display="none";
			}
	
	if (month.value < currMonth)
	{
		if (expYear > currYear ) {
			//	fld.style.background = '#fff';
			document.getElementById("monthExpErr").style.display="none";
		} else {
			//fld.style.background = '#ffffbb';
			//fld.style.border = '1 px solid red'; 
        	document.getElementById("monthExpErr").style.display="inline";
			//wrtie in cf variable
			error = "The required field has not been filled in";
			}
		 //fld.style.background = '#ffffbb';
			//fld.style.border = '1 px solid red'; 
        	//document.getElementById("monthExpErr").style.display="inline";
			//wrtie in cf variable
			//error = "The required field has not been filled in";
	}
	else {
       //	fld.style.background = '#fff';
		document.getElementById("monthExpErr").style.display="none";
		}
	
	
	if (expYear < currYear)
	{
	//fld.style.background = '#ffffbb';
			//fld.style.border = '1 px solid red'; 
        	document.getElementById("yearExpErr").style.display="inline";
			//wrtie in cf variable
			error = "The required field has not been filled in";
			//alert(expYear);
	}
	else {
        //	fld.style.background = '#fff';
			document.getElementById("yearExpErr").style.display="none";

			}
    return error; 
}

function numbersonly(e){
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode!=8){ //allow backspace
if (unicode<48||unicode>57) //if not a number
return false //disable key press
}
}
