// JavaScript Document
readCookie("ct_company");
readCookie("ct_name");
readCookie("street_add");
readCookie("city");
readCookie("state");
readCookie("zip");
readCookie("phone");
readCookie("fax");
readCookie("email");

function validate_form(thisform)
{	
	with (thisform)
	{	
	if (emptyvalidation(ct_company,"Woops! You forgot to fill in your Company Name")==false) 
		{
		ct_company.focus();
		return false;	
		}
	if (emptyvalidation(ct_name,"Woops! You forgot to fill in your Contact Name")==false) 
		{
		ct_name.focus();
		return false;	
		}	
	if (emptyvalidation(street_add,"Woops! You forgot to fill in your Street Address")==false) 
		{
		street_add.focus();
		return false;	
		}	
		if (emptyvalidation(city,"Woops! You forgot to fill in your City")==false) 
		{
		city.focus();
		return false;	
		}	
	if (emptyvalidation(state,"Woops! You forgot to fill in your Province/State")==false) 
		{
		state.focus();
		return false;	
		}	
	if (emptyvalidation(zip,"Woops! You forgot to fill in your Postal/Zip Code ")==false) 
		{
		zip.focus();
		return false;	
		}	
	if (emptyvalidation(phone,"Woops! You forgot to fill in your phone")==false) 
		{
		phone.focus();
		return false;	
		}	
	if (checkInternationalPhone(phone.value)==false){
		alert("Woops! You have entered an invalid  Phone Number")
		phone.select();
		phone.focus()
		return false
	}
	
	if(thisform.fax.value!="")
	{
		if (checkInternationalPhone(fax.value)==false){
		alert("Woops! You have entered an invalid Fax")
		fax.select();
		fax.focus()
		return false
	}	
		}
	/*if (emptyvalidation(fax,"Woops! You forgot to fill in your Fax ")==false) 
		{
		fax.focus();
		return false;	
		}*/	
	if (emptyvalidation(email,"Woops! You forgot to fill in your Email Address")==false) 
		{
		email.focus();
		return false;	
		}	
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = thisform.email.value;
  
	if(reg.test(address) == false) { 
      alert('Woops! You have entered an invalid Email Address');
	  email.focus();
	  email.select();
      return false;
   }
	
	

  
}
  
	if(thisform.ct_remember.checked==true)toMem(thisform); else delMem(thisform);
	thisform.submit();
}	



function emptyvalidation(entered, alertbox)
{
	with (entered)
	{
		while (value.charAt(0) == ' ')
			value = value.substring(1);
		while (value.charAt(value.length - 1) == ' ')
			value = value.substring(0, value.length - 1);
		if (value==null || value=="")
		{
			if (alertbox!="") alert(alertbox);
			return false;
		}
		else return true;
	}
}

function newCookie(name,value,days) 
{
 var days = 7;   // the number at the left reflects the number of days for the cookie to last
                 // modify it according to your needs
	if (days) 
	{
	   var date = new Date();
	   date.setTime(date.getTime()+(days*24*60*60*1000));
	   var expires = "; expires="+date.toGMTString(); 
	}
	
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/"; 
}

function readCookie(name) 
{
   var nameSG = name + "=";
   var nuller = '';
  if (document.cookie.indexOf(nameSG) == -1)
    return nuller;

   var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); }
    return null; 
}

function eraseCookie(name) 
{
	newCookie(name,"",1); 
}

function toMem(a) 
{
   
   newCookie('ct_company', document.mail_form.ct_company.value); 	// field you wish to have the script remember
	newCookie('ct_name', document.mail_form.ct_name.value); 	// field you wish to have the script remember
	newCookie('street_add', document.mail_form.street_add.value); 	
	newCookie('city', document.mail_form.city.value); 	
	newCookie('state', document.mail_form.state.value); 	
	newCookie('zip', document.mail_form.zip.value); 
	newCookie('phone', document.mail_form.phone.value); 	
	newCookie('fax', document.mail_form.fax.value); 	
	newCookie('email', document.mail_form.email.value); 	// field you wish to have the script remember
}

function delMem(a) 
{
readCookie("ct_company");
eraseCookie("ct_name");
eraseCookie("street_add");
eraseCookie("city");
eraseCookie("state");
eraseCookie("zip");
eraseCookie("phone");
eraseCookie("fax");
eraseCookie("email");
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// 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;

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 );
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
