function searchValidation(usdtoinr, curmonth,curyear,d)
{
	var selectedcity = '';
	for (var i=0; i < document.searchhotels.selectedcity.length; i++)		{
		if (document.searchhotels.selectedcity[i].checked)	{
			selectedcity = document.searchhotels.selectedcity[i].value;
		}
	}

	if( selectedcity == '' || selectedcity == null)	{
		alert("Please Select City for Hotels search");
		return false;
	}
	var roomrates = '';
	for (var i=0; i < document.searchhotels.rates.length; i++)		{
		if (document.searchhotels.rates[i].checked)	{
			roomrates = document.searchhotels.rates[i].value;
		}
	}
	var currdate = curmonth + "/" + d + "/" + curyear;
	var todaymillis = Date.parse(currdate);
	var today = new Date(todaymillis);

	var checkin= document.searchhotels.checki.value;
	if( checkin == null || checkin == "" )	{
		alert("Please enter Check in Date");
		return false;
	}
	var inmillis = Date.parse(checkin);
	var checkindate = new Date(inmillis);

	var checkout= document.searchhotels.checko.value;
	if( checkout == null || checkout == "" )	{
		alert("Please enter Check out Date");
		return false;
	}
	var outmillis = Date.parse(checkout);
	var checkoutdate = new Date(outmillis);
	if( validateDateNew(checkindate, checkoutdate, today) == false)	{
		alert("returning false");
		return false;
	}
	
	if( usdtoinr <= 0 && roomrates=="INR")	{
		alert( "Rates are not available in INR at present. Select your currency as USD.");
		return false;
	}
	return true;
	
	
}

function searchValidation2(curmonth,curyear,d,usdtoinr)		{
	
	var month=document.searchhotels.inmonth.value-1;
	var inM = (curmonth + month)%13;
	var inY = curyear;
	if( inM < curmonth )	{
		var inY = curyear+1;
	}
	var inD = document.searchhotels.inday.value;
	addDays(document.searchhotels.inday,  get_days_in_month(inM, inY));
	if( inD > get_days_in_month(inM, inY) )	{
		document.searchhotels.inday.value = 1;
		alert("Select valid dates of the check in month ");
		return false;
	}
	document.searchhotels.inday.value = inD;

	var inDate=new Date();
	inDate.setFullYear(parseInt(inY), parseInt(inM-1),parseInt(inD));
	var outmonth=document.searchhotels.outmonth.value-1;
	var outM = (curmonth + outmonth)%13;
	var outY = curyear;
	if( outM < curmonth )	{
		var outY = curyear+1;
	}
	var outday = document.searchhotels.outday.value;
	addDays(document.searchhotels.outday,  get_days_in_month(outM, outY));
	if( outday > get_days_in_month(outM, outY) )	{
		document.searchhotels.outday.value = 1;
		alert("Select valid dates of the check out month ");
		return false;
	}
	document.searchhotels.outday.value = outday;
	
	outD = document.searchhotels.outday.value;
	var outDate=new Date();
	outDate.setFullYear(parseInt(outY), parseInt(outM-1),parseInt(outD));

	var today = new Date();
	if( inDate < today )	{
		alert("Checkin date has to be higher than today");
		return false;
	}
	if( inDate >= outDate )	{
		alert("Checkout date has to be greater than Checkin Date");
		return false;

	}
	var nights= parseInt( (outDate - inDate)/(60*60*24 * 1000));
	if( nights > 31 )	{
		alert("You cannot book room for more than 31 days in a single booking. Make multiple room bookings instead.");
		return false;
	}
	document.searchhotels.checkindate.value =  inDate.toDateString();
	document.searchhotels.checkoutdate.value =  outDate.toDateString();
	return nights;
	
}


