var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 7;
function isInteger(s)
{   
 var i;
 for (i = 0; i < s.length; i++)
 {   
  // Check that current character is number.
  var c = s.charAt(i);
  if (((c < "0") || (c > "9"))) return false;
 }
  // All characters are numbers.
 return true;
}
function stripCharsInBag(s, bag)
{
 var i;
 var returnString = "";
 // Search through string's characters one by one.
 // If character is not in bag, append to returnString.
 for (i = 0; i < s.length; i++)
 {   
  // Check that current character isn't whitespace.
  var c = s.charAt(i);
  if (bag.indexOf(c) == -1) returnString += c;
 }
 return returnString;
}
function checkInternationalPhone(strPhone){
 s=stripCharsInBag(strPhone,validWorldPhoneChars);
 return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
function isValidPhone(Phone){
 if ((Phone.value==null)||(Phone.value=="")){
  Phone.focus()
  return false
 }
 if (checkInternationalPhone(Phone.value)==false){
  Phone.value=""
  Phone.focus()
  return false
 }
 return true
}
function isNumber(sText)
{
 if (sText == null || !sText.toString().match(/^-?\d+(\.\d\d)?$/)) return false;
 return true;
}
String.prototype.trim = function () {
   return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function isINT(sText)
{
 if (sText == null || !sText.toString().match(/^-?\d+$/)) return false;
 return true;
}
function isFirstAlpha(str)
{
 var re = /^[A-Za-z ]+/;
 return re.test(str);
}
function isAlphaNumeric(alphane)
{
 var numaric = alphane;
 for(var j=0; j<numaric.length; j++)
 {
  var alphaa = numaric.charAt(j);
  var hh = alphaa.charCodeAt(0);
  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
  {
  }
  else {
   return false;
  }
 }
  return true;
}
 
//added by Geoffrey Neumann 18/07/2009 - same as isAlphaNumeric but spaces are also allowed- for postcodes
function isAlphaNumeric_spacesAllowed(alphane)
{
 var numaric = alphane;
 for(var j=0; j<numaric.length; j++)
 {
  var alphaa = numaric.charAt(j);
  var hh = alphaa.charCodeAt(0);
  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh == 20)
  {
  }
  else {
   return false;
  }
 }
  return true;
}

function rmvSpaces(str)
{
 var tstring = "";
 var splitstring = str.split(" ");
 for(i = 0; i < splitstring.length; i++)
  if (splitstring[i] != "")tstring = tstring + " " + splitstring[i];
 tstring = tstring.trim();
 return tstring;
}
function check_register_form(frm)
{
 var fchecked = true;
 var err_msg = "";
 
 frm.username.value = frm.username.value.trim();
 frm.firstname.value = rmvSpaces(frm.firstname.value);
 frm.lastname.value = rmvSpaces(frm.lastname.value);
 frm.address1.value = rmvSpaces(frm.address1.value);
 frm.address2.value = rmvSpaces(frm.address2.value);
 frm.city.value = rmvSpaces(frm.city.value);
 frm.county.value = rmvSpaces(frm.county.value);
 frm.postcode.value = rmvSpaces(frm.postcode.value);
 frm.email.value = frm.email.value.trim();
 frm.phone.value = rmvSpaces(frm.phone.value);
 frm.fax.value = rmvSpaces(frm.fax.value);
 for (i=0; i<frm.membership.length; i++)
 {
  if (frm.membership[i].checked)
   frm.member.value = frm.membership[i].value;
 }
 
 if (!isValidPhone(frm.phone))
 {
  fchecked = false;
  err_msg = "Please check the phone number!";
 }
 if (frm.email.value == ''){
  fchecked = false;
  err_msg = "The email address should not be blank!";
  frm.email.focus();
 }
 if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.email.value)){
  fchecked = false;
  err_msg = "Please enter the valid email address!";
  frm.email.focus();
 }
 if (frm.postcode.value == ''){
  fchecked = false;
  err_msg = "The post code should not be blank!";
  frm.postcode.focus();
 }
 if (!isAlphaNumeric_spacesAllowed(frm.postcode.value)){ //"_spacesAllowed" added 18/07/09
  fchecked = false;
  err_msg = "The post code should be alphanumeric!";
  frm.postcode.focus();
 }
 if (frm.city.value == ''){
  fchecked = false;
  err_msg = "The town/city should not be blank!";
  frm.city.focus();
 }
 if (frm.address1.value == ''){
  fchecked = false;
  err_msg = "The first address line should not be blank!";
  frm.address1.focus();
 }
 if (!isFirstAlpha(frm.lastname.value)){
  fchecked = false;
  err_msg = "The last name should begin with the alphabets only and be not blank!";
  frm.lastname.focus();
 }
 if (!isFirstAlpha(frm.firstname.value)){
  fchecked = false;
  err_msg = "The first name should begin with the alphabet only and be not blank!";
  frm.firstname.focus();
 }
 if (frm.password.value.length <5){
  fchecked = false;
  err_msg = "The password should be at least 5 characters!";
  frm.password.value = "";
  frm.confirm.value = "";
  frm.password.focus();
 }
 else
 {
  if (frm.password.value != frm.confirm.value){
   fchecked = false;
   err_msg = "Two passwords do not match each other!";
   frm.password.value = "";
   frm.confirm.value = "";
   frm.password.focus();
  }
 }
 if (!isFirstAlpha(frm.username.value)){
  fchecked = false;
  err_msg = "The username should begin with the alphabet only and be not blank!";
  frm.username.focus();
 }
 if (frm.username.value.length <5){
  fchecked = false;
  err_msg = "The username should be at least 5 characters!";
  frm.username.focus();
 }
 if (!fchecked) {alert(err_msg); return false;}
 else {
  return true;
 }
 return false;
 
}
function check_editprofile_form(frm)
{
 var fchecked = true;
 var err_msg = "";
 frm.firstname.value = rmvSpaces(frm.firstname.value);
 frm.lastname.value = rmvSpaces(frm.lastname.value);
 frm.address1.value = rmvSpaces(frm.address1.value);
 frm.address2.value = rmvSpaces(frm.address2.value);
 frm.city.value = rmvSpaces(frm.city.value);
 frm.county.value = rmvSpaces(frm.county.value);
 frm.postcode.value = rmvSpaces(frm.postcode.value);
 frm.email.value = frm.email.value.trim();
 frm.phone.value = rmvSpaces(frm.phone.value);
 frm.fax.value = rmvSpaces(frm.fax.value);
 
 
 if (!isValidPhone(frm.phone))
 {
  fchecked = false;
  err_msg = "Please check the phone number!";
 }
  if (frm.email.value == ''){
  fchecked = false;
  err_msg = "The email address should not be blank!";
  frm.email.focus();
 }
 if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.email.value)){
  fchecked = false;
  err_msg = "Please enter the valid email address!";
  frm.email.focus();
 }
 if (!isAlphaNumeric(frm.postcode.value)){
  fchecked = false;
  err_msg = "The post code should be alphanumeric!";
  frm.postcode.focus();
 }
 if (frm.postcode.value == ''){
  fchecked = false;
  err_msg = "The post code should not be blank!";
  frm.postcode.focus();
 }
 if (frm.city.value == ''){
  fchecked = false;
  err_msg = "The town/city should not be blank!";
  frm.city.focus();
 }
 if (frm.address1.value == ''){
  fchecked = false;
  err_msg = "The first address line should not be blank!";
  frm.address1.focus();
 }
 if (!isFirstAlpha(frm.lastname.value)){
  fchecked = false;
  err_msg = "The last name should begin with the alphabets only and be not blank!";
  frm.lastname.focus();
 }
 if (!isFirstAlpha(frm.firstname.value)){
  fchecked = false;
  err_msg = "The first name should begin with the alphabet only and be not blank!";
  frm.firstname.focus();
 }
 if (!fchecked) {alert(err_msg); return false;}
 else {
  var agree = confirm("Do you want to continue updating your profile?");
  if (agree) return true;
  else return false;
 }
 return false;
}
function check_password_form(frm)
{
 var fchecked = true;
 var err_msg = "";
 if (frm.oldpassword.value == ""){
  fchecked = false;
  err_msg = "Please fill the old password!";
  frm.oldpassword.focus();
 }
 else
 {
  if (frm.newpassword.value.length <5){
   fchecked = false;
   err_msg = "The password should be at least 5 characters!";
   frm.newpassword.value = "";
   frm.newconfirm.value = "";
   frm.newpassword.focus();
  }
  else
  {
   if (frm.newpassword.value != frm.newconfirm.value){
    fchecked = false;
    err_msg = "Two passwords do not match each other!";
    frm.newpassword.value = "";
    frm.newconfirm.value = "";
    frm.newpassword.focus();
   }
  }
 }
 if (!fchecked) {alert(err_msg); return false;}
 else return true;
 return false;
}
function check_membership_form(frm)
{
 for (i=0; i<frm.membership.length; i++)
 {
  if (frm.membership[i].checked)
   frm.member.value = frm.membership[i].value;
 }
 if (frm.member.value=="")
 {
  alert("Please choose a membership to upgrade!");
  return false;
 }
 var agree = confirm("Do you want to continue upgrading your membership?");
 if (agree)  
  return true;
 else return false;
}
function check_ordertrack_form(frm)
{
 frm.tracknumber.value = rmvSpaces(frm.tracknumber.value);
 if (frm.tracknumber.value=="")
 {
  alert("Please put the track number of your order properly!");
  frm.tracknumber.focus();
  return false;
 }
 return true;
}
function isOrderCheckAll(doc)
{
 var flag=doc.orderdelform.orderall.checked;
 if (doc.orderdelform2 == null) return false;
 var x=doc.orderdelform2;
 if(x.length == 1)
  x.orderone.checked = flag;
 else
  for(i=0;i<x.length;i++)
   x[i].orderone.checked = flag;
}
function isOrderCheckOne(doc)
{
 var flag=true;
 if (doc.orderdelform2 == null) return false;
 var x=doc.orderdelform2;
 if(x.length == 1)
  flag = x.orderone.checked;
 else
  for(i=0;i<x.length;i++)
   if(x[i].orderone.checked == false) 
   {
    flag=false;
    break;
   }
 doc.orderdelform.orderall.checked=flag;
}
function check_orderdel_form(doc)
{
 
 var x;
 if (doc.orderdelform2 == null) return false;
 
 x=doc.orderdelform2;
 var str="";
 if(x.length == 1)
 {
  if (x.orderone.checked==true) str = x.orderone.value;
 }
 else
 {
  
  for(i=0;i<x.length;i++)   
   if(x[i].orderone.checked == true)
    str=str+x[i].orderone.value+",";
  str = str.substr(0,str.length-1);
  
 }
 if(str!="")
 {
  doc.orderdelform.deleting_orders.value=str;
  return confirm("Do you really want to delete selected orders? There might be the orders being managed yet.");
 }
 else 
 {
  alert("You didn't choose any one of them...");
  return false;
 }
}
function check_login_form(frm)
{
 var username=rmvSpaces(frm.username.value);
 var password=rmvSpaces(frm.password.value);
 if (username=="")
 {
  alert("Please enter the username!");
  frm.username.focus();
  return false;
 }
 if (password=="")
 {
  alert("Please enter the password!");
  frm.password.focus();
  return false;
 }
 return true;
}
function check_checkout_form(frm)
{
 for(var i=0;i<frm.bill_info.length; i++)
  frm.bill_info[i].value=rmvSpaces(frm.bill_info[i].value);
 for(var i=0;i<frm.ship_info.length; i++)
  frm.ship_info[i].value=rmvSpaces(frm.ship_info[i].value);
 
 if (frm.bill_info[0].value=="")
 {
  alert("The first name should not be blank!");
  frm.bill_info[0].focus();
  return false;
 }
 if (!isFirstAlpha(frm.bill_info[0].value)){
  alert("The first name should begin with the alphabets only!");
  frm.bill_info[0].focus();
  return false;
 }
 if (frm.bill_info[1].value=="")
 {
  alert("The last name should not be blank!");
  frm.bill_info[1].focus();
  return false;
 }
 if (!isFirstAlpha(frm.bill_info[1].value)){
  alert("The last name should begin with the alphabets only!");
  frm.bill_info[1].focus();
  return false;
 }
 if (frm.bill_info[2].value=="")
 {
  alert("The billing address should not be blank!");
  frm.bill_info[2].focus();
  return false;
 }
 if (frm.bill_info[7].value=="")
 {
  alert("The post code should not be blank!");
  frm.bill_info[7].focus();
  return false;
 }
 if (!isAlphaNumeric(frm.bill_info[7].value)){
  alert("The post code should be alphanumeric!");
  frm.bill_info[7].focus();
  return false;
 }
 if (frm.bill_info[8].value=="")
 {
  alert("The email address should not be blank!");
  frm.bill_info[8].focus();
  return false;
 }
 if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.bill_info[8].value)){
  alert("Please enter the valid email address!");
  frm.bill_info[8].focus();
  return false;
 }
 if ((frm.bill_info[9].value!="") && (!isValidPhone(frm.bill_info[9])))
 {
  alert("The phone number is not valid!");
  frm.bill_info[9].focus();
  return false;
 }
 if ((frm.bill_info[10].value!="") && (!isINT(frm.bill_info[10].value)))
 {
  alert("The fax number is not valid!");
  frm.bill_info[10].focus();
  return false;
 }
 if (frm.ship_info[0].value=="")
 {
  alert("The first name should not be blank!");
  frm.ship_info[0].focus();
  return false;
 }
 if (!isFirstAlpha(frm.ship_info[0].value)){
  alert("The first name should begin with the alphabets only!");
  frm.ship_info[0].focus();
  return false;
 }
 if (frm.ship_info[1].value=="")
 {
  alert("The last name should not be blank!");
  frm.ship_info[1].focus();
  return false;
 }
 if (!isFirstAlpha(frm.ship_info[1].value)){
  alert("The last name should begin with the alphabets only!");
  frm.ship_info[1].focus();
  return false;
 }
 if (frm.ship_info[2].value=="")
 {
  alert("The shipping address should not be blank!");
  frm.ship_info[2].focus();
  return false;
 }
 if (frm.ship_info[7].value=="")
 {
  alert("The postcode should not be blank!");
  frm.ship_info[7].focus();
  return false;
 }
 if (!isAlphaNumeric(frm.ship_info[7].value)){
  alert("The post code should be alphanumeric!");
  frm.ship_info[7].focus();
  return false;
 }
 if (frm.ship_info[8].value=="")
 {
  alert("The email address should not be blank!");
  frm.ship_info[8].focus();
  return false;
 }
 if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.ship_info[8].value)){
  alert("Please enter the valid email address!");
  frm.ship_info[8].focus();
  return false;
 }
 if ((frm.ship_info[9].value!="") && (!isValidPhone(frm.ship_info[9])))
 {
  alert("The phone number is not valid!");
  frm.ship_info[9].focus();
  return false;
 }
 if ((frm.ship_info[10].value!="") && (!isINT(frm.ship_info[10].value)))
 {
  alert("The fax number is not valid!");
  frm.ship_info[10].focus();
  return false;
 }
 return true;
}
function check_prodetails_form(frm)
{
 x = frm.quantity.value.trim();
 if(isINT(x))
 {
  if (parseInt(x)==0)
  {
   alert("The quantity should be more than 0!");
   frm.quantity.value = "1";
   return false;
  }
 }
 else
 {
  alert("The quantity should be numeric!");
  frm.quantity.value = "1";
  return false;
 }
 return true;
}
function check_products_search_form()
{ 
 var x = document.search_products.product_name.value.trim();
 x = rmvSpaces(x);
 document.search_products.product_name.value = x;
 if(x != "")
  return true;
 else
 {
  document.location.href='index.php';
  return false; 
 }
}
function check_contacts_form(frm)
{
 frm.fname.value=rmvSpaces(frm.fname.value);
 frm.lname.value=rmvSpaces(frm.lname.value);
 frm.phone.value=rmvSpaces(frm.phone.value);
 frm.email.value=rmvSpaces(frm.email.value);
 frm.comments.value=rmvSpaces(frm.comments.value);
 
 if (frm.fname.value=="")
 {
  alert("The first name should not be blank!");
  frm.fname.focus();
  return false;
 }
 if (!isFirstAlpha(frm.fname.value)){
  alert("The first name should begin with the alphabets only!");
  frm.fname.focus();
  return false;
 }
 if (frm.lname.value=="")
 {
  alert("The last name should not be blank!");
  frm.lname.focus();
  return false;
 }
 if (!isFirstAlpha(frm.lname.value)){
  alert("The last name should begin with the alphabets only!");
  frm.lname.focus();
  return false;
 }
 if ((frm.phone.value!="" ) && (!isValidPhone(frm.phone)))
 {
  alert("The phone number is invalid!");
  frm.phone.focus();
  return false;
 }
 if (frm.email.value == ''){
  alert("The email address should not be blank!");
  frm.email.focus();
  return false;
 }
 if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.email.value)){
  alert("Please enter the valid email address!");
  frm.email.focus();
  return false;
 }
 if (frm.comments.value == ''){
  alert("The comments should not be blank!");
  frm.comments.focus();
  return false;
 }
 return true;
}
function check_forgot_frm(frm)
{
 frm.username.value=rmvSpaces(frm.username.value);
 frm.email.value=rmvSpaces(frm.email.value);
 
 if (frm.username.value=="" && frm.email.value=="")
 {
  alert("Both the username and email address should not be blank!");
  frm.username.focus();
  return false;
 }
 if (frm.username.value!="" && !isFirstAlpha(frm.username.value))
 {
  alert("The username should begin with the alphabets only!");
  frm.username.focus();
  return false;
 }
 if (frm.email.value!="" && (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.email.value)))
 {
  alert("Please enter the valid email address!");
  frm.email.focus();
  return false;
 }
 return true;
}
