var startTime = new Date();
var endTime = new Date();
var redBorderSetField = '';
var highlightedDiv = '';

function open_link(link,linkname,x,y) {
	window.open(link, linkname, 'width='+x+',height='+y+',toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=1,copyhistory=0,screenX=100,screenY=100,top=100,left=100')
}

function open_link_pos(link,linkname,x,y,px,py) {
	window.open(link, linkname, 'width='+x+',height='+y+',toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=1,copyhistory=0,screenX='+px+',screenY='+py+',left='+px+',top='+py)
}

function leadZero(s){
	var st = new String(s);
	if (st.length==1){
		return "0"+s;
	}
	return s;
}

function getTimeObject(year, mon,day,hour,min){
	
	return new Date(year,mon-1,day,hour,min,0);
}

function getYear(t){
	var y = t.getYear();
	if (y<1900) y += 1900;
	return y;
}

function getMonLen(t){
	var d = new Date(getYear(t),t.getMonth()+1,0);
	return d.getDate();
}

function checkCarClass(go){
	var f = document.f;

	if (f.car_class.value==0){
		if (go==1)
		{
			document.location.href="#car_select";
		}
		setRedBorder('car_class');
		alert('Please select a Car Class!');
		return false;
	}
	return true;
}

function checkDateDiff(){
	var f = document.f;

	if (parseInt(f.pick_time_year.value)>f.drop_time_year.value){
		return false;
	} else if (parseInt(f.pick_time_year.value)==f.drop_time_year.value){
		if (parseInt(f.pick_time_mon.value)>f.drop_time_mon.value){
			return false;
		} else if (parseInt(f.pick_time_mon.value)==f.drop_time_mon.value){
			if (parseInt(f.pick_time_day.value)>f.drop_time_day.value){
				return false;
			} else if (parseInt(f.pick_time_day.value)==f.drop_time_day.value){
				if (parseInt(f.pick_time_hour.value)>f.drop_time_hour.value){
					return false;
				} else if (parseInt(f.pick_time_hour.value)==f.drop_time_hour.value && parseInt(f.pick_time_min.value)>f.drop_time_min.value){
					return false;
				}
			}
		}
	}
	return true;
}

function checkDate(year, mon, day, hour, min){
	if (book_start_time[0]>year){
		return false;
	} else if (book_start_time[0]==year){
		if (book_start_time[1]>mon){
			return false;
		} else if (book_start_time[1]==mon){
			if (book_start_time[2]>day){
				return false;
			} else if (book_start_time[2]==day){
				if (book_start_time[3]>hour){
					return false;
				} else if (book_start_time[3]==hour && book_start_time[4]>min){
					return false;
				}
			}
		}
	}
	return true;
}

function checkDateDay(type){
	var f = document.f;
	var t, mon, mObj, day;
	if (type == 'pick_time'){
		t = getTimeObject(f.pick_time_year.value, f.pick_time_mon.value,f.pick_time_day.value,f.pick_time_hour.value,f.pick_time_min.value);
		mon = f.pick_time_mon.value-1;
		mObj = f.pick_time_mon;
		day = f.pick_time_day;
		monLen = getMonLen(getTimeObject(f.pick_time_year.value, f.pick_time_mon.value,1,0,0));
	} else {
		t = getTimeObject(f.drop_time_year.value, f.drop_time_mon.value,f.drop_time_day.value,f.drop_time_hour.value,f.drop_time_min.value);
		mon = f.drop_time_mon.value-1;
		mObj = f.drop_time_mon;
		day = f.drop_time_day;
		monLen = getMonLen(getTimeObject(f.drop_time_year.value, f.drop_time_mon.value,1,0,0));
	}
	if (mon != t.getMonth()){
		alert("The "+((type=="pick_time")? "arrival" : "depature")+" date is not a valid date! There is no day "+ day.value +" in "+mObj.options[mObj.selectedIndex].text + ". Changing to "+monLen+".");
		day.options[monLen-1].selected=true;
//		return false;
	}
	return true;
}

function getInvalidDateMsg(){
	alert("You can't make a booking less than 4 hours before rental start. \n(current time in " + country_name + " is "+leadZero(today_time[2])+"/"+leadZero(today_time[1])+"/"+today_time[0]+ "  "+leadZero(today_time[3])+":"+leadZero(today_time[4])+")");
}

