function isblank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function CheckEmail(strEmail) {
	var i;
	var getChar;


	if ((Left(strEmail, 1) == "@") || (Right(strEmail, 1) == "@") )
		return false;
	

		getChar = 0;
	for (i = 0 ; i < strEmail.length ; i++ ) {
		if (strEmail.charAt(i)=='@'){
			getChar++;
		}
	}	    

	if ( getChar != 1 )
		return false;

	var email1;
	var email2;
	var temail;
	
	temail = strEmail.split('@')
	email1 = temail[0].replace(/\s/g, "");
	email2 = temail[1].replace(/\s/g, "");	
	
	var cemail;
	cemail = strEmail;
	cemail = cemail.replace(/\@/g, "");	
	cemail = cemail.replace(/\./g, "");	
	
	if (cemail.length == 0)
		return false;
	
	var allow;
	allow = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
	for(i = 0 ; i < cemail.length; i++ )
		if (allow.indexOf(cemail.charAt(i)) < 0 )
			return false;

	if (Left(email1, 1) == '.')
		return false;

	
	if (Left(email2, 1) == '.' || Right(email2, 1) == '.' || email2.replace(/\./g, "").length == email2.length)
		return false;

	if (email2.indexOf("..") >= 0 )
		return false;

	if (Left(email2, 1) == '-' || Right(email2, 1) == '-')
		return false;

	if (Left(email2, 1) == '_' || Right(email2, 1) == '_')
		return false;
	
	return true;
}	

function Left( sourceStr, charIdx ) {
	if((sourceStr==null) || (sourceStr=="")) return "";
	return sourceStr.substring(0, charIdx);
}

function Right( sourceStr, charIdx ) {	
	if((sourceStr==null) || (sourceStr=="")) return "";
	return sourceStr.substring(sourceStr.length-charIdx, sourceStr.length);
}


// Check Login
function check_login(e)
{
	var msg;
	var textun="";
	var textpw="";
	var fg=1;

	if ((e.username.value == null) || (e.username.value == "") || isblank(e.username.value)) { textun = "E-mail Address is Required.\n"; fg=0;}
	
	if (fg) { 
	  if (!CheckEmail(e.username.value)) { textun = "An Error occurred. Please enter a correct email address. \n";} 	
	}
	
	
	if ((e.password.value == null) || (e.password.value == "") || isblank(e.password.value)) { textpw = "Password is Required.\n"; }
		
	if (e.username.style) e.username.style.backgroundColor = textun != "" ? '#FFCC66':'#FFFFFF';
	if (e.password.style) e.password.style.backgroundColor = textpw != "" ? '#FFCC66':'#FFFFFF';
	
	if (textun == "" && textpw == "") { e.submit(); return true; }
	else { msg = textun+textpw;	} 
	
	alert(msg);
	return false;
}

//Contact Submit
function checkForm(e)
{
	var msg;
	var textAll = "Please check your Personal Information.";
	var textfn="";
	var textln="";
	var textem="";
	var fg=1;
	
	if ((e.fristname.value == null) || (e.fristname.value == "") || isblank(e.fristname.value)) { textfn = "First Name is Required.\n";}	
	if ((e.lastname.value == null) || (e.lastname.value == "") || isblank(e.lastname.value)) { textln = "Last Name is Required.\n";}
	if ((e.email.value == null) || (e.email.value == "") || isblank(e.email.value)) { textem = "E-mail Address is Required.\n"; fg=0;}
	if (fg) {
		 if (!CheckEmail(e.email.value)) { textem = "An Error occurred. Please enter a correct email address. \n";} 
		}
	
	if (e.fristname.style) e.fristname.style.backgroundColor = textfn != "" ? '#FFCC66':'#FFFFFF';
	if (e.lastname.style) e.lastname.style.backgroundColor = textln != "" ? '#FFCC66':'#FFFFFF';
	if (e.email.style) e.email.style.backgroundColor = textem != "" ? '#FFCC66':'#FFFFFF';
	
	if ((textfn == "") && (textln == "") && (textem == "")) { e.submit(); return true;}
	
	if (textfn && textln && textem) {
		msg = textAll;
	} else {
		msg = textfn + textln + textem;
	}		
	alert(msg);
	return false;
}


