// This code find the browser name and capture the keyDown or keyUp event

var isNS4 = document.layers ? true:false;
var isIE = document.all ? true:false;
var isNS6 = !isIE && document.getElementById ? true:false;

if (isNS6)  document.addEventListener("keyup", submitSearch, true);
if (isNS4)  document.captureEvents(Event.KEYUP);
if(isIE) document.onkeyup = submitSearch;

// This method is used to submit the form 
function submitSearch(event) {
	var theKeyValue = 0;
	if (isNS6)  theKeyValue = event.which;
	if (isNS4)  theKeyValue = event.which;
	if(isIE) theKeyValue = window.event.keyCode;
	/*if(theKeyValue == 13) {
		var searchToSearch = document.frmsrch.srchval.value;
		if(searchToSearch.length != 0)
			setCookie(document.frmsrch.FORM_ACTION.value);
	}*/
} 

// This method select the textfield value
function selectText() {
	document.frmsrch.srchval.select();
}

// This method write the cookie value
function setCookie(actionPerformed) {
//alert("actionPerformed :: "actionPerformed);
	if(document.frmsrch.srchval.value.length == 0 || document.frmsrch.srchval.value.length == '' || trimString(document.frmsrch.srchval.value).length == 0){
		alert("Please enter the keyword to search");
		document.frmsrch.srchval.value = '';
		document.frmsrch.srchval.focus();
		return;
	}
	if(document.frmsrch.srchval){
		document.cookie = "COOKIE_VALUE=" + document.frmsrch.srchval.value + ";";
	}
//	parent.mainFrame.location.href = actionPerformed;
	location.href = actionPerformed;
	
}

// This method is used to read the cookie value
function readCookie() {
	var cookieName = "COOKIE_VALUE";
	if (document.cookie == '' || document.cookie.length == 0 ) { // there's no cookie, so go no further
        return "";
	} else {   // there is a cookie
		var firstChar, lastChar;
		var cookieValue = document.cookie;

		firstChar = cookieValue.indexOf(cookieName);  // find the start of 'name'
		if(firstChar != -1)  {	// if you found the cookie
			firstChar += cookieName.length + 1;	// skip 'name' and '='
			lastChar = cookieValue.indexOf(';', firstChar);	// Find the end of the value string (i.e. the next ';').
			if(lastChar == -1)
				lastChar = cookieValue.length;
			return cookieValue.substring(firstChar, lastChar);
		} else {	// If there was no cookie of that name, return false.
			return "";
		}
    }
}