function checkRentDates(){
	var f = document.f;

	if (!checkDateDiff()){
		alert("Departure time can't precede Arrival time!");
		return false;
	}
	
	if (!checkDate(f.pick_time_year.value, f.pick_time_mon.value,f.pick_time_day.value,f.pick_time_hour.value,f.pick_time_min.value) || !checkDate(f.drop_time_year.value, f.drop_time_mon.value,f.drop_time_day.value,f.drop_time_hour.value,f.drop_time_min.value)){
		getInvalidDateMsg();
		return false;
	}

	if (!checkDateDay('pick_time')) return false;
	if (!checkDateDay('drop_time')) return false;

	if (!checkDateDiff()){
		alert("Departure time can't precede Arrival time!");
		return false;
	}

//	if (!checkSpecialBlock(parseInt(f.arrival_mon.value), parseInt(f.arrival_day.value), parseInt(f.depature_mon.value), parseInt(f.depature_day.value))) return false;

	return true;
}

function checkExpDate(year, mon){
	if (today_time[0]>year){
		return false;
	} else if (today_time[0]==year){
		if (today_time[1]>mon){
			return false;
		}
	}
	return true;
}

function checkAllDates(){
	var f = document.f;

	if (!checkRentDates()){
		return false;
	}

/*	if (!checkDate(f.pick_time_year.value, f.pick_time_mon.value,f.pick_time_day.value,f.pick_time_hour.value,f.pick_time_min.value) || !checkDate(f.drop_time_year.value, f.drop_time_mon.value,f.drop_time_day.value,f.drop_time_hour.value,f.drop_time_min.value) || !checkExpDate(f.license_exp_year.value, f.license_exp_mon.value)){
		getInvalidDateMsg();
		return false;
	}
	if (f.driver2_exp_year){
		if (!checkExpDate(f.driver2_exp_year.value, f.driver2_exp_mon.value)){
		getInvalidDateMsg();
		return false;
		}
	}
	if (f.driver3_exp_year){
		if (!checkExpDate(f.driver3_exp_year.value, f.driver3_exp_mon.value)){
		getInvalidDateMsg();
		return false;
		}
	}
*/
	if (f.offer_id){
		if (f.offer_id.checked && !checkSpecialOffer()){
			return false;
		}
	}
	return true;
}

// -1 kui start > end
// 0 kui start = end
// 1 kui start < end
function compareTimes(start, end){

	if (parseInt(start[0])>parseInt(end[0])){
		return -1;
	} else if (parseInt(start[0])==parseInt(end[0])){
		if (parseInt(start[1])>parseInt(end[1])){
			return -1;
		} else if (parseInt(start[1])==parseInt(end[1])){
			if (parseInt(start[2])>parseInt(end[2])){
				return -1;
			} else if (parseInt(start[2])==parseInt(end[2])){
				if (parseInt(start[3])>parseInt(end[3])){
					return -1;
				} else if (parseInt(start[3])==parseInt(end[3])){
					if (parseInt(start[4])>parseInt(end[4])){
						return -1;
					} else if (parseInt(start[4])==parseInt(end[4])){
						return 0;
					}
				}
			}
		}
	}
	return 1;
}

function checkSpecialOffer(){
	var f = document.f;
	var start = new Array(f.pick_time_year.value, f.pick_time_mon.value, f.pick_time_day.value, f.pick_time_hour.value, f.pick_time_min.value);
	var end = new Array(f.drop_time_year.value, f.drop_time_mon.value, f.drop_time_day.value, f.drop_time_hour.value, f.drop_time_min.value);
	if (compareTimes(sp_start,start)<0 || compareTimes(end, sp_end)<0){
		alert("If You want to get Special Offer arrival and depature times must be between Special Offer times. ("+leadZero(sp_start[2])+"/"+leadZero(sp_start[1])+"/"+sp_start[0]+" "+leadZero(sp_start[3])+":"+leadZero(sp_start[4])+" - "+leadZero(sp_end[2])+"/"+leadZero(sp_end[1])+"/"+sp_end[0]+" "+leadZero(sp_end[3])+":"+leadZero(sp_end[4])+")");
		return false;
	}
	if (f.pick_loc.value != kastrup_id || f.drop_loc.value != kastrup_id){
		alert("Special offers are available only in Kastrup.");
		return false;
	}
	return true;
}

