/************************************************************************************************************
(C) www.dhtmlgoodies.com, October 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Updated:	
	March, 11th, 2006 - Fixed positioning of tooltip when displayed near the right edge of the browser.
	April, 6th 2006, Using iframe in IE in order to make the tooltip cover select boxes.
	
Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/	
var dhtmlgoodies_tooltip = false;
var dhtmlgoodies_tooltipShadow = false;
var dhtmlgoodies_shadowSize = 4;
var dhtmlgoodies_tooltipMaxWidth = 300;
var dhtmlgoodies_tooltipMinWidth = 100;
var dhtmlgoodies_iframe = false;
var tooltip_is_msie = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all)?true:false;
function showTooltip(e,tooltipTxt)
{
	var bodyWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth) - 20;

	if(!dhtmlgoodies_tooltip){
		dhtmlgoodies_tooltip = document.createElement('DIV');
		dhtmlgoodies_tooltip.id = 'dhtmlgoodies_tooltip';
		dhtmlgoodies_tooltipShadow = document.createElement('DIV');
		dhtmlgoodies_tooltipShadow.id = 'dhtmlgoodies_tooltipShadow';
		
		document.body.appendChild(dhtmlgoodies_tooltip);
		document.body.appendChild(dhtmlgoodies_tooltipShadow);	
		
		if(tooltip_is_msie){
			dhtmlgoodies_iframe = document.createElement('IFRAME');
			dhtmlgoodies_iframe.frameborder='5';
			dhtmlgoodies_iframe.style.backgroundColor='#FFFFFF';
			dhtmlgoodies_iframe.src = '#'; 	
			dhtmlgoodies_iframe.style.zIndex = 100;
			dhtmlgoodies_iframe.style.position = 'absolute';
			document.body.appendChild(dhtmlgoodies_iframe);
		}
		
	}
	
	dhtmlgoodies_tooltip.style.display='block';
	dhtmlgoodies_tooltipShadow.style.display='block';
	if(tooltip_is_msie)dhtmlgoodies_iframe.style.display='block';
	
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	var leftPos = e.clientX + 10;
	
	dhtmlgoodies_tooltip.style.width = null;	// Reset style width if it's set 
	dhtmlgoodies_tooltip.innerHTML = tooltipTxt;
	dhtmlgoodies_tooltip.style.left = leftPos + 'px';
	dhtmlgoodies_tooltip.style.top = e.clientY + 10 + st + 'px';

	
	dhtmlgoodies_tooltipShadow.style.left =  leftPos + dhtmlgoodies_shadowSize + 'px';
	dhtmlgoodies_tooltipShadow.style.top = e.clientY + 10 + st + dhtmlgoodies_shadowSize + 'px';
	
	if(dhtmlgoodies_tooltip.offsetWidth>dhtmlgoodies_tooltipMaxWidth){	/* Exceeding max width of tooltip ? */
		dhtmlgoodies_tooltip.style.width = dhtmlgoodies_tooltipMaxWidth + 'px';
	}
	
	var tooltipWidth = dhtmlgoodies_tooltip.offsetWidth;		
	if(tooltipWidth<dhtmlgoodies_tooltipMinWidth)tooltipWidth = dhtmlgoodies_tooltipMinWidth;
	
	
	dhtmlgoodies_tooltip.style.width = tooltipWidth + 'px';
	dhtmlgoodies_tooltipShadow.style.width = dhtmlgoodies_tooltip.offsetWidth + 'px';
	dhtmlgoodies_tooltipShadow.style.height = dhtmlgoodies_tooltip.offsetHeight + 'px';		
	
	if((leftPos + tooltipWidth)>bodyWidth){
		dhtmlgoodies_tooltip.style.left = (dhtmlgoodies_tooltipShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth)) + 'px';
		dhtmlgoodies_tooltipShadow.style.left = (dhtmlgoodies_tooltipShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth) + dhtmlgoodies_shadowSize) + 'px';
	}
	
	if(tooltip_is_msie){
		dhtmlgoodies_iframe.style.left = dhtmlgoodies_tooltip.style.left;
		dhtmlgoodies_iframe.style.top = dhtmlgoodies_tooltip.style.top;
		dhtmlgoodies_iframe.style.width = dhtmlgoodies_tooltip.offsetWidth + 'px';
		dhtmlgoodies_iframe.style.height = dhtmlgoodies_tooltip.offsetHeight + 'px';
	
	}
			
}