function submitForm(theFormName) {
	var formObject = 'document.' + theFormName;
	Pat = /^.+@.+\.([a-z]|[A-Z]){2,3}$/;
	inString=formObject + '.elements["EmailId"]';
	newinString=eval(inString).value;
	with(eval(formObject)) {
	/*	if(WhyAreYouInterested.value == 'Other'){
			if(trimString(Other.value).length == 0) {
				alert('Please fill the Other field');
				Other.value = trimString(Other.value);
				Other.focus();
				return;
			}
		}*/
		if(trimString(CompanyName.value).length == 0) {
			CompanyName.value = trimString(CompanyName.value);
			alert('Please enter your Company name');
			CompanyName.focus();
			return;
		}
		/*if(CompanyType.selectedIndex == 0) {
			alert('Please select Company Type');
			CompanyType.focus();
			return;
		}*/
		if(trimString(WebSite.value).length == 0) {
			WebSite.value = trimString(WebSite.value);
			alert('Company Website field should not be left blank');
			WebSite.focus();
			return;
		}
		/*if(trimString(Address.value).length == 0) {
			Address.value = trimString(Address.value);
			alert('Please enter your address');
			Address.focus();
			return;
		}*/
		/*if(trimString(City.value).length == 0) {
			City.value = trimString(City.value);
			alert('Please enter your city');
			City.focus();
			return;
		}*/
		if(Country.selectedIndex == 0) {
			alert('Please select your country');
			Country.focus();
			return;
		}
		
		/* START - Added on Sep 8th 2008 */
		if(trimString(CityCode.value).length == 0){
			alert("Please enter your city code in Telephone Number");
			CityCode.value = trimString(CityCode.value);
			CityCode.focus();
			return;
		}
		if(CityCode.value != ''){
			if(isNaN(CityCode.value)) {
				alert('Please enter valid city code');
				CityCode.select();
				return;
			}
		}
		if(trimString(PhoneNo.value).length == 0) {
			alert('Please enter your phone number');
			PhoneNo.focus();
			return;
		}
		if(isNaN(PhoneNo.value)){
			alert('Please enter a valid phone number');
			PhoneNo.select();
			PhoneNo.focus();
			return;
		}
		/* END - Added on Sep 8th 2008 */

		// START - added on 23-11-2005

		if(trimString(JobTitle.value).length == 0) {
			alert('Please enter your Job Title');
			JobTitle.value = trimString(JobTitle.value);
			JobTitle.focus();
			return;
		}

		for(i=0; i<JobTitle.value.length; i++){
			JobTitleChar = JobTitle.value.charAt(i);
			if(JobTitleChar.search(/^[A-Za-z\s]$/) == -1){
				alert("Please enter alphabets only");
				JobTitle.select();
				return;
			}
		}

		// END - added on 23-11-2005

		if(trimString(FirstName.value).length == 0) {
			alert('Please enter your first name');
			FirstName.value = trimString(FirstName.value);
			FirstName.focus();
			return;
		}
		for(i=0; i<FirstName.value.length; i++){
			firstNameChar = FirstName.value.charAt(i);
			if(firstNameChar.search(/^[A-Za-z\s]$/) == -1){
				alert("Please enter alphabets only");
				FirstName.select();
				return;
			}
		}
		if(trimString(LastName.value).length == 0) {
			alert('Please enter your last name');
			LastName.value = trimString(LastName.value);
			LastName.focus();
			return;
		}
		for(i=0; i<LastName.value.length; i++){
			lastNameChar = LastName.value.charAt(i);
			if(lastNameChar.search(/^[A-Za-z\s]$/) == -1){
				alert("Please enter alphabets only");
				LastName.select();
				return;
			}
		}
		if(trimString(EmailId.value).length == 0) {
			alert('Please enter your Corporate e-mail Id');
			EmailId.focus();
			return;
		}
		if ((newinString.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z]+)*\.[A-Za-z]{2,4}$/) == -1) || trimString(newinString) == 0) {
//		if ((newinString.search(/^\w+((-\w+)|(\.\w+))*\@([0-9]+[A-Za-z]|[A-Za-z]|[A-Za-z]+[0-9])+((\.|-)[A-Za-z]+)*\.[A-Za-z]{2,3}$/) == -1) || trimString(newinString) == 0) {
			alert("Please enter valid Corporate e-mail Id");
			EmailId.focus();
			return;
		}
		if(trimString(MobileNumber.value) != ""){
			if(isNaN(MobileNumber.value)){
				alert('Please enter valid mobile number');	
				MobileNumber.select();
				return;
			}
		}
		/*if(!validateEmail(trimString(EmailId.value))) {
			alert('Please enter a valid E-mail id');
			EmailId.focus();
			return;
		}*/
		submit();
	}		
}

