
var agt = navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var iTV  = appVer.indexOf('pace');
var iTV2  = agt.indexOf('pace');
if (iTV !=-1 && iTV2 !=-1) {
	 window.top.location.href="/itv/index.html";
}

function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.v = parseInt(navigator.appVersion)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0)
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0)
	this.win = (navigator.appVersion.indexOf('Windows')>= 0)
	this.mac = (navigator.appVersion.indexOf('Windows')< 0)
	if (this.ie5) this.v = 5
	this.ie4mac=this.ie4 && navigator.userAgent.indexOf("Mac")>-1
	this.ie5mac=this.ie5 && navigator.userAgent.indexOf("Mac")>-1
	this.min = (this.ns||this.ie)
	this.dom = document.getElementById?1:0
	this.ie55=(navigator.userAgent.indexOf("MSIE 5.5")>-1 && this.dom)?1:0 
}

// automatically create the "is" object
is = new BrowserCheck()

function openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function isEmailAddress (string) {
  var addressPattern = 
    /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  return addressPattern.test(string);
}
function checkEmail (field) {
  if (!isEmailAddress(field.value)) {
    alert('Please enter a correct email address!');
    field.focus();
    field.select();
  }
}

function errorPop(alert_message)
{
var error_features = "status=no,directories=no,toolbar=no,location=no,resizable=no,scrollbars=yes,width=500,height=150";
      confirm_win = window.open("", "error", error_features);
confirm_win.document.open("text/html","replace");
confirm_win.document.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'><HTML><HEAD><TITLE>Travel-Insurance-Online.com</TITLE><link rel='stylesheet' type='text/css' href='ti-online/script/site.css'></HEAD><BODY TEXT='#FFFFFF' BGCOLOR='#00099F' LINK='#CC0000' VLINK='#FF9900' LEFTMARGIN='0' TOPMARGIN='0' MARGINWIDTH='0' MARGINHEIGHT='0'><TABLE BORDER=0 WIDTH=450 CELLPADDING='0' CELLSPACING='0'><TD ALIGN=LEFT>");
        confirm_win.document.write(alert_message);
confirm_win.document.write("</TD></TR></TABLE><TABLE WIDTH='480' BORDER=0><TR><TD ALIGN=right VALIGN=middle><A HREF='javascript:window.close()'><img src='/images/buttons/close_window.gif' width='31' height='19' border='0'></A></TD></TR></TABLE></BODY></HTML>");
confirm_win.focus();
}

var today = new Date();

var selectedDay   = getDayOfMonth( today );
var selectedMonth = today.getMonth();
var selectedYear  = today.getFullYear();
var selectedto_dayay = getDayOfMonth( today );

var windowOptions = 'alwaysRaised=yes,resizable=no,width=150,height=151,top=100,left=300';
var windowOptionsE = 'alwaysRaised=yes,resizable=no,width=250,height=281,top=100,left=300';
var calWin;
var selectedElement;
var outbound;
var firstday =today;


var selswitch;

var from_monthI, to_monthI, from_yearI, to_yearI;


var monthLength = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

function getMonthLength( month, year )
{


    if (( month == 1 ) && (( year % 4 == 0 ) || ( year % 100 == 0 ) || ( year % 400 == 0 )))
    {
        return 29;
    }

    return monthLength[ month ];
}


function getDayOfMonth( date )
{
    var dateString = date.toUTCString();
    var day = dateString.substring( 5, 7 );

    return parseInt( day );
}

function twoDigits( num )
{
    var str = num.toString();
    var len = str.length;

    if ( len == 1 )
    {
        str =  "0" + str;
    }
    else if ( len == 4 )
    {
        str = str.substring( 2, 4 );
    }

    return str;
}

function onsubmitp()
{
	var from_day, from_month, from_year,to_day,to_month, to_year;
	
	var cd = document.layers ? document.mainBar.document.premCalc : document.all ? document.all.premCalc : document.getElementById('premCalc');
	
	from_day = cd.from_day.selectedIndex;
	from_month = cd.from_month.selectedIndex;
	from_year = cd.from_year.value;
	
	to_day = cd.to_day.selectedIndex;
	to_month = cd.to_month.selectedIndex;
	to_year = cd.to_year.value;
	
	//get the from Date from the page
	var fromDate = new Date(from_year, from_month, from_day+2);
	var toDate = new Date(to_year, to_month, to_day+2);
	
	var today = new Date();
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	
	var isokay = true;
	
	//return true;
	if ((fromDate < today) || (toDate < fromDate))
	{
		errorPop("<p class='medwhitebold'>Invalid dates</P><P class='medwhite'>Sorry - you have entered an invalid date. Please make sure that your departure date is either today or in the future, and your return date is the same or later than your departure date.</p>");
		
		return false
	}
	if ( ! cd.page )
	{
		if ( cd.policy.value == 'singletrip' )
		{
			if (fromDate.getTime() + ( 1000 * 60 * 60 * 24 * 93 ) <= toDate.getTime()) 
			{
				if ( confirm ("Sorry these policies can only  be issued for a maximum of 93 days.\nPlease purchase a Longstay Policy.\nDo you wish to proceed to a longstay quote?") )
				{
					window.location="/ti-online/insure/shop/longstay.html";
				}
				return false;
			}	
		}
		if ( cd.policy.value == 'longstay' )
		{
			if (fromDate.getTime() + ( 1000 * 60 * 60 * 24 * 93 ) >= toDate.getTime()) 
			{
				if ( confirm ("Sorry these policies are issued for a minimum of 93 days.\nPlease purchase a SingleTrip Policy.\nDo you wish to proceed to a single trip quote?") )
				{
					window.location="/ti-online/insure/shop/singletrip.html";
				}
				return false;
			}	
		}
	}
	
}

