/*SUMMARY
* This is a set of JavaScript functions for validating input on
* an SignUp & Registration HTML forms.  Functions are provided to validate:
*
* User Name (4-32 string length, no special chars or spaces allowed)
* Password (4-32 string length, no special chars or spaces allowed)
* Confirm Password (4-32 string length and must be the same as password)
* Email addresses (Account and System Admin email)
* Server Host Name (4-32 string length or valid IP Address, special chars allowed, spaces allowed)
* Server Port (from 1024-65535)
* Server Host ID (hexadecimal number, must have 12 string length)
* License File Location (4-256)
* Usage Sync Interval (0-x, must be an integer)
* Product Serial Number (9-30 char, only alphanumeric, no special chars or spaces)
*
* Many of the below functions take an optional parameter eok (for "emptyOK")
* which determines whether the empty string will return true or false.
* Default behavior is controlled by global variable defaultEmptyOK.
*
* BASIC DATA VALIDATION FUNCTIONS:
*
* isWhitespace (s)                     Check whether string s is empty or whitespace.
* isLetter (c)                         Check whether character c is an English letter
* isDigit (c)                          Check whether character c is a digit
* isSignedInteger (s [,eok])           TRUE if all characters in string s are numbers; leading + or - allowed.
* isPositiveInteger (s [,eok])         TRUE if string s is an integer >= 0.
* isNegativeInteger (s [,eok])         TRUE if s is an integer < 0.
* isAlphanumeric (s [,eok])            TRUE if string s is English letters and numbers only.
* isHexString (s)                      TRUE is string s is a valid Hexadecimal number
*
* FUNCTIONS TO PROMPT USER:
*
* prompt (s)                          Display prompt string s in status bar.
* promptEntry (s)                     Display data entry prompt string s in status bar.
* warnEmpty (theField, s)             Notify user that required field theField is empty.
* warnInvalid (theField, s)           Notify user that contents of field theField are invalid.
*
* FUNCTIONS TO INTERACTIVELY CHECK FIELD CONTENTS:
*
* checkUserName (theField [,eok])            Check that theField.value is not empty or all whitespace.
* checkPassword (theField [,eok])            Check that theField.value has a valid length Password.
* checkRePassword (theField)                 Check that theField.value has the same value as Password.
* checkEmail (theField [,eok])               Check that theField.value is a valid Email Address.
* checkServerName (theField [,eok])          Check that theField.value is a valid Server Name.
* checkServerPort (theField [,eok])          Check that theField.value is a valid Server Port.
* checkServerHostID (theField [,eok])        Check that theField.value is a valid Server Host ID.
* checkLicenseFileLocation (theField [,eok]) Check that theField.value is a valid File Location.
* checkUsageSync (theField [,eok])           Check that theField.value is a valid Usage Sync Interval.
* checkSerialNum (theField [,eok])           Check that theField.value is a valid Serial Number.
*/

// VARIABLE DECLARATIONS

// whitespace characters
var whitespace = " \t\n\r";

// CONSTANT STRING DECLARATIONS

// s is an abbreviation for "string". These are the names of the fields.
//They are only used locally by the javascript and jsps =>No localization. If you do use
//them otherwise, make sure to localize them.
var sUsername = "User Name"
var sPassword = "Password"
var sPassword2 = "Confirm Password"
var sAccountEmail = "Accounts Payable e-mail"
var sAdminEmail = "System Administrator e-mail"
var sServerName = "Server Name"
var sServerPort = "Server Port"
var sServerHostID = "Server Host ID"
var sLicenseFileLocation = "License File Location"
var sUsageSync = "Usage Synchronization"
var sSerialNumber1 = "Product Serial Number 1"
var sSerialNumber2 = "Product Serial Number 2"
var sSerialNumber3 = "Product Serial Number 3"
var sSerialNumber4 = "Product Serial Number 4"
var sSerialNumber5 = "Product Serial Number 5"
var sSerialNumber6 = "Product Serial Number 6"
var sSerialNumber7 = "Product Serial Number 7"
var sSerialNumber8 = "Product Serial Number 8"
var sHelpSerialNumber = "Serial Number"

var lUserName, lPassword, lConfirmPassword;
var iPort, iUsageSync, iEmail, iHostID, iUserName, iPassword, iSerial, iSpecialSerial;
var iAlphaSerial, iPasswordMisMatch;

