 function isEmptyStringContactUs(text) {
   		if( text == null || text == '')
   			return true;
   			
   		var allSpaces = true;
   		
   		for(var k=0; k< text.length; k++)
   		{
   			if( text.charAt(k) != ' ' )
   				return false;
   		}
   		
   		return allSpaces;
   }
   
	function replaceAll( text, subString, replacedWithString ) {
		var strReplaceAll = text;
		var intIndexOfMatch = strReplaceAll.indexOf( subString ); 
	
		while ( intIndexOfMatch != -1 )
		{
			strReplaceAll = strReplaceAll.replace( subString, replacedWithString );
			intIndexOfMatch = strReplaceAll.indexOf( subString );
		}
		return strReplaceAll;
	}
	
	function encode(psEncodeString)
	{
		//&#169;&#174;&#153;
		psEncodeString = replaceAll(psEncodeString, "™","&#8482");
		psEncodeString =  replaceAll(psEncodeString,"®","&#174;");
		psEncodeString =  replaceAll(psEncodeString,"©","&#169;");
		return psEncodeString ;
	}
	
	function decode(psEncodeString)
	{
		psEncodeString = replaceAll(psEncodeString, "&#8482","™");
		psEncodeString =  replaceAll(psEncodeString,"&#174;","®");
		psEncodeString =  replaceAll(psEncodeString,"&#169;","©");
		return psEncodeString ;
	}
		
	function encodeParams()
	{
		document.contactUsForm.firstName.value = encode(document.contactUsForm.firstName.value);
		document.contactUsForm.lastName.value = encode(document.contactUsForm.lastName.value);
		document.contactUsForm.company.value = encode(document.contactUsForm.company.value );
		document.contactUsForm.userRequest.value = encode(document.contactUsForm.userRequest.value);
	}
	
	
	function decodeParams()
	{
		document.contactUsForm.firstName.value = decode(document.contactUsForm.firstName.value);
		document.contactUsForm.lastName.value = decode(document.contactUsForm.lastName.value);
		document.contactUsForm.company.value = decode(document.contactUsForm.company.value );
		document.contactUsForm.userRequest.value = decode(document.contactUsForm.userRequest.value);
	}
	
		
	function submitForm() {	
	    trimAttributes();
		if (validateAttributes()) {
			//encodeParams();
			document.contactUsForm.submit();  
			//decodeParams();
		} 
	}
	
	
	function trimAttributes()
   {
	   document.contactUsForm.firstName.value =trimAll(document.contactUsForm.firstName.value);
	   document.contactUsForm.lastName.value=trimAll(document.contactUsForm.lastName.value);
	   document.contactUsForm.company.value =trimAll(document.contactUsForm.company.value);
	   document.contactUsForm.phone.value =trimAll(document.contactUsForm.phone.value);
	   document.contactUsForm.email.value =trimAll(document.contactUsForm.email.value);
	   document.contactUsForm.fax.value =trimAll(document.contactUsForm.fax.value);
	   document.contactUsForm.userRequest.value =trimAll(document.contactUsForm.userRequest.value);
	   
   }
	
	
	function isInteger(s) {   
		//var digits = "0123456789"; // non-digit characters which are allowed in phone numbers
		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 checkPhoneLength (strPhone) {
		if (strPhone.length > 25 ) {		
			return false; 
		
		} else {
			return true;
		}
	}
	
	function checkInternationalPhone(strPhone){
		//var minDigitsInIPhoneNumber = 9;
		var s = checkValidChars(strPhone);
		return (isInteger(s));
	}

	function checkValidChars(s) {   		
		var phoneNumberDelimiters = "()- ";
		// characters which are allowed in international phone numbers// (a leading + is OK)
		var validWorldPhoneChars = phoneNumberDelimiters + "+";// Minimum no of digits in an international phone no.
		
		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 (validWorldPhoneChars.indexOf(c) == -1) returnString += c; 
		}
		return returnString;
	}

	function checkContactUsEnter(e) { 
		var characterCode;
		
		if(e && e.which){ 
			e = e;
			characterCode = e.which;
		}
		else{
			e = event;
			characterCode = e.keyCode;
		}
		
		if(characterCode == 13){ 
			submitForm();
		}
		return true;
	}
	
	function validateAttributes() {
	
		var firstName = document.contactUsForm.firstName.value;
		var lastName = document.contactUsForm.lastName.value ;
		var company = document.contactUsForm.company.value ;
		var country = (document.contactUsForm.country.options[document.contactUsForm.country.selectedIndex]).value ;
		var purpose = document.contactUsForm.purpose.options[document.contactUsForm.purpose.selectedIndex].value;
		var category = document.contactUsForm.category.options[document.contactUsForm.category.selectedIndex].value;
		var phone = document.contactUsForm.phone.value;
		var email = document.contactUsForm.email.value;
		var userRequest = document.contactUsForm.userRequest.value;
		var isAttrValid = true;
		
		 if ( isEmptyStringContactUs(firstName) )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.firstName.focus();
		 }else  if ( isEmptyStringContactUs(lastName) )
		 {
			isAttrValid = false;
		 	document.contactUsForm.lastName.focus();
		 }else  if ( isEmptyStringContactUs(company) )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.company.focus();
		 }else  if ( isEmptyStringContactUs(country) )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.country.focus();
		 }else  if ( isEmptyStringContactUs(phone) )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.phone.focus();
		 }
		 else  if ( validatePhoneNumber() == false )
		 {
		 	document.contactUsForm.phone.focus();
		 	return false;
		 }
		 else  if ( validateFaxNumber() == false )
		 {
		 	document.contactUsForm.fax.focus();
		 	return false;
		 }else  if ( isEmptyStringContactUs(email) )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.email.focus();
		 }
		  else  if ( validateEMailFormat() == false )
		 {
		 	document.contactUsForm.email.focus();
		 	return false;
		 }else  if ( isEmptyStringContactUs(purpose) )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.purpose.focus();
		 }else  if ( isEmptyStringContactUs(category) )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.category.focus();
		 }else  if ( isEmptyStringContactUs(userRequest)  )
		 {
		 	isAttrValid = false;
		 	document.contactUsForm.userRequest.focus();
		 }
		 
		if( isAttrValid == false )	 
		{
			alert (missingFieldsErrMsg);
			return false; 
		}
		
		return  checkFieldsSize(firstName, lastName, company, email, userRequest);
   }


	
	function validatePhoneNumber () {
		var phone = document.contactUsForm.phone.value;
			if (checkInternationalPhone (phone)) {
				return true;
			
			} else {
				alert(phoneNumErrMsg);
				return false;
			}
	}

	function validateFaxNumber () {
		var fax = document.contactUsForm.fax.value;  
		
		if (fax == null || fax== "" ) {
			return true;
		
		} else {
					
					if (checkInternationalPhone (fax)) {
					   return true;
					
					} else {
						alert(faxNumErrMsg);	
						document.contactUsForm.fax.focus();	
						return false;
					}
		}
	}
	 
	function validateEMailFormat () {
		var email = document.contactUsForm.email.value;		
		
		var atIndex = email.lastIndexOf('@');
		var dotIndex = email.lastIndexOf('.');	
		var stringBeforeAt =  email.substring(0, atIndex);     
		var stringafterAt =  email.substring(atIndex+1 , email.length);   

	/*	if(stringBeforeAt.indexOf("@") != -1 || stringBeforeAt.indexOf("\\") != -1
		 || stringBeforeAt.indexOf("[") != -1 || stringBeforeAt.indexOf("]") != -1  
		 || stringBeforeAt.indexOf(";") != -1  || stringBeforeAt.indexOf("\"") != -1 */
		 
		 if(email.indexOf('.')==0)
		 {
			alert(emailErrMsg);		
			return false;
		 }
	
	//	 !/^[+?^~!#$%&*+=/|®]+
		
		if( ( stringafterAt == null  || stringafterAt =='') || (stringBeforeAt == null || stringBeforeAt =='') ) {
			alert(emailErrMsg);		
			return false;
		}
		
		if(!/^[\.0-9A-Za-z-_-]+.$/.exec( stringafterAt) || !/^[`~\"'"+?^~!#$%&*=\/|®\.0-9A-Za-z-_-{}]+$/.exec( stringBeforeAt)
			||stringBeforeAt.indexOf("\"") != -1 ) {
			alert(emailErrMsg);		
			document.contactUsForm.email.focus();
			return false;
		}

	   if (email.indexOf('@') != email.lastIndexOf('@') || atIndex == -1 || atIndex == 0 ||
	 		dotIndex == -1 || dotIndex == 0 || atIndex == dotIndex +1 || atIndex +1 == dotIndex || 
			stringBeforeAt.lastIndexOf(".") + 1 == atIndex  || email.length == email.lastIndexOf(".") + 1 ||
			stringafterAt.indexOf(".") == 0) {
			
			alert(emailErrMsg);			
			document.contactUsForm.email.focus();
			return false;
		} else {
		
			dotIndex = stringafterAt.lastIndexOf('.');	
			stringAfterDot = stringafterAt.substring(dotIndex, stringafterAt.length);   

			if (stringAfterDot.indexOf('.') == -1 ) {
				alert(emailErrMsg);
				document.contactUsForm.email.focus();
				return false;
			}  
			
			while (dotIndex !=-1 ) {
				email = email.replace('.', '');

				if (dotIndex == email.indexOf('.')) {
					alert(emailErrMsg);
					document.contactUsForm.email.focus();
					return false;
				}	else {
					dotIndex = email.indexOf('.');
				}
			} 
		}
		return true;
	}

	function checkFieldsSize(firstName, lastName, company, email, userRequest) {
		if (firstName.length > 255 ) {
			alert(tooLongFirstName);
			document.contactUsForm.firstName.focus();
			return false;
			
		} else if (lastName.length > 255 ) {
			alert(tooLongLastNameErrMsg);
			document.contactUsForm.lastName.focus();
			return false;
		
		} else if (company.length > 255 ) {
			alert(tooLongCompanyErrMsg);	
			document.contactUsForm.company.focus();
			return false;
		
		} else if (email.length > 255 ) {
			alert(tooLongEmailErrMsg);
			document.contactUsForm.email.focus();	
			return false;
			
		} else if (userRequest.length > 1000 ){
			alert(tooLongrequestErrMsg);
			document.contactUsForm.userRequest.focus();	
			return false;
		}
		return true;
	}
	
	function trimAll(stringToTrim) 
    {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
    }



	function doKeypress(txtDesc, maxLenght)
		{
			maxLength = maxLenght;
			value = txtDesc.value;
			if(maxLength && value.length > maxLength-1)
			{
				event.returnValue = false;
				maxLength = parseInt(maxLength);
			}
		}
		function doBeforePaste(txtDesc, maxLenght)
		{
		   maxLength = maxLenght;
			if(maxLength)
			{
				event.returnValue = false;
			}
		}
		function doPaste(txtDesc, maxLenght)
		{
			maxLength = maxLenght;
			value = txtDesc.value;
			if(maxLength)
			{
				event.returnValue = false;
				maxLength = parseInt(maxLength);
				var oTR = txtDesc.document.selection.createRange();
				var iInsertLength = maxLength - value.length + oTR.text.length;
				var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
				oTR.text = sData;
			}
		}
		
	
	
			
