/*

	Routines to validate user input

	Converted to handle various pain-in-the-**** MSIE vs. Netscape inconsistencies

*/

function toUpper(str) {
	var sText = String(str)
	str=sText.toUpperCase()
}

function fieldUpper(oField) {
	var tstr = String(oField.value)
	oField.value = tstr.toUpperCase()
	return true
}
	
function fieldUpperNonNull(oField,errorText) {
	var tstr = String(oField.value)
	
	if (tstr.length == 0) {
		alert(errorText)
		oField.focus()
		return false
	} else {
		oField.value = tstr.toUpperCase()
		return true
	}
}
	
function selectedValue(selectBox) {
	with (selectBox) {
		return options[selectedIndex].value
	}
}
	
/*
 Function to validate Day (only ensures Numeric input)
*/
function fnValidDD(sDay) {
	var digits1 = "0123"
	var digits2 = "0123456789"
	var tsDay = String(sDay)
	if (digits1.indexOf(tsDay.charAt(0)) < 0) {
		alert("Invalid Day " + tsDay);
		document.forms[0].elements[0].focus();
		return false;
	}
	else if (digits2.indexOf(tsDay.charAt(1)) < 0) {
		alert("Invalid Day " + tsDay);
		document.forms[0].elements[0].focus();
		return false;
	}
	else if (eval(tsDay) > 31) {
		alert("Invalid Day " + tsDay);
		document.forms[0].elements[0].focus();
		return false;
    }
    return true;
}



/*
 Function to validate Departure-Date Month,
*/
function fnValidMM(sMonth) {
	var digits1 = "01"
	var digits2 = "0123456789"
	var tsMonth = String(sMonth)
	if (digits1.indexOf(tsMonth.charAt(0)) < 0) {
		alert("Invalid Month " + tsMonth);
		document.forms[0].elements[0].focus();
		return false;
	}
	else if (digits2.indexOf(tsMonth.charAt(1)) < 0) {
		alert("Invalid Month " + tsMonth);
		document.forms[0].elements[0].focus();
		return false;
	}
	else if (eval(tsMonth) > 12) {
		alert("Invalid Month " + tsMonth);
		document.forms[0].elements[0].focus();
		return false;
	}
	return true;	    
}

/*
 Function to validate Departure-Date Month, Converts input to Uppercase first
*/
function fnValidMMM(field) {
	var tfield = String(field)
	tfield = tfield.toUpperCase()
	var sMonth = tfield
	if (sMonth != "JAN" && sMonth != "FEB" && sMonth != "MAR" && sMonth != "APR" && sMonth != "MAY" && sMonth != "JUN" && sMonth != "JUL" && sMonth != "AUG" && sMonth != "SEP" && sMonth != "OCT" && sMonth != "NOV" && sMonth != "DEC")
		{alert("Invalid Month " + tfield);
		field.focus();
		return false;
    }
	return true;
}

