/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield){
	var today=new Date()
	var dayfield=document.getElementById(dayfield)
	var monthfield=document.getElementById(monthfield)
	var yearfield=document.getElementById(yearfield)
	for (var i=1; i<32; i++)
		dayfield.options[i]=new Option(i, i)
		dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
	for (var m=0; m<12; m++)
		monthfield.options[m]=new Option(monthtext[m], m+1)
		monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], today.getMonth()+1, true, true) //select today's month
	var thisyear=today.getFullYear()
	for (var y=0; y<5; y++){
		yearfield.options[y]=new Option(thisyear, thisyear)
		thisyear+=1
	}
	yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}

function validEmail(email) {
	invalidChars = " /:,;"
	if (email == "") {
	    return false
	}
	
	for (i=0; i<invalidChars.length; i++) {
	    badChar = invalidChars.charAt(i)
	    if (email.indexOf (badChar,0) != -1) {
	        return false
	    }
	}  // for (i=0.. 
	
	atPos = email.indexOf ("@",1)
	
	if (atPos == -1) {
	    return false
	}
	
	if ( email.indexOf ("@", atPos+1) != -1) {
	    return false
	}
	periodPos = email.indexOf (".", atPos)
	
	if ( periodPos == -1) {
	    return false
	}
	
	if ( periodPos+3 > email.length)    {
	    return false
	}
	return true
}  // validEmail 


function validForm(form) {
	var message;
	message = '';
	if(!validEmail(form.email.value) || form.email.value != form.email2.value) {
		message += "Please check email address and make sure the confirmed email address is correct\n";
	}
	
	if(form.fname.value == '' || form.lname.value == '') {
		message += "Please specify your full name\n";
	}
	
	if(message != '') {
		alert(message);
		return false;
	} else {
		return true;
	}	
}

function validReservation(form) {
	var message;
	var datemessage;
	var validDate;
	message 	= '';
	datemessage	= 'check out is earlier or same than check in date';
	if(form.country.value == 0 || form.country.value == '0') {
		message += "Please input country\n";
	}
	
	if(!validEmail(form.email.value)) {
		message += "Please check email address\n";
	}
	
	if(form.fname.value == '' || form.lname.value == '') {
		message += "Please specify your full name\n";
	}

	validDate = false;
	var myDate=new Date();
	var inDate=new Date();
	var outDate=new Date();
	dayin 	= parseInt(form.dayin.value);
	monthin = parseInt(form.monthin.value);
	yearin 	= parseInt(form.yearin.value);
	
	dayout 	= parseInt(form.dayout.value);
	monthout = parseInt(form.monthout.value);
	yearout	= parseInt(form.yearout.value);

	inDate.setFullYear(yearin,(monthin-1),dayin);
	outDate.setFullYear(yearout,(monthout-1),dayout);
	if(inDate < outDate && myDate < inDate) {
		validDate = true;
	}

	if(!validDate) {
		message += datemessage;
	}
	
	if(message != '') {
		alert(message);
		return false;
	} else {
		form.action = "http://www.webperspective.hk/enquiry.php";
		return true;
	}
}

function validReview(form) {
	var message;
	message = '';

	if(!validEmail(form.email.value)) {
		message += "Please check email address\n";
	}
	
	if(form.fname.value == '' || form.lname.value == '') {
		message += "Please specify your full name\n";
	}
	if(form.heading.value == '') {
		message += "Please specify your review heading\n";
	}
	if(form.comments.value == '') {
		message += "Please insert your review\n";
	}
	
	if(message != '') {
		alert(message);
		return false;
	} else {
		form.action = "http://www.webperspective.hk/v1/review_confirm.php";
		window.open('http://www.webperspective.hk/v1/review_confirm.php','reviewWin','resizable=yes,scrollbars=yes,width=400,height=400');
		//return true;
		return true;
	}
	
}

function PopupImage(titre,img)
{
	w=open("",'image','width=500,height=315,toolbar=no,scrollbars=no,resizable=yes');	
	w.document.write("<html><head><title>"+titre+"</title></head>");
	w.document.write('<script type="text/javascript" language="JavaScript">function checksize()  { if (document.images[0].complete) {  window.resizeTo(document.images[0].width+30,document.images[0].height+60); window.focus();} else { setTimeout("check()",250) } }</'+'script>');
	w.document.write('<body onload="checksize()" leftMargin="0" topMargin="0" marginwidth="0" marginheight="0"><center><img src="'+img+'" alt="" border="0" /></center>');
	w.document.write("");
	w.document.write("</body></html>");
	w.document.close();
}

function createSelections(f,s,a,id) {
	var form 		= document.forms[f];
	var dropdown 	= form.elements[s];
	var l = a[id].length;
	dropdown.options.length = l;
	dropdown.selectedIndex = 0;
	if(l == 0) {
		dropdown.options[0] = new Option('Unfortunately no options', 0);
	} else {
		for (i = 0; i < l; i++) {
			dropdown.options[0] = new Option('Please choose', 0);
			var information = a[id][i].split(":");
			var feltvalue = information[0];
			var feltoption = information[1];
			dropdown.options[i + 1] = new Option(feltoption, feltvalue);
		}
	}
	dropdown.selectedIndex = 0;
}


function makeSelection(f,s,id) {
	var form 		= document.forms[f];
	var dropdown	= form.elements[s]
	for(i = 0 ; i < dropdown.options.length ; i++) {
		if(dropdown.options[i].value == id) {
			dropdown.selectedIndex = i;
		}
	}
}