function validateFly() {
	var mark = true;
	/*
	if(!checkInput('date1x','ErrorarrivalDay')) {
		mark = false;
	}
	if(!checkInput('date2x','ErrordepartureDay')) {
		mark = false;
	}*/
	if(!checkInput('from','ErrorFrom')){
		mark = false;
	}
	if(!checkInput('to','ErrorTo')){
		mark = false;
	}
	if(!checkInput('telephone','ErrorTel')) {
		mark = false;
	}
	if(!checkInput('country','ErrorCountry')) {
		mark = false;
	}
	if(!checkEmail()) {
		mark = false;
	}
	if(!checkInput('last_name','ErrorLastName')) {
		mark = false;
	}
	if(!checkInput('first_name','ErrorFirstName')) {
		mark = false;
	}		
	if(!checkday('date1x','ErrorarrivalDay')) {
		mark = false;
	}
	if(!checkday2('date2x','ErrordepartureDay')) {
		mark = false;
	}
	if(mark == true) {
		checkCodeFlySubmit();	
		//document.flyBookingForm.submit();
	}	
}


function checkday(id,error) {
	valueInput = document.getElementById(id).value;
	//var month = parseInt(valueInput.substring(1,3));
	var day = valueInput.substring(0,2);
	var month = parseInt(valueInput.substring(3,5));
	var year = parseInt(valueInput.substring(6,10));
	var todalday =daysInMonth(month,year);
	var d = new Date();
	var currentY = parseInt(d.getFullYear());
	var currentD = parseInt(d.getDate()+1);
	var currentM = parseInt(d.getMonth()+1);	
	//alert(month+"\n"+day+"\n"+year+"\n"+currentD+"\n"+currentM+"\n"+currentY+"\n");
	
	if(!isNaN(day) && !isNaN(month) && !isNaN(year) && (todalday>=day) && (12>=month) ) {
		if(year<currentY) {
			document.getElementById(error).innerHTML = "Invalid date!";
			return false;
		} else if(year == currentY) {
			//alert(month+"\n"+currentM);
			if(month < currentM) {
				document.getElementById(error).innerHTML = "Invalid date!";
				return false;
			} else if(month == currentM) {
				//alert(day < currentD);
				if(day < currentD) {
					document.getElementById(error).innerHTML = "Invalid date!";
					return false;
				} else {
					document.getElementById(error).innerHTML = "";
					return true;
				}
			} else {
				document.getElementById(error).innerHTML = "";
				return true;	
			}
		} else {
			document.getElementById(error).innerHTML = "";
			return true;
		}
		
	} else {
		document.getElementById(error).innerHTML = "Invalid date!";
		return false;
	}
}

function checkday2(id,error) {
	valueInput = document.getElementById(id).value;
	//var month = parseInt(valueInput.substring(1,3));
	var day = valueInput.substring(0,2);
	var month = parseInt(valueInput.substring(3,5));
	var year = parseInt(valueInput.substring(6,10));
	var todalday =daysInMonth(month,year);
	var d = new Date();
	var currentY = parseInt(d.getFullYear());
	var currentD = parseInt(d.getDate()+1);
	var currentM = parseInt(d.getMonth()+1);	
	//alert(month+"\n"+day+"\n"+year+"\n"+currentD+"\n"+currentM+"\n"+currentY+"\n");
	
	if(!isNaN(day) && !isNaN(month) && !isNaN(year) && (todalday>=day) && (12>=month) ) {
		if(year<currentY) {
			document.getElementById(error).innerHTML = "Invalid date!";
			return false;
		} else if(year == currentY) {
			//alert(month+"\n"+currentM);
			if(month < currentM) {
				document.getElementById(error).innerHTML = "Invalid date!";
				return false;
			} else if(month == currentM) {
				//alert(day < currentD);
				if(day < currentD) {
					document.getElementById(error).innerHTML = "Invalid date!";
					return false;
				} else {
					document.getElementById(error).innerHTML = "";
					return true;
				}
			} else {
				document.getElementById(error).innerHTML = "";
				return true;	
			}
		} else {
			document.getElementById(error).innerHTML = "";
			return true;
		}
		
	} else {
		document.getElementById(error).innerHTML = "Invalid date!";
		return false;
	}
}

function Trim(STRING){
STRING = LTrim(STRING);
return RTrim(STRING);
}

function RTrim(STRING){
while(STRING.charAt((STRING.length -1))==" "){
STRING = STRING.substring(0,STRING.length-1);
}
return STRING;
}


function LTrim(STRING){
while(STRING.charAt(0)==" "){
STRING = STRING.replace(STRING.charAt(0),"");
}
return STRING;
}

function checkInput(id,errorid) {	
	valueInput = Trim(document.getElementById(id).value);
	if(valueInput == "") {
		document.getElementById(errorid).innerHTML = "Please enter!";
		if(id != "arrivalDay" && id != "departureDay") {
			document.getElementById(id).focus();
		}
		return false;
	} else {
		document.getElementById(errorid).innerHTML = "";
		return true;
	}
}

function checkEmail() {	
	valueInput = Trim(document.getElementById("email").value);			
	if(valueInput == "" || validateEmailv2(valueInput) == false) {
		document.getElementById("ErrorEmail").innerHTML = "Invalid email!";		
		return false;
	} else {
		document.getElementById("ErrorEmail").innerHTML = "";
		return true;
	}
}

