var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;

function setSrc(id, url) {
  if (NS4) {eval("document." + id + ".document." + id).src = url}
  else if (IE4) {eval("document.all." + id).src = url}
       else if (IE5 || N6) {document.getElementById(id).src = url};
}

function showHide(object) {

    if (document.getElementById && document.getElementById(object) != null){
		if (document.getElementById(object).style.display == 'none'){
	        document.getElementById(object).style.display ='';
	    }else {
	        document.getElementById(object).style.display ='none';
	    }
	}else if (document.layers && document.layers[object] != null){
        if (document.layers[object].visibility == 'hidden'){
			document.layers[object].visibility = 'visible'
		}else {
			document.layers[object].visibility = 'hidden'
		}
	}else if (document.all){
		if (document.all[object].style.display == 'none'){
			document.all[object].style.display = '';
		}else {
			document.all[object].style.display = 'none';
		}
	}
}

function showHideByAction(object,Action) {
    if (document.getElementById && document.getElementById(object) != null){
	        document.getElementById(object).style.display = (Action == 'show') ? '' : 'none';
	}else if (document.layers && document.layers[object] != null){
			document.layers[object].visibility = (Action == 'show') ? 'visible' : 'hidden';
	}else if (document.all){
			document.all[object].style.display = (Action == 'show') ? '' : 'none';
	}
}

function enableDisableByAction(object,Action) {
	
    if (document.getElementById && document.getElementById(object) != null){
        document.getElementById(object).disabled = (Action == 'show') ? false : true;
	}
	else if (document.layers && document.layers[object] != null){
			document.layers[object].disabled = (Action == 'show') ? false : true;
	}
	else if (document.all){
			document.all[object].disabled = (Action == 'show') ? false : true;
	}
}

function showHideExt(object,img) {

    if (document.getElementById && document.getElementById(object) != null){
		if (document.getElementById(object).style.display == 'none'){
	        document.getElementById(object).style.display ='';
	        setSrc(img,"img/interior/prodselect_exp.gif");
	    }else {
	        document.getElementById(object).style.display ='none';
	        setSrc(img,"img/interior/prodselect_min.gif");
	    }
	}else if (document.layers && document.layers[object] != null){
        if (document.layers[object].visibility == 'hidden'){
			document.layers[object].visibility = 'visible'
	        setSrc(img,"img/interior/prodselect_exp.gif");
		}else {
			document.layers[object].visibility = 'hidden'
			setSrc(img,"img/interior/prodselect_min.gif");
		}
	}else if (document.all){
		if (document.all[object].style.display == 'none'){
			document.all[object].style.display = '';
	        setSrc(img,"img/interior/prodselect_exp.gif");
		}else {
			document.all[object].style.display = 'none';
			setSrc(img,"img/interior/prodselect_min.gif");
		}
	}
}

function checkleapyear(datea)
{
	datea = parseInt(datea);

	if(datea%4 == 0)
	{
		if(datea%100 != 0)
		{
			return true;
		}
		else
		{
			if(datea%400 == 0)
				return true;
			else
				return false;
		}
	}
return false;
}

function getYearDays(year)
{
	if (checkleapyear(year) == true)
		return 365;
	else
		return 366;
}

function GetDateBeforeDays(currdate,days)
{
	var oneday = 60 * 1000 * 60 * 24 ;
	var daysbefore = parseInt(days);
	var currdatems = currdate.getTime();

	var addms = oneday * daysbefore;

	var daysbeforedate = new Date();
	daysbeforedate.setTime(currdatems - addms);
	return daysbeforedate;
}

function GetDateAfterDays(currdate,days)
{	
	var oneday = 60 * 1000 * 60 * 24 ;
	var daysafter = parseInt(days);
	var currdatems = currdate.getTime();

	var addms = oneday * daysafter;

	var daysafterdate = new Date();
	daysafterdate.setTime(currdatems + addms);
	return daysafterdate;
}

function DoExpand(divt,divd)
{
	showHide(divd)
	showHide(divt)
}

function DoExpandExt(divt,divd,img)
{
	showHideExt(divt,img)
	showHide(divd)
}

