// JavaScript Document


function checkSurveyValidation(SurveyForm) {

	
	for(i=0; i<document.SurveyForm.elements.length; i++)
{
document.SurveyForm.elements[i].className='regular';
}


// build strings to check
var phone = document.getElementById("phone1").value + document.getElementById("phone2").value + document.getElementById("phone3").value;

	
    var why = "";
	
	why += isEmpty(document.getElementById("firstname").value,'firstname','a first name');
	why += isEmpty(document.getElementById("lastname").value,'lastname','a last name');
	why += isEmpty(document.getElementById("address").value,'address','an address');
	why += isEmpty(document.getElementById("citytown").value,'citytown','a city/town');
	why += checkPostalCode(document.getElementById("postal").value,'postal','a postal code'); 
	why += checkPhone(phone,'phone');
    why += checkEmail(document.getElementById("email").value,document.getElementById("cemail").value,'email','email');
	
    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;

       return false;
    }
	
	

return true;
}

function checkOrderValidation(OrderForm) {


	
	for(i=0; i<document.OrderForm.elements.length; i++)
{
document.OrderForm.elements[i].className='regular';
}


// build strings to check
var phone = document.getElementById("phone1").value + document.getElementById("phone2").value + document.getElementById("phone3").value;

var amount = checkSelection(document.forms['OrderForm'].elements['amount']);
var service = checkSelection(document.forms['OrderForm'].elements['service_package']);

   var why = "";

if(amount=="" && service==""){
	why += "Please select a Gift Certificate amount or a Service Package." + "<br />"; 
	document.getElementById('amount').className='error'; 
	document.getElementById('service_package').className='error';
	
}
else{
document.getElementById('amount').className='formheading';	
document.getElementById('service_package').className='formheading';	
}

	
 
	
	
	
	why += isEmpty(document.getElementById("firstname").value,'firstname','a first name');
	why += isEmpty(document.getElementById("lastname").value,'lastname','a last name');
	why += isEmpty(document.getElementById("address").value,'address','an address');
	why += isEmpty(document.getElementById("citytown").value,'citytown','a city/town');
	why += checkDropdown(document.getElementById("province").value,'province','province');
	why += checkPostalCode(document.getElementById("postal").value,'postal','a postal code'); 
	why += checkPhone(phone,'phone');
    why += checkEmail(document.getElementById("email").value,document.getElementById("cemail").value,'email','email');
	
	
	why += isEmpty(document.getElementById("sfirstname").value,'sfirstname','a recipient first name');
	why += isEmpty(document.getElementById("slastname").value,'slastname','a recipient last name');
	why += isEmpty(document.getElementById("saddress").value,'saddress','a recipient address');
	why += isEmpty(document.getElementById("scitytown").value,'scitytown','a recipient city/town');
	why += checkDropdown(document.getElementById("sprovince").value,'sprovince','recipient province');
	why += checkPostalCode(document.getElementById("spostal").value,'spostal','a recipient postal code'); 
	
	why += checkDropdown(document.getElementById("shipping").value,'shipping','shipping');
	
    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;

       return false;
    }
	
	

return true;
}



function DJ_change(){
	
//alert(document.getElementById('makesame').checked);	

if(document.getElementById('makesame').checked){
	
	document.getElementById('sfirstname').value=document.getElementById('firstname').value;
	document.getElementById('slastname').value=document.getElementById('lastname').value;
	document.getElementById('saddress').value=document.getElementById('address').value;
	document.getElementById('scitytown').value=document.getElementById('citytown').value;
	document.getElementById('sprovince').value=document.getElementById('province').value;
	document.getElementById('spostal').value=document.getElementById('postal').value;
	
}

else{
	document.getElementById('sfirstname').value="";
	document.getElementById('slastname').value="";
	document.getElementById('saddress').value="";
	document.getElementById('scitytown').value="";
	document.getElementById('sprovince').value="";
	document.getElementById('spostal').value="";
	
}

}


// email

function checkEmail (strng, strng2, field) {
var error="";
if ((strng == "")||(strng != strng2)) {
   error = "Enter an email address and ensure it is the same as the confirm email.<br />";document.getElementById(field).className='error';
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.<br />";document.getElementById(field).className='error';
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.<br />";document.getElementById(field).className='error';
       }
    }
return error;    
}

// check to see user selected a GIFT

function checkSelection (radioObj){
	
if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";


	
	
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.<br />";
	   	    document.getElementById('phone1').className='error';
   document.getElementById('phone2').className='error';
   document.getElementById('phone3').className='error';
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.<br />";
	   	    document.getElementById('phone1').className='error';
   document.getElementById('phone2').className='error';
   document.getElementById('phone3').className='error';
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.<br />";
	   	    document.getElementById('phone1').className='error';
   document.getElementById('phone2').className='error';
   document.getElementById('phone3').className='error';
    } 
		if(strng.length>0){
	//document.getElementById(field).value=parseInt(stripped);
	}
return error;
}

// fax number - strip out delimiters and check for 10 digits

function checkFax (strng,field) {
var error = "";
if (strng == "") {
   error = "You didn't enter a fax number.<br />";document.getElementById(field).className='error';
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The fax number contains illegal characters.<br />";
	   	 document.getElementById(field).className='error';
  
    }
    if (!(stripped.length == 10)) {
	error = "The fax number is the wrong length. Make sure you included an area code.<br />";document.getElementById(field).className='error';
    } 
		if(strng.length>0){
	document.getElementById(field).value=parseInt(stripped);
	}
return error;
}



// non-empty textbox

function isEmpty(strng,field,propername) {

var error = "";
  if (strng.length == 0) {
     error = "Please enter "+propername+".<br />"
	 document.getElementById(field).className='error';
	 
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.<br />";}
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.<br />";}
return error;
}

// valid selector from dropdown list

function checkDropdown(choice,field,propername) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the " + propername + " drop-down list.<br />";document.getElementById(field).className='error';
    }    
return error;
}    

// sin number - strip out delimiters and check for 10 digits



function isNumber (strng,field){
	var error = "";
	
if(isNaN(strng)){
	error = strng + " is not a numeric value that was expected.<br />";document.getElementById(field).className='error';
}
	return error;
}


function checkPostalCode(strng,field){
var error = "";	
var postalPattern = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i;
var postalRegExp = new RegExp(postalPattern);

var testResult = postalRegExp.test(strng);


if(!(testResult))
{
	
	error = "A properly formatted postal code was not entered.<br />";document.getElementById(field).className='error';
}	

if ((strng == "")||(strng == null))
{
	error = "A Postal Code was not entered.<br />";document.getElementById(field).className='error';

}

document.getElementById(field).value=strng.replace(/ /,"");
return error;
		
}




