//
//	These JavaScript functions can be included in a JSP as follows :
//	<SCRIPT language="JavaScript" SRC="jscript.js"> </SCRIPT> (assuming this is the same directory)
//
//	NB Do not move the following variables as they are global and referenced by calling pages
//
var input_amount;
var daysFromToday;
var Pat;

function validate_monetary_amount (inString) {
//
//	Function  : validate_monetary_amount
//	Purpose   : ensure a valid monetary value is entered for calculations and remove any comma seperators (if used)
//	Parameter : string value
//	Return    : a) boolean true/false
//				b) if true - parsed numeric monetary value stored in field named -input_amount-
//				c) if false - field named -input_amount- set to "undefined"
//
//	alert('Made it to validate_monetary_amount !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	input_amount = "undefined";
	Pat = /\D/g;
	var matchArray = inString.match(Pat); 
	if (matchArray != null) 
	{
//	    alert('matchArray length is ' + matchArray.length);
	    for (var i=0; i < matchArray.length; i++) 
	    {
//			alert('Array position ' + i + ' is ' + matchArray[i]) ;
	        if (matchArray[i] != "." && matchArray[i] != ",") return false;
	    }
	}
	Pat = /^(\d{1,3}(\,\d{3})*|\d+)(\.\d{1,2})?$/;
	if (!Pat.test(inString)) return false;
	
	Pat = /\,/g;
	input_amount = inString.replace(Pat,"");
//	alert('Number entered is ' + input_amount);
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_numeric_only (inString) {
//
//	Function  : validate_numeric_only
//	Purpose   : ensure only whole numbers entered
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_numeric_only !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	Pat = /^\d+$/;
	if (!Pat.test(inString)) return false;
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_numeric_entry (inString) {
//
//	Function  : validate_numeric_entry
//	Purpose   : ensure at least one number entered
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_numeric_entry !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	Pat = /\d+/;
	if (!Pat.test(inString)) return false;
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_alphabetic_entry (inString) {
//
//	Function  : validate_alphabetic_entry
//	Purpose   : ensure at least one letter entered
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_alphabetic_entry !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	Pat = /[a-z]|[á]|[é]|[í]|[ó]|[ú] /i;
//	alert('Alpha chars are: ' + Pat);
	if (!Pat.test(inString)) return false;
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_alphanumeric_entry (inString) {
//
//	Function  : validate_alphanumeric_entry
//	Purpose   : ensure at least one letter or number is entered
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_alphabetic_entry !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	Pat = /[a-z]|[0-9]/i;
	if (!Pat.test(inString)) return false;
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_phone_entry (inString) {
//
//	Function  : validate_phone_entry
//	Purpose   : ensure at least one letter or number is entered
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_alphabetic_entry !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
    Pat = /[a-z]|[\*]|[!]|[£]|[\$]|[%]|[\^]|[&]|[=]|[¬]|[@]|[~]|[#]|[>]|[<]|[,]|[.]|[\?]|[\/] /i;
	if (Pat.test(inString)) return false;

//
//	All checks OK so set indicator true
//
	return true;
}

function validate_email_entry (inString) {
//
//	Function  : validate_email_entry
//	Purpose   : ensure valid email (string @ string)
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_email_entry !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	Pat = /^.+@.+\.([a-z]|[A-Z]){2,4}$/;
	if (!Pat.test(inString)) return false;
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_time (inString) {
//
//	Function  : validate_time
//	Purpose   : ensure valid time entered in 24 hr format i.e. from 00:00 to 23:59
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_time !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	Pat = /^([01]\d|2[0-3]):[0-5]\d$/;
	if (!Pat.test(inString)) return false;
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_date_entry (inString) {
//
//	Function  : validate_date_entry
//	Purpose   : ensure date is entered in dd/mm/yyyy format - range 1/1/1000 to 31/12/2999 and calculate difference in days from system date
//				NB variable todayDate MUST be set up with the system date in the page that includes this JS source (example follows) as it is required in this function
//
//					<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
//					<% SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
//					Date now = new Date();
//					String sysDate =  dateFormatter.format(now);
//					%>
//					.....
//					<SCRIPT language=JavaScript>
//					.....
//					var todayDate = "<%= sysDate %>"; 
//					.....
//					</SCRIPT>
//
//	Parameter : string value
//	Return    : a) boolean true/false
//				b) if true - number of days from todays date in field named -daysFromToday-
//				c) if false - field named -daysFromToday- is set to "undefined"
//
//	alert('Made it to validate_date_entry !');
//	alert('Length is ' + inString.length);
	var MINUTE = 60 * 1000
	var HOUR = MINUTE * 60
	var DAY = HOUR * 24
	var WEEK = DAY * 7
	daysFromToday = "undefined";
	Pat = /^(0?[1-9]|[12]\d|3[01])(\/)(0?[13578]|1[02])(\/)([12]\d{3})$|^(0?[1-9]|[12]\d|30)(\/)(0?[469]|11)(\/)([12]\d{3})$|^(0?[1-9]|1\d|2[0-8])(\/)(0?2)(\/)([12]\d{3})$|^(29)(\/)(0?2)(\/)([12]\d)([02468][048]|[13579][26])$/;
	if (!Pat.test(inString)) return false;
	Pat = /^(\d+)(\/)(\d+)(\/)(\d{4})$/;
	var matchArray = inString.match(Pat);
	var inputDate = new Date(matchArray[5], matchArray[3]-1, matchArray[1]);
	matchArray = todayDate.match(Pat);
	var today = new Date(matchArray[5], matchArray[3]-1, matchArray[1]);
	daysFromToday = parseInt((inputDate - today) / DAY);
//	alert('The difference in days from today is ' + daysFromToday);
//
//	All checks OK so set indicator true
//
	return true;
}

function validate_account_number(inString) {
//
//	Function  : validate_account_number
//	Purpose   : 8 digit modulus check for valid EBS account number
//	*NB*      : REMOVED - Legal issue if account validation rules can be verified (Rits report)
//              Left in check for 8 digits and zero value
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_account_number !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	if (inString == 0) return false;
	Pat = /^(\d{8})$/;
	if (!Pat.test(inString)) return false;
	return true;
}

function validate_EB_account_numbers(inString) {
//
//	Function  : validate_account_number
//	Purpose   : 8 digit modulus check for valid EBS account number
//	*NB*      : REMOVED - Legal issue if account validation rules can be verified (Rits report)
//              Left in check for 8 digits and zero value
//	Parameter : string value
//	Return    : boolean true/false
//
//	alert('Made it to validate_account_number !');
//	alert('Parameter is ' + inString);
//	alert('Length is ' + inString.length);
	if (inString == 0) return false;
	Pat = /^(\d{6})$/;
	if (!Pat.test(inString)) return false;
	return true;
}


function format_numeric (inString) {
//
//	Function  : format_numeric
//	Purpose   : reformat numeric fields for display, add commas for large values
//	Parameter : a number converted to string format
//	Return    : formatted number
//
//	alert('inString = ' + inString);
//	alert('InString Length = ' + inString.length);
	iStart = inString.indexOf(".");
	if (iStart < 0) iStart = inString.length;
	iStart = iStart - 3;
	while (iStart > 0) {
	    inString = inString.substr(0,iStart) + "," + inString.substr(iStart,inString.length);
	    iStart = iStart - 3;
	}
	return(inString);
}

function parse_field (inString) {
//
//	Function  : parse_field
//	Purpose   : to set up a string to display a numeric value of only up to 2 decimal places
//	Parameter : a number converted to string format
//	Return    : display field that has been parsed
//
//	alert('inString = ' + inString);
//	alert('InString Length = ' + inString.length);
	dec_point_pos = inString.indexOf(".");
//	alert('Dec Point Pos ' + dec_point_pos);
	if (dec_point_pos == -1)
	{
//		alert("Number = " + inString);
//	    display_field = inString;
		display_field = inString.concat(".00");
	    return(display_field);
	}
	if (inString.substr(dec_point_pos + 2, 1).length == 0)
	{
	    display_field = inString.substr(0, dec_point_pos + 2) + "0";
	}
	else
	{
	    display_field = inString.substr(0, dec_point_pos + 3);
	}
//	alert('Display Field ' + display_field);
	return(display_field);
}

function parse_field_all (inString) {
//
//	Function  : parse_field_all
//	Purpose   : to set up a string to display a numeric value of 2 or more decimal places
//	Parameter : a number converted to string format
//	Return    : display field that has been parsed
//
//	alert('inString = ' + inString);
//	alert('InString Length = ' + inString.length);
	dec_point_pos = inString.indexOf(".");
//	alert('Dec Point Pos ' + dec_point_pos);
	if (dec_point_pos == -1)
	{
//		alert("Number = " + inString);
//	    display_field = inString;
		display_field = inString.concat(".00");
	    return(display_field);
	}
	if (inString.substr(dec_point_pos + 2, 1).length == 0)
	{
	    display_field = inString.substr(0, dec_point_pos + 2) + "0";
	}
	else
	{
	    display_field = inString.substr(0, inString.length);
	}
//	alert('Display Field ' + display_field);
	return(display_field);
}

function round_and_format (x){

// this function rounds floats to 0 decimal places - it uses the math functions round and 
// pow.  The number to be rounded is multiplied by 10 to the power of the number of decimal places 
// one is rounding to - in this case 0

	inString = "" + (Math.round (x * Math.pow (10,0))) / Math.pow (10,0);
	
//this code inserts commas in large numbers ie 1000 becomes 1,000 and then inserts a £ sign at //the front

	iStart = inString.length;
	iStart = iStart - 3;
	while (iStart > 0) {
	    inString = inString.substr(0,iStart) + "," + inString.substr(iStart,inString.length);
	    iStart = iStart - 3;
	}
//	inString = "£".concat(inString);
	return(inString);
}

function removeCommas(inString) {
// This function removes commas from a string
var Pat = /\,/g;
var output =inString.replace(Pat,"");
 return output;

}
function reformatNumeric(input){

// This function rounds floats to 0 decimal places
// It first removes any existing User entered commas to avoid errors 
    var x = removeCommas(input);
    return round_and_format(x);
}



is_ns4up = (document.layers)? true:false
is_ie4up = (document.all)? true:false
verOK = (is_ns4up || is_ie4up)

function OpenCurrencyWindow (currency, amount){
//
//	Function  : OpenCurrencyWindow
//	Purpose   : Open a window coverting from IEP to Euro or Euro to IEP based on input currency type
//	Parameter : Currency Type and Amount to be converted
//	Return    : none
//
//		Extract server name directly from location
//		This will only work if the same directory structure is used on all machines
//
//		var where = new String(document.location);
//		var server = where.substr(where.indexOf("://")+3,where.indexOf("/",where.indexOf("://")+3)-(where.indexOf("://")+3));
//		var linkadd = "http://" + server + "/internet/ConvertCurrency.jsp?fromCurrency=" + escape(currency) + "&fromAmount=" + amount;
//		window.open(linkadd,"Latest","toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=no,height=150,width=400");
}

function OpenRepaymentAnnuityWindow (currency,z0,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10,z11,z12,z13,z14,z15,z16,z17,z18,z19,z20,z21,z22,z23,z24,z25,z26,z27,z28,z29,z30,z31,z32,z33,z34){
//
//	Function  : OpenRepaymentAnnuityWindow
//	Purpose   : Open a window coverting from IEP to Euro or Euro to IEP based on input curency type
//	Parameter : Currency Type and Amounts to be converted
//	Return    : none
//
//		Extract server name directly from location
//		This will only work if the same directory structure is used on all machines
//
//		var where = new String(document.location);
//		var server = where.substr(where.indexOf("://")+3,where.indexOf("/",where.indexOf("://")+3)-(where.indexOf("://")+3));
//		var linkadd = "http://" + server + "/internet/ConvertRepaymentCurrency.jsp?fromCurrency=" + escape(currency)+"&z0="+z0+"&z1="+z1+"&z2="+z2+"&z3="+z3+"&z4="+z4+"&z5="+z5+"&z6="+z6+"&z7="+z7+"&z8="+z8+"&z9="+z9+"&z10="+z10+"&z11="+z11+"&z12="+z12+"&z13="+z13+"&z14="+z14+"&z15="+z15+"&z16="+z16+"&z17="+z17+"&z18="+z18+"&z19="+z19+"&z20="+z20+"&z21="+z21+"&z22="+z22+"&z23="+z23+"&z24="+z24+"&z25="+z25+"&z26="+z26+"&z27="+z27+"&z28="+z28+"&z29="+z29+"&z30="+z30+"&z31="+z31+"&z32="+z32+"&z33="+z33+"&z34="+z34;
//		window.open(linkadd,"Latest","toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=no,height=280,width=400");
}

//
//	Function   : Open contact us window
//
function OpenContactUsWindowWithServerName() {
//
//		Extract server name directly from location
//		This will only work if the same directory structure is used on all machines
//
	var where = new String(document.location);
	var server = where.substr(where.indexOf("://")+3,where.indexOf("/",where.indexOf("://")+3)-(where.indexOf("://")+3));
	var linkadd = "http://" + server + "/internet/general/contact.jsp";
	window.open(linkadd,"Latest","toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=no,height=700,width=530");
}

function validate_cc_number(inString) {
//
//	Function  : validate_cc_number
//	Purpose   : Modulus check for valid credit card number
//	Parameter : String value
//	Return    : boolean true/false
//
	if (inString != 0) {
		Pat = /^(\d{16})$/;
		if (Pat.test(inString)) {
			Pat = /^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/;
			matchArray = inString.match(Pat);
			var modTotal = 0;
			var digitCalc = 0;
			for (var i=1; i<=16; i++) {
				if (i%2 != 0) {
					digitCalc = matchArray[i] * 2;
				} else {
					digitCalc = matchArray[i] * 1;
				}
				if (digitCalc > 9) digitCalc = digitCalc - 9;
				modTotal = modTotal + digitCalc;
			}
			if (modTotal % 10 == 0) {
				return true;
			}
		}
	}
	return false;
}

function validate_cc_Amex_number(inString) {
//
//	Function  : validate_cc_Amex_number
//	Purpose   : Modulus check for valid Amex credit card number
//	Parameter : String value
//	Return    : boolean true/false
//
	if (inString != 0) {
		Pat = /^(\d{15})$/;
		if (Pat.test(inString)) {
          return mod10(inString);
		}
	}
	return false;
}

/*function validate_cc_Amex_number(inString) {
//
//	Function  : validate_cc_Amex_number
//	Purpose   : Modulus check for valid Amex credit card number
//	Parameter : String value
//	Return    : boolean true/false
//
	if (inString != 0) {
		Pat = /^(\d{15})$/;
		if (Pat.test(inString)) {
			Pat = /^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/;
			matchArray = inString.match(Pat);
			var modTotal = 0;
			var digitCalc = 0;
			for (var i=1; i<=15; i++) {
				if (i%2 != 0) {
					digitCalc = matchArray[i] * 2;
				} else {
					digitCalc = matchArray[i] * 1;
				}
				if (digitCalc > 9) digitCalc = digitCalc - 9;
				modTotal = modTotal + digitCalc;
			}
			if (modTotal % 10 == 0) {
				return true;
			}
		}
	}
	return false;
}*/


function mod10( cardNumber ) { // LUHN Formula 
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;


    	for( i = 0; i < cardNumber.length; ++i ) {
    		ar[i] = parseInt(cardNumber.charAt(i));
    	}
    	for( i = ar.length -2; i >= 0; i-=2 ) { 
    		ar[i] *= 2;							
    		if( ar[i] > 9 ) ar[i]-=9;			
    	}										


        	for( i = 0; i < ar.length; ++i ) {
        		sum += ar[i];						 
        	}
        	return (((sum%10)==0)?true:false);	 	
    }



function OpenAnyURLNewWindow(url,h,w){
var newparams = '';
newparams = 'height=' + h+ ',width='+w+',top=200,left=200,scrollbars'
var newwind = window.open(url, 'new', newparams);
if (newwind.opener == null){
newwind.opener = self;}
newwind.focus();
}

function OpenContactUsURLNewWindow(url){
	var h = '650';
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("firefox") != -1) {
		h = '700'
	}
	var newparams = 'height=' + h + ',width=550,top=200,left=200,scrollbars'
	var newwind = window.open(url, 'new', newparams);
	if (newwind.opener == null){
		newwind.opener = self;
	}
	newwind.focus();
}