// Review Submit
function check_review(e)
{
	var msg;
	var textcm="";

	if ((e.comment.value == null) || (e.comment.value == "") || isblank(e.comment.value)) { textcm = "Comment is Required.\n"; }	
		
	if (e.comment.style) e.comment.style.backgroundColor = textcm != "" ? '#FFCC66':'#FFFFFF';
	
	if (textcm == "") { e.submit(); return true; }
	else { msg = textcm;	} 
	
	alert(msg);
	return false;
}


// Check Register
function check_info(e)
{
	var msg;
	var textAll = "Please check your Personal Information.";
	var textfn="";
	var textln="";
	var textem="";
	var textad="";
	var textct="";
	var textsp="";
	var textcy="";
	var textzp="";
	var texttn="";
	var textpw="";
	var fg=1;
	var vg=1;

	if ((e.customers_firstname.value == null) || (e.customers_firstname.value == "") || isblank(e.customers_firstname.value)) { textfn = "First Name is Required.\n";}	
	if ((e.customers_lastname.value == null) || (e.customers_lastname.value == "") || isblank(e.customers_lastname.value)) { textln = "Last Name is Required.\n";}
	if ((e.customers_email.value == null) || (e.customers_email.value == "") || isblank(e.customers_email.value)) { textem = "E-mail Address is Required.\n"; fg=0;}
	if (fg) {
		 if (!CheckEmail(e.customers_email.value)) { textem = "An Error occurred. Please enter a correct email address. \n";} 
		}
	if ((e.street_address.value == null) || (e.street_address.value == "") || isblank(e.street_address.value)) {textad = "Address is Required.\n";}
    if ((e.entry_zip.value == null) || (e.entry_zip.value == "") || isblank(e.entry_zip.value)) { textzp = "Zip or Post Code is Required.\n"; }
	if ((e.entry_city.value == null) || (e.entry_city.value == "") || isblank(e.entry_city.value)) { textct = "City is Required.\n";}
	if ((e.entry_state.value == null) || (e.entry_state.value == "") || isblank(e.entry_state.value)) { textsp = "State or Province is Required.\n";}
	if (e.entry_country.value == 0) { textcy = "Country is Required.\n";}
	if ((e.customers_telephone.value == null) || (e.customers_telephone.value == "") || isblank(e.customers_telephone.value)) { texttn = "Telephone Number is Required.\n";}
	if ((e.password.value == null) || (e.password.value == "") || isblank(e.password.value)) { textpw = "Password is Required.\n"; vg=0;}
	if (vg) {
	 if (e.password.value != e.confirmation.value) { textpw = "Passowrd does not match.\n"; }
	}
	if (e.customers_firstname.style) e.customers_firstname.style.backgroundColor = textfn != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_lastname.style) e.customers_lastname.style.backgroundColor = textln != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_email.style) e.customers_email.style.backgroundColor = textem != "" ? '#FFCC66':'#FFFFFF';
	if (e.street_address.style) e.street_address.style.backgroundColor = textad != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_zip.style) e.entry_zip.style.backgroundColor = textzp != "" ? '#FFCC66':'#FFFFFF'; 
	if (e.entry_city.style) e.entry_city.style.backgroundColor = textct != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_state.style) e.entry_state.style.backgroundColor = textsp != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_country.style) e.entry_country.style.backgroundColor = textcy != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_telephone.style) e.customers_telephone.style.backgroundColor = texttn != "" ? '#FFCC66':'#FFFFFF';
	if (e.password.style) e.password.style.backgroundColor = textpw != "" ? '#FFCC66':'#FFFFFF';
	if (e.confirmation.style) e.confirmation.style.backgroundColor = textpw != "" ? '#FFCC66':'#FFFFFF';
	
	if ((textfn == "") && (textln == "") && (textem == "") && (textad == "")&& (textct == "") && (textzp == "") && (textsp == "")  && (textcy == "")  && (texttn == "")  && (textpw == "")) { e.submit(); return true;}
	
	if (textfn && textln && textem && textad && textct && textzp && textsp && textcy && texttn && textpw) {
		msg = textAll;
	} else {
		msg = textfn + textln + textem + textad + textct + textzp  + textsp + textcy + texttn + textpw;
	}		
	alert(msg);
	return false;
}