function validateNumeric(field) {

	var valid = "0123456789";
	var ok = "yes";
	var temp;
	var tfield = String(field);
	// alert(tfield);
	
	for (var i=0; i<tfield.length; i++) {
				
		temp = "" + tfield.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
    if (tfield.length == 0) return false;
	if (ok == "no") {
		sErr = 'Invalid Character\r\r';
		sErr = sErr + 'The character you have entered is not valid \r';
		sErr = sErr + 'Please delete the character and try again.\r';
		//alert(sErr);
		return false;
	} else {
		return true;
	}
}

/*
 Function to validate year (only ensures Numeric input)
*/
function fnValidYY(sYear) {
	tsYear = String(sYear)
	var iLen = tsYear.length;
	var digits="0123456789";
	for (i=0; i<iLen; i++) {
		if (digits.indexOf(tsYear.charAt(i)) < 0) {
			alert("Invalid Year " + tsYear);
			document.forms[0].elements[0].focus();
			return false;
        }
    }
	return true;    
}


/*
Function must fill
*/
function fnMustFill(str, reqLen)
{

var tstr = String(str);
toUpper(tstr);
if (tstr.length != 0)
	{
	if (tstr.length < reqLen)
		{alert("Invalid field length must be " + reqLen + " characters long");
		document.forms[0].elements[0].focus();
		return false;
		}
    }
   return true;
}


function setSel( str) {
	checkunload=false;
	document.forms[0].elements["TxtSelection"].value = str;

	//document.forms[0].submit();
}

function setSelSubmit(str) {
	setSel(str)
	document.forms[0].submit();
	return false
}

/*
 Function to validate Confirm Response, Converts to Uppercase first 
*/ 
function fnConfirmYN(fi)
{
	toUpper(fi);
	if (fi != "Y" && fi != "N")
		{alert("Please enter Y or N " + fi);
		 document.forms[0].elements[0].focus()
		 return false;
		}
	return true;
} 
function fnConfirmYNF(fi)
{
	toUpper(fi);
	if (fi != "Y" && fi != "N" && fi != "F")
		{alert("Please enter Y, N or F " + fi);
		 document.forms[0].elements[0].focus()
		 return false;
		}
	return true;
} 
function fnConfirmYNC(fi)
{
	toUpper(fi);
	if (fi != "Y" && fi != "N" && !IsBlank(fi))
		{alert("Please enter Y or N " );
		fi = "";
		 document.forms[0].elements[0].focus()
		return false;
		}
	return true;
} 

/* Function to validate the SEX, Converts to Uppercase first
*/
function fnValidSex(str)
{
	toUpper(str);

	if (str != "M" && str != "F" && str != "I" && str != "C" && !IsBlank(str))
		{alert("Invalid sex M,F,C or I " + str);
		 document.forms[0].elements[0].focus();
		 return false;
		}	
	return true;
}
/*
 Function to make sure user has entered a string response
*/
function fnIsStringResReqd(str)
{
	var tstr = String(str)
  	toUpper(tstr);
  	str = tstr
    if (tstr.length == 0) 
		{alert("Response required");
		return false;
		}
	return true;
}

//function to validate that input is only numbers

function validateCurrency(field,blnAlert) {
	var valid = "0123456789.£,#"
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.length; i++) {
			
		temp = "" + field.substring(i, i+1);
		nChr = temp.charCodeAt(temp)
		
		//the nChr check is due to obscure iMac IE4.5 behaviour that doesn't accept that
		//the £ symbol is a valid character. So we check that its unicode 163 (£) before
		//raising the error to the user. 
		
		if ( (valid.indexOf(temp) == "-1") && (nChr != 163) && (nChr != 44) ) ok = "no";
	}
		
	if (ok == "no") {
		sErr = "Invalid Amount Entered\r\r"
		sErr = sErr+"The character you have entered is not valid, and \r"
		sErr = sErr+"cannot be interpreted by the system. The field \r"
		sErr = sErr+"you are editing is a currency field, and \r"
		sErr = sErr+"therefore cannot contain letters, special \r"
		sErr = sErr+"characters or spaces.\r\r" 
		sErr = sErr+"Please delete the character and try again."
		 
			alert(sErr); 
		
		return false;
	} else {
	
		//now we've evaluated that its got the right characters, we need to 
		//evaluate more literally, is it a number?
		
		//**HACK
		//strip £ symbols(163) and , (44) from iMac version	
		var newS = new String
		for (var i=0; i<field.length; i++) {
			temp = "" + field.substring(i, i+1);
			nChr = temp.charCodeAt(temp)
			if (nChr!=163 && nChr!=44){ newS = newS + temp }
		}
	
		//function expects a string with £., etc, so it trims it
		strAmount = newS
		iAmount = strAmount.toString().replace(/\£|\,/g,'');

		var iVar = new Number('0'+iAmount);
		
		if (isNaN(iVar)){
		
			//feasibly, the only thing they could have done wrong at this
			//stage is put two decimal points into the field.
			
			sErr = 'Invalid Character in Amount\r\r'
			sErr = sErr + 'The character you have entered is not valid, and \r'
			sErr = sErr + 'cannot be interpreted by the system. The field \r'
			sErr = sErr + 'you are editing is numeric, and can only contain\r'
			sErr = sErr + 'a single decimal point.\r\r'
			sErr = sErr + 'Please delete the extra decimal and try again.\r'
			
			 
			alert(sErr); 
			
			return false;
			
		} else {
			return true;
		}
		
	}
}

