/*****************************************************************************************/
/*************************CUSTOMERINFO FORM***********************************************/
function copyCustInfo(box){
	if(box.checked==true){
		$('#sname').val( $('#cname').val() );
		$('#scompany').val( $('#ccompany').val() );
		$('#saddr1').val( $('#caddr1').val() );
		$('#saddr2').val( $('#caddr2').val() );
		$('#scity').val( $('#ccity').val() );
		$('#szip').val( $('#czip').val() );

		$('#sname').attr('disabled',true);
		$('#scompany').attr('disabled',true);
		$('#saddr1').attr('disabled',true);
		$('#saddr2').attr('disabled',true);
		$('#scity').attr('disabled',true);
		$('#szip').attr('disabled',true);
	} else{		
		$('#sname').val('');
		$('#scompany').val('');
		$('#saddr1').val('');
		$('#saddr2').val('');
		$('#scity').val('');
		$('#szip').val('');
		
		$('#sname').attr('disabled',false);
		$('#scompany').attr('disabled',false);
		$('#saddr1').attr('disabled',false);
		$('#saddr2').attr('disabled',false);
		$('#scity').attr('disabled',false);
		$('#szip').attr('disabled',false);
	}
}

function custValidate(){
	resetForm();
	//customer	
	noError=true;
	if($('#cname').val()==""){
		$('#cname').addClass('inputError');
		$('#nameError').html("You didn't enter a name.");
		noError=false;
	}
	if($('#caddr1').val()==""){
		$('#caddr1').addClass('inputError');
		$('#addrError').html("You didn't enter an address.");
		noError=false;
	}
	if($('#ccity').val()==""){
		$('#ccity').addClass('inputError');
		$('#cityError').html("You didn't enter a city and state.");
		noError=false;
	}
	if(!validateZip( $('#czip').val(),"#zipError" )){
		$('#czip').addClass('inputError');
		noError=false;
	}
	if(!validatePhone() ){
		$('#cphone').addClass('inputError');
		noError=false;
	}
	if( !validateEmail($('#cemail').val(),1,1) ){
		$('#cemail').addClass('inputError');
		$('#emailError').html("You didn't enter a valid E-mail.");
		noError=false;
	}
	//shipping

	if($('#sname').val()==""){
		$('#sname').addClass('inputError');
		$('#nameError2').html("You didn't enter a name.");		
		noError=false;
	}
	if($('#saddr1').val()==""){
		$('#saddr1').addClass('inputError');
		$('#addrError2').html("You didn't enter an address.");		
		noError=false;
	}
	if($('#scity').val()==""){
		$('#scity').addClass('inputError');
		$('#cityError2').html("You didn't enter a city and state.");		
		noError=false;
	}
	if( !validateZip( $('#szip').val(), "#zipError2" ) ){
		$('#szip').addClass('inputError');
		noError=false;
	}
	
	return noError;
}

function validateZip(temp,id) {
	if (temp == "") {
		$(id).html("You didn't enter a zipcode.");
		return false;
	}else if (isNaN(parseInt(temp))) {
    	$(id).html("The zip code contains illegal characters.");	
		return false;
	} else if (!(temp.length == 5)) {
        $(id).html("The zip code is the wrong length. Enter 5 digit zipcode.");
		return false;
    } 
	return true;
}

function validatePhone() {
	fld=$('#cphone').val();
    var stripped = fld.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld == "") {
        $('#phoneError').html("You didn't enter a phone number.");
		return false;
    } else if (isNaN(parseInt(stripped))) {
       $('#phoneError').html("The phone number contains illegal characters.");
		return false;
    } else if (!(stripped.length == 10)) {
        $('#phoneError').html("The phone number is the wrong length. Make sure you included an area code.");
		return false;
    } 
    return true;
}

function resetForm(){
	$('#nameError').html('');
	$('#addrError').html('');
	$('#cityError').html('');
	$('#zipError').html('');
	$('#phoneError').html('');
	$('#emailError').html('');	

	$('#cname').removeClass('inputError');
	$('#caddr1').removeClass('inputError');
	$('#ccity').removeClass('inputError');
	$('#czip').removeClass('inputError');
	$('#cphone').removeClass('inputError');
	$('#cemail').removeClass('inputError');
	
	$('#nameError2').html('');
	$('#addrError2').html('');
	$('#cityError2').html('');
	$('#zipError2').html('');

	$('#sname').removeClass('inputError');
	$('#saddr1').removeClass('inputError');
	$('#scity').removeClass('inputError');
	$('#szip').removeClass('inputError');		
}
/*VALIDATE EMAIL ************************************************************************/
function validateEmail(addr,man,db) {
if (addr == '' && man) {
   if (db) //alert('email address is mandatory');
   return false;
}
if (addr == '') return true;
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) //alert('email address contains invalid characters');
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) //alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) //alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) //alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) //alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) //alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) //alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) //alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) //alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
/*if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) //alert('invalid primary domain in email address');
   return false;
}*/
return true;
}