function popupSP(url) 
{
 var width  = 450;
 var height = 350;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}

function setFocus() {
	document.forms[0].elements[0].focus();
}



//bill country selection to remove required notation for zipcode
function billcountrySelection(){
	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";
		}
}
function countrySelection(){
	if(document.address_info.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";
		}
}

//ship country selection to remove required notation for zipcode
function shipcountrySelection(){
	if (document.address_info.ship_country.value == "USA")
		{
		document.getElementById("szrequired").style.display="inline";
		document.getElementById("ssrequired").style.display="inline";
		} else {
		document.getElementById("ssrequired").style.display="none";	
		document.getElementById("szrequired").style.display="none";
		}
}


//form validation for bill address

function validateBillInfo(theForm){
var isValid = "";
var shipisValid = "";


	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);
	
	//check shipping 
	// if(document.address_info.same_as_billing.checked){
	//	document.getElementById("shipErrors").style.display="none";
	// } else {
	//	shipisValid += validateShipContact(theForm.ship_firstname);
	//	shipisValid += validateShipLastName(theForm.ship_lastname);
	//	shipisValid += validateShipAddress(theForm.ship_address1);
	//	shipisValid += validateShipCity(theForm.ship_city);
	//		if (document.address_info.ship_country.value == "USA"){
	//			shipisValid += validateShipZipCode(theForm.ship_zipcode);
	//			shipisValid += validateShipStPa(theForm.ship_state);
	//		}
	//	shipisValid += validateShipPhone(theForm.ship_phone);
	// }
	 
	 
 	//setFocus();  
  if (isValid != "") {
  	document.getElementById("billErrors").style.display="block";
	//if(shipisValid !=""){
		//document.getElementById("shipErrors").style.display="block";
	//	}
	return false;
  }
  document.getElementById("billErrors").style.display="none";
  //document.getElementById("shipErrors").style.display="none";
  return true;
}


function validateCard(theForm){
var isValid = "";


	isValid += validateCreditCard(theForm.CCNumber);
	isValid += validateExpirationDate(theForm.CCExpMonth,theForm.CCExpYear);
  
 	//setFocus();  
  if (isValid != "") {
  	document.getElementById("paymentErrors").style.display="block";
    return false;
	
  }
	document.getElementById("paymentErrors").style.display="none";
  	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="block";
		//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="block";
		//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="block";
		//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="block";
		//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="block";
		//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="block";
			//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 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="block";
    } 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="block";
    } 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="block";
    } 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="block";
    }
	else{
	//fld.style.background = 'White';
	document.getElementById("phoneErr").style.display="none";
	} 
    return error;
}


//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="block";
		//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="block";
		//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="block";
		//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="block";
		//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="block";
		//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.ship_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="block";
			//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="block";
    }
	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="block";
			//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="block";
			//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="block";
			//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="block";
			//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="block";
			//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
}
}
