/*****************************************************
* global variable declaration
*****************************************************/
var digits = "0123456789"; 											// Declaring required variables
var phoneNumberDelimiters = "()- "; 									// non-digit characters which are allowed in phone numbers
																// characters which are allowed in international phone numbers
																// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10; 										// Minimum no of digits in an international phone no.
var ids=new Array('errPrenom','errNom','errNomCie','errProfession','errEmail','errPhone','errWorkstation'); // id object for in case of an invalid entry
var ids2=new Array('errNomCie','errContactName','errContactEmail','errLicenseCount');
var invalidaddress = new Array()
invalidaddress[0]="hotmail"
invalidaddress[1]="rocketmail"
invalidaddress[2]="yahoo"
invalidaddress[3]="zdnetmail"
invalidaddress[4]="gmail"
invalidaddress[5]="msn"
invalidaddress[6]="free"
invalidaddress[7]="hushmail"
invalidaddress[8]="aim"
invalidaddress[9]="gmx"
invalidaddress[10]="inbox"
invalidaddress[11]="gawab"
invalidaddress[12]="bluebottle"
invalidaddress[13]="bigstring"
invalidaddress[14]="zapak"
invalidaddress[15]="hotpop"

/*****************************************************
* Functions declaration
*****************************************************/
//----------------------------------------------------
// checkEmail
// ----------
//
// verify if the email is is an valid format
//
// input : Email in str format
// output: true or false
// Date creation: 2007-08-28
//----------------------------------------------------
function checkEmail(Email) {
	var invalidcheck = 0;
	var str = Email.value.toLowerCase();
	var filter = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (filter.test(str))
	{
		var tempstring = str.split("@")
		tempstring = tempstring[1].split(".")
		for (i=0; i<invalidaddress.length; i++)
		{
			if (tempstring[0] == invalidaddress[i])
				invalidcheck = 1
		}
		if (invalidcheck != 1)
			testresults = true
		else
		{
			alert("Please Enter an official Email Address!")
			testresults = false
		}
	}
	else
	{
	     alert("Please Enter a valid Email Address!")
		testresults=false
	}
	return (testresults)
}
//----------------------------------------------------
// VerifEmptyRequiredFieldsPartner
// ------------------------
//
// validate all the fields in the form before sending
// informations.
//
// input : Form contain all objects in the form
// output: true or false, if false, display the error and point
//          to that erro
// Date creation: 2007-08-28
//----------------------------------------------------
function VerifEmptyRequiredFieldsPartner(Form)
{
    	var count = 0;

     hideallids(Form);
	with (Form)
	{
  		for (var i=0; i<Form.length; i++) {
	          if (Form.elements[i].type == 'checkbox') {
				if (Form.elements[i].name.substring(0,8) == 'products' &&
					Form.elements[i].checked == true) {
          			count++;
					break;
				}
	          }
	     }
	     // s'il y a des choix et qu'il en a choisit aucun
		if (count == 0) {
			alert("You must select at least one product!");
			return false;
		}

	 	if (IsEmpty(CieName)) {
               showdiv('errNomCie');
			alert("The name of the company is required!");
			CieName.focus();
			return false;
          }
   		if (IsEmpty(ContactName)) {
    		     showdiv('errContactName');
			alert("Your contact name is required!");
			ContactName.focus();
			return false;
    		}
    		if (IsEmpty(ContactEmail)) {
               showdiv('errContactEmail');
			alert("Your contact email address is required!");
			ContactEmail.focus();
			return false;
          }
		if (!checkEmail(ContactEmail)) {
               showdiv('errContactEmail');
               alert("Your contact email address is invalid!");
			ContactEmail.focus();
			return false;
    		}
    		if (LicenseCount.value == null) {
               showdiv('errLicenseCount');
			alert("Le nombre de licenses est obligatoire!");
			LicenseCount.focus();
			return false;
          }
    	}
    	return true;
}
//----------------------------------------------------
// VerifEmptyRequiredFields
// ------------------------
//
// validate all the fields in the form before sending
// informations.
//
// input : Form contain all objects in the form
// output: true or false, if false, display the error and point
//          to that erro
// Date creation: 2007-08-28
//----------------------------------------------------
function VerifEmptyRequiredFields(Form)
{
     var involve = "";
	var hearus = "";
    	var phone = "";
    	var count = 0;

     hideallids(Form);
	with (Form)
	{
	     for (var i=0; i<Form.length; i++) {
	          if (Form.elements[i].type == 'checkbox') {
				if (Form.elements[i].name.substring(0,8) == 'products' &&
					Form.elements[i].checked == true) {
          			count++;
					break;
				}
	          }
	     }
	     // s'il y a des choix et qu'il en a choisit aucun
		if (count == 0) {
			alert("You must select at least one product!");
			return false;
		}
	 	if (IsEmpty(Email)) {
               showdiv('errEmail');
			alert("Your email address is required!");
			Email.focus();
			return false;
          }
		if (!checkEmail(Email)) {
               showdiv('errEmail');
               //alert("Your email is invalid!");
			Email.focus();
			return false;
    		}
   		if (IsEmpty(FirstName)) {
    		     showdiv('errPrenom');
			alert("Your first name is required!");
			FirstName.focus();
			return false;
    		}
   		if (IsEmpty(LastName)) {
    		     showdiv('errNom');
	     	alert("Your last name is required!");
	     	LastName.focus();
	     	return false;
    		}
	    	if (IsEmpty(CieName)) {
    		     showdiv('errNomCie');
	     	alert("Your company name i s required!");
	     	CieName.focus();
	     	return false;
    		}
	    	if (IsEmpty(Job)) {
    		     showdiv('errProfession');
	     	alert("Your job title is required!");
	     	Job.focus();
	     	return false;
    		}
          if (IsEmpty(CiePhone)) {
	    	     showdiv('errPhone');
    		     alert("The phone number is required!");
	     	CiePhone.focus();
	     	return false;
          }
          if (Workstation.options[Workstation.selectedIndex].text == "-->Select<--") {
	    	     showdiv('errWorkstation');
    		     alert("The number of Workstations is required!");
	     	Workstation.focus();
	     	return false;
          }
    	}
    	return true;
}
//----------------------------------------------------
// IsEmpty
// -------
//
// verify if the object is empty
//
// input : aTextField in str format
// output: true or false
// Date creation: 2007-08-28
//----------------------------------------------------
function IsEmpty(aTextField) {
   	if ((aTextField.value.length == 0) || (aTextField.value == null)) {
      	return true;
   	} else { return false; }
}
//----------------------------------------------------
// isInteger
// ---------
//
// verify if the object is an integer
//
// input : s in str format
// output: true or false
// Date creation: 2007-08-28
//----------------------------------------------------
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;
}
//----------------------------------------------------
// validatePhone
// ----------
//
// verify if the phone is valid
//
// input : phone in str format
// output: true or false
// Date creation: 2007-08-28
//----------------------------------------------------
function validatePhone(phoneField, format) {
   	var num = phoneField.value.replace(/[\(\)\.\-\ ]/g, '');
	var retour;

	if (isInteger(num)) {
	   	if(num.length == 10) {
     	   	//Email was valid.  If format type is set, format the Phone to the desired style.
        		retour = "(" + num.substring(0,3) + ")-" + num.substring(3, 6) + "-" + num.substring(6);
	   	} else { retour = phoneField.value; }
	   	phoneField.value = retour;
 	} else { phoneField.value = "Invalid format" }
}

