$(document).ready(function(){

	// Highlight Input Fields on Focus
	
	$("input:text, input:password, textarea, select").focus(function(){
		$(this).addClass("field-highlighted");
		$(this).parent().addClass("field-highlighted-holder");
	});
	
	// Remove Highlighting on Input Fields on Blur
	
	$("input:text, input:password, textarea, select").blur(function(){
		$(this).removeClass("field-highlighted");
		$(this).parent().removeClass("field-highlighted-holder");
	});

	// Confirm reset of a Form
	
	$(".form-reset").click(function(){
		return confirm('Are you sure you wish to reset this form?\nAny Information you have entered will be lost.');
	});

	// Confirm a deletion
	$(".confirm-del").click(function(){
		return confirm("Are you sure you wish to delete this "+$(this).attr("rel")+"?\nThis process is not reversible!");
	});
	
	// Returns to page in rel attribute
	
	$(".back-btn").click(function(){
		if ($(this).attr("rel") != '') {
			window.location = $(this).attr("rel");
		}
	});

	$(".del-photo").bind('click', function() {
		
		var current_row = $(this);
		
		if (confirm('Are you sure you wish to delete this photo?\nThis process is not reversible!')) {
			$.post("ajax/ajax.del_photo.php",{
    			image_id: $(this).attr("rel")
	  		},function(xml){
				current_row.parents('tr').remove();
	  		});			
		}
				return false;
	});

	$(".print-link").click(function(){
		window.print();
	});

	// Show/Hide Optional Fields in a Form
	
	$("#toggle_optional_fields_link").click(function(){
		$(".optional").toggle();
		if ($("#toggle_optional_fields_link").html() == 'Hide Optional Fields') {
			$("#toggle_optional_fields_link").html('Show Optional Fields');
		} else {
			$("#toggle_optional_fields_link").html('Hide Optional Fields');		
		}
		return false;
	});

});

function getNights(from_date, to_date, resultObj) {

	try {
	var from_parts = from_date.split("/");
	var to_parts   = to_date.split("/");

	var from = new Date(Number(from_parts[2]), Number(from_parts[1])-1, Number(from_parts[0]));
	var to   = new Date(Number(to_parts[2]), Number(to_parts[1])-1, Number(to_parts[0]));
	if (from > to) { alert(wrongDateMsg); return false; }
	var one_day=1000*60*60*24
	var nights = (Math.round((to.getTime()-from.getTime())/(one_day)));
	//Calculate difference btw the two dates, and convert to days
		if (!isNaN(nights)) {
			document.getElementById(resultObj).value = nights;
		}
	} catch(e) {
		
	}
}

function spawnWindow(url, window_name) {
	window.open(url, '', config='width=700, height=520, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}


function reject(reject_id) {
	if (confirm("Are you sure you wish to reject this request?\nThis process is not reversible!")) {
		window.location = 'request_reject.php?request_id='+reject_id;		
	}
	return false;
}

function approve(approve_id, currency) {
	if (currency == 'pound') { var curr = 'IN POUNDS'; } else { var curr = 'IN EUROS'; }
	
	if (confirm("Are you sure you wish to approve this request?")) {
		var total = prompt('What is the total cost for the stay? ('+curr+')');
		var deposit = prompt('What is the desposit required? ('+curr+')')
		window.location = 'request_approve.php?request_id='+approve_id+'&total='+escape(total)+'&deposit='+escape(deposit);		
	}
	return false;
}


function approveOffer(approve_id, currency) {
	if (currency == 'pound') { var curr = 'IN POUNDS'; } else { var curr = 'IN EUROS'; }
	
	if (confirm("Are you sure you wish to approve this request?")) {
		var total = prompt('What is the total cost for the stay? ('+curr+')');
		var deposit = prompt('What is the desposit required? ('+curr+')')
		window.location = 'request_approve.php?request_id='+approve_id+'&total='+escape(total)+'&deposit='+escape(deposit);		
	}
	return false;
}