function hideTooltip()
{
	dhtmlgoodies_tooltip.style.display='none';
	dhtmlgoodies_tooltipShadow.style.display='none';		
	if(tooltip_is_msie)dhtmlgoodies_iframe.style.display='none';		
}


function emailck(field, alert_str)
{
	var str = field.value;
	//alert(str);
	if (window.RegExp) {
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		var reg1 = new RegExp(reg1str);
		var reg2 = new RegExp(reg2str);
		if (!reg1.test(str) && reg2.test(str)) {
			return true;
		}
		if(alert_str.length>0)
			alert(alert_str);
		field.select();
		return false;
	} else {
		if(str.indexOf("@") >= 0)
		{
			return true;
		}
		if(alert_str.length>0)
			alert(alert_str);
		field.select();
		return false;
	}
}

function isDate(dtStr, mes1, mes2, mes3)
{
	var myRegex = new RegExp("^([0-2][0-9]|[3][0-1])\/([0][0-9]|[1][0-2])\/[0-9]{4}$");
	//var myRegex2 = new RegExp("^([0-2][0-9]|[3][0-1])\.([0][0-9]|[1][0-2])\.[0-9]{4}$");
	// || (dtStr.match(myRegex2))
	if (dtStr.match(myRegex))
		{//valid date format, check for valid date dd/mm/yyyy
		var theDay = Math.round(dtStr.substr(0,2));
		var theMonth = Math.round(dtStr.substr(3,2));
		var theYear = Math.round(dtStr.substr(6,4));
		
		if (theYear< 2005) {
			alert (mes1);
			return false;
			}
		else if ((theYear%4 == 0) && (theDay > 29) && (theMonth == 2)) {
			alert (mes2);
			return false;
			}
		else if ((theYear%4 != 0) && (theDay > 28) && (theMonth == 2)) {
			alert (mes2);
			return false;
			}
		else if ((theDay > 30) && ((theMonth == 0) || (theMonth == 4) || (theMonth == 6) || (theMonth == 9) || (theMonth == 11) )) {
			alert (mes2);
			return false;
			}
		else {
			return true;
			}
		}
	else
		{
		alert (mes3);
		return false;
		}
	

}	

function dateisNotPossible(day,month,year, mes) {


	var datePoss = new Date().valueOf();
	var dateReal = new Date();
	dateReal.setMonth(Number(month) - 1);
	dateReal.setYear(parseInt(year));
	dateReal.setDate(Number(day));

	if (dateReal.valueOf() < datePoss) {
		alert(mes+day+"-"+month+"-"+year);
		return false;
	}
	return true;
}

function compare_dates(day1,month1,year1,day2,month2,year2) 
{
	var dateReal_1 = new Date();
	var dateReal_2 = new Date();

	dateReal_1.setMonth(month1 - 1);
	dateReal_1.setYear(parseInt(year1));
	dateReal_1.setDate(parseInt(day1));

	
	dateReal_2.setMonth(month2 - 1);
	dateReal_2.setYear(parseInt(year2));
	dateReal_2.setDate(parseInt(day2));

	if (dateReal_2.valueOf() < dateReal_1.valueOf())
	{
		return -1;
	}
	else if (dateReal_2.valueOf() == dateReal_1.valueOf())
	{
		return 0;
	}
	else
	{
		return 1;
	}
	return true;
}
			
			function isDate2(dtStr){
				var myRegex = new RegExp("^([0-2][0-9]|[3][0-1])\/([0][0-9]|[1][0-2])\/[0-9]{4}$");
				
				if ( (dtStr.match(myRegex)) )
					{//valid date format, check for valid date dd/mm/yyyy
					var theDay = Math.round(dtStr.substr(0,2));
					var theMonth = Math.round(dtStr.substr(3,2));
					var theYear = Math.round(dtStr.substr(6,4));
					
					if (theYear< 2005) {
						alert ("The year have to be greater then 2004.");
						return false;
						}
					else if ((theYear%4 == 0) && (theDay > 29) && (theMonth == 2)) {
						alert ("Not a valid date (dd/mm/yyyy).");
						return false;
						}
					else if ((theYear%4 != 0) && (theDay > 28) && (theMonth == 2)) {
						alert ("Not a valid date (dd/mm/yyyy).");
						return false;
						}
					else if ((theDay > 30) && ((theMonth == 0) || (theMonth == 4) || (theMonth == 6) || (theMonth == 9) || (theMonth == 11) )) {
						alert ("Not a valid date (dd/mm/yyyy).");
						return false;
						}
					else {
						return true;
						}
					}
				else
					{
					alert ("Date not in correct dd/mm/yyyy format of the date specified does not exist.");
					return false;
					}
				
			
			}	
			
