var domain = ' ';
var phoneNum = ' ';


function openChildWindow(url) {
		w = screen.availWidth-10;
		h = screen.availHeight-50;
		features = "width="+w+",height="+h;
		features += ",left=0,top=0,scrollbars=1,resizable=1,status=0";
		window.open(url, "", features);
}



function displayHideSelect(){

}



function setCountryDropDown(countries,us){
	

	
}

URL = {
	servicePath: "/sapi-travelinsurance"
}; 

function BindEnv( thisReference, func ){
	return function(){
		func.apply(thisReference, arguments);
	}
}

function jGet( id ){
	return jQuery( '#' + id ).get(0);
}

function setSelect(id, value){
	var select = jGet(id);
	var index = -1;
	for( var i=0; i<select.options.length; i++ ){
		select.options[i].selected = false;
		if( select.options[i].value == value && index == -1 )
			index = i;
	}
	if( index > -1)
		select.options[index].selected = true;
}

function mapBackDepartLocation( loc ){
	if( loc == 'United Kingdom')
		return 'uk1';
	if( loc == 'Channel Islands' )
		return 'uk2';
	if( loc == 'Isle of Man' )
		return 'uk3';
	return 'uk1';
}

function mapBackReturnLocation( loc ){
	if( loc == 'United Kingdom')
		return 'uk';
	if( loc == 'Europe')
		return 'europe';
	if( loc == 'Worldwide (Excl.US/Canada/Caribbean)')
		return 'world';
	if( loc == 'Worldwide')
		return 'usa';
} 

function formatCurrency( number ){
	var val = 0;
	try{
		val = Number(number).toFixed(2);
	} catch( e ) {}
	return val;
}

var curtainCancelTimerId = 0;

function lockInputs(){
	if( jGet('AjaxLoader') && jGet('AjaxLoader').firstChild &&  jGet('AjaxLoader').firstChild.nextSibling  ){
		jGet('AjaxLoader').firstChild.nextSibling.innerHTML = "Please wait";
		window.clearTimeout( curtainCancelTimerId );
		curtainCancelTimerId = window.setTimeout( 'addCancelLink()', 5000 ); 
	}
	jQuery('#Curtain').css( { opacity : "0.3"} );
	jQuery('#Curtain').fadeIn('def');
	jQuery('#AjaxLoader').fadeIn('def');
}

function unlockInputs(){
	jQuery('#Curtain').fadeOut('def');
	jQuery('#AjaxLoader').fadeOut('def'); 
	window.clearTimeout( curtainCancelTimerId );
}

function addCancelLink(){
	if( jGet('AjaxLoader') && jGet('AjaxLoader').firstChild &&  jGet('AjaxLoader').firstChild.nextSibling  ){
		jGet('AjaxLoader').firstChild.nextSibling.innerHTML =  "Please wait<br/><a id='AjaxLoaderCancel' style='padding:-15px;cursor: pointer;' onclick='unlockInputs();' href='#' >Cancel</a>" ;
	}
}


