function enable_amcc() {
	var obj = document.step1.amcc_1;
	obj.disabled = false;
	obj.setAttribute('id', 'required');
	var obj = document.step1.amcc_2;
	obj.disabled = false;
	obj.setAttribute('id', 'required');
}

function disable_amcc() {
	var obj = document.step1.amcc_1;
	obj.disabled = true;
	obj.value = '';
	obj.setAttribute('id', '');
	var obj = document.step1.amcc_2;
	obj.disabled = true;
	obj.value = '';
	obj.setAttribute('id', '');
}

function enable_port_base(id, port_id) {
	var xmlhttp = false;
	if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', 'ebooking.ajax.php?action=enable_port_base&id=' + id + '&port_id=' + port_id, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var content = xmlhttp.responseText;
			document.getElementById('port_base_holder').innerHTML = content;
		}
	}
	xmlhttp.send(null);
	var obj = document.getElementById('port_base');
	obj.focus();
	return;
}

function chk_disembark_date() {
	var obj1 = document.step1.embark_date;
	var tmp = obj1.value.split("/");
	var date1 = new Date(tmp[2] + "/" + tmp[1] + "/" + tmp[0]);
	var obj2 = document.step1.disembark_date;
	var tmp = obj2.value.split("/");
	var date2 = new Date(tmp[2] + "/" + tmp[1] + "/" + tmp[0]);
	if (date2-date1 <= 0) {
		Ext.MessageBox.alert('Warning', 'You must select a Disembark date posterior to the Embark date.', void(0));
		return false;
	}
	return true;
}

function header_validate() {
	var inp = document.getElementsByTagName('input');
	var flag = 0;
	var msg = "";
	for (var i=0; i<inp.length; i++) {
		var ii = inp[i].getAttribute('id');
		if (ii == 'header_required' && inp[i].value == '') {
			var titlos = inp[i].getAttribute('title');
			msg = msg + titlos + "<br>";
			flag++;
		}
	}
	var inp = document.getElementsByTagName('select');
	for (var i=0; i<inp.length; i++) {
		var ii = inp[i].getAttribute('id');
		if (ii == 'header_required' && inp[i].value == '0') {
			var titlos = inp[i].getAttribute('title');
			msg = msg + titlos + "<br>";
			flag++;
		}
	}
	if (flag > 0) {
		Ext.MessageBox.alert('Warning', 'Please, fill the required fields:<br><font color="red">' + msg + '</font>', void(0));
		return false;
	}
	else return true;
}

function header_country_holder() {
	var xmlhttp = false;
	if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', 'ebooking.ajax.php?action=header_country_holder', true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var content = xmlhttp.responseText;
			document.getElementById('header_country_holder').innerHTML = content;
		}
	}
	xmlhttp.send(null);
	return;
}

function header_chk_disembark_date() {
	var obj1 = document.step1_header.header_embark_date;
	var tmp = obj1.value.split("/");
	var date1 = new Date(tmp[2] + "/" + tmp[1] + "/" + tmp[0]);
	var obj2 = document.step1_header.header_disembark_date;
	var tmp = obj2.value.split("/");
	var date2 = new Date(tmp[2] + "/" + tmp[1] + "/" + tmp[0]);
	if (date2-date1 <= 0) {
		Ext.MessageBox.alert('Warning', 'You must select a Disembark date posterior to the Embark date.', void(0));
		return false;
	}
	return true;
}

function header_enable_port_base(id) {
	var xmlhttp = false;
	if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', 'ebooking.ajax.php?action=header_port_base&id=' + id, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var content = xmlhttp.responseText;
			document.getElementById('header_port_base_holder').innerHTML = content;
		}
	}
	xmlhttp.send(null);
	var obj = document.getElementById('header_port_base');
	obj.focus();
	return;
}