function searchValidationCalender(curmonth,curyear,d,usdtoinr)		{
	
	var month=document.searchhotels.inmonth.value-1;
	var inM = (curmonth + month)%13;
	var inY = curyear;
	if( inM < curmonth )	{
		var inY = curyear+1;
	}
	var inD = document.searchhotels.inday.value;
	addDays(document.searchhotels.inday,  get_days_in_month(inM, inY));
	if( inD > get_days_in_month(inM, inY) )	{
		document.searchhotels.inday.value = 1;
		alert("Select valid dates of the check in month ");
		return false;
	}
	document.searchhotels.inday.value = inD;

	var inDate=new Date();
	inDate.setFullYear(parseInt(inY), parseInt(inM-1),parseInt(inD));
	var outmonth=document.searchhotels.outmonth.value-1;
	var outM = (curmonth + outmonth)%13;
	var outY = curyear;
	if( outM < curmonth )	{
		var outY = curyear+1;
	}
	var outday = document.searchhotels.outday.value;
	addDays(document.searchhotels.outday,  get_days_in_month(outM, outY));
	if( outday > get_days_in_month(outM, outY) )	{
		document.searchhotels.outday.value = 1;
		alert("Select valid dates of the check out month ");
		return false;
	}
	document.searchhotels.outday.value = outday;
	
	outD = document.searchhotels.outday.value;
	var outDate=new Date();
	outDate.setFullYear(parseInt(outY), parseInt(outM-1),parseInt(outD));

	var today = new Date();
	if( inDate < today )	{
		alert("Checkin date has to be higher than today");
		return false;
	}
	if( inDate >= outDate )	{
		alert("Checkout date has to be greater than Checkin Date");
		return false;

	}
	var nights= parseInt( (outDate - inDate)/(60*60*24 * 1000));
	if( nights > 31 )	{
		alert("You cannot book room for more than 31 days in a single booking. Make multiple room bookings instead.");
		return false;
	}
	document.searchhotels.checkindate.value =  inDate.toDateString();
	document.searchhotels.checkoutdate.value =  outDate.toDateString();
	return nights;
	
}



function calcCheckOut(curmonth, curyear)	{
	var month=document.searchhotels.month.value;
	var day = document.searchhotels.day.value;
	var nights = document.searchhotels.nights.value;

	if( nights <= 31 )	{

	}
	else {
		
	}
}

function validateDateNew(checkindate, checkoutdate, today)	{

	if( today >= checkindate)	{
		alert(" Check in date must be higher than today");
		return false;
	}
	if( checkindate >= checkoutdate)	{
		alert(" Checkout date must be higher than checkin date");
		return false;
	}

	var diff =  (checkoutdate - checkindate)/(1000*60*60*24);
	var validnights = 31 - diff;
	if( validnights <  0  )	{
		alert("Sorry, You can book rooms for maximum 31 days only. If you want to book for more days, make a second booking.");
		return false;
	}
	return true;
}

function validateDate(curmonth, curyear)	{

	var month=document.searchhotels.month.value-1;
	var selectedM = (curmonth + month)%12;
	var selectedY = curyear;
	if( selectedM < curmonth )	{
		var selectedY = curyear+1;
	}
	
	var day = document.searchhotels.day.value;
	addDays(document.searchhotels.day,  get_days_in_month(selectedM, selectedY));
	if( day > get_days_in_month(selectedM, selectedY) )	{
		document.searchhotels.day.value = 1;
	}
	document.searchhotels.day.value = day;
	var nights = document.searchhotels.nights.value;
	if( nights > 31 )	{
		return null;
	}
	var adddate = parseInt(day) + parseInt(nights);
	var outDate=new Date();
	outDate.setFullYear(parseInt(selectedY),( parseInt(selectedM)-parseInt(1)),parseInt(adddate));

	var inDate=new Date();
	inDate.setFullYear(parseInt(selectedY),( parseInt(selectedM)-parseInt(1)),parseInt(day));

	var checkout = outDate.toDateString();;//()+"-"+outDate.Month()+"-"+outDate.getYear();
	document.getElementById('checkout').innerHTML= checkout;//(outdate.getDay() + '-' + outDate.getMonth());;

	var today = new Date();
	if( inDate < today )	{
		alert("Checkin date has to be higher than today");
		return null;
	}
	return inDate.toDateString();;

}