function set_from_ddls()
{
	var d=0, m=0, y=0, today, tomorrow, t;
	today = new Date();

	// don't do anything if dropdown has been changed,
	// ie it is different from the default.
	var srch = document.layers ? document.mainBar.document.premCalc : document.all ? document.all.premCalc : document.getElementById('premCalc');
	var d = srch.from_day.selectedIndex
	+ srch.from_month.selectedIndex
	+ srch.from_year.selectedIndex;
	if ( d != 0 ) { return; }

	// get tomorrow
	//t = today.getTime() + 1000 * 60 * 60 * 24;
	//tomorrow = new Date(t);
	//UPDATED 08/1/2003 - Uses today's date rather than tomorrows
	d = today.getDate();
	m = today.getMonth()+1;
	y = today.getFullYear();

	srch.from_day.selectedIndex = d-1;
	srch.from_month.selectedIndex = m-1;
	srch.from_year.selectedIndex = y-2005;

}


function set_both_ddls()
{
	set_from_ddls();

	var d=0, m=0, y=0, today, nextweek, t;
	today = new Date();

	// don't do anything if dropdown has been changed,
	// ie it is different from the default.
	var srch = document.layers ? document.mainBar.document.premCalc : document.all ? document.all.premCalc : document.getElementById('premCalc');
	var d = srch.to_day.selectedIndex
	+ srch.to_month.selectedIndex
	+ srch.to_year.selectedIndex;
	if ( d != 0 ) { return; }

	// get tomorrow
	//UPDATED 08/1/2003 - now uses 7 days in the future rather than 8
	if ( srch.policy.value == "longstay" )
	{
		t = today.getTime() + 1000 * 60 * 60 * 24 * 93;
	}
	else
	{
		t = today.getTime() + 1000 * 60 * 60 * 24 * 7;
	}
	tomorrow = new Date(t);

	d = tomorrow.getDate();
	m = tomorrow.getMonth()+1;
	y = tomorrow.getFullYear();
	srch.to_day.selectedIndex = d-1;
	srch.to_month.selectedIndex = m-1;
	srch.to_year.selectedIndex = y-2005;
}
function css(id,left,top,width,height,vis,z,other) {
	if (id=="START") return '<STYLE TYPE="text/css">\n'
	else if (id=="END") return '</STYLE>'
	var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;'
	if (arguments.length>=4 && width!=null) str += ' width:'+width+'px;'
	if (arguments.length>=5 && height!=null) {
		str += ' height:'+height+'px;'
		if (arguments.length <= 9 || other.indexOf('clip')==-1) str += ' clip:rect(0px '+width+'px '+height+'px 0px);'
	}
	if (arguments.length>=6 && vis!=null) str += ' visibility:'+vis+';'
	if (arguments.length>=7 && z!=null) str += ' z-index:'+z+';'
	if (arguments.length==8 && other!=null) str += ' overflow:'+other
	str += '}\n'
	return str
}

function writeCSS(str,showAlert) {
	str = css('START')+str+css('END')
	document.write(str)
	if (showAlert) alert(str)
}

function findWH() {
	winW = (is.ns)? window.innerWidth : document.body.offsetWidth-20 ;
	winH = (is.ns)? window.innerHeight : document.body.offsetHeight-4 ;
}

function makeLiquid() {
	if ((is.ns && (winW!=window.innerWidth || winH!=window.innerHeight)) || is.ie)
	history.go(0)
}

