/**
 * Copyright (c) 2005 Expotel Hotel Reservations Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Expotel Hotel Reservations Ltd. Redistribution and use in source
 * and binary forms, with or without modification, are not permitted
 * without the knowledge of Expotel Hotel Reservations Ltd. The code
 * is developed by California software company.
 *
 * FileName        EventSearch.js
 * Version         0.1   
 * Author Name     K Thiruthuvadas
 * MailId          thiruthuvadask@calsoft.co.in
 * Created on      
 *
 *  Date           Modified by                  Description
 *
 *
 */
/**
 * @see Package Definition.
 * 
 * @see package com.calsoft.ews.bean.search
 * 
 * @since version 0.1
 *  
 */
	var tmpDayEvt = "";
	var tmpMonthEvt = "";
	var tmpYearEvt = "";
 
	var tmpDayEvtar = "";
	var tmpMonthEvtar = "";
	var tmpYearEvtar = "";

//Function to Hide the select Menu's that gets displayed though the Date Picker value is displayed
//This function is introduced as the Z-index value for select menu's are infinity
	function fnElegantHide(dt,obj)
	{
		if(document.getElementById("sltNights").style.visibility == "hidden")
			document.getElementById("sltNights").style.visibility = "visible";
		else
			document.getElementById("sltNights").style.visibility = "hidden";
			
		if(document.getElementById("sltRooms").style.visibility == "hidden")
			document.getElementById("sltRooms").style.visibility = "visible";
		else
			document.getElementById("sltRooms").style.visibility = "hidden";
			
			//Now call the javascript Date Picker code	
			DPPopUp(dt, obj);
	}

/*
//Function to Show  the select Menu's ===== after they are hidden by fnElegantHide ==== that gets displayed though the Date Picker value is displayed
//This function is introduced as the Z-index value for select menu's are infinity
*/
function fnTempDirty()
{	
			//&& condition added to distinguish fnTempDirty between the two DatePickers
			if(document.getElementById("arrival_dsm") != null) {
				if(document.getElementById("arrival_dsm").value == "" && 
					document.getElementById("sltNights").style.visibility =="hidden") {
						document.getElementById("arrival_dsm").value = "0";
				}
				document.getElementById("sltNights").style.visibility = "visible";
				document.getElementById("sltRooms").style.visibility = "visible";
			}
			
		//	if(document.getElementById("arrival_dsm").value == "")
			//document.getElementById("arrival_dsm").value = "0";
						
			if(document.eventForm.eventDate_dsm.value == "")
			document.eventForm.eventDate_dsm.value= "0";
			
}

/* Function to retain the Arrival dates - even after Searching,sorting and paging  on paging and sorting  */
function setParamsRetainingArrivalDate()
{
		//Not to execute the function as the booking requirements tile is moved to other page
		return;
			document.eventForm.reqHidArrDay.value = document.getElementById("arrival_dsd").options[document.getElementById("arrival_dsd").selectedIndex].value;
			document.eventForm.reqHidArrMon.value = document.getElementById("arrival_dsm").options[document.getElementById("arrival_dsm").selectedIndex].value;
			document.eventForm.reqHidArrYear.value = document.getElementById("arrival_dsy").options[document.getElementById("arrival_dsy").selectedIndex].value;

			if(isObject(document.goToPageBelow) == true)
			document.goToPageBelow.reqHidArrDay.value = document.getElementById("arrival_dsd").options[document.getElementById("arrival_dsd").selectedIndex].value;

			if(isObject(document.goToPageBelow) == true)
			document.goToPageBelow.reqHidArrMon.value = document.getElementById("arrival_dsm").options[document.getElementById("arrival_dsm").selectedIndex].value;

			if(isObject(document.goToPageBelow) == true)
			document.goToPageBelow.reqHidArrYear.value = document.getElementById("arrival_dsy").options[document.getElementById("arrival_dsy").selectedIndex].value;

			if(isObject(document.goToPage) == true)
			document.goToPage.reqHidArrDay.value = document.getElementById("arrival_dsd").options[document.getElementById("arrival_dsd").selectedIndex].value;

			if(isObject(document.goToPage) == true)
			document.goToPage.reqHidArrMon.value = document.getElementById("arrival_dsm").options[document.getElementById("arrival_dsm").selectedIndex].value;

			if(isObject(document.goToPage) == true)
			document.goToPage.reqHidArrYear.value = document.getElementById("arrival_dsy").options[document.getElementById("arrival_dsy").selectedIndex].value;

}
/*
//Function used to validate the user inputs Called when the user clicks the Search button
*/
function validate(opMode)
{
		var day = document.eventForm.eventDate_dsd.value;
		var month = document.eventForm.eventDate_dsm.value;
		var year = document.eventForm.eventDate_dsy.value;
		//document.getElementById("idEventName").value = document.getElementById("idTxtEditable_eventNameE").value;
		document.getElementById("eventName").value = 
			document.getElementById("eventRef").options[document.getElementById("eventRef").selectedIndex].text;
		
		if(!fnValidateCurDate(day,eval(month)+1,year))
		{
			alert(gblAlertMsg);
			document.getElementById("eventDate_dsd").focus();
			return;
		}
		if(!validateDate(day,month,year) || opMode != "Search"){
			alert(gblAlertMsg);
			document.getElementById("eventDate_dsm").focus();
		//	document.eventForm.eventDate_dsd.value = tmpDayEvt;
		////	document.eventForm.eventDate_dsm.value = tmpMonthEvt;
			//document.eventForm.eventDate_dsy.value = tmpYearEvt;
			/* End of - the next three lines of code is to set the user's last selected values in the Date select menus */
			return;
		}
		else
		{
		
			tmpDayEvt = document.eventForm.eventDate_dsd.value;
			tmpMonthEvt = document.eventForm.eventDate_dsm.value;
			tmpYearEvt = document.eventForm.eventDate_dsy.value;
			if(opMode == "Search")
			{
				document.eventForm.hidOpFirst.value = "1";  // hidOpFirst is added to remove the bug of search parameters maintained even on clicking a new values
						//Code to retain the Arrival dates - even after Searching,sorting and paging  on paging and sorting 
							setParamsRetainingArrivalDate();
							//End of code to retain the Arrival Dates - even after searching,sorting and paging
							//alert(document.eventForm.eventName.value);

				//document.eventForm.eventName.value = document.getElementById("idTxtEditable_eventNameE").value;
				// Dilip To show the event search tile if the user clicks the search
				document.getElementById('idEventSearch').value = 'true';
				document.eventForm.submit();
			}
		}
}		
/*
//Function called when the user clicks the sort icon, Ascending or Descending
*/
function sort(orderby,mode) {
		document.eventForm.orderBy.value = orderby;
		document.eventForm.orderMode.value = mode;
		document.eventForm.hidOpFirst.value = "1";  // hidOpFirst is added to remove the bug of search parameters maintained even on clicking a new values
			//Code to retain the Arrival dates - even after Searching,sorting and paging  on paging and sorting 
			setParamsRetainingArrivalDate();
			//End of code to retain the Arrival Dates - even after searching,sorting and paging
		document.eventForm.submit();
}