function submitFormContact(theFormName) {
	var formObject = 'document.' + theFormName;
	with(eval(formObject)) {
		if(trimString(USER_NAME.value).length == 0) {
			alert('Please enter your name');
			USER_NAME.value = trimString(USER_NAME.value);
			USER_NAME.focus();
			return;
		}
		if(trimString(USER_EMAILID.value).length == 0) {
			alert('Please enter your E-mail id');
			USER_EMAILID.focus();
			return;
		}
		if(!validateEmail(trimString(USER_EMAILID.value))) {
			alert('Please enter a valid E-Mail Address');
			USER_EMAILID.focus();
			return;
		}
		if(trimString(COMMENT.value).length == 0) {
			alert('Please enter your comments');
			COMMENT.focus();
			return;
		}
		submit();
	}		
}
/*

function submitFormContact(theFormName) {
	var formObject = 'document.' + theFormName;
	with(eval(formObject)) {

		if(trimString(Name.value).length == 0) {
			Name.value = trimString(Name.value);
			alert('Please enter your Name');
			Name.focus();
			return;
		}
		/*if(CompanyType.selectedIndex == 0) {
			alert('Please select Company Type');
			CompanyType.focus();
			return;
		}
		if(trimString(PhoneNumber.value).length == 0) {
			PhoneNumber.value = trimString(PhoneNumber.value);
			alert('Please enter your Phone Number');
			PhoneNumber.focus();
			return;
		}*/
		/*if(trimString(Address.value).length == 0) {
			Address.value = trimString(Address.value);
			alert('Please enter your address');
			Address.focus();
			return;
		}
		if(trimString(Address.value).length == 0) {
			Address.value = trimString(Address.value);
			alert('Please enter your Address');
			City.focus();
			return;
		}


		if(trimString(Email.value).length == 0) {
			alert('Please enter your E-mail Id');
			Email.focus();
			return;
		}
		if ((newinString.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z]+)*\.[A-Za-z]{2,4}$/) == -1) || trimString(newinString) == 0) {
//		if ((newinString.search(/^\w+((-\w+)|(\.\w+))*\@([0-9]+[A-Za-z]|[A-Za-z]|[A-Za-z]+[0-9])+((\.|-)[A-Za-z]+)*\.[A-Za-z]{2,3}$/) == -1) || trimString(newinString) == 0) {
			alert("Please enter valid E-mail Id");
			EmailId.focus();
			return;
		}
		submit();
			
}

}*/


// This function return true if the email is correct else false
function validateEmail(emailAddress) {
	invalidChars = " /:,;"
	for (i = 0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (emailAddress.indexOf(badChar, 0) > -1) {
			return false
		}
	}
	atPos = emailAddress.indexOf("@", 1)
	if (atPos == -1) {
		return false
	}
	if (emailAddress.indexOf("@", atPos + 1) > -1) {
		return false
	}
	periodPos = emailAddress.indexOf(".", atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > emailAddress.length)	{
		return false
	}
	return true
}

function activateField(theFormName){
	var formObject = 'document.' + theFormName;
	with(eval(formObject)) {
		for(i=0; i<WhyAreYouInterested.length; i++){
			if(WhyAreYouInterested.options[i].selected){
				if(WhyAreYouInterested.value == 'Other'){
					Other.disabled = false;
					Other.focus();
				} else {
					Other.value = '';
					Other.disabled = true;
				}
			}		
		}
	}
}


// This method remove the string trailing and leading spaces
function trimString(stringToTrim){
	i = 0;
	while(i == 0){
		stringlength = stringToTrim.length;
		if(stringToTrim.substr(0,1) == " ")
			stringToTrim = stringToTrim.substring(1,stringlength);
		else
			i = 1;
	}
	while(i == 0 || i==1){
		stringlength = stringToTrim.length;
		if(stringToTrim.substr(stringlength - 1,1) == " ")
			stringToTrim = stringToTrim.substring(0,stringlength - 1);
		else
			i = 2;
	}
	return stringToTrim;
}

/*contact info page form  */
function submitFormContact(theFormName) {
	var formObject = 'document.' + theFormName;
	with(eval(formObject)) {
		if(trimString(USER_NAME.value).length == 0) {
			alert('Please enter your name');
			USER_NAME.value = trimString(USER_NAME.value);
			USER_NAME.focus();
			return;
		}
		if(trimString(USER_EMAILID.value).length == 0) {
			alert('Please enter your E-mail id');
			USER_EMAILID.focus();
			return;
		}
		if(!validateEmail(trimString(USER_EMAILID.value))) {
			alert('Please enter a valid E-Mail Address');
			USER_EMAILID.focus();
			return;
		}
		if(trimString(COMMENT.value).length == 0) {
			alert('Please enter your comments');
			COMMENT.focus();
			return;
		}
		submit();
	}		
}