function check_acct(e)
{
	var msg;
	var textAll = "Please check your Personal Information.";
	var textfn="";
	var textln="";
	var textem="";
	var texttn="";
	var fg=1;
	
	if ((e.customers_firstname.value == null) || (e.customers_firstname.value == "") || isblank(e.customers_firstname.value)) { textfn = "First Name is Required.\n";}	
	if ((e.customers_lastname.value == null) || (e.customers_lastname.value == "") || isblank(e.customers_lastname.value)) { textln = "Last Name is Required.\n";}
	if ((e.customers_email.value == null) || (e.customers_email.value == "") || isblank(e.customers_email.value)) { textem = "E-mail Address is Required.\n"; fg=0;}
	if (fg) {
		 if (!CheckEmail(e.customers_email.value)) { textem = "An Error occurred. Please enter a correct email address. \n";} 
		}
	if ((e.customers_telephone.value == null) || (e.customers_telephone.value == "") || isblank(e.customers_telephone.value)) { texttn = "Telephone Number is Required.\n";}
	
	if (e.customers_firstname.style) e.customers_firstname.style.backgroundColor = textfn != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_lastname.style) e.customers_lastname.style.backgroundColor = textln != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_email.style) e.customers_email.style.backgroundColor = textem != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_telephone.style) e.customers_telephone.style.backgroundColor = texttn != "" ? '#FFCC66':'#FFFFFF';
	
	if ((textfn == "") && (textln == "") && (textem == "") && (texttn == "")) { e.submit(); return true;}
	
	if (textfn && textln && textem && texttn) {
		msg = textAll;
	} else {
		msg = textfn + textln + textem + texttn;
	}		
	alert(msg);
	return false;
}


function check_addr(e)
{
	var msg;
	var textAll = "Please check your Personal Information.";
	var textfn="";
	var textln="";
	var textad="";
	var textct="";
	var textsp="";
	var textzp="";
	var textcy="";


	if ((e.customers_firstname.value == null) || (e.customers_firstname.value == "") || isblank(e.customers_firstname.value)) { textfn = "First Name is Required.\n";}	
	if ((e.customers_lastname.value == null) || (e.customers_lastname.value == "") || isblank(e.customers_lastname.value)) { textln = "Last Name is Required.\n";}
	if ((e.street_address.value == null) || (e.street_address.value == "") || isblank(e.street_address.value)) {textad = "Address is Required.\n";}
	if ((e.entry_city.value == null) || (e.entry_city.value == "") || isblank(e.entry_city.value)) { textct = "City is Required.\n";}
	if ((e.entry_state.value == null) || (e.entry_state.value == "") || isblank(e.entry_state.value)) { textsp = "State or Province is Required.\n";}
	if ((e.entry_zip.value == null) || (e.entry_zip.value == "") || isblank(e.entry_zip.value)) { textzp = "ZIP or Post Code is Required.\n";}
	if (e.entry_country.value == 0) { textcy = "Country is Required.\n";}
	
	if (e.customers_firstname.style) e.customers_firstname.style.backgroundColor = textfn != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_lastname.style) e.customers_lastname.style.backgroundColor = textln != "" ? '#FFCC66':'#FFFFFF';
	if (e.street_address.style) e.street_address.style.backgroundColor = textad != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_city.style) e.entry_city.style.backgroundColor = textct != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_state.style) e.entry_state.style.backgroundColor = textsp != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_zip.style) e.entry_zip.style.backgroundColor = textzp != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_country.style) e.entry_country.style.backgroundColor = textcy != "" ? '#FFCC66':'#FFFFFF';
	
	if ((textfn == "") && (textln == "") && (textad == "") && (textct == "") && (textsp == "") && (textzp == "")  && (textcy == "")) { e.submit(); return true;}
	
	if (textfn && textln && textad && textct && textzp&& textsp && textcy) {
		msg = textAll;
	} else {
		msg = textfn + textln + textad +   textzp + textct  + textsp + textcy;
	}		
	alert(msg);
	return false;
}