function checkmyDates(day1,day2,month1,month2,year1,year2, mes1, mes2) {
			
		if (parseFloat(year1) > parseFloat(year2)) {
			alert(mes1);
			return false;
		}
		if (parseFloat(year1) == parseFloat(year2)) {
			if (parseFloat(month1) > parseFloat(month2)) {
				alert(mes1);
 				return false;
			}
			if (parseFloat(month1) == parseFloat(month2)) {
				if (parseFloat(day1) > parseFloat(day2)) {
					alert(mes1);
					return false;
				}
				if (parseFloat(day1) == parseFloat(day2)) {
					alert(mes2);
					return false;
				}
			}
		}
	return true;
	}		
	
	function mindatePossible(day,month,year) {
		var datePoss = new Date().valueOf() + 86400000;
		var dateReal = new Date();
		dateReal.setMonth(month - 1);
		dateReal.setYear(parseInt(year));
		dateReal.setDate(parseInt(day));
		//if (dateReal.valueOf() < datePoss) { alert("Earliest possible date of service is 1 day from now. You specify the date: "+day+"-"+month+"-"+year+"  "+dateReal);	return false; }
		return true;
	}
	
	function myisDate(dtStr){
				var myRegex = new RegExp("^([0-2][0-9]|[3][0-1])\/([0][0-9]|[1][0-2])\/[0-9]{4}$");
				
				if ( (dtStr.match(myRegex)) )
					{//valid date format, check for valid date dd/mm/yyyy
					var theDay = Math.round(dtStr.substr(0,2));
					var theMonth = Math.round(dtStr.substr(3,2));
					var theYear = Math.round(dtStr.substr(6,4));
					
					if (theYear< 2005) {
						alert ("The year have to be greater then 2004.");
						return false;
						}
					else if ((theYear%4 == 0) && (theDay > 29) && (theMonth == 2)) {
						alert ("Not a valid date (dd/mm/yyyy).");
						return false;
						}
					else if ((theYear%4 != 0) && (theDay > 28) && (theMonth == 2)) {
						alert ("Not a valid date (dd/mm/yyyy).");
						return false;
						}
					else if ((theDay > 30) && ((theMonth == 0) || (theMonth == 4) || (theMonth == 6) || (theMonth == 9) || (theMonth == 11) )) {
						alert ("Not a valid date (dd/mm/yyyy).");
						return false;
						}
					else {
						return true;
						}
					}
				else
					{
					alert ("Date not in correct dd/mm/yyyy format of the date specified does not exist.");
					return false;
					}
				
			
			}
	
	function GetHighSeasonDays(start_date, end_date, nr_days) {
		//high season
				
		var myGetHighSeasonDays=0;		
		if (nr_days<0)	{nr_days=0;}		
		var current_date=start_date;
		
		var n = 0; var current_date2;
		for (n=1; n<=nr_days; n++) {			
			var mmA = current_date.split(/[\\\/]/); 			
			mm = mmA[0];			
			if  ((mm>=7) && (mm<=9)) {	myGetHighSeasonDays=myGetHighSeasonDays+1;	}
			current_date2 = dateAdd("d", 1, current_date);								
			var curr_date = current_date2.getDate(); var curr_month = current_date2.getMonth(); curr_month++; var curr_year = current_date2.getFullYear();						
			current_date  =  curr_month + "/" + curr_date + "/" + curr_year;			
		}
		if (myGetHighSeasonDays<0)	{myGetHighSeasonDays=0;}					
		return myGetHighSeasonDays;	
	}
	
	function GetMidSeasonDays(start_date, end_date, nr_days) {
		//high season
				
		var myGetMidSeasonDays=0;		
		if (nr_days<0)	{nr_days=0;}		
		var current_date=start_date;
		
		var n = 0; var current_date2;
		for (n=1; n<=nr_days; n++) {			
			var mmA = current_date.split(/[\\\/]/); 			
			mm = mmA[0];			
			if ( ((mm>=4) && (mm<=6)) || (mm==10) ) {	myGetMidSeasonDays=myGetMidSeasonDays+1;	}
			
			current_date2 = dateAdd("d", 1, current_date);								
			var curr_date = current_date2.getDate(); var curr_month = current_date2.getMonth(); curr_month++; var curr_year = current_date2.getFullYear();						
			current_date  =  curr_month + "/" + curr_date + "/" + curr_year;			
		}
		if (myGetMidSeasonDays<0)	{myGetMidSeasonDays=0;}					
		return myGetMidSeasonDays;	
	}
	
	function GetLowSeasonDays(start_date, end_date, nr_days) {
		//high season
				
		var myGetLowSeasonDays=0;		
		if (nr_days<0)	{nr_days=0;}		
		var current_date=start_date;
		
		var n = 0; var current_date2;
		for (n=1; n<=nr_days; n++) {			
			var mmA = current_date.split(/[\\\/]/); 			
			mm = mmA[0];					
			if ((mm>=11) || (mm<=3)) {	myGetLowSeasonDays=myGetLowSeasonDays+1; }
			
			current_date2 = dateAdd("d", 1, current_date);
			var curr_date = current_date2.getDate(); var curr_month = current_date2.getMonth(); curr_month++; var curr_year = current_date2.getFullYear();						
			current_date  =  curr_month + "/" + curr_date + "/" + curr_year;			
		}
		if (myGetLowSeasonDays<0)	{myGetLowSeasonDays=0;}					
		return myGetLowSeasonDays;	
	}				
		
		function booknow() {
			
				if (!emailck(document.onlinequote.myemail)) { return false; }
				if (!calculatemyprice()) { return false; }
				
				document.onlinequote.submit();
			
		}

