function changeMarket(formName, market)
{	
	formName.market.value = market;
	formName.AppId.value = '';
	formName.OperationId.value = '';
	formName.StepId.value = '';
	formName.submit();
}
function changeSubMarket(formName, submarket)
{	
	formName.submarket.value = submarket;
	formName.OperationId.value = '1';
	formName.StepId.value = '';
	formName.StepId.value = '';
if (formName.PageSideLing!=null)
	formName.PageSideLink.value = '';
	formName.submit();
}
function changeSubMarketHome(formName, submarket)
{	
	formName.submarket.value = submarket;
	formName.submit();
}

function changeLanguage(formName, language)
{	
	formName.language.value = language;
	formName.submit();
}

/* ======================================================================
FUNCTION:	handleSubmitPage - Used to submit the form whose name is passed.

INPUT:		formName - Name of the form to be submitted (Passed as document.formName).
		Action - Target action for the form.
		AppId - The application id of the target
		OperationId - Operation Id of the target.
		StepId - StepId of the target.
====================================================================== */
function handleSubmitPage(formName, Action, AppId, OperationId, StepId)
{
	formName.AppId.value=AppId;
	formName.OperationId.value = OperationId;
	formName.StepId.value = StepId;
	formName.action = Action;
	formName.submit();
}

/* ======================================================================
FUNCTION:	handleSubmitLink - Used to submit the form whose name is passed and assigns
					the PageSideLink parameter for showing a link selected.

INPUT:		formName - Name of the form to be submitted (Passed as document.formName).
		Action - Target action for the form.
		AppId - The application id of the target
		OperationId - Operation Id of the target.
		StepId - StepId of the target.
		PageLink - The page link value to be shown highlighted.

CALLS:		handleSubmitPage()
====================================================================== */
function handleSubmitLink(formName, Action, AppId, OperationId, StepId, PageLink){
	formName.PageSideLink.value = PageLink;
	handleSubmitPage(formName, Action, AppId, OperationId, StepId);
}

/* ======================================================================
FUNCTION:	openWindow - Opens the passed link in a new browser window.

INPUT:		url - The url to be opened in new window.
		windowName - The name to be given to new opened window.
		widthParam - The width of window
		heightParam - Height of window
		scrollParam - Whether scrollbars should appear in new window.
		menuParam - Whether menubar should appear in new window.
		toolbarParam - Whether toolbar should appear in new window.
		resizeParam - Whether the new window should be resizable.
		maximizeParam - Whether the new window sould be maximizable.
====================================================================== */
function openWindow(url, windowName, widthParam, heightParam, scrollParam, menuParam, toolbarParam, resizeParam, maximizeParam){
	var parameters = "top=0,left=0,width=" + widthParam + ",height=" + heightParam + ",scrollbars=" + scrollParam + ",menubar=" + menuParam + ",toolbar=" + toolbarParam + ",directories=no,location=no,copyhistory=no,resizable=" + resizeParam + ",maximize=" + maximizeParam;
	var TheNewWin = window.open(url, windowName, parameters);
	TheNewWin.focus();
}

/* ======================================================================
FUNCTION:	isBlank

INPUT:		val - the value to be tested

RETURN:  	true, if the string is null, undefined or an empty string, ""
      		false, otherwise.

CALLS:		IsNull(), IsUndef() which are defined elsewhere in the Script Library

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isBlank( str ) {
	var isValid = false;
	str = trimString(str);

 	if ( isNull(str) || isUndefined(str) || (str + "" == ""))
 		isValid = true;
		
	return isValid;
}  // end isBlank

/* ======================================================================
FUNCTION:	isNull
 
INPUT:		str - the value to be tested

RETURN:  	true, if the value is null;
      		false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isNull( str ) {
	var isValid = false;

 	if (str + "" == "null")
 		isValid = true;
		
	return isValid;
}  // end isNull

/* ======================================================================
FUNCTION:	isUndefined
 
INPUT:		str - the value to be tested

RETURN:  	true, if the value is undefined
      		false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isUndefined( str ) {
	var isValid = false;

 	if (str + "" == "undefined")
 		isValid = true;
		
	return isValid;
}  // end isUndefined

/* ======================================================================
FUNCTION:  	isAlpha

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only alphabetic characters 
				false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isAlpha( str ) {
	// Return immediately if an invalid value was passed in
	if (isBlank( str ))
		return false;

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  for (i = 0; i < str.length; i++) {
		// Alpha must be between "A"-"Z", or "a"-"z"
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
				((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
         				isValid = false;
         				break;
      			}
   } // end for loop
   
	return isValid;
}  // end IsAlpha 	

/* ======================================================================
FUNCTION:  	isDouble

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only double or integers 
				false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isDouble( str ) {
	// Return immediately if an invalid value was passed in
	if (isBlank( str ))
		return false;

	var isValid = true;

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  if(isNaN(str))
		isValid = false; 
	else if(str <= 0)
		isValid = false; 	 	
   
	return isValid;
} // end IsAlpha 

/* ======================================================================
FUNCTION:  	isInteger

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only integers 
				false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isInteger( str ) {
	// Return immediately if an invalid value was passed in
	if (isBlank( str )){
		return false;
	}

	var isValid = true;

	if(isNaN(str)){
		isValid = false;
	}else if(parseInt(str) != parseFloat(str)){
		isValid = false;
	}else if(str <= 0){
		isValid = false;
	}else if(str.indexOf(".") > -1){
		isValid = false;
	}
   
	return isValid;
}  // end isInteger 

/* ======================================================================
FUNCTION:  	isAlphaNumeric

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only alphanumeric characters , no spaces are allowed.
				false, otherwise.
			Chars must be between "A"-"Z", or "a"-"z", or "0"-"9", or "-", or "_".

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isAlphaNumeric( str ) {
	// Return immediately if an invalid value was passed in
	/*if (isBlank( str ))
		return false;*/

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  for (i = 0; i < str.length; i++) {
		// Chars must be between "A"-"Z", or "a"-"z", or "0"-"9"
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
				((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
				(str.charAt(i) == "_") || (str.charAt(i) == " ") ||
				(str.charAt(i) == "-") ||
      			((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) ) ) {
         				isValid = false;
         				break;
      			}
   } // end for loop
   
	return isValid;
}  // end IsAlphaNumeric 

