// Contents copyright 1999-2003 YukonSoft Corporation 
//
function getCookie(name) {
var cname = name + "=";
var dc = document.cookie;
	if (dc.length > 0) {
		begin = dc.indexOf(cname);
			if (begin != -1) {
				begin += cname.length;
				end = dc.indexOf(";",begin);
				if (end == -1) end = dc.length;
				return unescape(dc.substring(begin,end));
			}
		}
		return null;
	}

function setCookie(name, value) {
var now = new Date();
var then = new Date(now.getTime() + 315360000);
document.cookie = name + "=" + escape(value) + ";expires=" + then.toGMTString() + ";path=/";
}

/* Get information from browser. */

function getInfo(form) {
	form.info.value = "Browser Information: " + navigator.userAgent;
}

/* Get previous value of element. */

function getValue(element) {
var value = getCookie(element.name);
    if (value != null) element.value = value;
}
    
/* Set new value for element. */

function setValue(element) {
setCookie(element.name, element.value);
}

function getSelectedButton(buttonGroup) {
	for (var i = 0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked) {
			return buttonGroup[i].value
			}
		}
	return 0
}

/* Function to efficiently fix elements that are not filled. */

function fixElement(element, message) {
alert(message);element.focus();
}

function check(input,teststring) {
	var ok = true;
	var chr;
	var found;


	for (var i = 0; i < input.length && ok; i++) {
		chr = input.charAt(i);
		var found = false;
		for (var j = 0; j < teststring.length && !found; j++) {
			if ( chr == teststring.charAt(j) ) found = true;
		}
		if (!found) ok = false;
		}
	return ok;
	}

function isMailReady(form) {
var passed = false;    

	if (form.first_name.value == "")
	{
	    fixElement(form.first_name, "Please include your First Name.");    
        form.first_name.focus();
        form.first_name.select();
     }
	else if (!isValidName(form.first_name))
	{
	}

	else if (form.last_name.value == "")
	{
	    fixElement(form.last_name, "Please include your Last Name.");    
        form.last_name.focus();
        form.last_name.select();
	}

	else if (!isValidName(form.last_name))
	{
	}

    else if (form.address.value == "") 
    {
	    fixElement(form.address, "Please include your Billing street address.");    
        form.address.focus();
        form.address.select();
    }    

	else if (!isValidStreet(form.address))
	{
	}

    else if (form.city.value == "") 
    {
    	fixElement(form.city, "Please include your City.");    
        form.city.focus();
        form.city.select();
    }    

	else if (!isValidName(form.city))
	{
	}

    else if (form.state.value == "") 
    {
		alert("Please include your State."); 
    }    


    else if (form.zip.value == "") 
    {
	    fixElement(form.zip, "Please include ZIP or Postal Code.");    
        form.zip.focus();
        form.zip.select();

	}

	else if (!isValidZip(form.zip))
	{
	}

    else if (form.pnpcountry.value == "") 
    {
		alert("Please include your Country.");     	    
    }    


    else if (form.phone.value == "") 
    {
	    fixElement(form.phone, "Please include your Telephone Number.");    
        form.phone.focus();
        form.phone.select();

	}

	else if (!check(form.phone.value, "1234567890/- ()" )) {
		alert(form.phone.value+" is not a valid phone number.\nPlease try again.");
   	}

    else if (form.email.value.indexOf("@") == -1 ||
             form.email.value.indexOf(".") == -1) 
    {
    	fixElement(form.email, "Please include a proper email address.");    
        form.email.focus();
        form.email.select();
   
    }
	else
	{
	  passed = true;
	}

	if (passed == true)
	{
      if (form.address2.value == "" && form.city2.value == "" && form.zip2.value == "")  {
         form.first_name2.value= form.first_name.value;
         form.last_name2.value= form.last_name.value;
         form.address2.value = form.address.value;
         form.city2.value = form.city.value;
         form.state2.value = form.state.value;
         form.zip2.value = form.zip.value;
         form.country2.value = form.pnpcountry.value;
      }
      else {
         passed = false;
         if (form.address2.value == "") 
         {
	         fixElement(form.address2, "Please include your Shipping Street Address.");    
            form.address2.focus();
            form.address2.select();
         }    
   	   else if (!isValidStreet(form.address2))
	      {
	      }
         else if (form.city2.value == "") 
         {
    	      fixElement(form.city2, "Please include your Shipping Address City.");    
            form.city2.focus();
            form.city2.select();
         }    
         else if (!isValidName(form.city2))
	      {
	      }
         else if (form.state2.value == "") 
         {
            //	    fixElement(form.state, "Please include your State.");    
		      alert("Please include your Shipping Address State."); 
         }    

         else if (form.zip2.value == "") 
         {
	         fixElement(form.zip2, "Please include your Shipping Address Zipcode.");    
            form.zip2.focus();
            form.zip2.select();
	      }

      	else if (!isValidZip(form.zip2))
	      {
	      }

         else if (form.country2.value == "") 
         {
    	    fixElement(form.country2, "Please include your Shipping Address Country.");    
            form.country2.focus();
            form.country2.select();
         }
         else passed = true;

      }
	}
	return passed;
	
}