/* ***HISTORICAL FUNCTIONS:*** */
/*
function showCal(type){
	if (type=='depart_'){
		document.getElementById('calTitle').innerHTML = 'Departure Date';
		dateCalendar.openDateChooser(type);
	}
	if (type == 'return_'){
		document.getElementById('calTitle').innerHTML = 'Return Date';
		dateCalendar.openDateChooser(type);
	}
	if (type == 'motorRenewal_'){
		document.getElementById('calTitle').innerHTML = 'Motor Insurance Renewal Date';
		dateCalendar.openDateChooser(type);
	}
	if (type == 'homeRenewal_'){
		document.getElementById('calTitle').innerHTML = 'Home Insurance Renewal Date';
		dateCalendar.openDateChooser(type);
	}
	document.getElementById('calDiv').style.display = '';
	document.getElementById('calParentDiv').style.display = '';
}
*/
function returnDateChange() {
	 
	var objForm = document.frmStep1;
	var landingType = objForm.landingType.value;
	if (landingType == 'backpacker'){
		backpackerReturnDateChange();
		return;
	}
	 
	var objDays = objForm.days;
	var objReturnDate = objForm.return_date;
	var objDepartDate = objForm.depart_date;
	var strReturnDate = objReturnDate.value;
	var strDepartDate = objDepartDate.value;
		
	
	if (strDepartDate == '') return false;
	if (strReturnDate == '') return false;
	
	
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		return false;
	}
	
	if (objRegExpDate.test(strReturnDate) == false) {
		return false;
	}	
	
	
	arrDepartDate = strDepartDate.split('/');
	arrReturnDate = strReturnDate.split('/');
	
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0], 0,0,0);
	objReturnDate = new Date(arrReturnDate[2], arrReturnDate[1] - 1, arrReturnDate[0],0,0,0);

	
	var intDaysDifference = Math.round( (objReturnDate - objDepartDate) / intOneDay );
	
	intDaysDifference = intDaysDifference + 1;
	
	var landing = objForm.landingType.value;
	if ((landing!='longstay')&&(landing!='backpacker')) if (intDaysDifference > 0 && intDaysDifference <= 100) {
		objDays.selectedIndex = intDaysDifference;
	}
	uktoukCheck();
	wrongReturnDateCheck();
	longStaySetDate();
}