function checkCustomerInfo(){
	var f = document.f;
	var fields = new Array('name','address','city','state','postal', 'country','email','phone');
	var boxes = new Array('Name', 'Address', 'City', 'State/Province', 'Zip/Postal', 'Country','E-mail Address', 'Phone');


	for(var i=0; i<fields.length; i++){
		if (!f.elements[fields[i]].value)
		{
			document.location.href="#cust_info";
			setRedBorder(fields[i]);
			highlight_div('help_4');
			alert('Please fill the "'+boxes[i]+'" box!');
			return false;
		}
	}
	if (f.date_of_birth_day.value == "0")
	{
		document.location.href="#cust_info";
		setRedBorder("date_of_birth_day");
		highlight_div('help_4');
		alert('Please fill date of birth!');
		return false;
	}
	else if (f.date_of_birth_mon.value == "0")
	{
		document.location.href="#cust_info";
		setRedBorder("date_of_birth_mon");
		highlight_div('help_4');
		alert('Please fill date of birth!');
		return false;
	}
	else if (f.date_of_birth_year.value == "0")
	{
		document.location.href="#cust_info";
		setRedBorder("date_of_birth_year");
		highlight_div('help_4');
		alert('Please fill date of birth!');
		return false;
	}

	if (f.license.value == "")
	{
		document.location.href="#cust_info";
		setRedBorder("license");
		highlight_div('help_4');
		alert('Please fill the "License #" box!');
		return false;
	}

	if (f.pass.value == "")
	{
		document.location.href="#cust_info";
		setRedBorder("pass");
		highlight_div('help_4');
		alert('Please fill the "Passport #" box. If not available now, write "N/A" and submit it later');
		return false;
	}

	var s = new String(f.email.value);
	if (s.indexOf('@') == -1 || s.lastIndexOf('.') < s.indexOf('@') || s.length == s.lastIndexOf('.')+1){
		alert('Please give a valid e-mail address.');
		return false;
	}
	s = new String(f.country.value);
//	if (s.toLowerCase().indexOf('denmark') != -1 || s.toLowerCase().indexOf('danmark') !=-1 || s.toLowerCase().toString() == 'dk')	{
//		alert('Your address must be outside Denmark.');
//		return false;
//	}
	return true;
}

function checkCommentBox(){
	var f = document.f;
	var s = new String(f.comments.value);
//	var a = s.split("\n");

	if (s.length>200){
		alert("Text in comment box is too big! (max. 200 characters allowed. You have "+s.length+")");
		return false;
	}

//	if (a.length>4){
//		alert("There are too many line breaks ('enter' presses) in comment box (max. 3 allowed)");
//		return 0;
//	}
	return true;
}

// for rent.php
function check_archive(){
	var f = document.f;

	if (!checkCustomerInfo()) {return false;}

	f.submit_type.value=3;
	f.submit();
}

function getFormEl(f, name){
	var e = f.elements;
	for (var i=0; i<e.length; i++){
		if (e[i].name == name)
			return e[i];
	}
}

function checkExtras(){
	var f = document.f;
	
	return true;
}

function checkFlightAndTime(){
	var f = document.f;
	if (!parseInt(f.bid.value) || flight_and_time_check) return true; // new book
	var start = new Array(f.pick_time_year.value, f.pick_time_mon.value, f.pick_time_day.value, f.pick_time_hour.value, f.pick_time_min.value);
	var start_current = new Array(f.current_pick_time_year.value, f.current_pick_time_mon.value, f.current_pick_time_day.value, f.current_pick_time_hour.value, f.current_pick_time_min.value);
	var end = new Array(f.drop_time_year.value, f.drop_time_mon.value, f.drop_time_day.value, f.drop_time_hour.value, f.drop_time_min.value);
	var end_current = new Array(f.current_drop_time_year.value, f.current_drop_time_mon.value, f.current_drop_time_day.value, f.current_drop_time_hour.value, f.current_drop_time_min.value);
	var msg = '';
	if ((compareTimes(start,start_current)!=0 && f.arrival_flight.value==f.current_arrival_flight.value)){
		msg += "You have changed arrival time, but not arrival flight #.\n";
	} else if ((compareTimes(start,start_current)==0 && f.arrival_flight.value!=f.current_arrival_flight.value)){
		msg += "You have changed arrival flight #, but not arrival time.\n";
	}
//	if ((compareTimes(end,end_current)!=0 && f.depature_flight.value==f.current_depature_flight.value)){
//		msg += "You have changed depature time, but not depature flight #.\n";
//	} else if ((compareTimes(end,end_current)==0 && f.depature_flight.value!=f.current_depature_flight.value)){
//		msg += "You have changed depature flight #, but not depature time.\n";
//	}
	if (msg){
		flight_and_time_check = true;
		alert(msg + "Please double-check the data !");
		return false;
	}
	return true;
}

function setRedBorder(field){
	var fl = document.getElementById(field);
	fl.style.borderWidth='3px';
	fl.style.borderColor='#FF0000';
	redBorderSetField = field;
	fl.focus();
}

function check_book_info() {
	var f = document.f;

	if (redBorderSetField!=''){
		var fl = document.getElementById(redBorderSetField);
		fl.style.borderWidth='1px';
		fl.style.borderColor='#000000';
	}

	unhighlight_div(highlightedDiv);

	if (!f.arrival_flight.value) {
		if (!f.comments.value){
				setRedBorder('arrival_flight');
			document.location.href="#delivery_info";
			highlight_div('help_1');
			alert("Flight # missing. Please fill or explain in comment box!");
			return false;
		}
	}

	if (!checkFlightAndTime()) return false;
	if (!checkCarClass(1)){return false;}
	if (!checkExtras()) return false;
	if (!checkCustomerInfo()) {return false;}

	if (!checkAllDates()){ return false;}
	if (!checkCommentBox()) {return false;}
	return true;
}

