 var intHit = 1;

 function hit() {
     if (intHit == 1) {
         intHit = 1;
         return true;
     } else {
         return false;
     }
 }


function validate_form(theForm) {
	if(hit())
     {
		var reason = "";

		reason += validateCity(theForm.departure);
		reason += validateCity(theForm.destination);
		reason += validateDates(theForm);
		reason += validatePassengers(theForm);
		if (reason != "") {
			alert("Some fields need correction:\n" + reason);
			return false;
		}

		return true;
	}
  return false;
}

 function validateDates(form) {
	var m1 = form.odate_1.value ;      
	var d1   = form.odate_2.value ;  
	var y1 = form.odate_3.value ;
	var m2 = form.rdate_1.value ;   
	var d2   = form.rdate_2.value ;
	var y2 = form.rdate_3.value ;

	d1 = d1 < 10 ? '0' + d1 : d1;
	d2 = d2 < 10 ? '0' + d2 : d2;
	y1 = y1 < 1000 ? y1 + 1900 : y1;
	y2 = y2 < 1000 ? y2 + 1900 : y2;  

	form.departureDate.value=d1+'/'+m1+'/'+y1; //odate_dep
	form.returnDate.value=d2+'/'+m2+'/'+y2; //rdate_ret

	//alert(form.departureDate.value + ':' + form.returnDate.value);

	var fmt = "%d/%m/%y";
	var d1 = Date.parseDate(form.departureDate.value, fmt);
	var d2 = Date.parseDate(form.returnDate.value, fmt);
	if(d1.getTime() > d2.getTime() && form.oneway[0].checked) { // only for return flights 
		return "Departure date cannot be after Return Date";
	}
	return "";
 }
function validateCity(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a city.\n";
    } /*else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The city code contains illegal characters.\n";
    } */else {
        fld.style.background = 'White';
    }
    return error;
}
function validateMonth(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a city.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 2)) {
        fld.style.background = 'Yellow'; 
        error = "The month is the wrong length.\n";
    } else if (!illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The month contains illegal characters.\n";
    } else {
         if(fld.value.length == 1)
         {
            fld.value = "0" + fld.value;
         }
        fld.style.background = 'White';
    }
    return error;
}
function validateYear(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a year.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 4)) {
        fld.style.background = 'Yellow'; 
        error = "The year is the wrong length.\n";
    } else if (!illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The year contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateDay(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a day.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 2)) {
        fld.style.background = 'Yellow'; 
        error = "The day is the wrong length.\n";
    } else if (!illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The day contains illegal characters.\n";
    } else if (fld.value<1 || fld.value>31) {
        fld.style.background = 'Yellow'; 
        error = "The day contains illegal value.\n";
    } else {
         if(fld.value.length == 1)
         {
            fld.value = "0" + fld.value;
         }
        fld.style.background = 'White';
    }
    return error;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validatePassengers(form) {
    var error = "";
	var ad = parseInt(form.adult.value) ;
	var ch = parseInt(form.child.value);
	var inf = parseInt(form.infant.value);
	var total = ad+  ch + inf;
	if(total==0){
		error = ("Select atleast one passenger");
	}
	if(ad==0 && ((ch+inf)>0)){
		error = ("Atleast one Adult is required");
	}
    return error;
}
