function setCookie(name,value,days) {
	if (days > 0) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	//document.cookie = name+"="+value+expires+"; path=/ ; domain=discoverbank.com";
	document.cookie = name+"="+value+expires+"; path=/ ;";
}

/* Retrieve the existing cookies value */
function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/* Invalidate the cookie */
function eraseCookie(name) {
	setCookie(name,"",-1);
}


/* Validate the bnksrc30d cookie expiry day 30 */
function dateDiff(bnksrc30d)
{
	src_date = new Date();
	src_date.setDate(bnksrc30d.substring(2,4));
	src_date.setMonth((parseInt(bnksrc30d.substring(0,2))-1));
	src_date.setYear(bnksrc30d.substring(4,8));
	src_date.setHours(bnksrc30d.substring(8,10));
	src_date.setMinutes(bnksrc30d.substring(10,12));
	src_date.setSeconds(bnksrc30d.substring(12,14));
		
	today = new Date();
	difference = today - src_date;
	days = Math.round(difference/(1000*60*60*24));
	return days;
}

/*current date and time (MMDDYYYYhhmmss) Format */
function currentDateFormat()
{
	now = new Date();
	var mon = doubleDigit((now.getMonth()+1));
	var date = doubleDigit(now.getDate());
	var year = now.getFullYear();
	var hr = doubleDigit(now.getHours());
	var min = doubleDigit(now.getMinutes());
	var sec = doubleDigit(now.getSeconds());

	return mon+date+year+hr+min+sec;
}

/* Leading zero for single digit number */
function doubleDigit(pre)
{
 var str=""+pre
 if(str.length == 1)
   return "0"+pre;
  else return pre;
}

var bnk_src_sub = null;
var bnk_srctd_sub = null;

/* Validate the src code and get the bnksrc30 and bnksrc30d values from cookies */
function checkCookie()
{
var bnksrc30;
var bnksrc30d;
var days;

if(location.search){
vals=(location.search.substring(1).replace(/\+/g," ")).split("&");
for(i=0;i<vals.length;i++){
vals[i]=vals[i].split("=");

 if(vals[i][0] == 'src')
 {
	var regex=/^[0-9A-Za-z]+$/; 
	var src = vals[i][1];
	if(regex.test(vals[i][1]))   //Check whether src code matches the regular expression pattern ([0-9A-Za-z]+)
	{ 
		bnksrc30=getCookie('bnksrc30');
		bnksrc30d=getCookie('bnksrc30d'); 
						
		/*If the cookie (bnksrc30) is empty (ie not set) then set the value of cookie (bnksrc30) to  src value received from the query string(step 1) and set the  bnksrc30d (Cookie #2 ) to current date and time (MMDDYYYYhhmmss)*/

		if(bnksrc30 == null){
		setCookie("bnksrc30",src,30);
		setCookie("bnksrc30d",currentDateFormat(),30);
		}
		
		if(bnksrc30d != null){	
		days = dateDiff(bnksrc30d);    //for validate the bnksrc30d cookie expire days
		}
		
		/*If the cookie bnksrc30 (Cookie #1 ) is not empty and bnksrc30d (Cookie #2 )date is lesser than 30 days then src will be set to the value received from the cookie(bnksrc30)(ie when forward the request to the secure pages the new src values will be utilized)*/
					
		if( bnksrc30 != null && (days >= 0 && days <= 30) )
		{
		 if(document.getElementById("genericForm") != null) document.forms[0].src.value = bnksrc30;
		 if(document.getElementById("secure-login") != null) document.forms[1].src.value = bnksrc30;
		}
		
		/* If the  interval between  bnksrc30d (Cookie #2 )   and current date is more than 30 days  and  valid src  value received form query string then bnksrc30(Cookie #1) will be reset to the src value and bnksrc30d (Cookie #2 ) will be set to current date and time*/
		if(days > 30)
		{
		setCookie("bnksrc30",src,30);
		setCookie("bnksrc30d",currentDateFormat(),30);
		}
		
		/*If the src code is empty and interval between bnksrc30d (Cookie #2 ) and current date is more than 30 days then bnksrc30 (Cookie #1 )	will be set to empty*/
		if( (src == null || src == "") && days > 30)
		{
		setCookie("bnksrc30","",-1);
		}
		
		/* incoming src code is valid and  different from the cookie #1 (bnksrc30) then we have to use the src code (ie reset the cookie #1) even if the cookie #2 lesser than 30days*/
		/*if(src != bnksrc30 && days < 30){
			setCookie("bnksrc30",src,30);		
		} */
		
		//If the cookies and src value both are empty then request will be handled same as today
		if( (src == null || src == "") && (bnksrc30 == null || bnksrc30 == "")) return false;
		
		//The bnk_src_sub and bnk_srctd_sub values send to private pages from the persistent Cookie values
		
		bnksrc30=getCookie('bnksrc30');
		bnksrc30d=getCookie('bnksrc30d'); 
						
		if(bnksrc30.length == 0 || bnksrc30 == ""){
		bnksrc30 = "NULL";
		}
		if(bnksrc30d.length == 0 || bnksrc30d == ""){
		bnksrc30d = "NULL";
		}
		
		bnk_src_sub = bnksrc30;
		bnk_srctd_sub = bnksrc30d;
		
		if(document.getElementById("genericForm") != null){
		document.forms[0].bnk_src_sub.value = bnksrc30;
		document.forms[0].bnk_srctd_sub.value = bnksrc30d;}
       			
		if(document.getElementById("secure-login") != null){
		document.forms[1].bnk_src_sub.value = bnksrc30;
       	document.forms[1].bnk_srctd_sub.value = bnksrc30d;}

								
	return true;	
	}
	
 }

}

}


}

function appendLoginQuery()
{
 location.href="https://global1.onlinebank.com/vtg/dologinfihosted.htm?banknr=1299"+"&bnk_src_sub="+bnk_src_sub+"&bnk_srctd_sub="+bnk_srctd_sub;
}

function appendRegisterQuery()
{
 location.href="https://global1.onlinebank.com/vtgv/vtgr.htm?tmpl=reginfo.htm&amp;banknr=1299"+"&bnk_src_sub="+bnk_src_sub+"&bnk_srctd_sub="+bnk_srctd_sub;
}