/* ======================================================================
FUNCTION:  	isEmailAddressValid
 
INPUT:    	str (string) - an e-mail address to be tested

RETURN:  	true, if the string contains a valid e-mail address which is a string
				plus an '@' character followed by another string containing at least 
				one '.' and ending in an alpha (non-punctuation) character.
				false, otherwise

CALLS:		isBlank(), isAlpha() which are defined elsewhere in the Script Library

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isEmailAddressValid( str ) {
	// Return immediately if an invalid value was passed in
	if (isBlank( str ))
		return false;
	
	var isValid = true;
	str += "";

	// Trim the spaces 
	str = trimString(str);
	var name = str.indexOf("@");
	if(name == -1) {
	  return false;
	} else {
		namestr = str.substring(0, name);  // everything before the '@'
	}

	domainstr = str.substring(str.indexOf("@")+1, str.length); // everything after the '@'

	// Rules: namestr cannot be empty, or that would indicate no characters before the '@',
	// domainstr must contain a period that is not the first character (i.e. right after
	// the '@').  The last character must be an alpha.
   	if ((namestr.length == 0) || (domainstr.indexOf(".") <= 0) ||
			(domainstr.indexOf("@") != -1) ||
			!isAlpha(str.charAt(str.length-1)))
		isValid = false;

   	return isValid;
} // end isEmailAddressValid


/* ======================================================================
FUNCTION:	isDateValid
 
INPUT:		str - the value to be tested

RETURN:  	true, if the value is a correct date;
      		false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function isDateValid( str ) {
	// Return immediately if an invalid value was passed in
	if (isBlank( str ))
		return false;
	
	return validateDate(str);
}  // end isDateValid

/* ======================================================================
FUNCTION:	validateDate
 
INPUT:		str (string): the date string to be validated

RETURN:  	true/false depending on the string

DESC:		This function is used to check the date string validation in dd/mm/yyyy format for UK.
====================================================================== */
function validateDate(str){
	var day, month, year;
	var a = parseDate(str);

	if ( !a ){
		return false;
	}

	day = a[0];
	month = a[1];
	year = a[2];

   //check correct number of day for given month/year

   if (day.selectedIndex != 0 && month.selectedIndex != 0 && year.selectedIndex != 0){
      switch(parseInt(month,10)) {
         case 2 :
            //February
            if (parseInt(year,10) == Math.round(parseInt(year,10) / 4) * 4){
                 //leap year
	       		if (parseInt(day,10) > 29){
                  return false;
                }
            } else {
               //non-leap year
               if (parseInt(day,10) > 28) {
               		return false;
               }
            }
            return true;
            break;
         case 4 :
            //April
            if (parseInt(day,10) > 30) {
               return false;
            }
            return true;
            break;
         case 6:
            //June
            if (parseInt(day,10) > 30)  {
               return false;
            }
            return true;
            break;
         case 9:
            //September
            if (parseInt(day,10) > 30) {
               return false;
            }
            return true;
            break;
         case 11:
            //November
            if (parseInt(day,10) > 30) {
               return false;
            }
            return true;
            break;
         default:
            //date is valid
            return true;
            break;
      }
   }
   return false;
}

/* ======================================================================
FUNCTION:	parseDate
 
INPUT:		str (string): the date string to be parsed

RETURN:  	false depending on the string if invalid date string else array conating day month and year

DESC:		This function is used to parse the date string validation in dd/mm/yyyy format for UK.
====================================================================== */

