// 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;
var availWidth = screen.availWidth;

// 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;
} 

// This method select the textfield value
function selectText() {
	document.frmsrch.srchval.select();
}

// This method write the cookie value
function setCookie(actionPerformed) {
	var searchVal = document.frmsrch.srchval.value;
	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;
	} else {
		document.cookie = "COOKIE_VALUE" + "=" + searchVal +"; path=/";
	}
	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 "";
		}
    }
}

// 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;
}

function openPrintFriendlyPage(url) {
	var theWidth = 450;
	var theHeight = 450;
	var theWinProperties = 'toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=1,resizable=no';
	var etindiaPrinterWindow = window.open(url,'','width=' + theWidth + ',height=' + theHeight + ',' + theWinProperties);
	etindiaPrinterWindow.focus();
}

function showPrintWindow(OLECMDID) {
    //Script to open print preview for the current page
    /* OLECMDID values:
     * 6 - print
     * 7 - print preview
     * 1 - open window
     * 4 - Save As
     */
	var PROMPT = 1; // 2 DONTPROMPTUSER
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(OLECMDID, PROMPT);
    WebBrowser1.outerHTML = "";
}

// This method is used to submit the form of the feedback.html page
function submitForm(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(CONTACT_NUMBER.value).length == 0) {
			alert('Please enter your Contact Number');
			CONTACT_NUMBER.focus();
			return;
		}
		/*if (document.MAIN_FORM.menu1.options[document.MAIN_FORM.menu1.selectedIndex].value == "other") {
		if(trimString(OTHER.value).length == 0) {
			alert('Please enter others value');
			OTHER.value = trimString(OTHER.value);
			OTHER.focus();
			return;
		}		
		}*/
		if(trimString(COMMENT.value).length == 0) {
			alert('Please enter your comments');
			COMMENT.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
		}
	}
	emaillength = emailAddress.length;
	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 (emailAddress.indexOf("..", atPos) != -1) {
		return false
	}

	firstVal = emailAddress.substring(0,1);
	lastValue = emailAddress.substring(emaillength-1, emaillength);

	if(firstVal == "@" || firstVal == "."){
		return false;
	}

	if(lastValue == "."){
		return false;
	}
	
	if (periodPos+3 > emailAddress.length)	{
		return false
	}
	return true
}

function openWin(theURL,winname,features) {
	var myWin = window.open(theURL,winname,features);
	myWin.moveTo(100,10);
	myWin.focus();
}

function setheight(){
	if(isNS6){
		//document.getElementById('tdgerman').style.height=0;
	}
}