function validateNumericDecPlaces(oField, iDecimalPlaces) {

	
	sValue = oField.value;
	
	//trim currency characters	
	sValue = sValue.toString().replace(/\£|\,/g,'');
	
	//check its a number to start with, if its not theres a problem (return false)
	if (isNaN(sValue)) {
		return false;
	
	//now we have a number
	} else {
	
		//split it either side of the decimal, and assign the right side to 'afterDot'
		aValue = sValue.split(".");
		var afterDot = aValue[1];
		
		//if afterDot exists, there are decimal places
		if (afterDot) {
			nDecPlaces = afterDot.length;
		
			//if the number of digits is greater than the number we need to trim to
			if (nDecPlaces > iDecimalPlaces) {
				
				//reset the trimmed value back into the page
				oField.value = aValue[0]+"."+aValue[1].substr(0,2);
						
				//raise an error
				sErr = "Invalid Amount Entered\r\r"
				sErr = sErr+"You have entered more than 2 decimals places for\r"
				sErr = sErr+"the amount and this cannot be interpreted by the\r"
				sErr = sErr+"system. The field you are editing has a maximum \r"
				sErr = sErr+"of "+iDecimalPlaces+" digits after the decimal place.\r\r"
				sErr = sErr+"The extra digit has been deleted."
				alert(sErr);
				
				//return false to set this function value to false
				return false;
				
			//else the number of digits is fine, return true	
			} else {
					
				return true;
			}
			
		
		//else there were no decimal places, which is fine (return true)
		} else {
		
			return true;
		}
	}	
}

function IsNull( val ) {
	var isValid = false;

 	if (val+"" == "null")
 		isValid = true;
		
	return isValid;
}  // end IsNull

function IsUndef( val ) {
	var isValid = false;

 	if (val+"" == "undefined")
 		isValid = true;
		
	return isValid;
}  // end IsUndef

function IsBlank( str ) {
	var isValid = false;

 	if ( IsNull(str) || IsUndef(str) || (str+"" == "") )
 		isValid = true;
		
	return isValid;
}  // end IsBlank

/* ======================================================================
FUNCTION:  	IsInt
 
INPUT:  		numstr (string/number) 	 - the string that will be tested to ensure 
      										   that each character is a digit
				allowNegatives (boolean) - (optional) when true, allows numstr to be
													negative (contain a '-').  When false,
											      any negative number or a string starting
													with a '-' will be considered invalid.

RETURN:  	true, if all characters in the string are a character from 0-9,
				regardless of value for allowNegatives
				true, if allowNegatives is true and the string starts with a '-', and all other
				characters are 0-9.
     			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 IsInt( numstr, allowNegatives ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	// Default allowNegatives to true when undefined or null
	if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null")	
		allowNegatives = true;

	var isValid = true;

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special case for negative numbers (first char == '-').   
	for (i = 0; i < numstr.length; i++) {
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) || 
				(numstr.charAt(i) == "-" && !allowNegatives)) {
       	isValid = false;
       	break;
      }
         	         	       
   } // END for   
   
   	return isValid;
}  // end IsInt

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validateNumericSp(field) {

	var valid = "0123456789 ";
	var ok = "yes";
	var temp;
	var tfield = String(field);
	// alert(tfield);
	
	for (var i=0; i<tfield.length; i++) {
				
		temp = "" + tfield.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
    if (tfield.length == 0) return false;
	if (ok == "no") {
		sErr = 'Invalid Character\r\r';
		sErr = sErr + 'The character you have entered is not valid \r';
		sErr = sErr + 'Please delete the character and try again.\r';
		//alert(sErr);
		return false;
	} else {
		return true;
	}
}
function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
} function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

}function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

}function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
}function isSwitch(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length >= 16) && ((firstdig == 6) &&
      ((seconddig == 7) || (seconddig == 3) )) ||      (firstdig == 4) && (seconddig ==9))
    return isCreditCard(cc);
  return false;
}function isAnyCard(cc)
{
  if (!isCreditCard(cc))
    return false;
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
      !isSwitch(cc)) {
    return false;
  }
  return true;

}
function GetRadio(radio_array) {

	var i;
	if (radio_array.length) {
		for (i = 0; i < radio_array.length; ++i)
		if (radio_array[i].checked)
		return radio_array[i].value;
	}
	else
	{	
		if (radio_array.checked)
			{ return radio_array.value ;}
	}	
	return null;
}