function calculate_price_check(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format)
{
	f = document.onlinequote;

	
	//var error = $("company_id"); 
	if (f.location_from.value==0 || f.location_from.value=='airport' || f.location_from.value=='down_town') 
	{ 
		alert(err_pick_up);
		f.location_from.focus(); 
		return false;
	}

	if (f.location_to.value==0 || f.location_to.value=='airport' || f.location_to.value=='down_town') 
	{ 
		alert(err_drop_off);
		f.location_to.focus(); 
		return false;
	}

	if(isNaN(f.car_id.value) || (f.car_id.value == 0))
	{
		alert(err_car_id);
		f.car_id.focus(); 
		return false;
	}
	
	var date1String = f.pickup.value;
	var day1    = date1String.substr(0,2);
	var month1  = date1String.substr(3,2);
	var year1   = date1String.substr(6,4);

	var date2String = f.dropoff.value;
	var day2    = date2String.substr(0,2);
	var month2  = date2String.substr(3,2);
	var year2   = date2String.substr(6,4);


	if ((date1String==date_format) || (date1String=='')) 
	{
		alert(err_pick_up_date);
		f.pickup.value='';
		f.pickup.focus();
		return false;
	}

	if (isDate(date1String, mes_isdate_1, mes_isdate_2, mes_isdate_3)==false) { f.pickup.focus(); return false; }
	if (dateisNotPossible(day1,month1,year1,mes_date)==false) { f.pickup.focus(); return false; }	

	if ((date2String==date_format) || (date2String=='')) 
	{
		alert(err_drop_off_date);
		f.dropoff.value='';
		f.dropoff.focus();
		return false;
	}

	if (isDate(date2String, mes_isdate_1, mes_isdate_2, mes_isdate_3)==false) { f.dropoff.focus(); return false; }
	if (dateisNotPossible(day2,month2,year2,mes_date)==false) { f.dropoff.focus(); return false; }

	if(date1String == date2String)
	{
		alert(err_diff_date);
		f.dropoff.focus();
		return false;
	}


	if (!checkmyDates(day1,day2,month1,month2,year1,year2, mes1, mes2)) {return false;}

	//calculate_price();

	return true;
}

function onlinequote_check(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format, mes_valid_email)
{
	
	if(!calculate_price_check(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format))
	{
		return false;
	}

	return true;
}


function myonlinequote_check(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format, mes_valid_email)
{
	f = document.onlinequote;
	f.final_price.value = "yes";
	if(!calculate_price_check(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format, mes_valid_email))
	{
		return false;
	}

	if (!emailck(f.email, mes_valid_email)) 
	{
		return false;
	}

	calculate_price();
	return true;
}

function change_currency(rate)
{
	f = document.onlinequote;

	if(f.totalprice_euro.value > 0)
	{
		if(f.currency.value == 'gbp' && f.current_currency.value == 'euro')
		{
			f.total_price.value = Math.round((f.totalprice_euro.value / rate) * 100) / 100;
		}

		if(f.currency.value == 'euro' && f.current_currency.value == 'gbp')
		{
			f.total_price.value = f.totalprice_euro.value;
		}
		f.current_currency.value = f.currency.value;
	}
	else
	{
		if(f.currency.value == 'gbp')
		{
			f.currency.value = 'euro';
		}
	}
	return true;
}