function check_address(e)
{
	var msg;
	var textAll = "Please check your Personal Information.";
	var textfn="";
	var textln="";
	var textad="";
	var textct="";
	var textsp="";
	var textzp="";
	var textcy="";

    if ((e.customers_firstname.value == null) || (e.customers_firstname.value == "") || isblank(e.customers_firstname.value)) { e.submit(); return true;}
	
	if ((e.customers_firstname.value == null) || (e.customers_firstname.value == "") || isblank(e.customers_firstname.value)) { textfn = "First Name is Required.\n";}	
	if ((e.customers_lastname.value == null) || (e.customers_lastname.value == "") || isblank(e.customers_lastname.value)) { textln = "Last Name is Required.\n";}
	if ((e.street_address.value == null) || (e.street_address.value == "") || isblank(e.street_address.value)) {textad = "Address is Required.\n";}
	if ((e.entry_city.value == null) || (e.entry_city.value == "") || isblank(e.entry_city.value)) { textct = "City is Required.\n";}
	if ((e.entry_state.value == null) || (e.entry_state.value == "") || isblank(e.entry_state.value)) { textsp = "State or Province is Required.\n";}
	if ((e.entry_zip.value == null) || (e.entry_zip.value == "") || isblank(e.entry_zip.value)) { textzp = "ZIP or Post Code is Required.\n";}
	if (e.entry_country.value == 0) { textcy = "Country is Required.\n";}
	
	if (e.customers_firstname.style) e.customers_firstname.style.backgroundColor = textfn != "" ? '#FFCC66':'#FFFFFF';
	if (e.customers_lastname.style) e.customers_lastname.style.backgroundColor = textln != "" ? '#FFCC66':'#FFFFFF';
	if (e.street_address.style) e.street_address.style.backgroundColor = textad != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_city.style) e.entry_city.style.backgroundColor = textct != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_state.style) e.entry_state.style.backgroundColor = textsp != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_zip.style) e.entry_zip.style.backgroundColor = textzp != "" ? '#FFCC66':'#FFFFFF';
	if (e.entry_country.style) e.entry_country.style.backgroundColor = textcy != "" ? '#FFCC66':'#FFFFFF';
	
	if ((textfn == "") && (textln == "") && (textad == "") && (textct == "") && (textsp == "")  && (textcy == "") && (textzp == "")) { e.submit(); return true;}
	
	if (textfn && textln && textad && textct && textsp && textcy && textzp) {
		msg = textAll;
	} else {
		msg = textfn + textln + textad + textct  + textsp + textcy  + textzp;
	}		
	alert(msg);
	return false;
}


function check_payment(e) {
  var error = 0;
  var error_message = "Errors have occured during the process of your form.\n\nPlease make the following corrections:\n\n";
  var payment_value = null;
  if (e.payment.length) {
    for (var i=0; i<e.payment.length; i++) {
      if (e.payment[i].checked) {
        payment_value = e.payment[i].value;
      }
    }
  } else if (e.payment.checked) {
    payment_value = e.payment.value;
  } else if (e.payment.value) {
    payment_value = e.payment.value;
  }

  if (payment_value == "cc") {
    var cc_owner = e.cc_owner.value;
    var cc_number = e.cc_number.value;
    if (cc_owner == "" || cc_owner.length < 3) {
      error_message = error_message + "* The owner's name of the credit card must be at least 3 characters.\n";
      error = 1;
    }
    if (cc_number == "" || cc_number.length < 10) {
      error_message = error_message + "* The credit card number must be at least 10 characters.\n";
      error = 1;
    }
  }

  if (payment_value == null) {
    error_message = error_message + "* Please select a payment method for your order.\n";
    error = 1;
  }

  if (error == 1) {
    alert(error_message);
    return false;
  } else {
	e.submit();  
    return true;
  }
}

function check_shipping(e) {
/*
  var error = 0;
  var error_message = "Errors have occured during the process of your form.\n\nPlease make the following corrections:\n\n";
  var shipping_value = null;
  if (e.shipping_method.length) {
    for (var i=0; i<e.shipping_method.length; i++) {
      if (e.shipping_method[i].checked) {
        shipping_value = e.shipping_method[i].value;
      }
    }
  } else if (e.shipping_method.checked) {
    shipping_value = e.shipping_method.value;
  } else if (e.shipping_method.value) {
    shipping_value = e.shipping_method.value;
  }

  if (shipping_value == null) {
    error_message = error_message + "* Please select a Shipping method for your order.\n";
    error = 1;
  }
  
  if ((shipping_value == "UPS") && (e.ups_fg.value==0)) {
	error_message = error_message + "* Please wait while system generating UPS Service.\n";
    error = 1;  
  }
  
 if ((shipping_value == "UPS") && (e.ups_fg.value==1) && (e.ups_rate.value=="")) {
	error_message = error_message + "* Sorry, UPS Service is Unavailable, please select other Shipping method or try again.\n";
    error = 1;  
  }
 
 if ((shipping_value == "USPS") && (e.usps_fg.value==0)) {
	error_message = error_message + "* Please wait while system generating USPS Service.\n";
    error = 1;  
  }
  
 if ((shipping_value == "USPS") && (e.usps_fg.value==1) && (e.usps_rate.value=="")) {
	error_message = error_message + "* Sorry, USPS Service is Unavailable, please select other Shipping method or try again.\n";
    error = 1;  
  }
  
  if ((shipping_value == "Fedex") && (e.fedex_fg.value==0)) {
	error_message = error_message + "* Please wait while system generating Fedex Service.\n";
    error = 1;  
  }
  
 if ((shipping_value == "Fedex") && (e.fedex_fg.value==1) && (e.fedex_rate.value=="")) {
	error_message = error_message + "* Sorry, Fedex Service is Unavailable, please select other Shipping method or try again.\n";
    error = 1;  
  }

  if (error == 1) {
    alert(error_message);
    return false;
  } else {*/
	e.submit();  
    return true;
  //}
}