function showRoomOccupancy()	{
	var rooms=document.searchhotels.roomcount.value;
	if(rooms >= 2 )	{
		document.getElementById('room2').style.display="";
	}
	else {
		document.getElementById('age2').style.display="none";
		document.getElementById('room2').style.display="none";
	}
	if(rooms >= 3 )	{
		document.getElementById('room3').style.display="";
	}
	else{
		document.getElementById('age3').style.display="none";
		document.getElementById('room3').style.display="none";
	}
	if(rooms >= 4 )	{
		document.getElementById('room4').style.display="";
	}
	else {
		document.getElementById('age4').style.display="none";
		document.getElementById('room4').style.display="none";
	}
}

function showAgeSelection(children, room)	{
	var childCount = children.value;
	if( childCount > 0 )	{
		document.getElementById('age'+room).style.display="";
	}
	else {
		document.getElementById('age'+room).style.display="none";
	}
	for( var i=1;i < 4; i++ )	{
		if( i <= childCount)	{
			document.getElementById('r'+room+'c'+i).style.display="";
		}
		else {
			document.getElementById('r'+room+'c'+i).style.display="none";
		}
	}
}

function get_days_in_month($month, $year)
{
	return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}

function addDays(datefield, numDays) {
	datefield.options.length=0;
	for(i=1; i<=numDays; i++) {
		datefield.options[datefield.options.length]=new Option(i, i);
	}
	if(datefield.options[0].value=="")
		datefield.remove(0);
		
}

function updateSession(hid, rid)	{
alert("inside upfate session");
}


function bookingValidation()	{
	var agreement = document.getElementById('agreement').value;
	if( document.getElementById('agreement').checked == false )	{
		alert("You must agree with Hotel Policies and Cancellation Policies to book a room" );
		return false;
	}
	var firstname = document.booknowconfirmed.firstname.value.split(' ').join('');
	var retval = validateUsername( firstname, "First Name");
	if( retval != "")	{
		alert( retval);
		return false;
	}
	var lastname = document.booknowconfirmed.lastname.value.split(' ').join('');
	var retval = validateUsername( lastname, "Last Name");
	if( retval != "")	{
		alert( retval);
		return false;
	}
	var ccode = document.booknowconfirmed.ccode.value;
	var retval = validatePhone(ccode, "Country Code");
	if( retval != "")	{
		alert( retval);
		return false;
	}
	if( ccode.length > 6 )	{
		alert("Invalid Country Code. It must be less than 5 digits.");
		document.booknowconfirmed.ccode.value = '';
		return false;
	}
	var acode = document.booknowconfirmed.acode.value;
	var retval = validatePhone(acode, "Area Code");
	if( retval != "")	{
		alert( retval);
		return false;
	}
	if( acode.length > 7 )	{
		alert("Invalid Area Code. It must be less than 7 digits.");
		document.booknowconfirmed.acode.value = '';
		return false;
	}

	var pcode = document.booknowconfirmed.pcode.value;
	var retval = validatePhone(pcode, "Phone Number");
	if( retval != "")	{
		alert( retval);
		return false;
	}
	if( pcode.length > 11 )	{
		alert("Invalid Phone number. It must be less than 10 digits.");
		document.booknowconfirmed.pcode.value = '';
		return false;
	}


	var email = document.booknowconfirmed.email.value.split(' ').join('');;
	var retval = validateEmail(email, "Email Address");
	if( retval != "")	{
		alert( retval);
		return false;
	}

	var address = document.booknowconfirmed.address.value.split(' ').join('');
	var retval = validateEmpty(address, "Address");
	if( retval != "")	{
		alert( retval);
		return false;
	}
	if( address.length > 128 )	{
		alert("Address must be less than 128 characters.");
		document.booknowconfirmed.address.value = '';
		return false;
	}

	var area = document.booknowconfirmed.area.value.split(' ').join('');
	var retval = validateEmpty(area, "Area");
	if( retval != "")	{
		alert( retval);
		return false;
	}

	if( area.length > 33 )	{
		alert("Address must be less than 33 characters.");
		document.booknowconfirmed.area.value = '';
		return false;
	}

	var city = document.booknowconfirmed.city.value.split(' ').join('');
	var retval = validateEmpty(city, "City");
	if( retval != "")	{
		alert( retval);
		return false;
	}

	var state = document.booknowconfirmed.state.value;
	var retval = validateEmpty(state, "State");
	if( retval != "")	{
		alert( retval);
		return false;
	}

	var pincode = document.booknowconfirmed.pincode.value;
	var retval = validatePhone(pincode, "Pin Code");
	if( retval != "")	{
		alert( retval);
		return false;
	}
	if( pincode.length > 11 )	{
		alert("Address must be less than 10 characters.");
		document.booknowconfirmed.pincode.value = '';
		return false;
	}

	var country = document.booknowconfirmed.country.value;
	return true;
	
}



