// JavaScript Document

 function TrimStr(data)
 {
 /*
 Remove leading and trailing spaces of a string.
 
 Functions required
    NONE
 
 */
    var input=data.value;
 
    while (input.charAt(0) == " ") {
       input = input.substring(1,(input.length));
    };
    while (input.charAt(input.length-1) == " ") {
       input = input.substring(0,(input.length-1));
    }
    data.value = input;
    return data;
 }

function stripCharSin (str)
{
	var strsin="";
	var ch="";
	var fldvalue = str.value;

	for (var x = 0 ; x < fldvalue.length; x++)
            {
		ch=fldvalue.substring(x, x+1);
		if (ch == " "){
	   		continue;
		}else{
			strsin=strsin+ch;
		}
	    }
	str.value=strsin;
}


 
function IsDigit(Number)
  {
  /*
  Accept only numbers from 0 to 9
  
  Functions required
     NONE
  
  */
     if ((Number > "0" ||  Number=="0") &&  (Number < "9" || Number=="9"))
         return true;
      else
         return false;
  
  } 
 
function IsLetter(Letter)
  {
  /*
  Accept only uppercase letter from A to Z
  
  Functions required
     NONE

  */
     if ((Letter> "A" ||  Letter=="A") &&  (Letter< "Z" || Letter=="Z"))
         return true;
      else
         return false;
  
  } 
 

function isPostalCode(o){
  var str = o.value;
  
  str = str.replace(/[\s\-]/g,'');
  
  
	var exp = /^[a-z]\d[a-z]\d[a-z]\d$/i; //Canadian Postal Code
  
	if (exp.test(str)){
		str= str.toUpperCase();
		o.value = str.substring(0,3) + ' ' + str.substring(3);
	}else{
		if (str != ''){
				alert (o.value + " is not an acceptable value for Postal Code. \nPlease use the format: A9A9A9 (no space) for Canadian Postal Code.");
				o.focus();
        }
        o.value = '';
    }
  
}  

/******** Phone number functions ***************/
function isNum (str){

	fldValue = str.value;
	for (var i = 0 ; i < fldValue.length; i++) {
		var ch=fldValue.substring(i, i+1);
		if (ch < "0" || ch > "9"){ 
			return false
			break;
 }
}
return true;}

function CheckPhone(data)
{

/*
Starts the validation process of a phone number.

Functions required
   Highlight
   TrimTrim
   TestPhone

*/

   data=TrimStr(data);

   if (data.value == "") {
      return true;
   }

   if ((TestPhone(data)) && (data.value.length >= 12 && data.value.length < 18)) {
      return true;
   } else {
      alert (data.value+" Is not an acceptable value for the Phone Number.  It should be of the format ###-###-####, please change.");
      data.focus();
      Highlight(data);
      return false;
   }
}

function TestPhone(data)
{

/*
A phone number must follow a specific format : 999-999-9999 9999
	area code - city code - number ["space" extension]
	Position 3 and 7 have to be dash
	if extension, position 13 must be a space.

Functions required
   IsDigit

*/

    var tempvar="";
    var input=data.value;
    var ch;
    var k;

    for ( k = 0; k < input.length; k++)
        { ch = input.substring(k, k + 1);
        if (((ch > "0" ||  ch=="0") &&  (ch < "9" || ch=="9")) || (ch=="-") || (ch==" ")) {
	    if (!(IsDigit(ch)) && (k !=3 && k !=7 && k != 12)){
		return false;
            } continue;
        } else
           return false;
       }
        if (input.charAt(3) !=  "-" || input.charAt(7) !=  "-"  ) {
	    return false;
	}

	if ((input.length >12) && (input.charAt(12) != " "))
            return false;
    return true;

}

function verifyPhone(str){

	stripCharPhone(str);
	if ((isNum(str)) && (isPhoneLength(str)) && (checkArea(str))){ 
		formatPhone(str);
	}else{
	errorPhone(str)
 }
}

function checkArea(str){

		var area1=str.value.substring(0,1);

		if (area1=="1"){
			return false
		}
return true}

function isPhoneLength(str)
  {
	var strlength=str.value;
	if(strlength.length > 0 && strlength.length < 10) 
    //if(strlength.length != 10) 
	{
		return false;
    }
    return true;
  }



function errorPhone(data)
{
    alert (data.value + " Is not an Acceptable Value for a phone number.\n Please enter the number with the area code first (omit spaces or dashes).");
    data.focus();
    return;
 }


function stripCharPhone (str){

	var strchar="";
	var ch="";
	var fldvalue = str.value;
	for (var x = 0 ; x < fldvalue.length; x++)
	{
		ch=fldvalue.substring(x, x+1);
		if (ch == " " || ch == ")" || ch== "-" || ch== "(")
		{
	   		continue;
		}
		else
		{
			strchar=strchar+ch;
		}

	}
	str.value=strchar;
}

function formatPhone(str){

	var char1="(";
	var char2=")";
	var char3="-";
	var char4=" ";
	var strphone=str.value;
	var str1=strphone.substring(0,3);
	var str2=strphone.substring(3,6);
	var str3=strphone.substring(6,10);

	if(str.value.length == 0) {
		strphone = ""
	} else {
		strphone=char1+str1+char2+char4+str2+char3+str3;
	}
	str.value=strphone;

}