var xmlhttp;
xmlhttp=GetXmlHttpObject();
function checkCode() {
	var code = Trim(document.getElementById("codeInput").value);
	if(code == "") {
		document.getElementById("ErrorCode").innerHTML="Please enter!"
		document.getElementById("ErrorCode").focus();
	} else {		
		var url="http://www.catcityholidays.com/html/checkcode.php";
		url=url+"?code="+code;		
		xmlhttp.onreadystatechange=stateChanged;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function stateChanged() {
	if (xmlhttp.readyState==4) {
		if(xmlhttp.responseText != 1) {
			document.getElementById("ErrorCode").innerHTML="Please enter!"
			document.getElementById("ErrorCode").focus();
		} else {
			document.getElementById("ErrorCode").innerHTML= "";
		}		
	}
}
function checkCodeSubmit() {
	var code = Trim(document.getElementById("codeInput").value);
	if(code == "") {
		document.getElementById("ErrorCode").innerHTML="Please enter!"
		document.getElementById("ErrorCode").focus();
	} else {			
		var url="http://www.catcityholidays.com/html/checkcode.php";
		url=url+"?code="+code;				
		xmlhttp.onreadystatechange=stateChangedSubmit();
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function stateChangedSubmit() {	
	if (xmlhttp.readyState==4) {
		if(xmlhttp.responseText != 1) {
			document.getElementById("ErrorCode").innerHTML="Please enter!"
			document.getElementById("ErrorCode").focus();
		} else {			
			document.getElementById("ErrorCode").innerHTML= "";			
			document.bookingForm.submit();			
		}		
	}
}
function checkCodeFlySubmit() {
	var code = document.getElementById("codeInput").value;
	if(code == "") {
		document.getElementById("ErrorCode").innerHTML="Please enter!"
		document.getElementById("ErrorCode").focus();
	} else {			
		var url="http://www.catcityholidays.com/html/checkcode.php";
		url=url+"?code="+code;				
		xmlhttp.onreadystatechange=stateChangedFlySubmit();
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function stateChangedFlySubmit() {	
	if (xmlhttp.readyState==4) {
		if(xmlhttp.responseText != 1) {
			document.getElementById("ErrorCode").innerHTML="Please enter!"
			document.getElementById("ErrorCode").focus();
		} else {			
			document.getElementById("ErrorCode").innerHTML= "";			
			document.flyBookingForm.submit();			
		}		
	}
}
function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return null;
}
function resetForm() {
	document.flyBookingForm.first_name.value = "";
	document.flyBookingForm.last_name.value = "";
	document.flyBookingForm.email.value = "";
	document.flyBookingForm.country.value = "";
	document.flyBookingForm.telephone.value = "";
	document.flyBookingForm.from.value = "";
	document.flyBookingForm.to.value = "";
	document.flyBookingForm.arrivalMonthYear.selectedIndex = 0;
	document.flyBookingForm.departureMonthYear.selectedIndex = 0;
	document.flyBookingForm.arrivalDay.selectedIndex = 0;
	document.flyBookingForm.departureDay.selectedIndex = 0;
	document.flyBookingForm.noOfAdult.selectedIndex = 0;
	document.flyBookingForm.noOfChildren.selectedIndex = 0;
	document.flyBookingForm.left2years.selectedIndex = 0;
	document.flyBookingForm.flyclass.selectedIndex = 0;
	document.flyBookingForm.comment.value = "";	
}
function writeSource(div) {
if (!document.getElementById) { return; }
var o = document.getElementById(div);
if (typeof(o) == "undefined" || o==null) { return; }
var s = o.innerHTML;
if (s==null || s.length==0) { 
	return;
	}
else {
	var i;
	for(i=0;s.charAt(i)==" "||s.charAt(i)=="\n"||s.charAt(i)=="\r"||s.charAt(i)=="\t";i++) {}
	s = s.substring(i);
	for (i = s.length; i>0; i--) {
		if (s.charAt(i)=="<") {
			s = s.substring(0,i) + "&lt;" + s.substring(i+1) ;
			}
		}
	for (i = s.length; i>0; i--) {
		if (s.charAt(i)==">") {
			s = s.substring(0,i) + "&gt;" + s.substring(i+1) ;
			}
		}
	for (i = s.length; i>0; i--) {
		if (s.charAt(i)=="\t") {
			s = s.substring(0,i) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + s.substring(i+1) ;
			}
		}
	for (i = s.length; i>0; i--) {
		if (s.charAt(i)=="\n") {
			s = s.substring(0,i) + "<BR>" + s.substring(i+1) ;
			}
		}
	s = s + "<BR>";
	}
document.write('<A STYLE="font-family:arial; font-size:x-small; text-decoration:none;" HREF="#" onClick="var d=document.getElementById(\'jssource'+div+'\').style; if(d.display==\'block\'){d.display=\'none\';this.innerText=\'+ Show Source\';}else{d.display=\'block\';this.innerText=\'- Hide Source\';} return false;">+ Show Source</A><BR>');
document.write('<SPAN ID="jssource'+div+'" STYLE="display:none;background-color:#EEEEEE"><TT>'+s+'</TT></SPAN>');
}

function daysInMonth(month,year) {
    return new Date(year, month, 0).getDate();
}
function clearn_input(id) {
	var nameInput = document.getElementById(id);
	if(nameInput.value == "dd/mm/yyyy") {
		nameInput.value = "";
	}
}
