/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var sym ='-()x.X[] ';
var sym2=',.&-/';
var sym3 ='-';
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

// onBlur="checkPhone(document.employee_form.workphone)" 
function checkPhone(field) {
var val=numb+sym+' ';
var parm=field.value;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1){
       alert("Invalid Phone Format");
	   field.value="";
	   field.focus();
	   return false;
	}
  }
  return true;
}

function activate_countyres()
{
	if(document.getElementById)
	{
		var docF=document.pay;
		if(docF.shippingstate.value=="Oklahoma")
		{
			docF.shippingcounty.disabled = false;
		}
		else
		{
			docF.shippingcounty.disabled = true;
			docF.shippingcounty.value = "";
		}
	}
}

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 ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }


function checkRegForm(whichform) {
	var cgi = false;
	var name  = document.forms[whichform].username;
	var password  = document.forms[whichform].userpassword;
	var contactperson = document.forms[whichform].contactperson;
	var address  = document.forms[whichform].address;
	var city = document.forms[whichform].city;
	var state = document.forms[whichform].state;
	var zip = document.forms[whichform].zip;
	var country = document.forms[whichform].country;
	var phone  = document.forms[whichform].phone;
	var email  = document.forms[whichform].email;
 	var vat  = document.forms[whichform].vat;
	var alertMessage = "";
	if (checkString(name.value)) 
	{
		alertMessage = alertMessage + "Please enter your business name.\n";
	}
	if (checkString(password.value)) 
	{
		alertMessage = alertMessage + "Please enter your password.\n";
	}
	if (checkString(contactperson.value)) 
	{
		alertMessage = alertMessage + "Please enter your contact person.\n";
	}
	if (checkString(vat.value)) 
	{
		alertMessage = alertMessage + "Please enter your vat number.\n";
	}
	if (checkString(address.value)) 
	{
		alertMessage = alertMessage + "Please enter your address.\n";
	}
	if (checkString(city.value)) 
	{
		alertMessage = alertMessage + "Please enter your city.\n";
	}
	if (checkString(state.value)) 
	{
		alertMessage = alertMessage + "Please enter your state.\n";
	}
	if (checkString(zip.value)) 
	{
		alertMessage = alertMessage + "Please enter your zip code.\n";
	}
	if (checkString(country.value)) 
	{
		alertMessage = alertMessage + "Please enter your country.\n";
	}
	if (!checkInternationalPhone(phone.value)) 
	{
		alertMessage = alertMessage + "Please enter a valid phone number.\n";
	}
	if (!validEmail(trim(email.value))) 
	{
		alertMessage = alertMessage + "Please enter a vaild E-mail address.\n";
	}
	if(alertMessage != "")
	{
		alert(alertMessage);
		cgi = false;
	}
	else 
	{
		cgi = true;
	}

	// IF CGI TESTS TRUE THEN SEND IT OUT!
	if (cgi) 
	{
		document.forms[whichform].actionType.value ="validateRegform";
		return true;
	}
	else 
	{
		return false;
	}
}

function resetRegForm(whichform) {
	document.forms[whichform].username.value='';
	document.forms[whichform].userpassword.value='';
	document.forms[whichform].contactperson.value='';
	document.forms[whichform].address.value='';
	document.forms[whichform].city.value='';
	document.forms[whichform].state.value='';
	document.forms[whichform].zip.value='';
	document.forms[whichform].country.value='';
	document.forms[whichform].phone.value='';
	document.forms[whichform].email.value='';
 	document.forms[whichform].vat.value='';
	document.forms[whichform].fax.value='';
	document.forms[whichform].mobile.value='';
	return false;
}
function checkContactForm(whichform) {
	var cgi = false;
	var name  = document.forms[whichform].name;
	var lname  = document.forms[whichform].lname;
	var phone  = document.forms[whichform].phone;
	var email  = document.forms[whichform].email;
	var comments  = document.forms[whichform].comments;
 	
	var alertMessage = "";
	if (checkString(name.value)) 
	{
		alertMessage = alertMessage + "Please enter your first name.\n";
	}
	if (checkString(lname.value)) 
	{
		alertMessage = alertMessage + "Please enter your last name.\n";
	}
	if (checkString(comments.value)) 
	{
		alertMessage = alertMessage + "Please enter your comments.\n";
	}
	if (!checkInternationalPhone(phone.value)) 
	{
		alertMessage = alertMessage + "Please enter a valid phone number.\n";
	}
	if (!validEmail(trim(email.value))) 
	{
		alertMessage = alertMessage + "Please enter a vaild E-mail address.\n";
	}
	if(alertMessage != "")
	{
		alert(alertMessage);
		cgi = false;
	}
	else 
	{
		cgi = true;
	}

	// IF CGI TESTS TRUE THEN SEND IT OUT!
	if (cgi) 
	{
		return true;
	}
	else 
	{
		return false;
	}
}

function checkString(field) {
// THIS FUNCTION CHECKS FOR BLANK FIELDS AND RETURNS TRUE IF BLANK
  if ((field == "") || (field == null)) {
    return (true);
  }
}

function checkWords(field, number) {
// THIS FUNCTION CHECKS FOR A CERTAIN NUMBER OF WORDS IN A FIELD
  var splitString = field.split(" ");
  if (splitString.length < number) {
    return (true);
  }
}

function checkForLetters(field) {
// THIS FUNCTION RETURNS TRUE IF A FIELD CONTAINS CHARACTERS OTHER THAN NUMBERS
  for (var i = 0; i < field.length; i++) {
    if ((field.charAt(i) < "0") || (field.charAt(i) > "9")) {
      return (true);
      break;
    }
  }
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
function validEmail(field) {
// THIS FUNCTION TESTS FOR A VALID EMAIL ADDRESS AND RETURNS TRUE IF VALID
  var email = field;
  var invalid = "~`|#$%&*()+={}!\"<>?/[]:; \\";
  var atCount = 0;
  var result = true;

  if (email.length < 5) {
    result = false;
  }
  if ((email.indexOf("@") == -1) || (email.indexOf("@") == 0)) {
    result = false;
  }
  if (email.indexOf(".") == -1) {
    result = false;
  }

  for (var i = 0; i < email.length; i++) {
    if ((invalid.indexOf(email.charAt(i)) != -1) && (result == true)){
      result = false;
    } 

    if (email.charAt(i) == "@") {
      atCount++;
    }
  }
  
  if (atCount > 1) {
    result = false;
  }

  if (result == false) {
    return (false);
  }
  else {
    return (true);
  }
}
function ValidateNum(input,event)
{
	var keyCode = event.which ? event.which : event.keyCode;
	if(parseInt(keyCode)>=48 && parseInt(keyCode)<=57)
	{
		return true;
	}
	return false;
}
function ValidateNum1(input,event)
{
	var keyCode = event.which ? event.which : event.keyCode;
	if(parseInt(keyCode)>=48 && parseInt(keyCode)<=57)
	{
		return true;
	}
	else if(parseInt(keyCode)==45)
	{
		return true;
	}
	else if(parseInt(keyCode)==32)
	{
		return true;
	}
	return false;
}