function rent() {
	var f = document.f;

	if (!check_book_info())
		return false;
	f.submit_type.value=2;
	f.submit();
	show_ticker();
}

function add_waiting_list() {
	var f = document.f;

	if (!check_book_info())
		return false;
	f.submit_type.value=4;
	f.submit();
	show_ticker();
}

function check_calc_info(){
	var f = document.f;

	if (redBorderSetField!=''){
		var fl = document.getElementById(redBorderSetField);
		fl.style.borderWidth='1px';
		fl.style.borderColor='#000000';
	}

	unhighlight_div(highlightedDiv);

	if (!checkRentDates()){
		return false;
	}

	if (f.offer_id){
		if (f.offer_id.checked && !checkSpecialOffer()){
			return false;
		}
	}

	if (!checkCarClass(0)){
		return false;
	}
	return true;
}

function check_qq_avail(){
	var f = document.f;
	
	if (!check_calc_info()){
		return false;
	}
	f.submit_type.value=1;
	document.f.submit();
}

function check_avail(){
	var f = document.f;
	
	if (!check_calc_info()){
		return false;
	}
	if (!checkExtras()) return false;
	f.submit_type.value=1;
	document.f.submit();
//	show_ticker();
}

function quick_quote_book_now(){
	var f = document.f;
	
	if (!check_calc_info()){
		return false;
	}
	f.submit_type.value=1;
	f.action='https://www.scandinaviancarrental.com/' + country_short_name + '/book.php?from_quick_quote=1';
//	f.action='book.php?from_quick_quote=1';
	document.f.submit();
}

function selectOption(sel, opt){
	for (var i=0; i<sel.options.length; i++){
		if (sel.options[i].value==opt){
			sel.options[i].selected=true;
			break;
		}
	}
}

function dateselect(yr, mon, field){
	y = yr[yr.selectedIndex].value;
	m = mon[mon.selectedIndex].value;
  window.open('/dateselect.php?mon_year='+y+m+'&field='+field,'dateselect', 'width=200,height=200,toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=0,copyhistory=0,screenX=350,screenY=300,top=300,left=350'); 
}

function takeDate(yr,m,d,field){
	ds = eval('document.f.'+field+'_day');
	ms = eval('document.f.'+field+'_mon');
	ys = eval('document.f.'+field+'_year');

	for(i=0; i<ds.length; i++){
		if (ds[i].value==d){
			ds[i].selected=true;
			break;
		}
	}

	for(i=0; i<ms.length; i++){
		if (ms[i].value==m){
			ms[i].selected=true;
			break;
		}
	}

	for(i=0; i<ys.length; i++){
		if (ys[i].text==yr){
			ys[i].selected=true;
			break;
		}
	}

}

function change_additional_drivers_sel(){
	var f = document.f;
	
	f.submit_type.value=10;
	f.action=f.action+'#additional_drivers_pos';
	document.f.submit();
}

var ticker_preload = new Image();
ticker_preload.src="img/ticker3.gif";

function show_ticker(){
	var ticker_img = document.getElementById("ticker_img");
	ticker_img.src=ticker_preload.src;
	var ticker = document.getElementById("ticker");
	if (typeof(document.body.clientWidth)!='undefined')
		ticker.style.left=parseInt(parseInt(document.body.clientWidth)/2)-35;
	if (typeof(document.body.scrollTop)!='undefined' && typeof(document.body.clientHeight)!='undefined')
		ticker.style.top=parseInt(document.body.scrollTop)+parseInt(document.body.clientHeight)-200;
	ticker.style.display='block';
}
function WriteEmailLink(username, domainName, linkClass)
{
	var at = "@";
	document.write("<a href='mailto:"+username+""+at+""+domainName+"' class=\"" + linkClass + "\">"+username+""+at+""+domainName+"</a>");
}

function highlight_div(div_id)
{
	var myDiv = document.getElementById(div_id);
	if (myDiv != null)
	{
		myDiv.innerHTML = '<img src="img/helpfaqred.jpg" width="68" height="21" alt="Help/FAQ" Title="Help/FAQ" border="0">';
		highlightedDiv = div_id;
	}
}

function unhighlight_div()
{
	if (highlightedDiv != '')
	{
		var myDiv = document.getElementById(highlightedDiv);
		if (myDiv != null)
		{
			myDiv.innerHTML = '<img src="img/helpfaq.jpg" width="68" height="21" alt="Help/FAQ" Title="Help/FAQ" border="0">';
		}
	}
}