function setDates(Type)
{
	var iDay   = eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'StartDateDay').value;
	var iMonth = eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'StartDateMonth').value;
	var iYear  = eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'StartDateYear').value;
	var pType  = parseInt(eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'PolicyType').value);
	
	switch (pType)
	{
		case 1:
			var cDate = new Date(iMonth.toString() + '/' + iDay.toString() + '/' + iYear.toString());
			var nDate = GetDateAfterDays(cDate,1);
			
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateDay').value   = nDate.getDate()
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateMonth').value = nDate.getMonth() + 1
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateYear').value  = nDate.getFullYear()
			break;
		case 3:
			var cDate = new Date(iYear, iMonth-1, iDay);
			var nDate = GetDateAfterDays(cDate,365);

			iDay   = nDate.getDate();
			iMonth = nDate.getMonth() + 1;
			iYear  = nDate.getFullYear();

			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateDay').value   = --iDay;
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateMonth').value = iMonth;
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateYear').value  = ++iYear;
			break;
		case 5:
			setDatesEVA(Type);
	}
}

function setDatesHome()
{
	var startdate = document.frmPageForm.cntrlInsSearch_txtStartDate.value;
	if (startdate.indexOf('/') > 0)
	{
		
		var iDay   = startdate.split('/')[0];
		var iMonth = startdate.split('/')[1];
		var iYear  = startdate.split('/')[2];
		var pType  = parseInt(document.frmPageForm.cntrlInsSearch_ddlPolicyType.value);

		if (!(isNaN(iDay) || isNaN(iMonth) || isNaN(iYear)))
		{
			switch (pType)
			{
				case 1:
					var cDate = new Date(iYear, iMonth-1, iDay);

					var nDate = GetDateAfterDays(cDate,1);
					iDay   = nDate.getDate();
					iMonth = nDate.getMonth() + 1;
					iYear  = nDate.getFullYear();

					iDay   = (iDay < 10)   ? '0' + iDay  : iDay;
					iMonth = (iMonth < 10) ? '0' + iMonth: iMonth;
					
					var newDate = iDay + '/' + iMonth + '/' + iYear;
					document.frmPageForm.cntrlInsSearch_txtEndDate.value = newDate;
					break;

				case 3:
					var cDate = new Date(iYear, iMonth-1, iDay);
					var nDate = GetDateAfterDays(cDate,365);

					iDay   = nDate.getDate();
					iMonth = nDate.getMonth() + 1;
					iYear  = nDate.getFullYear();

					iDay   = (iDay   < 10) ? '0' + iDay  : iDay;
					iMonth = (iMonth < 10) ? '0' + iMonth: iMonth;
					var newDate = iDay + '/' + iMonth + '/' + iYear;
					document.frmPageForm.cntrlInsSearch_txtEndDate.value = newDate;
					
					break;

				case 5:
					setDatesEVAHome();
			}
		}else{
			alert('Please enter correct date');
		}
	}else{
		alert('Please enter correct date');
	}
}

function setDatesEVA(Type)
{
	
	var iDay   = parseInt(eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'StartDateDay').value);
	var iMonth = parseInt(eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'StartDateMonth').value);
	var iYear  = parseInt(eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'StartDateYear').value);

	var cDate = new Date(iMonth.toString() + '/' + iDay.toString() + '/' + iYear.toString());
	var nDate = GetDateAfterDays(cDate,1);

	eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateDay').value   = nDate.getDate()
	eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateMonth').value = nDate.getMonth() + 1
	eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'EndDateYear').value  = nDate.getFullYear()
}

function setDatesEVAHome()
{
	var startdate = document.frmPageForm.cntrlInsSearch_txtStartDate.value;
	
	var iDay   = startdate.split('/')[0];
	var iMonth = startdate.split('/')[1];
	var iYear  = startdate.split('/')[2];

	var cDate = new Date(iMonth.toString() + '/' + iDay.toString() + '/' + iYear.toString());
	var nDate = GetDateAfterDays(cDate,1);

	iDay = nDate.getDate();
	iMonth = nDate.getMonth() + 1;
	iYear = nDate.getFullYear();

	iDay = (iDay < 10 ? '0' + iDay: iDay);
	iMonth = (iMonth < 10 ? '0' + iMonth: iMonth);

	var newDate = iDay + '/' + iMonth + '/' + iYear;
	document.frmPageForm.cntrlInsSearch_txtEndDate.value = newDate;
	
}