/*
//Function used to validate the user inputs
// Called when the user clicks the Search button from the validate function
*/
function validateDate(day,month,year){
		var	monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		if((month=='') || (day=='') || (year==''))
			return false;

		if(isNaN(day) || isNaN(month))
			return false;

		if(isNaN(year))
			year=4;

		if(year%4 == 0)
			monthDays[1] = 29;

		if(day > monthDays[month])
			return false;

		return true;
}
function validateDateNew(day,month,year){
		var	monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
//		if((month=='') || (day=='') || (year==''))
	//		return false;

		if(isNaN(day) || isNaN(month) || isNaN(year))
			return false;

		if(isNaN(year))
			year=4;

		if(year%4 == 0)
			monthDays[1] = 29;

		if(day > monthDays[month])
			return false;

		return true;
}

/*
//Function used to validate the user inputs
// Called when the user clicks the "Book Now" button
*/
function validate2()
{
		var day = document.getElementById("arrival_dsd").value;
		var month = document.getElementById("arrival_dsm").value;
		var year = document.getElementById("arrival_dsy").value;
		
		var dd = document.getElementById("arrival_dsd").options[document.getElementById("arrival_dsd").selectedIndex].value;
		var mm = document.getElementById("arrival_dsm").options[document.getElementById("arrival_dsm").selectedIndex].value;
		var yy = document.getElementById("arrival_dsy").options[document.getElementById("arrival_dsy").selectedIndex].value;

		if(!validateDate(day,month,year)){
		 	return;
		}
		else
		{
		//	tmpDayEvtar = document.getElementById("arrival_dsd").value;
			//tmpMonthEvtar = document.getElementById("arrival_dsm").value;
		//	tmpYearEvtar = document.getElementById("arrival_dsy").value;
		}
}		
function fnOnClickPaging(pageNo)
{
		document.goToPage.hidOpFirst.value = "1"; 
		document.goToPage.pageNo.value = pageNo; 
			//Code to retain the Arrival dates - even after Searching,sorting and paging  on paging and sorting 
			setParamsRetainingArrivalDate();
			//End of code to retain the Arrival Dates - even after searching,sorting and paging
		document.goToPage.submit();
}