function validateEmailAddress(theAddress)
{
	var a = theAddress.value.indexOf("@");
	
	if ((a < 1) || (a == (theAddress.value.length - 1)))		
	{	
		alert("Invalid Email Address Format!");
		theAddress.value = "";
		theAddress.focus();
		return false;
	}else
	{	
		var emailName = theAddress.value.substring(0, a);
		var emailDomain = theAddress.value.substring(a+1);
		
		if (!validateEmailName(emailName))
		{	
			alert("Invalid Email Address Format!");
			theAddress.focus();
			theAddress.value = "";
			return false;
		}
		
		if (!validateEmailDomain(emailDomain))
		{	
			alert("Invalid Email Address Format!");
			theAddress.focus();
			theAddress.value = "";
			return false;
		}		
	}
	
	return true;
}


function validateEmailDomain(theDomain)
{
	var dot = theDomain.indexOf(".");
	
	if ((dot < 1) || (dot == (theDomain.length -1)))
	{	return false;
	}
	
	return validateEmailName(theDomain);
}


function validateEmailName(theName)
{	/*	Returns true if theAddress is a valid email name, else returns false.
		The name is the bit before the @sympatico.ca.
	*/

	if ((theName.length < 1) || (theName.length > 25))
	{	return false;
	}

	var encoded = escape(theName);
	if ((encoded.indexOf("%") < 0) &&
		(encoded.indexOf("*") < 0) &&
		(encoded.indexOf("+") < 0) &&
		(encoded.indexOf("/") < 0) &&
		(encoded.indexOf("@") < 0))
	{	return true;
	}else
	{	return false;
	}	
}

function checkempty() {
   errors5=false;
   errorMessage="";

   if (document.step2.FirstName.value == "") {
		errorMessage = errorMessage + "Your First Name \n";
		errors5=true;   
   }
    if (document.step2.LastName.value == "") {
		errorMessage = errorMessage + "Your Last Name \n";
		errors5=true;   
   }
    if (document.step2.ContactPhone.value == "") {
		errorMessage = errorMessage + "Your Contact Phone \n";
		errors5=true;   
   }
    if (document.step2.Email.value == "") {
		errorMessage = errorMessage + "Your Email Address \n";
		errors5=true;   
   }
   if (document.step2.MonthlyPlan.selectedIndex == "0") {
		errorMessage = errorMessage + "Choose Monthly Plan \n";
		errors5=true;   
   }
   if (document.step2.MainGoal.selectedIndex == "0") {
		errorMessage = errorMessage + "What Is Your Main Goal? \n";
		errors5=true;   
   }
   if (document.step2.ProjectStart.selectedIndex == "0") {
		errorMessage = errorMessage + "When Would You Like To Start? \n";
		errors5=true;   
   }
   if (document.step2.HowDidYouHear.selectedIndex == "0") {
		errorMessage = errorMessage + "How Did You Hear About Us? \n";
		errors5=true;   
   }
      	  
   return errors5;
}

function checkForm(form) {

    errorMessage="";
    
    /*isPostalCode(document.step2.PostalZipCode);
    
    isSIN(document.step2.SIN);*/
    
    checkempty(form);

	if (errors5) {
		alert("The following information is required: \n"+errorMessage); 
		return false;
	
    } else {
        return true;
    }
}

function errorDigit(data){

	alert (data.value + " Is not an Acceptable Value. Please try another number Again");
    data.focus();
    data.value="";
    return;
 }

function isNum (str){

	fldValue = str.value;
	for (var i = 0 ; i < fldValue.length; i++) {
		var ch=fldValue.substring(i, i+1);
		if (ch < "0" || ch > "9"){ 
			return false
			break;
 }
}
return true;}

function IsNumber(number){

	if (isNum(number)==false){
		errorDigit(number)
	}
}

function save()
{
  if(checkForm(document.step2))
  {
	if (confirm ('Please click \"OK\" to proceed or \"Cancel\" to review form.')){
	document.step2.SaveApp.value="Y";
	document.step2.submit();
	}
  }
}

function nextpage()
{
  document.step2.forward.value="Y";
  document.step2.submit();
}

 function confirmsubmit()
 {
   if(checkForm(document.step2))
   {
   	 if(confirm ('Thank you for taking the time to fill out this quote request form. To ' +
			'submit the form press \"OK\" and we ' +
			'will contact you shortly by email or phone or press \"Cancel\" to review the form.')){ /*+
			'our web site and make yourself familiar with our policies and procedures.')){*/
   	   document.step2.action="/cgi-bin/emailitold.pl";
   	   document.step2.submit();
   	 }
   }
 }
 
 function UpperCase(data){

	data.value=data.value.toUpperCase();

}

function isCity(o){
	var str = o.value;
	if (str.length > 0 && str.length < 3){
		alert("Invalid Entry: City must be at least 3 characters long.");
		o.focus();
		o.value = "";
	}
}

function verifyWord(word){

	UpperCase(word);
	var wordOrig=word.value
	stripCharWord(word);
	
	if (IsWord(word)==false){
		alert (word.value + " is not an acceptable value. Please try again.")
		word.value="";
        word.focus();

	}else{
		word.value=wordOrig
 }
}

function stripCharWord(word){

	var strchar="";
	var ch="";
	var fldvalue = word.value;
	for (var x = 0 ; x < fldvalue.length; x++){
		ch=fldvalue.substring(x, x+1);
		if (ch == " " || ch == "." || ch== "'" || ch== "-"){
	   		continue;
		}else{
			strchar=strchar+ch;
		}
	}
	word.value=strchar;
}

function IsWord(word)
{

  var str = word.value;
  for (var i = 0; i < str.length; i++) 
  {
    var ch = str.substring(i, i + 1);
    if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ')
	{
	  return false;
    }
  }
  return true;
}