function step1_submit() {
	var obj = document.step1.amcc_1;
	if (obj.getAttribute('id') == 'required') {
		var part1 = document.step1.amcc_1.value;
		var part2 = document.step1.amcc_2.value;
		var xmlhttp = false;
		if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
		xmlhttp.open('GET', 'ebooking.ajax.php?action=chk_amcc&code=' + part1 + ' ' + part2, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var tmp = xmlhttp.responseText;
				if (tmp == '') {
					Ext.MessageBox.alert('Warning', 'Your card number is not correct.', void(0));
					return false;
				}
				if (validate() && chk_disembark_date()) {
					step1.submit();
					document.getElementById('btn_holder').innerHTML = 'Please wait while loading...';
				}
			}
		}
		xmlhttp.send(null);
	}
	else if (validate() && chk_disembark_date()) {
		step1.submit();
		document.getElementById('btn_holder').innerHTML = 'Please wait while loading...';
	}
}

function set_disembark_date(type) {
	if (type == 'main') {
		var obj1 = document.step1.embark_date;
		var obj2 = document.step1.disembark_date;
	}
	else if (type == 'header') {
		var obj1 = document.step1_header.header_embark_date;
		var obj2 = document.step1_header.header_disembark_date;
	}
	var tmp = obj1.value.split("/");
	if (tmp[2] < 10) tmp[2] = '0' + tmp[2];
	var d = new Date(tmp[2] + "/" + tmp[1] + "/" + tmp[0]);
	d.setDate(d.getDate()+7);
	if (d.getDate() < 10) var tmp_day = '0' + d.getDate();
	else var tmp_day = d.getDate();
	var tmp_month = d.getMonth()+1;
	if (tmp_month < 10) tmp_month = '0' + tmp_month;
	obj2.value = tmp_day+"/"+tmp_month+"/"+d.getFullYear();
}

function add_remove_extra(obj_id) {
	var obj = document.getElementById(obj_id + '_div');
	if (obj.className == 'ebooking_extra_not_selected') {
		obj.className = 'ebooking_extra_selected';
		var obj2 = document.getElementById(obj_id + '_img');
		obj2.src = 'main_images/new/ebooking/remove_extra.png';
		var obj3 = document.getElementById(obj_id + '_hidden');
		obj3.value = document.getElementById('price_' + obj_id).getAttribute('title');
	}
	else {
		obj.className = 'ebooking_extra_not_selected';
		var obj2 = document.getElementById(obj_id + '_img');
		obj2.src = 'main_images/new/ebooking/add_extra.png';
		var obj3 = document.getElementById(obj_id + '_hidden');
		obj3.value = 0;
	}
	calculateTotalPrice();
}

function select_linen() {
	document.getElementById('LinenSet_hidden').value = document.getElementById('LinenSet_quantity').value;
	calculateTotalPrice();
}

function calculateTotalPrice() {
	var price = parseFloat(document.getElementById("price_webDiscountedPriceOut").getAttribute('title')) +
				parseFloat(document.getElementById("price_endCleaningOut").getAttribute('title')) +
				parseFloat(document.getElementById("price_TransitLogBook").getAttribute('title'));
	if (document.getElementById('DamageWaiver_div').className == 'ebooking_extra_selected') price += parseFloat(document.getElementById("price_DamageWaiver").getAttribute('title'));
	if (document.getElementById('BadDebts_div').className == 'ebooking_extra_selected') price += parseFloat(document.getElementById("price_BadDebts").getAttribute('title'));
	if (document.getElementById('CancellationInsurance_div').className == 'ebooking_extra_selected') price += parseFloat(document.getElementById("price_CancellationInsurance").getAttribute('title'));
	if (document.getElementById('Outboard_div').className == 'ebooking_extra_selected') price += parseFloat(document.getElementById("price_Outboard").getAttribute('title'));
	if (document.getElementById('SkipperFees_div').className == 'ebooking_extra_selected') price += parseFloat(document.getElementById("price_SkipperFees").getAttribute('title'));
	if (document.getElementById('HostessCookFees_div').className == 'ebooking_extra_selected') price += parseFloat(document.getElementById("price_HostessCookFees").getAttribute('title'));
	if (document.getElementById('RailNet_div').className == 'ebooking_extra_selected') price += parseFloat(document.getElementById("price_RailNet").getAttribute('title'));
	if (document.getElementById('LinenSet_quantity').value != 0) price += parseFloat(document.getElementById("price_LinenSet").getAttribute('title') * document.getElementById('LinenSet_quantity').value);
	document.getElementById('totalPrice').innerHTML = number_format(price, 2, ',', '.');
	document.getElementById('totalPrice_hidden').value = number_format(price, 2, '.', '');
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
		};
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
	}
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
	return s.join(dec);
}

