
function disableForm() {
	var oform = document.orderForm;
	
	oform.monthArrive.disabled = true;
	oform.monthDepart.disabled = true;
	oform.dayArrive.disabled = true;
	oform.dayDepart.disabled = true;
	oform.numPeople.disabled = true;
	oform.apartment.disabled = true;
	
	oform.amount.disabled = false;
	oform.useValue.checked = true;
	oform.useForm.checked = false;
}

function enableForm() {

	var oform = document.orderForm;
	
	oform.monthArrive.disabled = false;
	oform.monthDepart.disabled = false;
	oform.dayArrive.disabled = false;
	oform.dayDepart.disabled = false;
	oform.numPeople.disabled = false;
	oform.apartment.disabled = false;
	
	oform.amount.disabled = true;
	oform.useForm.checked = true;
	oform.useValue.checked = false;
}

function populateDays(ad) {

	var days = 0;
	var olist = null;
	var mon = null;
	
	if(ad == "arr") {
		olist = document.orderForm.dayArrive;
		mon = document.orderForm.monthArrive;
	} else {
		olist = document.orderForm.dayDepart;
		mon = document.orderForm.monthDepart;
	}
	
	days = getDays(mon.value);
	
	while (olist.options.length > 0) {
		olist.remove(0);
	}
	
	
	for (var i = (mon.value != 2 ? 0 : 2); i < days; i++) {
		var newopt = document.createElement('option');
		newopt.value = i + 1;
		newopt.text = "" + (i + 1);
		olist.add(newopt,null);
	}
}

function getDays(m){
	switch (m) {
			case '3':	case '5': case '7':	case '8': case '10': case '12':
				return 31;
			case '4': case '6': case '9': case '11':
				return 30;
			case '2':
				return 28;
			case '1': 
				return 2;
			default:
				return 31;
	}
}

function validate() {

	var form = document.orderForm;
	var ad = parseInt(form.dayArrive.value);
	var am = parseInt(form.monthArrive.value);
	var dd = parseInt(form.dayDepart.value);
	var dm = parseInt(form.monthDepart.value);
	var ppl = parseInt(form.numPeople.value);
	var ap = parseInt(form.apartment.value);
	var name = form.oname.value;
	
	var duration;
	var total;
	var msg = "";
	var valid = true;
	
	if (name.length < 3) {
		valid = false;
		msg+="Name must be at least 3 characters\n";
	}
	
	var bad = "!£$%^&*(),./?@;:[{]}#~_=+`¬|\\\"\'<>";
	for(var b = 0; b < bad.length; b++){
		if(name.indexOf(bad.charAt(b)) != -1) {
			valid = false;
			msg += "Name must not contain any of the following characters: " + bad + "\n";
		}
	}
	
	if (form.useForm.checked) {
		//calculate duration of stay
		if (am == dm) {
			duration = dd - ad;
			if (duration < 1) {
				valid = false;
				msg +="One of your selected dates is invalid\n";
			}
		} else {
			var d1 = getDays(am) - ad;
			duration = d1 + dd;
			if (duration < 1) {
				valid = false;
				msg +="One of your selected dates is invalid\n";
			}
		}
		if(dm - am > 1){
			valid = false;
			msg+="Please phone to arrange an extended trip\n";
		}
		if (duration < 7) duration = 7;
		var weeks = Math.ceil(duration / 7);
		
		
		var deposit = (50 * ppl) + 100;
		
		var rent = 0;
		switch (am) {
			case 2: case 3: case 11:
				rent = 185;
				break;
			case 4: case 5: case 9: case 10:
				rent = 200;
				break;
			case 1: case 6: case 7: case 8: case 12:
				rent = 250;
				break;
			default: 
				rent = 0;
				valid = false;
		}
		if ((dm == 12) && (dd < 20)) rent = 185;
		rent = rent * weeks;
		
		var price = deposit + rent
		
		total = (ap != 3 ? price : price * 2);
	} else {
		total = parseInt(form.amount.value);
		am = -1;
		if ((total < 1) || (isNaN(total))) {
			valid = false;
			msg +="Please enter a valid amount in the box";
		}
	}	
	if(!valid) alert(msg);
	if(valid) submit(am,ad,dm,dd,deposit,ap,name,total);
}
	
function submit(am,ad,dm,dd,deposit,ap,name,total){

	var ref = "";
	ref += name.replace(/ /,"") + "-";
	if (am == -1) {
		ref += "amt-" + total;
	} else {
		ref += (ad < 10) ? "0" + ad : ad;
		ref += (am < 10) ? "0" + am : am;
		ref += "-"
		ref += (dd < 10) ? "0" + dd : dd;
		ref += (dm < 10) ? "0" + dm : dm; 
		ref += "-" + deposit + "-" + ap;
	}	
	location.href = "submit.html?t=" + total + "&r=" + ref;
	
}

function generateButton(){

	var hr = document.location.href.split("?")[1];
	var hr2 = hr.split("&");
	var amt = hr2[0].split("=")[1];
	var ref = hr2[1].split("=")[1];
	
	var st = document.getElementById("summaryText");
	var h4 = '<h4 class="sumH4">';
	var h4e = '</h4>';
	
	var x = ref.split("-");
	st.innerHTML = "Reference: " + ref;
	st.innerHTML += h4 + "Name: " + x[0] + h4e;
	if(ref.indexOf("amt") < 0){
		var ad = x[1].substr(0,2); ad +="/"+x[1].substr(2,2);
		st.innerHTML += h4 + "Arrival date: " + ad + h4e;
		var dd = x[2].substr(0,2); dd +="/"+x[2].substr(2,2);
		st.innerHTML += h4 + "Depature date: " + dd + h4e;
		st.innerHTML += h4 + "Deposit: &pound;" + x[3] + h4e;
	} else {
		st.innerHTML += h4 + "Pre arranged price" + h4e;
	}
	st.innerHTML += h4 + "Total: &pound;" + amt + h4e;
	
	var ih1 = '<a href="https://www.transx.co.uk/payment/index.php?companyID=11144&amount=';
	var ih2 = '&identifier=';
	var ih3 = '¤cy=GBP" target="_blank"><img class="oButton" src="https://www.transx.co.uk/images/buynow01.gif" border="0"></a>'
	
	var pb = document.getElementById("paymentButton");
	pb.innerHTML = ih1 + amt + ih2 + ref + ih3;
		 
}






		