// AllWays Car Rentals Modules Javascript Code


function MM_findObj(formName, n, d) { //v4.01
  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])) for (i=0;!x&&i<d.forms.length;i++) if(d.forms[i].id==formName) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(formName, n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0 Modified by Peter
  var i,p,q,nm,test,test2,num,min,max,errors="",args=MM_validateForm.arguments;
  for (i=1; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[0], args[i]);
	if (val) { nm=val.name; if ((val=val.value)!="") {
	  if (test.indexOf("isEmail")!=-1) { p=val.search(/^[^@]+@[^@]+.[a-z]{2,}$/i);
		if (p<0 || p==(val.length-1) || val.indexOf(" ")!=-1) {
			errors+="- "+nm+" must contain an e-mail address.\n";
		} else {
			test2 = MM_findObj(args[0], "re-email");
			if(test2 && test2.value != val) errors+="- "+nm+" must match re-entered email.\n";
		}
	  } else if (test!="R") { num = parseFloat(val);
		if (isNaN(val)) errors+="- "+nm+" must contain a number.\n";
		if (test.indexOf("inRange") != -1) { p=test.indexOf(":");
		  min=test.substring(8,p); max=test.substring(p+1);
		  if (num<min || max<num) errors+="- "+nm+" must contain a number between "+min+" and "+max+".\n";
	} } } else if (test.charAt(0) == "R") {
			if(nm == "v_code") nm = "verification_code";
			nm = nm.replace("_", " ");
			errors += "- " + Formmail_capitalize(nm) + " is required.\n";
		}
	}
  } if (errors) alert("The following error(s) occurred:\n"+errors);
  document.MM_returnValue = (errors == "");
}
function Formmail_capitalize(string) {
	return string.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};function seekom_checkDate() {
	var PickUpDate = document.getElementById('PickUpDate').value.split('-');
	var ReturnDate = document.getElementById('ReturnDate').value.split('-');
	var dtePU = new Date(PickUpDate[0], (parseFloat(PickUpDate[1])-1), PickUpDate[2]);
	var dteDO = new Date(ReturnDate[0], (parseFloat(ReturnDate[1])-1), ReturnDate[2]);
	var d = new Date();
	var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());
    if (d.getFullYear() == PickUpDate[0] && d.getMonth() == (parseFloat(PickUpDate[1])-1) && PickUpDate[2] < d.getDate()) {
        if (document.getElementById('PickUpDate_month')) document.getElementById('PickUpDate_month').selectedIndex = parseFloat(PickUpDate[1]);
		dtePU = new Date(parseFloat(PickUpDate[0]), parseFloat(PickUpDate[1]), PickUpDate[2]);
	} else if (dtePU < today) {
		if (document.getElementById('PickUpDate_year')) document.getElementById('PickUpDate_year').value = parseFloat(PickUpDate[0]) + 1;
		dtePU = new Date((parseFloat(PickUpDate[0]) + 1), (parseFloat(PickUpDate[1])-1), PickUpDate[2]);
	}
	if(dtePU >= dteDO) {
		var one_day=1000*60*60*24;
		var newDO = new Date(dtePU.getTime() + (one_day * 1));
		var intYear = newDO.getFullYear();
		if(newDO.getMonth() + 1 < 10) var intMonth = '0' + (newDO.getMonth() + 1);
		else var intMonth = newDO.getMonth() + 1;
		if(newDO.getDate() < 10) var intDay = '0' + newDO.getDate();
		else var intDay = newDO.getDate();
		if(intMonth == '13') {
			intDay = '01'; 
			intMonth = '01'; 
			intYear++; 
		}
		document.getElementById('ReturnDate').value = intYear + '-' + intMonth + '-' + intDay;
		if(document.getElementById('ReturnDate_day')) document.getElementById('ReturnDate_day').value = intDay;
		if(document.getElementById('ReturnDate_month')) document.getElementById('ReturnDate_month').value = intMonth;
		if(document.getElementById('ReturnDate_year')) document.getElementById('ReturnDate_year').value = intYear;
	}
}
function seekom_calculateBooking() {
	var total	= 0;
	var rowTotal= 0;
	var groupid	= 0;
	var mainid	= 0;
	var mainPrice = 0;
	var min		= 0;
	var qty		= 0;
	var price	= 0;
	var oldmain	= -1;
	
	arrGroups = document.getElementById('seekom_groups').value.split(',');
	for(i = 0; i < arrGroups.length; i++) {
		groupid = arrGroups[i];
		mainid = parseInt(document.getElementById('group_main_' + groupid).value);
		qty = parseInt(document.getElementById('group_qty_' + groupid).value);
		min = parseInt(document.getElementById('group_min_' + groupid).value);
		price = parseFloat(document.getElementById('group_price_' + groupid).value);
		rowTotal = 0;
		if(!min) min = 1;
		if(qty > 0) rowTotal = (price * min) * qty;
		total = total + rowTotal;
		document.getElementById('price_' + groupid).innerHTML = formatNumber(rowTotal);
		if(mainid != oldmain) document.getElementById('group_total_amount_' + mainid).value = 0;
		mainPrice = parseFloat(document.getElementById('group_total_amount_' + mainid).value);
		mainPrice = mainPrice + rowTotal;
		document.getElementById('group_total_amount_' + mainid).value = mainPrice;
		document.getElementById('group_total_' + mainid).innerHTML = formatNumber(mainPrice);
		oldmain = mainid;
	}
	document.getElementById('booking_total').innerHTML = formatNumber(total);
}
function seekom_changeRadio(groupid, extraCode, value) {
	var loop = document.getElementsByName('groupid_' + groupid).length;
	for(var i = 0; i < loop; i++) {
		var code = document.getElementsByName('groupid_' + groupid)[i].value;
		if(code == extraCode) {
			document.getElementsByName('groupid_' + groupid)[i].checked = true;
			document.getElementById('group_qty_' + code).value = value;
		} else {
			document.getElementsByName('groupid_' + groupid)[i].checked = false;
			document.getElementById('group_qty_' + code).value = '0';
		}
	}
	seekom_calculateBooking();
}
function formatNumber(value) {
	lowValue=Math.floor(value);
	var p=100*(value-lowValue)+0.6;
	
	if(p>99.99) {
		returnNumber = lowValue + 1;
		returnNumber +=".00";
	} else {
		returnNumber=lowValue+".";
		returnNumber+=Math.floor(p/10);
		returnNumber+=Math.floor(p%10);
	}
	return returnNumber;
}
function seekom_pickupChange(id) {
	var domain = (("https:" == document.location.protocol) ? "https://www.allwaysrental.com/" : "http://www.allwaysrental.com/");
	AjaxRequest.get(
		{
		  'url' : domain + 'modules/display.php?modulename=Seekom&ajax=true&pickup=' + id
		  ,'onSuccess':function(req){
			document.getElementById("seekom_PickUpTime").innerHTML = req.responseText;
		  }
		}
	);
}
function seekom_returnChange(id) {
	var domain = (("https:" == document.location.protocol) ? "https://www.allwaysrental.com/" : "http://www.allwaysrental.com/");
	AjaxRequest.get(
		{
		  'url' : domain + 'modules/display.php?modulename=Seekom&ajax=true&return=' + id
		  ,'onSuccess':function(req){
			document.getElementById("seekom_ReturnTime").innerHTML = req.responseText;
		  }
		}
	);
}
