/************************************************************************************************************
	(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 checkmyDates_ajax(pickup, dropoff) 
	{
		var date1String = pickup;
		var day1    = date1String.substr(0,2);
		var month1  = date1String.substr(3,2);
		var year1   = date1String.substr(6,4);

		var date2String = dropoff;
		var day2    = date2String.substr(0,2);
		var month2  = date2String.substr(3,2);
		var year2   = date2String.substr(6,4);
			
		if (parseFloat(year1) > parseFloat(year2)) {
			return false;
		}
		if (parseFloat(year1) == parseFloat(year2)) {
			if (parseFloat(month1) > parseFloat(month2)) {
 				return false;
			}
			if (parseFloat(month1) == parseFloat(month2)) {
				if (parseFloat(day1) > parseFloat(day2)) {
					return false;
				}
				if (parseFloat(day1) == parseFloat(day2)) {
					return false;
				}
			}
		}
	return true;
	}		


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 dateisNotPossible_clients(day,month,year, mes) 
{
	//var datePoss = new Date().valueOf();

	var datePoss = new Date();
	datePoss.setDate(datePoss.getDate()+2);
	//datePoss.setTime(datePoss.getTime() + 2*(1000*3600*24));
	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 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;
}		
	

//======================================================================
//======added by Irina on 13 July 2010 - for the promo section==========
//======================================================================
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< 2009) {
			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 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,earliest_date_clients)
{
	f = document.onlinequote;

	cal_adv = f.cal_adv.value;

	//var error = $("company_id"); 
	if (isNaN(f.location_from.value) || (f.location_from.value==0)) 
	{ 
		alert(err_pick_up);
		f.location_from.focus(); 
		return false;
	}

	if (isNaN(f.location_to.value) || (f.location_to.value==0)) 
	{ 
		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(cal_adv == 1)
	{
		if(dateisNotPossible(day1,month1,year1,mes_date)==false) 
		{ 
			f.pickup.focus(); return false; 
		}
	}
	else
	{
		if(dateisNotPossible_clients(day1,month1,year1,earliest_date_clients)==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 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,earliest_date_clients)
{
	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,earliest_date_clients))
	{
		return false;
	}

	if (!emailck(f.email, mes_valid_email)) 
	{
		return false;
	}
	f.email_valid.value = 'yes';

	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;
}
//======================================================================
//======added by Irina on 13 July 2010 - for the promo section==========
//======================================================================

//=====function which checks if a value is integer or not====================
function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))){
      return true;
 } else {
      return false;
 }
}

function check_edit_client()
{
	var f = document.booking;
	if(f.client_fname.value.length < 2)
	{
		alert("Please insert the first name");
		f.client_fname.focus();
		return false;

	}
	if(f.client_lname.value.length < 2)
	{
		alert("Please insert the last name");
		f.client_lname.focus();
		return false;

	}
	if(f.client_mobile.value.length < 5)
	{
		alert("Please insert the mobile phone");
		f.client_mobile.focus();
		return false;

	}
	/*if(!is_int(f.client_age.value))
	{
		alert("Please insert the correct age - numeric");
		f.client_age.focus();
		return false;
	}
	if(f.client_age.value <= 0)
	{
		alert("Please insert a positive value for the age");
		f.client_age.focus();
		return false;
	}*/
	if(!emailck(f.myemail, "Please insert a valid e-mail address"))
	{
		f.myemail.focus();
		return false;
	}
	return true;
}

//===================

function checktxt(myobj, maxlen, mes1, mes2)
{
	if (myobj.value=='') {
		alert(mes1);
		myobj.focus();
		return false;
	}
	if (myobj.value.length<maxlen) {
		alert(mes2);
		myobj.focus();
		return false;
	}
	return true;
}


function check_personal_data(session_client_email, previous_page, client_existing)
{
	f = document.booking;

	/*if((session_client_email == "") && (client_existing == 1))
	{
		window.location.href = previous_page +"?msg=1";
	}
	else
	{*/
		var flight_no_js = eval("f.flight_no_js.value");
		var flight_no_js_2 = eval("f.flight_no_js_2.value");
		var first_name_js = eval("f.first_name_js.value");
		var first_name_js_2 = eval("f.first_name_js_2.value");
		var last_name_js = eval("f.last_name_js.value");
		var last_name_js_2 = eval("f.last_name_js_2.value");
		var correct_age_numeric_js = eval("f.correct_age_numeric_js.value");
		var positive_age_js = eval("f.positive_age_js.value");
		var address_js = eval("f.address_js.value");
		var address_js_2 = eval("f.address_js_2.value");
		var city_js = eval("f.city_js.value");
		var city_js_2 = eval("f.city_js_2.value");
		var select_a_country = eval("f.select_a_country.value");
		var mobile_js = eval("f.mobile_js.value");
		var mobile_js_2 = eval("f.mobile_js_2.value");
		var read_terms = eval("f.read_terms.value");

		if(f.location_from_airport.value == '1')
		{
			if(!checktxt(f.booking_flight_number, 2, flight_no_js, flight_no_js_2))
			{
				return false;
			}
		}

		if(f.existing_client.value == 0)
		{
			//the client is not an already existing client
			if(!checktxt(f.client_fname, 2,first_name_js, first_name_js_2))
			{
				return false;
			}
			if(!checktxt(f.client_lname, 2,last_name_js, last_name_js_2))
			{
				return false;
			}

			/*if(!is_int(f.client_age.value))
			{
				alert(correct_age_numeric_js);
				f.client_age.focus();
				return false;
			}
			if(f.client_age.value <= 0)
			{
				alert(positive_age_js);
				f.client_age.focus();
				return false;
			}*/

			if(!checktxt(f.client_address, 5,address_js, address_js_2))
			{
				return false;
			}
			if(!checktxt(f.client_city, 2,city_js, city_js_2))
			{
				return false;
			}

			if (f.client_country_id.selectedIndex == 0)
			{
				alert(select_a_country);
				f.client_country_id.focus();
				return false;
			}

			if(!checktxt(f.client_mobile, 5,mobile_js,mobile_js_2))
			{
				return false;
			}
		}

		if(f.booking_agreement.checked === false)
		{
			alert(read_terms);
			f.booking_agreement.focus();
			return false;
		}

		f.submit();	

	//}
	return true;
}

function CreateBookmarkLink(title, url) 
{
 if (window.sidebar) { // Mozilla Firefox Bookmark
  window.sidebar.addPanel(title, url,"");
 } else if( window.external ) { // IE Favorite
  window.external.AddFavorite( url, title); }
 else if(window.opera && window.print) { // Opera Hotlist
  return true; }
}