// p is an abbreviation for "prompt"
var pUserName, pPassword, pPassword2, pValidEmail, pServerName, pServerHost, pServerHostID;
var pUsageSync, pSerialNumber, pLicenseFileLocation, pSerialTextArea, pConfirmLicense;
var pPleaseWait, pLocalHost, pAlphaIP, pRangeIP, pDotsIP, pSelectLanguage, pSelectRegion;

var eUserName, ePassword2, eServerHostID, eSerialNumber, eAccounteMail, eAdminEmail;
var eServerName, eServerPort, eUsageSync, eSerialText;

/*
* Global variable defaultEmptyOK defines default return value
* for many functions when they are passed the empty string.
* By default, they will return defaultEmptyOK.
*
* defaultEmptyOK is false, which means that by default,
* these functions will do "strict" validation.  Function
* isSignedInteger, for example, will only return true if it is
* passed a string containing an integer; if it is passed
* the empty string, it will return false.
*
* You can change this default behavior globally (for all
* functions which use defaultEmptyOK) by changing the value
* of defaultEmptyOK.
*
* Most of these functions have an optional argument emptyOK
* which allows you to override the default behavior for
* the duration of a function call.
*
* This functionality is useful because it is possible to
* say "if the user puts anything in this field, it must
* be an integer (or a phone number, or a string, etc.),
* but it's OK to leave the field empty too."
* This is the case for fields which are optional but which
* must have a certain kind of content if filled in.
*/
var defaultEmptyOK = false

// Language constants (the values MUST be used in the html page)
var EN = "en";
var ES = "es";
var FR = "fr";
var DE = "de";
var DA = "da";
var NL = "nl";
var IT = "it";
var JA = "ja";
var NO = "no";
var SV = "sv";

// the following 2 globals are used to retain user choices on the
// QLA language/region selection page
var langSelIndex = -1;		// English = 2
var regionSelIndex = -1;	// US = 0

// Region constants (the values MUST be used in the html page)
var US = "us";
var EUROPE = "europe";
var ASIA = "asia";

//Site name constants
var CONTROLLER = "controller.jsp";
var SITEDOWN = "sitedown.jsp";

// URL Constants .. please update this accordingly!
//var usWebServer = "http://localhost/registration";
//var europeanWebServer = "http://localhost/registration";
//var usWebServer = "https://qlaweb.quark.com/registration";
var usWebServer = "https://qlaweb.quark.com/registrationMercury";
//var europeanWebServer = "https://qlaweb.quark.ch/registration";
var europeanWebServer = "https://qlaweb.quark.ch/registrationMercury";
var asiaWebServer = "https://qlajapan.quark.com/registrationMercury";

// Cookie name
var COOKIE_NAME = "qla";


// Check whether string s is empty.
function isEmpty(s){
	return ((s == null) || (s.length == 0) || (s=="") || (s=='') )
}

// For Product Serial Number, check length that must be 9 -30 characters
function ExceedSerialLength(s){
	if ((s.length < 9) || (s.length > 30)) return true;
   else return false;
}

// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace (s){
	var i;

	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}

    // All characters are whitespace.
    return true;
}


/*
* Returns true if character c is an English letter
* (A .. Z, a..z).
*/
function isLetter (c){
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

/* Returns true if character c is a digit
* (0 .. 9).
*/
function isDigit (c){
	return ((c >= "0") && (c <= "9"))
}


/* isSignedInteger (STRING s)
* Returns true if all characters in string s are numbers.
* A leading + or - is valid.
*/
function isSignedInteger (s){
	var i;

	if (isEmpty(s))
		if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
		else return (isSignedInteger.arguments[1] == true);

	// Search through string's characters one by one
	// until we find a non-numeric character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++) {
		// Check that current character is number.
		var c = s.charAt(i);
		if (i == 0) {
			if ((c == "+") || (c == "-")) {
				continue;
			}
		}
		if (!isDigit(c)) return false;
	}

	// All characters are numbers.
	return true;
}