//=================for the admin section====================================
function calculate_price_check_admin(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format)
{
	f = document.onlinequote;

	
	//var error = $("company_id"); 
	if (f.location_from.value==0 || f.location_from.value=='airport' || f.location_from.value=='down_town') 
	{ 
		alert(err_pick_up);
		f.location_from.focus(); 
		return false;
	}

	if (f.location_to.value==0 || f.location_to.value=='airport' || f.location_to.value=='down_town') 
	{ 
		alert(err_drop_off);
		f.location_to.focus(); 
		return false;
	}

	if(isNaN(f.car_id.value) || (f.car_id.value == 0))
	{
		alert(err_car_id);
		f.car_id.focus(); 
		return false;
	}
	
	var date1String = f.pickup.value;
	var day1    = date1String.substr(0,2);
	var month1  = date1String.substr(3,2);
	var year1   = date1String.substr(6,4);

	var date2String = f.dropoff.value;
	var day2    = date2String.substr(0,2);
	var month2  = date2String.substr(3,2);
	var year2   = date2String.substr(6,4);


	if ((date1String==date_format) || (date1String=='')) 
	{
		alert(err_pick_up_date);
		f.pickup.value='';
		f.pickup.focus();
		return false;
	}

	if (isDate(date1String, mes_isdate_1, mes_isdate_2, mes_isdate_3)==false) { f.pickup.focus(); return false; }
	//if (dateisNotPossible(day1,month1,year1,mes_date)==false) { f.pickup.focus(); return false; }	

	if ((date2String==date_format) || (date2String=='')) 
	{
		alert(err_drop_off_date);
		f.dropoff.value='';
		f.dropoff.focus();
		return false;
	}

	if (isDate(date2String, mes_isdate_1, mes_isdate_2, mes_isdate_3)==false) { f.dropoff.focus(); return false; }
	//if (dateisNotPossible(day2,month2,year2,mes_date)==false) { f.dropoff.focus(); return false; }

	if(date1String == date2String)
	{
		alert(err_diff_date);
		f.dropoff.focus();
		return false;
	}


	if (!checkmyDates(day1,day2,month1,month2,year1,year2, mes1, mes2)) {return false;}

	//calculate_price();

	return true;
}


function onlinequote_check_admin(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format)
{
	
	if(!calculate_price_check_admin(err_car_id, err_pick_up, err_drop_off, err_pick_up_date, err_drop_off_date, err_diff_date, mes1, mes2, mes_date, mes_isdate_1, mes_isdate_2, mes_isdate_3, date_format))
	{
		return false;
	}
	admin_calculate_price();

	return true;
}

function show_email_box()
{
	f = document.onlinequote_prices;
	if(f.send_email_quote.checked === true)
	{
		document.getElementById("div_email").style.display = "block";
	}
	else
	{
		document.getElementById("div_email").style.display = "none";
	}
	return true;

}

//======used for check form in caradmin/add_promotions.php============
function check_form()
{
	f = document.search;

	if(f.start_date.value == '')
	{
		alert('Please insert a start date for this promotion.');
		f.start_date.focus();
		return false;
	}
	if(f.end_date.value == '')
	{
		alert('Please insert an end date for this promotion.');
		f.end_date.focus();
		return false;
	}

	var A = f.start_date.value.split(/[\\\.]/); 
	A = [A[1],A[0],A[2]]; 
	strdate1s = A.join('/');

	var B = f.end_date.value.split(/[\\\.]/); 
	B = [B[1],B[0],B[2]]; 
	strdate2s = B.join('/');


	var nr_days = DateDiff("d", strdate1s, strdate2s);
	if(nr_days < 0)
	{
		alert("End date cannot be smaller than the start date.Please check.");
		f.end_date.focus();
		return false;
	}

	var total = f.total_cars.value;
	for(i = 1; i < total; i++)
	{
		goldcar_car_price = eval("f.goldcar_price_"+i+".value");

		if(isNaN(goldcar_car_price) || (goldcar_car_price == ''))
		{
			alert('Please put a "." not a "," (like 123.45 not 123,45 ). Use POINT Not COMMA. Thank you.');
			goldcar_car = eval("f.goldcar_price_"+i);
			goldcar_car.focus();
			return false;
		}

		car_price = eval("f.price_"+i+".value");
		if(isNaN(car_price) || (car_price == ''))
		{
			alert('Please put a "." not a "," (like 123.45 not 123,45 ). Use POINT Not COMMA. Thank you.');
			price_car = eval("f.price_"+i);
			price_car.focus();
			return false;
		}
	}
	return true;
}