//----------------------------------------------------
// hideallids
// ----------
//
// hide all object identify by is id in the array ids
//
// input : none
// output: none
// Date creation: 2007-08-28
//----------------------------------------------------
function hideallids(Form){
 	if (Form.name == "AskDemo") {
		//loop through the array and hide each element by id
		for (var i=0; i<ids.length; i++) {
			hidediv(ids[i]);
		}
	} else if (Form.name == "AskDemoPartner"){
	     //loop through the array and hide each element by id
		for (var i=0; i<ids2.length; i++) {
			hidediv(ids2[i]);
		}
	}
}
//----------------------------------------------------
// hidediv
// -------
//
// hide the object identify by is id
//
// input : id in string format
// output: none
// Date creation: 2007-08-28
//----------------------------------------------------
function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	} else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		} else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}
//----------------------------------------------------
// showdiv
// -------
//
// show the object identify by is id
//
// input : id in string format
// output: none
// Date creation: 2007-08-28
//----------------------------------------------------
function showdiv(id) {
	//safe function to show an element with a specified id

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	} else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		} else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}
//----------------------------------------------------
// Capitalize
// ----------
//
// Set the first letter in uppercase
//
// input : string to change
// output: none
// Date creation: 2007-08-28
//----------------------------------------------------
function Capitalize(str)
{
  	var Newstr = str.value.split(" ");
  	var max = Newstr.length;

  	str.value = "";
  	for(var Compt = 0; Compt < max; Compt++)
  	{
    		var FirstLetter = Newstr[Compt].substr(0,1);
    		var OtherLetter = Newstr[Compt].substr(1,99);

    		if(Compt == 0)
        		str.value = str.value.concat(FirstLetter.toUpperCase() + OtherLetter.toLowerCase());
    		else
        		str.value = str.value.concat(" " + FirstLetter.toUpperCase() + OtherLetter.toLowerCase());
  	}
}
//----------------------------------------------------
// CloseWindow
// ----------
//
// Close the window without asking comfirmation
// the open is use for IE7 because it ask confirmation
//
// input : none
// output: none
// Date creation: 2007-09-26
//----------------------------------------------------
function CloseWindow()
{
 	window.open('','_self','');
	window.close();

}