function check_pwd(e)
{
	var msg;
	var textAll = "Please check your submit form.";
	var textop="";
	var textnp="";
	var fg=1;
	
	
   if ((e.o_pass.value == null) || (e.o_pass.value == "") || isblank(e.o_pass.value)) { textop = "Current Password is Required.\n"; fg=0;}
   if (fg) { if (calcMD5(e.o_pass.value) != e.mdpass.value) { textop = "Current Password is Wrong.\n";} }
   if ((e.n_pass.value == null) || (e.n_pass.value == "") || isblank(e.n_pass.value)) { textnp = "New Password is Required.\n"; }
   
   if (e.n_pass.value != e.c_pass.value) { textnp = "New Passowrd does not match.\n"; }
  
	if (e.o_pass.style) e.o_pass.style.backgroundColor = textop != "" ? '#FFCC66':'#FFFFFF';
	if (e.n_pass.style) e.n_pass.style.backgroundColor = textnp != "" ? '#FFCC66':'#FFFFFF';
	if (e.c_pass.style) e.c_pass.style.backgroundColor = textnp != "" ? '#FFCC66':'#FFFFFF';
		
	if ((textop == "") && (textnp == "")) {
		e.submit(); 
	return false;
	}
	
	if (textop && textnp) {
		msg = textAll;
	} else {
		msg = textop + textnp;
	}		
	
	alert(msg);
	return false;
}

function check_email(e)
{
	var msg;
	var textem="";
	var fg=1;

	if ((e.email.value == null) || (e.email.value == "") || isblank(e.email.value)) { textem = "E-mail Address is Required.\n"; fg=0;}
	
	if (fg) { 
	  if (!CheckEmail(e.email.value)) { textem = "An Error occurred. Please enter a correct email address. \n";} 	
	}
		
	if (e.email.style) e.email.style.backgroundColor = textem != "" ? '#FFCC66':'#FFFFFF';
	
	if (textem == "") { e.submit(); return true; }
	else { msg = textem;	} 
	
	alert(msg);
	return false;
}


function check_type(e){
var txt="Date of Birth";
  if (!checkStr(e.value)) {
    e.style.backgroundColor = '#FFCC66';
	e.focus(); 
	e.select(); 
    txt = txt+" that you input is error!";
	alert(txt);
	return false;
  }
  e.style.backgroundColor = '#FFFFFF';
  return true;
}

function checkStr(str)
{
    for( var i=0; i < str.length; i++) {
        var ch = str.substring( i, i+1 );
        if( (ch < "0" || ch > "9") && ch != "-") 
            return false;              
    }    
    return true;
}

function check_num(e){
var txt="Quantity ";
  if (!checkData(e.value)) {
    e.style.backgroundColor = '#FFCC66';
	e.focus(); 
	e.select(); 
	e.value = "";
    txt = txt+" should be number!";
	alert(txt);
	return false;
  }
  e.style.backgroundColor = '#FFFFFF';
  return true;
}

function checkData(str)
{
    for( var i=0; i < str.length; i++) {
        var ch = str.substring( i, i+1 );
        if(ch < "0" || ch > "9") 
            return false;              
    }    
    return true;
}

function check_card(e){
var txt="Card Number";
  if (!checkCard(e.value)) {
    e.style.backgroundColor = '#FFCC66';
	e.focus(); 
    txt = txt+" has illegal character!";
	alert(txt);
	return false;
  }
  e.style.backgroundColor = '#FFFFFF';
  return true;
}

function checkCard(str)
{
    for( var i=0; i < str.length; i++) {
        var ch = str.substring( i, i+1 );
        if( (ch < "0" || ch > "9") && ch != " ") 
            return false;              
    }    
    return true;
}