function parseDate(str){
	var a;

	a = str.split('/');
	if((a.length != 3)  || isNaN(a[0]) || isNaN(a[1]) || isNaN(a[2]) 
		 || (parseInt(a[0],10) > 31) || (parseInt(a[0],10) <= 0) 
		 || (parseInt(a[1],10) > 12) || (parseInt(a[1],10) <= 0) 
		 || (parseInt(a[2],10) <= 0) || (a[2].length != 4) || (parseInt(a[2],10) < 1850) ) {
		return false;
	}

	return a;
}

/* ======================================================================
FUNCTION:	compareDates
 
INPUTS:		date1 (string): the first date string to be compared
		date2 (string): the second date string to be compared	

RETURN:  	1 if date1 > date2
		0 if date1 = date2
		-1 if date1 < date2
		-2 if any of the date formats is invalid.

DESC:		This function is used to parse the date string validation in dd/mm/(yy)yy format.
====================================================================== */
function compareDates( date1, date2) {
	var dateArray1 = parseDate(date1);
	var dateArray2 = parseDate(date2);

	if ( !dateArray1 || !dateArray2 ){
		return -2;
	}

	var strMM1 = dateArray1[1];
	//strMM1 -= 1;

	var strMM2 = dateArray2[1];
	//strMM2 -= 1;

	var strDD1 = dateArray1[0] ;
	var strDD2 = dateArray2[0] ;

	var lngDate2 = 0;
	var lngDate1 = 0;

	if (strMM1.length == 1) { strMM1 = "0" + strMM1};
	if (strDD1.length == 1) { strDD1 = "0" + strDD1};
	if (strMM2.length == 1) { strMM2 = "0" + strMM2};
	if (strDD2.length == 1) { strDD2 = "0" + strDD2};
	lngDate1 = parseInt(dateArray1[2] + strMM1 + strDD1,10);
	lngDate2 = parseInt(dateArray2[2] + strMM2 + strDD2,10);
	if (lngDate1 > lngDate2 ) {
		//alert('Date1('+lngDate1+') > Date2('+lngDate2+')');
		return 1;
	} else if (lngDate1 == lngDate2){
		//alert('Date1('+lngDate1+') = Date2('+lngDate2+')');
		return -1;
	} else {
		//alert('Date1('+lngDate1+') < Date2('+lngDate2+')');
		return 0;
	}
}

/* ======================================================================
FUNCTION:	isDoubleTwoDecimalPlace
 
INPUTS:		String Numeric value 

RETURN:  	true - if double value is till two places of decimal

DESC:		This function is used to check whether the double value is till two places of decimal
====================================================================== */

function isDoubleTwoDecimalPlace(str)
{
	// Return immediately if an invalid value was passed in
	if (isBlank( str ))
		return false;
	var isValid = true;

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  if(isNaN(str))
		isValid = false; 
	else if(str <= 0)
		isValid = false; 	
	else{

	decStr = str.substring(str.indexOf(".")+1,str.length);  // everything after the '.'
	if(decStr.length>8)
		isValid=false;
	}
	return isValid;
}

/* ======================================================================
FUNCTION:	trimString
 
INPUTS:		str 

RETURN:  	trimmed string

DESC:		This function is used trim the string with spaces
====================================================================== */
function trimString (str) {
	if(str.length > 0) {
		while(''+ str.charAt(str.length-1)==' '){
	 		str = str.substring(0,str.length-1);
		}
	}
	
	if(str.length > 0) {
		while(''+ str.charAt(0)==' '){
			str = str.substring(1,str.length);
		}
	}
	return str;
}

/* ======================================================================
FUNCTION:	birthAgeCompare
 
INPUTS: currentDate,date to be compared,no of years		 

RETURN:  	

DESC:	This will determine the age of person	
====================================================================== */

function birthAgeCompare(birthDate,todayDate,age) {

	var birthDay, birthMonth, birthYear;
	var todayDay, todayMonth, todayYear;

	var birthDate = parseDate(birthDate);
	var todayDate = parseDate(todayDate);

	if ( !birthDate || !todayDate){
		return false;
	}

	birthDay = birthDate[0];
	birthMonth = birthDate[1];
	birthYear = birthDate[2];

	todayDay = todayDate[0];
	todayMonth = todayDate[1];
	todayYear = todayDate[2];

	var date1 = new Date();
	var date2 = new Date();
	var returnVal = 0;	  

	date2.setDate(birthDay);
	date2.setMonth(birthMonth);
	date2.setYear(birthYear);

	date1.setDate(todayDay);
	date1.setMonth(todayMonth);
	date1.setYear(todayYear);

	var days = (date1 - date2)/(60*60*1000*24);
	var age  = (365*age);

	if(days < age){
		returnVal = 1;
	}

	return returnVal;
}

/* ======================================================================
FUNCTION:	isValidLength
 
INPUTS: value of field, minimum length, maximum length

RETURN:  true if value is between minimum and maximum length else false

DESC:	This function is used to check whether the supplied value is between minimum and maximum length
====================================================================== */
function isValidLength(value,minlen,maxlen){
	if(!isBlank(value) && (value.length >= minlen) && (value.length <= maxlen)){
		return true;
	}
	return false;
}