// form validation functions below


function validateEmpty(fld, message) {
    var error = "";
 
    if (fld == null) {
        //fld.style.background = 'Yellow';
        error = "Please enter " +  message + ".\n";
    }
	else if (fld.length == 0) {
       // fld.style.background = 'Yellow'; 
        error = "Please enter " + message + ".\n"
    } else {
       //fld.style.background = 'White';
    }
    return error;  
}

function validateEmail(fld, message) {
    var error="";
    //var tfld = trim(fld);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld == null) {
        //fld.style.background = 'Yellow';
        error = "Please enter your email address.\n";
    }
	else if (fld == "") {
        //fld.style.background = 'Yellow';
        error = "Please enter your email address.\n";
    } else if (!emailFilter.test(fld)) {              //test email for illegal characters
        //fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.match(illegalChars)) {
       // fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
       // fld.style.background = 'White';
    }
    return error;
}
// If the element's string matches the regular expression it is all letters
function validateUsername(fld, message) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld == null) {
        error = "Please enter your " + message + ".\n";
    }
	else if (fld == "") {
        error = "Please enter "+ message+".\n";
    } else if ((fld.length < 2) || (fld.length > 15)) {
        error = "The " + message + " is the wrong length.\n";
    } else if (illegalChars.test(fld)) {
        error = "The "+ message + " contains illegal characters.\n";
    } else {
    }
    return error;
}
function validatePhone(fld, message) {
    var error = "";
    var stripped = fld.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld == null) {
        error = "Please enter " +  message + ".\n";
   }
   else  if (fld == "") {
        error = "Please Enter "+ message + "\n";
   } else if (isNaN((stripped))) {
        error = "The "+ message + " must contain Numerals only.\n";
    } 

    return error;
}

function googlesearch()
{
	if(document.getElementById('q').value=="")
	{
		alert("Please enter search text");
		document.getElementById('q').focus();
		return false;
	}
}

function getHTTPObject()
{
	if (window.ActiveXObject) 
	return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
	return new XMLHttpRequest();
	else {
	alert("Your browser does not support AJAX.");
	return null;
	}
}

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

function showHotelInfo(hotelid, divid, whattoshow)	{
	var url= getBaseURL() + "includes/hoteldescriptions.php?hotelid="+hotelid+"&show="+whattoshow+"&divid="+divid;
	var httpObject1 = getHTTPObject();
	var open = httpObject1.open("GET", url, true);
	document.getElementById("hotelcontent"+divid).style.display="block";
	document.getElementById("hotelcontent"+divid).innerHTML = " <center>Fetching information. Wait for a second.. </center>";
	httpObject1.onreadystatechange = function()		{
		if(httpObject1.readyState == 4)	{  
			document.getElementById("hotelcontent"+divid).style.display="block";
			document.getElementById("showhide"+divid).style.display="block";
			//alert( httpObject1.responseText.tostring);
			if(httpObject1.responseText!=""){
				document.getElementById("hotelcontent"+divid).innerHTML=httpObject1.responseText;			
			}
			else
				document.getElementById("hotelcontent"+divid).style.display="block";
		}
	}
	httpObject1.send(null);
}

function hideHotelInfo(hotelid, divid, whattoshow)	{ 
	document.getElementById("hotelcontent"+divid).style.display="none";
	document.getElementById("showhide"+divid).style.display="none";
}

