var dp_cal;
var dp_xml;
var calendarPopUp;
var calendarPopUp2;
var addEndDate;

function init() {
	initTimer();
	if (typeof(EpochPrime) !== 'undefined') {
		calendarPopUp = new EpochPrime($('calendarDate'),dp_xml);
	}
}

function isValidEmail(str) {
	return str.match(new RegExp("^([a-zA-Z0-9_]|\\-|\\.)+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}$"));
}

function toBoolean(inputString){
	if(typeof(inputString)=="string")
		inputString=inputString.toLowerCase();
	switch(inputString){
		case "1":
		case "true":
		case "yes":
		case "y":
		case "on":
		case 1:
		case true:
		return true;
		break;
	default: return false;
	}
}

// Sets cookie values. Expiration date is optional//
function setCookie(name, value, expire) {
	document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

function getCookie(nm) {
	if (document.cookie.length > 0) {
		var re1 = /\s*;\s*/;
		var cooks = document.cookie.split(re1);
		var re2 = /\s*=\s*/;
		for ( i=0; i<cooks.length; i++ ) {
				var parts = cooks[i].split(re2,2);
				if (nm == parts[0]) {
					return unescape(parts[1]);
				}
		}
	}
	return "";
}

function deleteCookie(Name) {
	expireDate = new Date;
	expireDate.setDate(expireDate.getDate()-1);
	document.cookie = Name + "=; expires=" + expireDate.toGMTString();
}

function checkFields() {
	var i, a=checkFields.arguments;
	for(i=0;i<a.length;i++) {
		var obj = $(a[i]);
		if(obj && !obj.value.length) {
			alert("Empty field '"+obj.name+"'");
			return false;
		}
		if (obj && obj.name.match(/(.*?)email(.*?)/i)) {
			if (!isValidEmail(obj.value)) {
				alert("Wrong email format");
				return false;
			}
		}
	}
	return true;
}

function loadEvents(date) {
	$('events').innerHTML = '<img src="/img/progress.gif" width="16" height="16" border="0" alt="" />';
	new Ajax.Request('/utils/ajax_controller',
	{
		method: 'post',
		parameters: {
			m: 'calendar',
			action: 'getDayEvents',
			date: date
		},
		onSuccess: function(transport){
			$('events').innerHTML = transport.responseText.evalJSON();
		},
		onFailure: function() {
			$('events').innerHTML = "";
		}
	});
}

function checkDateFormat(str) {
	if (!str.match(new RegExp("^[0-9]{2}/[0-9]{2}/[0-9]{4}$"))) {
		return false;
	}
	return true;
}

function goBooking() {
	var f = $('bookingForm');
	if (f) {
		$('check_in').value = $('qcheck_in').value;
		$('check_out').value = $('qcheck_out').value;
		f.submit();
	}
}

function qSearchAdjust() {
	var f = $('qsearch');
	if (f) {
		var date1 = $('qcheck_in').value;
		if (!date1.length) {
			alert('Select check in date');
			return false;
		}
		if (!checkDateFormat(date1)) {
			alert('Wrong check in date');
			return false;
		}
		var date2 = $('qcheck_out').value;
		if (!date2.length) {
			alert('Select check out date');
			return false;
		}
		if (!checkDateFormat(date2)) {
			alert('Wrong check out date');
			return false;
		}
		var dt1 = new Date(date1);
		var dt2 = new Date(date2);
		var nights = Math.ceil((dt2 - dt1)/86400000);
		if (nights<2) {
			alert('Min stay is 2 nights');
			return false;
		}
		var now = new Date();
		var tmp1 = Math.ceil((dt1 - now)/86400000);
		var tmp2 = Math.ceil((dt2 - now)/86400000);
		if (tmp1>365 || tmp2>365) {
			alert('Please call the Napili Sunset front desk at (800) 447-9229 \nto book rooms more than 365 days in advance.');
			return false;
		}
		var bedrooms = $('qbedrooms').value;
		var inp = document.createElement('input');
		inp.setAttribute('type','hidden');
		inp.setAttribute('name','check_in');
		inp.setAttribute('value',date1);
		f.appendChild(inp);
		inp = document.createElement('input');
		inp.setAttribute('type','hidden');
		inp.setAttribute('name','check_out');
		inp.setAttribute('value',date2);
		f.appendChild(inp);
		inp = document.createElement('input');
		inp.setAttribute('type','hidden');
		inp.setAttribute('name','bedrooms');
		inp.setAttribute('value',bedrooms);
		f.appendChild(inp);
		setCookie('qcheck_in',date1);
		setCookie('qcheck_out',date2);
		setCookie('qbedrooms',bedrooms);
		f.submit();
	}
}

function booking(id) {
	var check_in, check_out, adults_num, children_num, bedrooms, nights;
	$('prop_id').value = id;
	if ($('check_in'))
		check_in = $('check_in').value;
	else if ($('qcheck_in'))
		check_in = $('qcheck_in').value;
	else if ($('checkin'))
		check_in = $('checkin').value;
	else if (getCookie('check_in'))
		check_in = getCookie('check_in');
	else
		check_in = "";
	if ($('check_out'))
		check_out = $('check_out').value;
	else if ($('qcheck_out'))
		check_out = $('qcheck_out').value;
	else if ($('checkout'))
		check_out = $('checkout').value;
	else if (getCookie('check_out'))
		check_out = getCookie('check_out');
	else
		check_out = "";
	if ($('nights'))
		nights = $('nights').value;
	else if (getCookie('nights'))
		nights = getCookie('nights');
	else
		nights = 2;
	if ($('bedrooms'))
		bedrooms = $('bedrooms').value;
	else if (getCookie('bedrooms'))
		bedrooms = getCookie('bedrooms');
	else
		bedrooms = 1;
	var f = $('bookingForm');
	var inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','check_in');
	inp.setAttribute('value',check_in);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','check_out');
	inp.setAttribute('value',check_out);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','nights');
	inp.setAttribute('value',nights);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','bedrooms');
	inp.setAttribute('value',bedrooms);
	f.appendChild(inp);
	f.submit();
}

function booking2(special_id, prop_id, stype) {
	var check_in;
	var nights;
	$('special_id').value = special_id;
	$('prop_id').value = prop_id;
	if ($('check_in'))
		check_in = $('check_in').value;
	else if ($('checkin'))
		check_in = $('checkin').value;
	else if (getCookie('check_in'))
		check_in = getCookie('check_in');
	else
		check_in = "";
	if ($('check_out'))
		check_out = $('check_out').value;
	else if ($('qcheck_out'))
		check_out = $('qcheck_out').value;
	else if ($('checkout'))
		check_out = $('checkout').value;
	else if (getCookie('check_out'))
		check_out = getCookie('check_out');
	else
		check_out = "";
	if ($('bedrooms'))
		bedrooms = $('bedrooms').value;
	else if (getCookie('bedrooms'))
		bedrooms = getCookie('bedrooms');
	else
		bedrooms = 1;
	var f = $('bookingForm');
	var inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','check_in');
	inp.setAttribute('value',check_in);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','check_out');
	inp.setAttribute('value',check_out);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','bedrooms');
	inp.setAttribute('value',bedrooms);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','s_type');
	inp.setAttribute('value',stype);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','special_id');
	inp.setAttribute('value',special_id);
	f.appendChild(inp);
	inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','prop_id');
	inp.setAttribute('value',prop_id);
	f.appendChild(inp);
	f.submit();
}
function bookingSpecials(id) {
	var f = $('bookingForm');
	var inp = document.createElement('input');
	inp.setAttribute('type','hidden');
	inp.setAttribute('name','special_id');
	inp.setAttribute('value',id);
	f.appendChild(inp);
	f.submit();
}

function cvvHelp() {
	Shadowbox.open({
		content: 'cvv.html',
		type:	'iframe',
		width:	400,
		height:	400
	});
}

function highlightMenuItem(id) {
	if ($(id)) {
		$(id).addClassName('current');
	}
}

function switchEl(id) {
	if ($(id)) {
		if ($(id).style.display=='none') {
			$(id).show();
		} else {
			$(id).hide();
		}
	}
}

function printThis() {
	var a = window.open('','printing','scrollbars=yes,width=700');
	a.document.open("text/html");
	a.document.write('<html><head><link href="/css/styles.css" rel="stylesheet" type="text/css" /><link href="/css/print.css" rel="stylesheet" type="text/css" /></head><body>');
	a.document.write($('print_area').innerHTML);
	a.document.write('</body></html>');
	a.document.close();
	a.print();
	a.close();
}

function recalcExtraGuests() {
	var adults = parseInt($('adults_num').getValue());
	var children = parseInt($('children_num').getValue());
	var nights = parseInt($('nights').getValue());
	var extra = parseInt(adults+children-2);
	var totalRent = parseInt($('totalRent').getValue());
	if (extra<=0) {
		$('extraguests').setValue(0);
		$('extraguestspay').setValue(0);
		$('extra_guests').innerHTML = 0;
		$('extra_guests_pay').innerHTML = 0;
		$('bookingtaxes').innerHTML = formatPrice(totalRent*bookingTaxes/100);
		$('bookingtotal').innerHTML = formatPrice(totalRent+totalRent*bookingTaxes/100);
	} else {
		$('extraguests').setValue(extra);
		$('extra_guests').innerHTML = extra;
		$('extraguestspay').setValue(extra*15);
		$('extra_guests_pay').innerHTML = formatPrice(extra*15*nights);
		$('bookingtaxes').innerHTML = formatPrice((totalRent+extra*15*nights)*bookingTaxes/100);
		$('bookingtotal').innerHTML = formatPrice(totalRent+extra*15*nights+(totalRent+extra*15*nights)*bookingTaxes/100);
	}
}

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
    var x = Math.round(num * Math.pow(10,dec));
    if (x >= 0) n1=n2='';
    var y = (''+Math.abs(x)).split('');
    var z = y.length - dec;
    y.splice(z, 0, pnt);
    while (z > 3) {
        z-=3;
        y.splice(z,0,thou);
    }
    var r = curr1+n1+y.join('')+n2+curr2;
    return r;
}

function formatPrice(num) {return formatNumber(num, 2, ',', '.', '', '', '', '');}