var min = 07;
var sec = 00;
function countdownTimer(agent_id, msg) {
	sec--;
	if (sec == -01) {
		sec = 59;
		min = min - 1;
	}
	else min = min;
	if (sec <= 9) sec = "0" + sec;
	time = msg + " <b style=\"color: #f36e20\">" + (min<=9 ? "0" + min : min) + ":" + sec + "</b>";
	if (document.getElementById) document.getElementById('theTime').innerHTML = time;
	SD = window.setTimeout("countdownTimer('" + agent_id + "', '" + msg + "');", 1000);
	if (min == '00' && sec == '00') {
		sec = "00";
		window.clearTimeout(SD);
		window.location = 'ebooking.php?agent_id=' + agent_id + '&timeout=1';
	}
}

function step4_retype_email() {
	if (document.step4.email.value == document.step4.retype_email.value) return true;
	else {
		Ext.MessageBox.alert('Warning', 'Please, check your e-mail addresses.', void(0));
		return false;
	}
}

function step4_show_hide_div() {
	if (document.getElementById('want_invoice').checked == true) {
		document.getElementById('want_invoice_div').style.display = '';
		document.step4.company_name.setAttribute("id", "required");
		document.step4.company_address.setAttribute("id", "required");
		document.step4.company_postal_code.setAttribute("id", "required");
		document.step4.company_city.setAttribute("id", "required");
		document.step4.company_tax_number.setAttribute("id", "required");
		document.step4.company_country.setAttribute("id", "required");
	}
	else {
		document.getElementById('want_invoice_div').style.display = 'none';
		document.step4.company_name.setAttribute("id", "");
		document.step4.company_address.setAttribute("id", "");
		document.step4.company_postal_code.setAttribute("id", "");
		document.step4.company_city.setAttribute("id", "");
		document.step4.company_tax_number.setAttribute("id", "");
		document.step4.company_country.setAttribute("id", "");
	}
}

function step4_copy_from_field_to_field() {
	if (document.step4.same_address.checked) {
		document.step4.company_address.value = document.step4.address.value;
		document.step4.company_postal_code.value = document.step4.postal_code.value;
		document.step4.company_city.value = document.step4.city.value;
		document.step4.company_country.value = document.step4.client_country.value;
	}
	else {
		document.step4.company_address.value = '';
		document.step4.company_postal_code.value = '';
		document.step4.company_city.value = '';
		document.step4.company_country.value = '';
	}
}

function step4_agree() {
	if (document.getElementById('i_agree').checked == true) return true;
	else {
		Ext.MessageBox.alert('Warning', 'Please, agree with Terms and Conditions.', void(0));
		return false;
	}
}

function step4_submit() {
	if (validate() && step4_retype_email() && step4_agree()) {
		step4.submit();
		document.getElementById('btn_holder').innerHTML = 'Please wait while loading...';
	}
}

function step4_receive_sms() {
	if (document.getElementById('want_receive_sms').checked == true) {
		document.step4.mobile_sms.setAttribute("id", "required");
		document.step4.country_code_sms.setAttribute("id", "required");
	}
	else {
		document.step4.mobile_sms.setAttribute("id", "");
		document.step4.country_code_sms.setAttribute("id", "");
	}
}