function preloadImages() { 
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function swapImgRestore() { 
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function swapImage() { 
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function reloadPage(init) {  
  if (init==true) with (navigator) {if (is.ns4) {
    document.pgW=innerWidth; document.pgH=innerHeight; onresize=reloadPage; }}
  else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}
reloadPage(true);

function makeObj(obj,nest){
    nest=(!nest) ? '':'document.'+nest+'.'
   	this.css=is.dom? document.getElementById(obj).style:is.ie4?document.all[obj].style:is.ns4?eval(nest+"document.layers." +obj):0;					
	this.evnt=is.dom? document.getElementById(obj):is.ie4?document.all[obj]:is.ns4?eval(nest+"document.layers." +obj):0;		
	this.height=is.ns4?this.css.document.height||this.css.clip.bottom:this.evnt.offsetHeight||this.css.pixelHeight
	this.width=is.ns4?this.css.document.width||this.css.clip.right:this.evnt.offsetWidth||this.css.pixelWidth
	this.moveIt=b_moveIt; this.x; this.y; this.moveBy=b_moveBy;
	return this
}
function b_moveIt(x,y){this.x=x;this.y=y; this.css.left=this.x; this.css.top=this.y}
function b_moveBy(x,y){this.moveIt(this.x+x,this.y+y)}

function showhide(id,visStr) {
el = document.all ? document.all[id] : is.dom ? document.getElementById(id) : document.layers[id];
els = is.dom ? el.style : el;
if (is.dom){
 if (visStr == 'show')
    els.visibility = "visible";
  else els.visibility = 'hidden' ;
  }
else if (is.ns4){
 if (visStr == 'show')
    els.visibility = "show";
  else els.visibility = 'hide' ;
  }
}

function showhideFrame(id,visStr) {
el =  document.id;
els = is.dom ? el.style : el;
if (is.dom){
 if (visStr == 'show')
    els.visibility = "visible";
  else els.visibility = "hidden";
  }
else if (is.ns4){
 if (visStr == 'show')
    els.visibility = "show";
  else els.visibility = "hide";
  }
}

function rewriteLayer (idOrPath, html) {
  if (document.layers) {
    var l = idOrPath.indexOf('.') != -1 ? eval(idOrPath) : document[idOrPath];
    if (!l.ol) {
      var ol = l.ol = new Layer (l.clip.width, l);
      ol.clip.width = l.clip.width;
      ol.clip.height = l.clip.height;
      ol.bgColor = l.bgColor;
      l.visibility = 'hide';
      ol.visibility = 'show';
    }
    var ol = l.ol;
    ol.document.open();
    ol.document.write(html);
    ol.document.close();
  }
  else if (document.all || document.getElementById) {
    var p = idOrPath.indexOf('.');
    var id = p != -1 ? 
              idOrPath.substring(idOrPath.lastIndexOf('.') + 1) 
              : idOrPath;
    if (document.all)
      document.all[id].innerHTML = html;
    else {
      var l = document.getElementById(id);
      var r = document.createRange();
      r.setStartAfter(l);
      var docFrag = r.createContextualFragment(html);
      while (l.hasChildNodes())
        l.removeChild(l.firstChild);
      l.appendChild(docFrag);
    }
  }
}

function showHideLayers() { 
  var i,p,v,obj,args=showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}


function switchAgreeButton () {
	// Check that the user has checked the right tick boxes
	// if they have enable the button, otherwise disable it
	
	var agreeMedical = document.getElementById( 'agreeMedical' ).checked;
	var agreeSummary = document.getElementById( 'agreeSummary' ).checked;
	var agreeAdequate = document.getElementById( 'agreeAdequate' ).checked;

	var agreeButton = document.getElementById( 'agreeButton' );
	
	if (agreeMedical && agreeSummary && agreeAdequate) {
		agreeButton.disabled = false;
	}
	else {
		agreeButton.disabled = true;
	}
}


function printSummary(pdf) {
	alert( "Printing the Policy Summary\n\nThe Policy Summary is currently being loaded. When it has loaded you can print the policy summary by clicking the print button or choosing 'Print' from the 'File' menu." );
	window.open( pdf );
}


function popupKeyFacts()
{
	window.open( "/keyfacts.html", 'popupKeyFacts', 'width=350,height=400,left=20,top=20,,scrollbars=yes' );
}

function quickjump(url){
	
	 parent.location=url;
	 
	 return true
}


var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield,weeksadvanced){
var today=new Date()
var ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
// get the date advanced from today
var advanced = new Date(today.getTime() + (ONE_WEEK * weeksadvanced)); 


var dayfield=document.getElementById(dayfield)
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var i=0; i<31; i++)
dayfield.options[i]=new Option(i+1, i+1)
dayfield.options[advanced.getDate()]=new Option(advanced.getDate()+1, advanced.getDate()+1, true, true) //select today's day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], m) // not using monthtext for time being!
monthfield.options[advanced.getMonth()]=new Option(monthtext[advanced.getMonth()], advanced.getMonth(), true, true) //select today's month
var thisyear=advanced.getFullYear()
for (var y=0; y<3; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}