/*
* isPositiveInteger (STRING s [, BOOLEAN emptyOK])
*
* Returns true if string s is an integer >= 0.
*/
function isPositiveInteger (s){
	var secondArg = defaultEmptyOK;

   if (isPositiveInteger.arguments.length > 1)
		secondArg = isPositiveInteger.arguments[1];

	// The next line is a bit byzantine.  What it means is:
	// a) s must be a signed integer, AND
	// b) one of the following must be true:
	//    i)  s is empty and we are supposed to return true for
	//        empty strings
	//    ii) this is a number >= 0
	return (isSignedInteger(s, secondArg)
			&& ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}



/* isNegativeInteger (STRING s [, BOOLEAN emptyOK])
*
* Returns true if string s is an integer < 0.
*
*/
function isNegativeInteger (s){
	var secondArg = defaultEmptyOK;

	if (isNegativeInteger.arguments.length > 1)
		secondArg = isNegativeInteger.arguments[1];

	return (isSignedInteger(s, secondArg)
			&& ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}

/* isAlphanumeric (STRING s [, BOOLEAN emptyOK])
*
* Returns true if string s is English letters
* (A .. Z, a..z) and numbers only.
*/
function isAlphanumeric (s){
	var i;

	for (i = 0; i < s.length; i++)	{
		var c = s.charAt(i);
		if (! (isLetter(c) || isDigit(c) ) ) return false;
   }

	return true;
}

/* function isHexString(STRING s)
* Returns true if the string is a valid hex string
*/
function isHexString(s) {
	var i;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!(isDigit(c) || ((c >= "a") && (c <= "f")) || ((c >= "A") && (c <= "F")) )) {
			return false;
		}
	}

	return true;
}

/* function isAlpha
* Returns true if string s has ONLY English letters
* (A .. Z, a..z)
*/
function isAlpha (s){
	var i;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isLetter(c)) {
			return false;
      }
	}

	return true;
}


/* function isNumeric
* Returns true if all string s is numeric
* (1234567890)
*/
function isNumeric (s){
	var i;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if  (!isDigit(c)) {
			return false;
      }
	}

	return true;
}


function hasDot (s){
	var i;
	for (i = 0; i < s.length; i++) {
		if (s.charAt(i) == ".") return true;
	}

	return false;
}

/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */

// Display prompt string s in status bar.
function prompt (s){
	window.status = s
}

// Display data entry prompt string s in status bar.
function promptEntry (s){
	window.status = s;
}