function EnableDisableControls(ddlCoverFor,Type)
{
	DisableControls(Type);

	var PolicyType = eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'PolicyType').value;

	switch (ddlCoverFor)
	{
		case '1':
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Infants').value = 0;
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Children').value = 0;
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Adults').value = 1;
			break;
		case '2'://Couple
			switch (PolicyType)
			{
				case '1':
					enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Infants','show');			
					eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Children').value = 0;
					eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Adults').value = 2;
					break;
				default :
					eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Infants').value = 0;
					eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Children').value = 0;
					eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Adults').value = 2;
					enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Infants','hide');			
					break;
			}
			break;
		case '3':
			enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Infants','show');			
			enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Children','show');			
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Adults').value = 1;
			break;
		case '4':
			enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Infants','show');			
			enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Children','show');	
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Adults').value = 2;
			break;	
		case '5':
			enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Adults','show');
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Infants').value = 0;
			eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'Children').value = 0;
			break;
	}
	
	switch (PolicyType)
	{
		case '5':
		eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'CoverFor').value = 1;		
		break;
	}

}

function ShowHideSelection(ddlCoverFor,Type)
{
	EnableDisableControls(ddlCoverFor,Type);

	if (Type == ''){

		switch (ddlCoverFor)
		{
			case '6':
				for (var i = 1 ; i < 5; i++)
					showHideByAction('insSearch' + i,'hide');
				for (var i = 1 ; i < 4; i++)
					showHideByAction('evaSearch' + i,'show');
				break;
			default:
				for (var i = 1 ; i < 5; i++)
					showHideByAction('insSearch' + i,'show');
					
				for (var i = 1 ; i < 4; i++)
					showHideByAction('evaSearch' + i,'hide');
				break;				
		}
	}
}

function ShowHideSelectionHome(ddlPolicyType,Type)
{
	EnableDisableControls(document.frmPageForm.cntrlInsSearch_ddlCoverFor.value,Type);

	switch (ddlPolicyType)
	{
		case '5':
			for (var i = 1 ; i < 5; i++)
				showHideByAction('insSearch' + i,'hide');
			for (var i = 1 ; i < 4; i++)
				showHideByAction('evaSearch' + i,'show');
			break;
		default:
			for (var i = 1 ; i < 5; i++)
				showHideByAction('insSearch' + i,'show');
				
			for (var i = 1 ; i < 4; i++)
				showHideByAction('evaSearch' + i,'hide');
			break;				
	}
}

function DisableControls(Type)
{
	enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Infants','hide');			
	enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Children','hide');	
	enableDisableByAction('cntrlInsSearch_ddl' + Type + 'Adults','hide');			
}

function GetSelectedOption()
{
	for (i=1;i<=6;i++)
	{
		if (eval('document.frmPageForm.cntrlInsSearch_rdoCoverFor' + i).checked == true)
		{
			return eval('document.frmPageForm.cntrlInsSearch_rdoCoverFor' + i).value;
			break;
		}
	}
}

function ResetSelection(Type)
{
	if (Type == '')
		ShowHideSelectionHome(document.frmPageForm.cntrlInsSearch_ddlPolicyType.value,Type);
	else
		ShowHideSelection(eval('document.frmPageForm.cntrlInsSearch_ddl' + Type + 'CoverFor').value,Type);
}

function go_insurance(x)
{
	if(!x){
	x = document.frmPageForm
	}
	
	var cf = x.coverFor.value;
	var pt = x.policyType.value;
	var ar = x.area.value;
	window.open("http://www.cheap4travelcover.com/?CoverFor=" + cf + "&PolicyType=" + pt + "&Area=" + ar + "&r=171");
}