function fnValidateCurDate(d,m,y)
{
				m = eval(m); //to adjust the incompatibility in month of the date picker

				var curr_date = getServerCurDay();
				var curr_month = getServerCurMM();
				var curr_year = getServerCurYYYY();
				
				if(y < curr_year)
				{
					return false;
				}
				if((m < curr_month) && (y == curr_year))
				{
					return false;
				}
				if((d < curr_date) && (m == curr_month) && (y == curr_year))
				{
					return false;
				}
				return true;
}
/*
	//Function to validate whether the arrival date is between the selected events 
	//startdate and enddate
*/
function fnValidateBtStartEndDate(arrivalDD,arrivalMM,arrivalYY,strEvtEndDate,strEvtStartDate)
{
//Checking for Event End date
	var arTmpEnd = strEvtEndDate.split(" ");
	var iMax = arTmpEnd.length;
	var arMon = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	
				var evtEndDD = eval(arTmpEnd[iMax-3]);
				var evtEndMM = arTmpEnd[iMax-2];
				var evtEndYY = eval(arTmpEnd[iMax-1]);


				for(i=0;i<12;i++)
				{
					if(arMon[i] == evtEndMM)
					evtEndMM = i+1;
				}

				if(arrivalYY > evtEndYY)
				{
					return false;
				}
				if((arrivalMM > evtEndMM) && (arrivalYY == evtEndYY))
				{
					return false;
				}
				if((arrivalDD > evtEndDD) && (arrivalMM == evtEndMM) && (arrivalYY == evtEndYY))
				{
					return false;
				}
				return true;
}
function winpop(url)
{
//    window.open(url,"records","width=850,height=300,resizable=no,scrollbars=no, statusbar=no title=Focus left=127, top=234" );
	var leftPost,topPost;
 	leftPos =(1024-640)/2;
    topPos =(768-550)/2;  //Future - Change this logic and find it dynamically for screen resolution and for different browsers
    if(navigator.appName=="Microsoft Internet Explorer")
        window.open(url);
    else
		window.open(url,"Exhibition Details","toolbar=true,location=true,resizable,height=550,width=640,left="+ leftPos +",top="+topPos+",scrollbars");
}
function fnGetEventHotels(id) {
	//Dilip 14 Mar 06
	//Updated to submit to new action with out validating arrvial date and etc as booking requirements tile moved to other page.
	//alert(document.getElementById("hidEventRef"+id).value)
	document.eventHotelsSearchForm.eventRef.value = document.getElementById("hidEventRef"+id).value;
	//alert(document.eventHotelsSearchForm.eventRef.value);
	document.eventHotelsSearchForm.submit();
	return;
	//End
	document.eventHotelsSearchForm.eventRef.value = document.getElementById("hidEventRef"+id).value;
	document.eventHotelsSearchForm.city.value = document.getElementById("hidCity"+ id).value;
	document.eventHotelsSearchForm.country.value = document.getElementById("hidCountry"+ id).value;
	document.eventHotelsSearchForm.eventRef.value = document.getElementById("hidEventRef"+ id).value;
	document.eventHotelsSearchForm.eventName.value = document.getElementById("hidEventName"+ id).value;

	var dd = document.getElementById("arrival_dsd").options[document.getElementById("arrival_dsd").selectedIndex].value;
	var mm = eval(document.getElementById("arrival_dsm").options[document.getElementById("arrival_dsm").selectedIndex].value)+1;
	var yy = document.getElementById("arrival_dsy").options[document.getElementById("arrival_dsy").selectedIndex].value;
	var strEvtEndDate = document.getElementById('hidEventEndDate'+id).value;
	var strEvtStartDate = document.getElementById('hidEventStartDate'+id).value;
	/*If condition Added on 1July2005 to fix CR of moving date validation to Book Now button on click event */
	if(!validateDateNew(dd,mm-1,yy))
	{

		alert(jsSltValidDateOfArrival);
		document.getElementById("arrival_dsd").focus();
		return;
	}
		if(dd == "" || mm == "" || yy == "" || isNaN(dd) || isNaN(mm) || isNaN(yy))
		{
			alert(jsSltValidDateOfArrival);
			document.getElementById("arrival_dsd").focus();
		}
		else
		{
			if(fnValidateCurDate(dd,mm,yy))
			{
				if(!fnValidateBtStartEndDate(dd,mm,yy,strEvtEndDate,strEvtStartDate) )
				{
					alert(jsArrivalDateNotBetweenExhiStartEndDate);
					document.getElementById("arrival_dsd").focus();
				}
				else
				{ 
					document.eventHotelsSearchForm.arrival_dsd.value = document.getElementById("arrival_dsd").options[document.getElementById("arrival_dsd").selectedIndex].value;
					document.eventHotelsSearchForm.arrival_dsm.value = eval(document.getElementById("arrival_dsm").options[document.getElementById("arrival_dsm").selectedIndex].value)+1;
					document.eventHotelsSearchForm.arrival_dsy.value = document.getElementById("arrival_dsy").options[document.getElementById("arrival_dsy").selectedIndex].value;
					document.eventHotelsSearchForm.rooms.value = document.getElementById("sltRooms").options[document.getElementById("sltRooms").selectedIndex].value;	
					document.eventHotelsSearchForm.nights.value = document.getElementById("sltNights").options[document.getElementById("sltNights").selectedIndex].value;
					document.eventHotelsSearchForm.country_name.value = document.getElementById("hidCountryName"+ id).value;
					document.eventHotelsSearchForm.submit();
				}	
			}
			else
			{
				alert(jsInvalidArrivalDate);
			}	
		}
}
function fnClearEditExhiTxt()
{
	var obj = document.getElementById("idTxtEditable");
	obj.value = "";
}