// Notify user that required field theField is empty.
function warnEmpty (theField, s){
	theField.focus();
   if (s == sUsername) {
      alert (eUserName);
   } else if (s == sPassword2) {
        alert (ePassword2);
   } else if (s == sServerHostID) {
        alert (eServerHostID);
   } else if (s == sHelpSerialNumber) {
        alert (eSerialNumber);
   } else if (s == sAdminEmail) {
        alert (eAdminEmail);
   } else if (s == sAccountEmail) {
        alert (eAccounteMail);
   } else if (s == sServerName) {
        alert (eServerName);
   } else if (s == sServerPort) {
        alert (eServerPort);
   } else if (s == sUsageSync) {
        alert (eUsageSync);
   }
   //  alert(mPrefix + s + mSuffix);
   return false;
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.
function warnInvalid (theField, s){
	theField.focus();
   theField.select();
     alert(s);
   return false;
}

function showError (theField,errorMessage) {
      theField.focus();
   theField.select();
     alert(errorMessage);
   return false;
}


/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */
function checkUserName(theField, emptyOK){
	// Next line is needed on NN3 to avoid "undefined is not a number" error
   // in equality comparison below.
   if (checkUserName.arguments.length == 1) emptyOK = defaultEmptyOK;
   if ((emptyOK == true) && (isEmpty(theField.value))) return true;

   var userName = theField.value;
   if (isWhitespace(userName)) {
		return warnEmpty (theField, sUsername);
   }

   if (userName.length < 4) {
   	theField.focus();
        alert(lUserName);
      return false;
   }

   if (!isAlphanumeric(userName)) {
      return warnInvalid (theField, iUserName);
   }

   return true;
}

function checkLicenseFileLocation (theField, emptyOK){
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.
	if (checkLicenseFileLocation.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

	if (isWhitespace(theField.value)) {
		theField.focus()
		if (confirm(pConfirmLicense))
			return true;
		else
			return false;
	}
}

function checkPassword (theField, emptyOK) {
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.
	if (checkPassword.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

   var password = theField.value;
   var error = false;
   var errMessage = "";

	if (isWhitespace(password)) {
      error = true;
      errMessage = "You did not enter a password. Please enter it now.";
   }

   if (password.length < 4) {
      error = true;
      errMessage = lPassword;
   }

   if (!isAlphanumeric(password)) {
      error = true;
      errMessage = iPassword;
   }

  if (error) {
       return showError(theField,errMessage);
   }

   return true;
}

function checkRepeatPassword(theField, emptyOK){
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.
	if (checkRepeatPassword.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

   var confirm = theField.value;
   var error = false;
   var errMessage = "";
	if (isWhitespace(confirm)) {
      warnEmpty(theField,sPassword2);
   }

   if (confirm.length < 4) {
      error = true;
      errMessage = lConfirmPassword;
   }

	if (theField.value != window.document.form.userPassword.value) {

      error = true;
      errMessage = iPasswordMismatch;
   }

   if (error) {
      return showError(theField,errMessage);
   }

   return true;
}


/* FUNCTIONS TO CHECK the SERVER HOST ID THAT IS REQUIRED FIELD */
function checkServerHostIDString (theField, emptyOK){
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.
	if (checkServerHostIDString.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

	if (isWhitespace(theField.value))
		return warnEmpty (theField, sServerHostID);

	if (theField.value.length != 12) {
      theField.focus();
        alert(iHostID);
      return false;
   }

	if (!isHexString(theField.value))
		return warnInvalid (theField, iHostID);
	else
		return true;
}

function serialErrorCheck(serial) {
   if (ExceedSerialLength(serial)) {
        alert(iSerial)
      return false;
   }

   if (!isAlphanumeric(serial)) {
        alert(iSpecialSerial);
      return false;
   }

   if (isAlpha(serial) || isNumeric(serial)) {
        alert(iAlphaSerial);
      return false;
   }

	return true;

}

/* FUNCTIONS TO CHECK the first PRODUCT SERIAL NUMBERS THAT IS REQUIRED FIELD */
function checkRequiredSerialString (theField, s, emptyOK){
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.
	if (checkRequiredSerialString.arguments.length == 2) emptyOK = defaultEmptyOK;
   if (typeof (theField) == 'undefined') return true;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

	if (isWhitespace(theField.value)) {
		return warnEmpty (theField, s);
   }

   return serialErrorCheck(theField.value);
}


/* FUNCTIONS TO CHECK PRODUCT SERIAL NUMBERS THAT ARE NOT REQUIRED FIELDS */
function checkSerialString (theField, s, emptyOK){
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below. here I am setting the emptyOK to true by
   // default since it is a text area and it need not be filled.

	if (checkSerialString.arguments.length == 2) emptyOK = true;
   if (typeof (theField) == 'undefined') return true;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

   if (isWhitespace(theField.value)) {
      return true;
   }

   return serialErrorCheck(theField.value);
}



// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
function checkEmail (theField, s, emptyOK){
	if (checkEmail.arguments.length == 2) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true

	if (isWhitespace(theField.value)) {
		return warnEmpty (theField, s);
   }

	// there must be >= 1 character before @, so we
	// start looking at character position 1
	// (i.e. second character)
	var i = 1;
	var sLength = theField.value.length;

	// look for @
	while ((i < sLength) && (theField.value.charAt(i) != "@")) {
		i++;
	}

	if ((i >= sLength) || (theField.value.charAt(i) != "@")) {
      return warnInvalid (theField, iEmail);
   }
	else i += 2;

	// look for .
	while ((i < sLength) && (theField.value.charAt(i) != ".")){
		i++;
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (theField.value.charAt(i) != ".")) {
      return warnInvalid (theField, iEmail);
   }

	else {
		return true;
   }
}

// Check that string theField.value is a valid Server Name.
//
function checkServerName (theField, emptyOK) {
	if (checkServerName .arguments.length == 1) emptyOK = defaultEmptyOK;
   if ((emptyOK == true) && (isEmpty(theField.value))) return true

   if (isWhitespace(theField.value))  {
		return warnEmpty (theField, sServerName);
   }

	if ( (theField.value.toLowerCase()=="localhost") ||
			((theField.value)=="127.0.0.1") ) {
		theField.focus()
        alert(pLocalHost);
      return false;
   }

	else if (hasDot(theField.value)) {
		//For Server Name by IP address, characters should not contain abcdef...
		if (isAlpha(theField.value)) {
			theField.focus()
			  alert(pAlphaIP);
		   return false;
		}

		//For Server Name by IP address, the characters should not should not exceed 15 long.
		if (theField.value.length > 15) {
			theField.focus()
			  alert(pRangeIP);
			return false;
		}

		//if all string is numeric, tell them to provide dot
		else if (isNumeric(theField.value)) {
			theField.focus()
			  alert(pDotsIP);
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkServerPort (theField, emptyOK){
	if (checkServerPort.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

   var port = theField.value;

   if (isWhitespace(port))  {
		return warnEmpty (theField, sServerPort);
   }

   if (!isPositiveInteger(port)) {
	   return warnInvalid (theField, iPort);
   }

   var portValue = parseInt(port);
   if ( (portValue < 1024) || (portValue > 65535) ) {
      return warnInvalid(theField,iPort);
   }

   return true;
}

function checkUsageSync (theField, emptyOK){
	if (checkUsageSync.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;

   if (isWhitespace(theField.value))  {
		return warnEmpty (theField, sUsageSync);
   }

	if (!isPositiveInteger(theField.value) || parseInt(theField.value) > 365) {
		return warnInvalid (theField, iUsageSync);
   }

	return true;
}


function checkSerialText(theField,emptyOK) {

	if (checkSerialText.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	var s = theField.value;
	if (isEmpty(s))  {
		return warnEmpty (theField, sHelpSerialNumber);
   }

/*
   var start = 0;
   var finish = 0;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ( (c == '\n') || (c == ';') || (i == (s.length-1)) ) {
         finish = i;
         if ( i == (s.length-1)) {
            finish++;
         }
         var serial = s.substring(start,finish)
         start=i+1;
         if (!serialErrorCheck(serial)) {
            return false;
         }
      }
	}*/

	return true;
}



/** The following methods pertain to LanguageRegion.html */

// Validate the language/region page
function checkLangRegion(form) {
	with (form) {

		// loop through the language radio buttons and see if any checked
		var found = false;
	 if(language.selectedIndex==0)
		{
		   alert("Please select your language of choice.");
		language.focus();
	
		return false;
		}
		if(region.selectedIndex==0)
		{
		alert("Please select the region in which you purchased your Quark product");
		region.focus();
		return false;
		}
		
	/*	for (var i = 0; i < language.length; i++) {
			if (language[i].checked == true) {
				found = true;
			}
		}
		if (!found) {
			return showError(language[2], "Please select your language of choice.");
		}
*/
		// loop through the region radio buttons and see if any checked
		//found = false;
/*	for (var i = 0; i < region.length; i++) {
		if (region[i].checked == true) {
				found = true;
			}
		}
		
		if (!found) {
			return showError(region[0], "Please select the region in which you purchased your Quark product.");
			}*/

	/*	if (!found) {
		alert("raj here");
		alert("Please select the region in which you purchased your Quark product);
		return false;
			//return showError(region[0], "Please select the region in which you purchased your Quark product.");	
			}*/
	}
	return true;
}

// Used only for the page where the user picks language and region of purchase
function forwardToWebServer(form) {
	with (form) {
		if (regionSelIndex == 1) {
			window.location.href = usWebServer + "/" + CONTROLLER + "?language=" + language[langSelIndex].value;
		}
      else if (regionSelIndex == 2) {
			window.location.href = europeanWebServer + "/" + CONTROLLER + "?language=" + language[langSelIndex].value;
		}
      else if (regionSelIndex == 3) {
         window.location.href = asiaWebServer + "/" + CONTROLLER + "?language=" + language[langSelIndex].value;
      }
	}
	return false;
}


// onclick handler for the language radio buttons
function langOnClick(langIndex) {
	
	langSelIndex = langIndex;
	return true;
}
// onclick handler for the region selection radio buttons
function regionOnClick(regIndex) {

	regionSelIndex = regIndex;
	return true;
}

// pick off the URL param for language selection (?language=de)
function pickOffLangFromURL() {
	with (window) {
		var langParams = location.href.substring(location.href.indexOf("?") + 1, location.href.length);

		// params on the URL take precedence, otherwise, check for cookie
		if (langParams.indexOf("=") != -1) {
			var lang = langParams.substring(langParams.indexOf("=") + 1, langParams.length);
			setLangCheckBox(lang);
		} else {
			checkForCookie(COOKIE_NAME);
		}
	}
   //onLoad ();
}

// helper function for language checkbox
function setLangCheckBox(lang) {
   if (lang.toLowerCase() == DA) {
		langOnClick(0);
		document.regionForm.language[0].checked = true;
	}else if (lang.toLowerCase() == DE) {
		langOnClick(1);
		document.regionForm.language[1].checked = true;
	} else if (lang.toLowerCase() == EN) {
		langOnClick(2);
		document.regionForm.language[2].checked = true;
	} else if (lang.toLowerCase() == ES) {
		langOnClick(3);
		document.regionForm.language[3].checked = true;
	} else if (lang.toLowerCase() == FR) {
		langOnClick(4);
		document.regionForm.language[4].checked = true;
	} else if (lang.toLowerCase() == IT) {
		langOnClick(5);
		document.regionForm.language[5].checked = true;
	}  else if (lang.toLowerCase() == NL) {
		langOnClick(6);
		document.regionForm.language[6].checked = true;
	} else if (lang.toLowerCase() == NO) {
		langOnClick(7);
		document.regionForm.language[7].checked = true;
	} else if (lang.toLowerCase() == SV) {
		langOnClick(8);
		document.regionForm.language[8].checked = true;
	} else {
		// default to english
		langOnClick(2);
		document.regionForm.language[2].checked = true;
	}
}

// helper function for region checkbox
function setRegionCheckBox(region) {
	if (region.toLowerCase() == US) {
		regionOnClick(0);
		regionForm.region[0].checked = true;
	}
   else if (region.toLowerCase() == EUROPE) {
		regionOnClick(1);
		regionForm.region[1].checked = true;
	}
   else if (region.toLowerCase() == ASIA) {
      regionOnClick(2);
      regionForm.region[2].checked = true;
   }
   else {
		// default to US
		regionOnClick(0);
		regionForm.region[0].checked = true;
	}
}

// get a cookie value given a cookie name
function getCookieValue(cookieName) {
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");

	if (cookieStartsAt == -1) {
		cookieStartsAt = cookieValue.indexOf(cookieName + "=");
	}

	if (cookieStartsAt == -1) {
		cookieValue = null;
	} else {
		cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
		if (cookieEndsAt == -1) {
			cookieEndsAt = cookieValue.length;
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
	}

	return cookieValue;
}


// create a cookie based on params
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
							((expires) ? "; expires=" + expires.toGMTString() : "") +
							((path) ? "; path=" + path : "") +
							((domain) ? "; domain=" + domain : "") +
							((secure) ? "; secure" : "");
}

// called when html is loaded
function checkForCookie(name) {
	if (name != null) {
		var val = getCookieValue(name);
		if (val != null) {
			val = val.split(";");
			setLangCheckBox(val[0]);
			setRegionCheckBox(val[1]);
		}
	}

	return true;
}

// called when html is submitted
function createCookie(form) {
	with (form) {
		// alert (language[langSelIndex].value + " " + region[regionSelIndex].value);
		var expDate = new Date();
		expDate.setMonth(expDate.getMonth() + 1);
		if ((langSelIndex != -1) && (regionSelIndex != -1)) {
			setCookie(COOKIE_NAME,
						 language[langSelIndex].value + ";" + region[regionSelIndex].value,
						 expDate);
		}
		// alert("cookie " + COOKIE_NAME + " created!");
	}
	return true;
}

// test code
/*
var expireDate = new Date();
expireDate.setMonth(expireDate.getMonth() + 1);
setCookie("Name", "Bob", "/Mystore", expireDate.toGMTString());

setCookie("asfsd", "fasd;asfsd;afd;asdf;10 Jan 2002", "", "");
var val = getCookieValue("asfsd");
val = val.split(";");
val[0] ...
*/


/************************************************************************/
// Read resources from a resource file - using an applet to do the reading
//ResourceApplet here is the name of the applet class.
/************************************************************************/
/*
var ResourceApplet ;
var javaString = java.lang.String;

function onLoad () {
   ResourceApplet = document.applets.ResourceApplet ;
   ResourceApplet.setLanguage (new javaString(language[langSelIndex].value));
   ResourceApplet.start ();
   alert ("loaded on load");
}

function   alertString (s) {
   //var actString = ResourceApplet......
   if (actString != "*") {
      alert (actString);
   }
   return true;
}

*/


