/****************** MACROMEDIA FUNCTIONS FROM PREVIOUS VERSION ***********************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(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])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}




/****************** UPDATED JAVASCRIPT STARTS ***********************/

function pop(url,w,h) {
	var popWin = window.open(url,'','width='+w+',height='+h+',location=no,menubar=no,scrollbars=no,toolbar=no,resizable=no');
	//popWin.moveTo((window.screen.availWidth -  800) / 2, (window.screen.availHeight - 600) / 2);
	popWin.focus();
}

/******************* VALIDATE FUNCTIONS START *************************/

// ADVANCED SEARCH VALIDATION
function validateAdvSearch(theForm) 
{
	
	if (theForm.do_search.value == 0) 
	{
		alert("There are no properties matching this category");
		theForm.cat.focus();
		return false;
	}
	
	/*
	if (theForm.cat.value == '') 
	{
		alert("Please select category");
		theForm.cat.focus();
		return false;	
	}
	*/
	
	var arriving = theForm.a_date.value;
	var arr_date = arriving.split("-");
	a_day = strip_zero(arr_date[0]);
	a_month = strip_zero(arr_date[1]);
	a_year = arr_date[2];
	
	var departing = theForm.d_date.value;
	var dep_date = departing.split("-");
	d_day = strip_zero(dep_date[0]);
	d_month = strip_zero(dep_date[1]);
	d_year = dep_date[2];
	
	if (!check_dates_search(a_day,a_month,a_year,d_day,d_month,d_year)) {
		return false;
	}
	
	return true;
}

function strip_zero(str) {
	return ( str.indexOf('0') == 0 ) ? str.replace('0','') : str;
}

// HOME PAGE SEARCH VALIDATION
function validateHomeSearch(theForm) {
	
	if (theForm.do_search.value == 0) {
		alert("There are no properties matching this category");
		theForm.cat.focus();
		return false;
	}
	
	if (theForm.cat.value == '') {
		alert("Please select category");
		theForm.cat.focus();
		return false;	
	}
	/*
	if (theForm.area.value == '') {
		alert("Please select an area");
		theForm.area.focus();
		return false;	
	}
	*/
	return true;
}


// BOOKING FORM VALIDATION
function validateBooking(theForm) {
	
	if (theForm.firstname.value == '') {
		alert("Please enter your first name");
		theForm.firstname.focus();
		return false;
	}
	if (theForm.surname.value == '') {
		alert("Please enter your surname");
		theForm.surname.focus();
		return false;
	}
	
	if (theForm.email.value == '' && theForm.tel.value == '') {
		alert("Please enter your email address or/and your telephone number");
		theForm.email.focus();
		return false;
	}
	
	if (theForm.email.value != '') {
		
		if (!validEmail(theForm.email.value)) {
			alert("Please enter a valid email address");
			theForm.email.focus();
			return false;
		}
		if (!validEmail(theForm.conf_email.value)) {
			alert("Please enter a valid email address");
			theForm.conf_email.focus();
			return false;
		}
		
		if (theForm.email.value != theForm.conf_email.value) {
			alert("Emails do not match, please check");
			theForm.email.focus();
			return false;
		}
	}

	if (theForm.tel.value != '') {
		
		if (!validNum(theForm.tel.value)) {
			alert("Please enter a valid telephone number");
			theForm.tel.focus();
			return false;
		}
		if (theForm.tel_int.value == '' || !validNum(theForm.tel_int.value)) {
			alert("Please enter your international dialing code (eg. +27 - digits only)");
			theForm.tel_int.focus();
			return false;
		}
		if (theForm.tel_loc.value == '' || !validNum(theForm.tel_loc.value)) {
			alert("Please enter your local telephone dialing code (eg. 21 - digits only)");
			theForm.tel_loc.focus();
			return false;
		}
	}
	
	if (theForm.pref_price.value == '') {
		alert("Please select your price range");
		theForm.pref_price.focus();
		return false;
	}
	
	if (theForm.rooms.value == '') {
		alert("Please select number of rooms");
		theForm.rooms.focus();
		return false;
	}
	
	if (theForm.guests.value == '') {
		alert("Please select number of guests");
		theForm.guests.focus();
		return false;
	}
	
	if (theForm.country.value == '') {
		alert("Please select your country");
		theForm.country.focus();
		return false;
	}
	
	return true;
}


function y2k(year) { 
	return (year < 1000) ?year + 1900 : year; 
}

function check_dates_search(a_d,a_m,a_y,d_d,d_m,d_y) {
	a_m = a_m - 1;
	d_m = d_m - 1;

	var sys_date = new Date();
	var today = new Date(y2k(sys_date.getYear()),sys_date.getMonth(),sys_date.getDate());
	var arrival_date = new Date(a_y,a_m,a_d);
	var departure_date = new Date(d_y,d_m,d_d);
	
	if (arrival_date < today) {
		alert("Your arrival date is before today");
		return false;
	}
	if (departure_date < today) {
		alert("Your departure is before today");
		return false;
	}
	if (departure_date < arrival_date) {
		alert("Your departure date is before arrival date");
		return false;
	}
	if (arrival_date+'' == departure_date+'') {
		alert("Arrival and departure dates are the same");
		return false;
	}
	
	return true;
}


function validNum(telno) {
	var pattern = "0123456789+-)( ";
	var i = 0;
	
	do {
		var pos = 0;
		for (var j=0;j<pattern.length;j++)
			if (telno.charAt(i)==pattern.charAt(j))
				pos = 1;
		i++;
	}
	while (pos==1 && i<telno.length)
	
	if (pos==0) return false;
	return true;
}


function validEmail(addr){
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=addr.match(emailPat)
	
	if (matchArray==null) {
		//alert("Please enter a valid email address (check @ and .'s)")
		return false
	}
	
	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null) {
		//alert("The username doesn't seem to be valid.")
		return false
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Destination IP address is invalid!")
				return false
			}
		}
		return true
	}
	
	var domainArray=domain.match(domainPat)
	
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		//alert("The address must end in a three-letter domain, or two letter country.")
		return false
	}
	if (len<2) {
		//alert("This address is missing a hostname!")
		return false
	}
	return true;
}


/******************* VALIDATE FUNCTIONS END *************************/