
/*============================================================================*/

/*

This routine checks the submission form

1. All mandatory fields have been entered
2. The email and confirmation email are the same
3. validates that the email format is valid xxxx@yyyy.yyy
4. validates that the zip code is valid A9A9A9
*/


function checksubform()
{			
	var errmess = "";
	
	
	if (document.subform.f_name.value == "") 
	{
		errmess += "Prenom obligatoire\n";
	}		
	if (document.subform.l_name.value == "") 
	{
		errmess += "Nom obligatoire\n";
	}
	if (document.subform.address1.value == "") 
	{
		errmess += "Adresse obligatoire\n";
	}	
		
	
	if (document.subform.zip.value == "") 
	{
		errmess += "Code Postal obligatoire\n";
	}	
	
	if (document.subform.city.value == "") 
	{
		errmess += "Ville obligatoire\n";
	}
	if (document.subform.phone.value == "") 
	{
		errmess += "Numero de telephone obligatoire\n";
	}

	if (document.subform.province.value == "") 
	{
		errmess += "Province obligatoire\n";
	}
	
	
	if (document.subform.message.value == "") 
	{
		errmess += "Message obligatoire\n";
	}

	if (document.subform.email.value == "") 
	{
		errmess += "Courriel obligatoire\n";
	}	
	if (document.subform.email_confirm.value == "") 
	{
		errmess += "Courriel de confirmation obligatoire\n";
	}
	
	
	if (emailcheck(document.subform.email.value)==false)
	{
		errmess += "Format de courriel invalide\n";
	}	
	if (emailcheck(document.subform.email_confirm.value)==false)
	{
		errmess += "Format de courriel de confirmation invalide\n";
	}	
	
	if (document.subform.email.value != "" && document.subform.email_confirm.value != "") 
	{
		if (document.subform.email.value != document.subform.email_confirm.value)
		{
			errmess += "Courriel est différent du courriel de confirmation\n";
		}
	}	

	if (errmess != "")
	{
		alert(errmess);
		return false;
	}
	return true;
}
function emailcheck(str) 
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1)
		{
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1)
		 {
		    return false
		 }

 		 return true					
}

function isUSZip(s) 
{

     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

     if (!reZip.test(s)) {
          //alert("Zip Code Is Not Valid");
          return false;
     }

	return true;
}
function isCanValidPostalcode(postalcode) 
{
    if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;	
    else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) 
			return true;			
    else return false;
  
   return true;
}

function submitchanges()
{
	document.recep.submit();
}
/*=======  phone number validation javasctipt if necessary ==================================*/

function checkInternationalPhone(strPhone)
{
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

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 trim(s)
{   
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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;
}