function hotelListingPageChanged(page, isonline)	{
	if( isonline == 0 )	{
		var url="templates/common/allhotelslisting.php?pageid="+page;
	}
	else {
		var url="includes/searchhotelsnewupdate.php?pageid="+page;
	}
	var httpObject1 = getHTTPObject();
	httpObject1.open("GET", url, true);
	httpObject1.onreadystatechange = function()		{
		if(httpObject1.readyState == 4)	{  
			document.getElementById("listing").style.display="block";
			if(httpObject1.responseText!=""){
				document.getElementById("listing").innerHTML=httpObject1.responseText;	
				
			}
			else
				document.getElementById("listing").style.display="block";
		}
	}
	httpObject1.send(null);
}


function showhotel(value,isonline,hotelid, hotelname)	{
	var baseurl = getBaseURL();
	var urlref = new String ( location.href);
	//if( urlref.indexOf('searchhotels') != -1 )	{
		if( isonline == 0 )	{
			var url=baseurl + "templates/common/allhotelslisting.php?pageid="+page;
		}
		else {
			var url=baseurl +"includes/searchhotelsnewupdate.php?hotelindex="+value;
		}
		var httpObject1 = getHTTPObject();
		httpObject1.open("GET", url, true);
		httpObject1.onreadystatechange = function()		{
			if(httpObject1.readyState == 4)	{  
				document.getElementById("listing").style.display="block";
				if(httpObject1.responseText!=""){
					document.getElementById("listing").innerHTML=httpObject1.responseText;	
				}
				else	{
					document.getElementById("listing").style.display="block";
				}
			}
		}
		httpObject1.send(null);
		/*var x=document.getElementsByName("cbox");
		for( var i=0; i <= x.length; i++ )	{
			if( i == value )	{
				document.getElementById('checkbox'+i).checked = true;
			}
			else {
				document.getElementById('checkbox'+i).checked = false;
			}
		}*/
	/*}
	else {
		if( isonline == 0 )	{
			var url="templates/common/allhotelslisting.php?pageid="+page;
		}
		else {
			var url=baseurl + hotelid + "/" + hotelname + ".html";
		}
		window.location = baseurl + hotelid + "/" + hotelname + ".html";
	}*/
	
}


function showallhotels()	{
	var baseurl = getBaseURL();
	var isonline = 1;
	if( isonline == 0 )	{
		var url= baseurl + "templates/common/allhotelslisting.php?pageid="+page;
	}
	else {
		var url= baseurl +"includes/searchhotelsnewupdate.php";
	}
	var httpObject1 = getHTTPObject();
	httpObject1.open("GET", url, true);
	httpObject1.onreadystatechange = function()		{
		if(httpObject1.readyState == 4)	{  
			document.getElementById("listing").style.display="block";
			if(httpObject1.responseText!=""){
				document.getElementById("listing").innerHTML=httpObject1.responseText;	
				
			}
			else
				document.getElementById("listing").style.display="block";
		}
	}
	httpObject1.send(null);
	var x=document.getElementsByName("cbox");
	for( var i=0; i <= x.length; i++ )	{
		document.getElementById('checkbox'+i).checked = true;
	}
}


function has_id(id)		{
	try{
		var tmp=document.getElementById(id).value;
	}catch(e){
		return false;
	}
	return true;
}


function booknow(roomtype,hindex,rindex)	{
	alert(" inside book now");
	var baseurl = getBaseURL();
	var url= baseurl + "booknow/" + roomtype + "/" + hindex + "/" + rindex;
	alert( url);
	window.location = url;
	/*var httpObject1 = getHTTPObject();
	httpObject1.open("GET", url, true);
	httpObject1.onreadystatechange = function()		{
		if(httpObject1.readyState == 4)	{  
			document.getElementById("listing").style.display="block";
			if(httpObject1.responseText!=""){
				document.getElementById("listing").innerHTML=httpObject1.responseText;	
				
			}
			else
				document.getElementById("listing").style.display="block";
		}
	}
	httpObject1.send(null);
	var x=document.getElementsByName("cbox");
	for( var i=0; i <= x.length; i++ )	{
		document.getElementById('checkbox'+i).checked = true;
	}*/
}