function isContactReady(form) {
var passed = false;    

	if (form.first_name.value == "")
	{
	    fixElement(form.first_name, "Please include your First Name.");    
        form.first_name.focus();
        form.first_name.select();
     }
	else if (!isValidName(form.first_name))
	{
	}

	else if (form.last_name.value == "")
	{
	    fixElement(form.last_name, "Please include your Last Name.");    
        form.last_name.focus();
        form.last_name.select();
	}

	else if (!isValidName(form.last_name))
	{
	}

    else if (form.address.value == "") 
    {
	    fixElement(form.address, "Please include your Billing street address.");    
        form.address.focus();
        form.address.select();
    }    

	else if (!isValidStreet(form.address))
	{
	}

    else if (form.city.value == "") 
    {
    	fixElement(form.city, "Please include your City.");    
        form.city.focus();
        form.city.select();
    }    

	else if (!isValidName(form.city))
	{
	}

    else if (form.state.value == "") 
    {
	    ///fixElement(form.state, "Please include your State.");    
		alert("Please include your State."); 
        //form.state.focus();
        //form.state.select();
    }    


    else if (form.zip.value == "") 
    {
	    fixElement(form.zip, "Please include ZIP or Postal Code.");    
        form.zip.focus();
        form.zip.select();

	}

	else if (!isValidZip(form.zip))
	{
	}

    else if (form.pnpcountry.value == "") 
    {
       //	fixElement(form.pnpcountry, "Please include your Country.");
		alert("Please include your Country.");            
       // form.pnpcountry.focus();
       // form.pnpcountry.select();
	   //	return false;
    }    

    else if (form.phone.value == "") 
    {
	    fixElement(form.phone, "Please include your Telephone Number.");    
        form.phone.focus();
        form.phone.select();

	}

	else if (!check(form.phone.value, "1234567890/- ()" )) {
		alert(form.phone.value+" is not a valid phone number.\nPlease try again.");
   	}

    else if (form.email.value.indexOf("@") == -1 ||
             form.email.value.indexOf(".") == -1) 
    {
    	fixElement(form.email, "Please include a proper email address.");    
        form.email.focus();
        form.email.select();
   
    }
	else
	{
		passed = true;
	}

	return passed;
	
}


//Name is characters and , . ' ` -
function isValidName(theForm)
{
   var theName = 
      new String(theForm.value)
   var theChar
   for (var i = 0; i < theName.length; i++)
   {
      theChar = theName.charAt(i)
      //is it not a character?

      if ( !isLetter(theChar) )
      {
         //is it not one of the allowed punctuation?
         if ( !( (theChar == '-') || 
                 (theChar == '.') || 
                 (theChar == ',') ||
                 (theChar == '\'') ||
                 (theChar == '`') ) )
         {
            alert(theForm.value+"Use only letters and these characters: - . , \' `; try again")
            theForm.focus();
        	theForm.select();
            return false;            
         } 
      }
   }
   return true
}


//Street is alpha-numeric and , . ' ` \ -
function isValidStreet(theForm)
{
   var theStreet = 
      new String(theForm.value)
   var theChar
   for (var i = 0; i < theStreet.length; i++)
   {
      theChar = theStreet.charAt(i)
      //is it not a character or a digit?
      if ( !isAlphaNumeric(theChar) )
      {
         //is it not one of the allowed punctuation?
         if ( !( 
               (theChar == '.') || 
               (theChar == ',') ||
               (theChar == '\'') ||
               (theChar == '`') ||
               (theChar == '\\') ||
               (theChar == '-') ) )
         {
            alert("Use only letters and these characters: . , \' ` \\ -; try again");
            theForm.focus();
            theForm.select();
            return false;            
         } 
      }
   }
   return true
}

//Zip is alphanumeric and includes -
function isValidZip(theForm)
{
   var theZip = 
      new String(theForm.value)
   var theChar
   for (var i = 0; i < theZip.length; i++)
   {
      theChar = theZip.charAt(i)
      //is it not a character or a digit?
      if ( !isAlphaNumeric(theChar) )
      {
         //is it not one of the allowed punctuation?
         if ( !( 
               (theChar == '-') ) )
         {
            alert("Invalid Zip; try again")
            theForm.focus();
            theForm.select();
            return false;            
         } 
      }
   }
   return true
}

//2 letters only
function isValidState(theForm)
{
   var theState = new String(theForm.value);
   var theChar;

   if (!isLength(theState,2))
   {
      alert("only 2 characters please")
      theForm.focus();
      theForm.select();
      return false;            
   } 

   for (var i = 0; i < theState.length; i++)
   {
      theChar = theState.charAt(i)
      //is it not a character?
      if ( !isLetter(theChar) )
      {
         alert("Invalid state; try again")
         theForm.focus();
         theForm.select();
         return false;            
      }
   }
   return true
}

//theChar includes letters only
function isLetter(theChar)
{
   //Blank space is considered a character
   var charArray = new Array(
      ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
      'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
      'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 
      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
      'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
      's', 't', 'u', 'v', 'w', 'x', 'y', 'z')

      for (j = 0; j < charArray.length; j++)
         if (theChar == charArray[j]) return true;
       return false;
}

//Characters and digits
function isAlphaNumeric(theChar)
{
   if ( !isDigit(theChar) )
      if ( !isLetter(theChar) ) return false;
   return true;
} 

//Digit is one of the characters 0, 1, ..., 9
function isDigit(theDigit)
{
   digitArray = new Array('0','1','2','3','4',
         '5','6','7','8','9')

      for (j = 0; j < digitArray.length; j++)
         if (theDigit == digitArray[j])
            return true
   return false
}

//---Test that aString is exactly aLength
function isLength(aString, aLength)
{
   if (isMaxLength(aString, aLength) && 
         isMinLength(aString, aLength))
      return true
   else
      return false
}

//---Test that aString is no longer than maxLength
function isMaxLength(aString, maxLength) 
{
	if (aString.length <= maxLength)
      return true
   else
      return false
}

//---Test that aString is no shorter than minLength
function isMinLength(aString, minLength) 
{
	if (aString.length >= minLength)
      return true
   else
      return false
}