function getCookie(c_name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function capitalize( str ){
	var ns = str.split(' ');
	var val = '';
	for( var i = 0; i < ns.length; i++ )
	    ns[i] = ns[i].substr(0,1).toUpperCase() + ns[i].substr(1).toLowerCase()
	for( var i = 0; i < ns.length; i++ )
	    val += ns[i] + ' ';
	return val.substr(0, val.length-1);
}

function showHelp(objAnchor, theEvent, strText) {

	if (objAnchor.helpTip) {
		objAnchor.helpTip.style.visibility = "hidden";
		objAnchor.onblur = null;
		objAnchor.helpTip = null;
		return;
	}
	
	var objHelpTip = document.createElement("div");
	objHelpTip.className = "help-tooltip";
	document.body.appendChild(objHelpTip);
	objHelpTip.innerHTML = strText;

	//objHelpTip.style.top = (theEvent.clientY + 20) + (document.body.scrollTop) + "px";
	//using JQuery
	var offsets = jQuery( objAnchor ).offset();
	objHelpTip.style.top = (offsets.top + 20) + 'px';
	
	if( offsets.left + 275 > document.body.clientWidth ){
		objHelpTip.style.left = (document.body.clientWidth - 275) + "px";
	} else {
		objHelpTip.style.left = (offsets.left - 8) + "px";	
	}	
	
	
	/*if ((theEvent.clientX + 275) > document.body.clientWidth) {
		objHelpTip.style.left = (document.body.clientWidth - 275) + "px";
	} else {
		objHelpTip.style.left = (theEvent.clientX - 8) + "px";	
	}*/
	
	objAnchor.helpTip = objHelpTip;
	
	objAnchor.onblur = function () {
		document.body.removeChild(objHelpTip);
		objAnchor.onblur = null;
		objAnchor.helpTip = null;
	};	
	
}



function showHelpPreset(objAnchor, theEvent, message_name) {
	
var AMT_MES 	="Covers any number of trips up to 183 days abroad per annum. Maximum 50 days per trip up to age 65 (extendable to 100 days) or 35 days per trip up to age 80. <a href=javascript:void(window.open(\'amt.html\',\'\',\'toolbar=no,width=660,height=700\'))>See Other AMT Benefits<\/a>"; //index,instantquote,long,longqote
var GOLF_MES 	="Cover includes: Golf Equipment &pound;1,000, Course Closure &pound;150, Hole-in-One &pound;150, Equipment Hire &pound;100, plus trace & delivery assistance for lost sets of clubs. Excludes hired or loaned equipment and items over 3 years old, <a href=javascript:void(window.open(\'Staysure_Policy_Wording.pdf\',\'\',\'\'))>click here for more information<\/a>";//instant qouete,long qote
var WEDING_MES	="NOTE: Requires Personal Baggage upgrade also. Cover includes: Wedding Attire &pound;1,500, Wedding Gifts &pound;1,000, Wedding Photographs & Videos &pound;750, Wedding Rings &pound;250 (per ring). <a href=javascript:void(window.open(\'Staysure_Policy_Wording.pdf\',\'\',\'\'))>Click here for more information<\/a> and exclusions that apply.')";//instantqoote,long qoute
var SING_MES	="NOTE: Requires Personal Baggage upgrade also. This option provides cover beyond the maximum &pound;300 ceiling allowed in the Personal Baggage section for one item of personal belongings. <a href=javascript:void(window.open(\'singleItem.html\',\'\',\'\'))>Click here for more information<\/a> and exclusions that apply.";//instantquote,long quote
var SING_MES2	="You have requested to insure an item (or set) valued above &pound;300. If you have not already done so we suggest you read the  <a href=javascript:void(window.open(\'singleItem.html\',\'\',\'\'))>Increased Single Item Limit summary<\/a> before proceeding, then enter a description here that accurately identifies your item or set";//instquote,longqoute
var FREESPIRIT	="Please select the relevant condition you need to declare from the drop-down list below. There is no additional premium payable for conditions accepted by us and the latter shall be noted on your validation certificate under <b>Medical Reference</b>.<br>If your condition is not listed or if any person travelling has more than one condition to declare please <a href=javascript:void(window.open(\'freespirit.html\',\'\',\',,\'))>CLICK HERE<\/a>";//pd
var PREPOL		="If you do not have your current policy number to hand please enter 1234567 to continue. We will still honour the loyalty discount. Just contact us once within 7 days quoting both your new policy number and the previous one.";//
var MEDICAL		="Please select the relevant condition you need to declare from the drop-down list below. There is no additional premium payable for conditions accepted by us and the latter shall be noted on your validation certificate under <b>Medical Reference</b>.<br>If your condition is not listed or if any person travelling has more than one condition to declare please <a href=javascript:void(window.open(\'freespirit.html\',\'\',\',,\'))>CLICK HERE<\/a>"
var VERIF       ="The verification number is located on the signature strip on the back of your card. It is the last three digits.";
var BK          ="In recognition of increased safety we offer a reduction for joint travel.";
var BKGOLF_MES 	="Cover includes: Golf Equipment &pound;1,000, Course Closure &pound;150, Hole-in-One &pound;150, Equipment Hire &pound;100, plus trace & delivery assistance for lost sets of clubs. Excludes hired or loaned equipment and items over 3 years old, <a href=javascript:void(window.open(\'Backpacker_Policy_Wording.pdf\',\'\',\'\'))>click here for more information<\/a>";//instant qouete,long qote
var BKWEDING_MES="NOTE: Requires Personal Baggage upgrade also. Cover includes: Wedding Attire &pound;1,500, Wedding Gifts &pound;1,000, Wedding Photographs & Videos &pound;750, Wedding Rings &pound;250 (per ring). <a href=javascript:void(window.open(\'Backpacker_Policy_Wording.pdf\',\'\',\'\'))>Click here for more information<\/a> and exclusions that apply.')";//instantqoote,long qoute
var TRANS_FEE = "Credit cards will incur a 2% fee to cover the costs we get charged. Debit cards have NO fee and are free to use with Staysure.";// Transaction fee on payment page

	var text= "&nbsp;";
	
	if (message_name == 'wedding') 			text = WEDING_MES;
	else if (message_name == 'amt') 		text = AMT_MES;
	else if (message_name == 'golf') 		text = GOLF_MES;  
	else if (message_name == 'sin1') 		text = SING_MES;
	else if (message_name == 'sin2') 		text = SING_MES2;
	else if (message_name == 'freespirit') 	text = FREESPIRIT;
	else if (message_name == 'prePol') 		text = PREPOL;
	else if (message_name == 'medi') 		text = MEDICAL;
	else if (message_name == 'veri') 		text = VERIF;
	else if (message_name == 'bk') 			text = BK;
	else if (message_name == 'bkgolf')		text = BKGOLF_MES;
	else if (message_name == 'bkwed') 		text = BKWEDING_MES;
	else if (message_name == 'trans') 		text = TRANS_FEE;
	
	showHelp(objAnchor, theEvent, text) ;

}