function phoneValidator(s)
{
  if (s.value.length < 12)
  {
    alert("Please format phone/fax numbers as 999-999-9999 ");
	  s.focus();
	return (false);
 }

  var checkOK = "0123456789--";
  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
	   break;
    }
  }
  if (!allValid)
  {
    alert("Please format phone/fax numbers as 999-999-9999 ");
	 s.focus();     
         return (false);
  }
  return (true);
}

function textValidator(s)
{
if (s.value.length > 255)
  {
    alert("Please do not enter more than 255 characters ");
	  s.focus();
	return (false);
 }


var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:$%#!',. +/@%?_--&.\t\r\n\f";

  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only text and \" / % ? @ ! & . ' - $ #\" in this field.");
     	s.focus();   
		return (false);
  }
  return (true);
}

function zipValidator(s)
{
  if (s.value.length < 5)
  {
    alert("Please format zipcodes as 99999-9999 ");
	  s.focus();
     return (false);
 }

  var checkOK = "0123456789--";
  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
	   break;
    }
  }
  if (!allValid)
  {
    alert("Please format zipcodes as 99999-9999 ");
	 s.focus();     
	 return (false);
  }
  return (true);
}

function requiredValidator(s)
{
if (s.value.length < 1)
  {
    alert("This field is required! " );
	  s.focus();
     return (false);
 }


var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:$%#@%?!',. +/_--&.\t\r\n\f";

  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only text and \"! % ? / @ & ' $ # . - \" this field.");
     	s.focus();   
		return (false);
  }
  return (true);
}

var STATECODES = "AL/AK/AZ/AR/AS/CA/CO/CT/DE/DC/FM/FL/GA/GU/HI/ID/IL/IN/IA/KS/KY/LA/ME/MD/MA/MI/MN/MS/MO/MT/NE/NV/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/PW/RI/SC/SD/TN/TX/UT/VT/VA/VI/WA/WV/WI/WY/AE/AA/AP";


function stateValidator(myfield) {
	if (notNull(myfield.value) && isSize(myfield.value, 2) &&  isStateCode(myfield.value))
		return (true);
	else {
		myfield.focus();
		alert("Please enter a valid State postal code.");
		return (false);
	}
}

function isStateCode (str) {
	var newstring = str.toUpperCase()
	if (STATECODES.indexOf(newstring) != -1 && str.indexOf("/") ==  -1)
		return (true);
	else 
		return (false);
}


function notNull(str) {
	if (str.length == 0 )
		return (false);
	else 
		return (true);
}

function isSize(str, size) {
	if (str.length == size) 
		return (true);
	else
		return (false);
}

function priceValidator(s)
{
  var checkOK = "0123456789. ";
  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
	   break;
    }
  }
  if (!allValid)
  {
    alert("Please enter prices formatted as 999.99 ");
	 s.focus();     
         return (false);
  }
  return (true);
}

function numberValidator(s)
{

  var checkOK = "0123456789";
  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
	   break;
    }
  }
  if (!allValid)
  {
    alert("Please enter numbers only ");
	 s.focus();     
         return (false);
  }
  return (true);
}

function dateValidator(s){
        
        var indate=s.value;

        if (indate.indexOf("-")!=-1){
                var sdate = indate.split("-")
        }
        else {
                var sdate = indate.split("/")
        }

        var chkDate=new Date(Date.parse(indate))
        
        var cmpDate=(chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(chkDate.getYear())
        var indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
        if (indate2!=cmpDate){
                alert("You've entered an invalid date or date format.  Please use the MM/DD/YY format.");
	s.focus();
	return (false);
        }
        else {
                if (cmpDate=="NaN/NaN/NaN"){
                        alert("You've entered an invalid date or date format.  Please use the MM/DD/YY format.");
			s.focus();     
         		return (false);

                }
                else {
			return (true);
                        //alert("Your date is valid!");
                }       
        }

}

function intlPhoneValidator(s)
{
if (s.value.length < 1)
  {
    alert("Please enter a Phone Number! " );
	  s.focus();
     return (false);
 }
  var checkOK = "0123456789 +--";
  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
	   break;
    }
  }
  if (!allValid)
  {
    alert("International Phone numbers may include numbers and \" - & +\"");
	 s.focus();     
         return (false);
  }
  return (true);
}

function USAValidator(country,state,zip,postal){
var c = country;
var s= state;
var z = zip;
var post = postal;

	if (c=="USA") {
		return(stateValidator(s) && 	
		zipValidator(z))
		}
	if (c!="USA") {
		//alert(c.options[c.selectedIndex].value + " NOT USA!");
		return(requiredValidator(post))
	}
}

function phoneCheck(country,phone){
	var s = phone;
	var c = country;
		//alert(c.options[c.selectedIndex].value);
	
	if (c == "USA") {
		//alert(c.options[c.selectedIndex].value + "=USA!");
		return(phoneValidator(s))
		}
	if (c != "USA") {
		//alert(c.options[c.selectedIndex].value + " NOT USA!");
		return(intlPhoneValidator(s))
	}
}


// Checks the E-MAIL field.
function isEmail(s){   
	// Return false if e-mail field is blank.   
	if (s.value == ""){      
		alert("\nThe E-MAIL field is blank.\n\nPlease enter your e-mail address.")      
		s.focus();      
		return false;       
	}   
	// Return false if e-mail field does not contain a '@' and '.' .   
	if (s.value.indexOf ('@',0) == -1 || s.value.indexOf ('.',0) == -1){      
		alert("\nThe E-MAIL field requires a \"@\" and a \".\"be used.\n\nPlease re-enter your e-mail address.")      
		s.select();      
		s.focus();      
		return false;      
	}   
	else {      
		return true;      
	}   
}






function memoValidator(s)
{
if (s.value.length > 1000)
  {
    alert("Please enter fewer than 1000 characters! " );
	  s.focus();
     return (false);
 }


var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:$%#@%'?!,. +/_--&.\t\r\n\f";

  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only text and \"! % ? / @ & $ # . - \" this field.");
     	s.focus();   
		return (false);
  }
  return (true);
}


function simpleTextValidator(s)
{
if (s.value.length > 255)
  {
    alert("Please do not enter more than 255 characters ");
	  s.focus();
	return (false);
 }


var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters and numbers in this field.");
     	s.focus();   
		return (false);
  }
  return (true);
}





function simpleRequiredValidator(s)
{

if (s.value.length < 1)
  {
    alert("This field is required! " );
	  s.focus();
     return (false);
 }

if (s.value.length > 255)
  {
    alert("Please do not enter more than 255 characters ");
	  s.focus();
	return (false);
 }


var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  var checkStr = s.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters and numbers in this field.");
     	s.focus();   
		return (false);
  }
  return (true);
}

