var postcodeLucene="";
function funpopulatePostcode()
{ 
	 if($("cityLocationsID").value!='select')	
	  $("searchPostcode").value=$("cityLocationsID").value;
	 else
	  $("searchPostcode").value="";
 }	
function getLocationsNew()
{
	document.forms["RestaurantSelectionForm"].city.value=document.forms["RestaurantSelectionForm"].cities[document.forms["RestaurantSelectionForm"].cities.selectedIndex].text
	document.forms["RestaurantSelectionForm"].cityId.value=$("citiesID").value;
	new Ajax.Updater('locationListID', 'locations.do?param=locations', {
		onComplete:function()
		{ 
		  $("searchPostcode").value="";			
		  }  ,parameters:Form.serialize(document.forms[1]),asynchronous:true,evalScripts:true});
}		
 function searchNewInd(a)
{	
	var formType="restaurantForm";
	var rstp="";
	var message="";
	var word="";
	var searchWith="";
	var postcode="";
	var place="";
	var dish="";
	var country="";
	var restName="";
	var searchlocations="";
	errorMessage1="";
	errorMessage=errorMessage1;
	var	status=true;
	var visitortime = new Date();
    var timezone=(visitortime.getTimezoneOffset()/60)*(-1);
	var cuisine= document.getElementById("restSelectFormMain").cuisineTypes.value;
	
	if(document.forms["RestaurantSelectionForm"].cities[document.forms["RestaurantSelectionForm"].cities.selectedIndex].text=='select')  //mu071009
	{
		alert("please select city\n");
	}
	else
	{
		document.forms["RestaurantSelectionForm"].city.value=document.forms["RestaurantSelectionForm"].cities[document.forms["RestaurantSelectionForm"].cities.selectedIndex].text;
	}
	if(document.getElementById("restSelectFormMain").dishValue.value!="" && document.getElementById("restSelectFormMain").dishValue.value!=null)
		{dish= document.getElementById("restSelectFormMain").dishValue.value.replace(/'/,"");}
	if(document.getElementById("restSelectFormMain").searchRest.value!="" && document.getElementById("restSelectFormMain").searchRest.value!=null)
	   {restName= document.getElementById("restSelectFormMain").searchRest.value;}
	if(document.getElementById("restSelectFormMain").takeaway1.checked==true){
		{	rstp =1+"|";}
			 document.getElementById("takeaway1").value=true;
	}
	else{
		 document.getElementById("takeaway1").value=false;}
	
	if( document.getElementById("restSelectFormMain").delivery1.checked==true){
			rstp +=2+"|";
		 document.getElementById("delivery1").value=true;
	}
	else{
		 document.getElementById("delivery1").value=false;}

	if( document.getElementById("restSelectFormMain").restaurant1.checked==true)
	{		rstp +=3+"|";
	 document.getElementById("restaurant1").value=true;
	}
	else{
		 document.getElementById("restaurant1").value=false;}
		
	if( document.getElementById("restSelectFormMain").onlinedel1.checked==true){
			rstp +=4+"|";
			document.getElementById("onlinedel1").value=true;
	}
	else{
		 document.getElementById("onlinedel1").value=false;}
	if( document.getElementById("restSelectFormMain").onlinecol1.checked==true){
			rstp +=5+"|";
			document.getElementById("onlinecol1").value=true;
	}
	else{
		 document.getElementById("onlinecol1").value=false;}

	if(rstp==""){
		rstp='1|2|3|4|5';}
//	 document.getElementById("restSelectFormMain").restaurantsType.value=rstp;
    if(document.getElementById("restSelectFormMain").searchCountry!=null)
    {
       if( document.getElementById("restSelectFormMain").cities[ document.getElementById("restSelectFormMain").cities.selectedIndex].text=='select')
		{
			errorMessage+="please select the City\n\n";	
		  if(status)
			{
				document.getElementById("restSelectFormMain").cities.focus();
				status=false;
			}			
		}			
      }
      	if( document.getElementById("restSelectFormMain").searchPostcode.value=="" &&  document.getElementById("restSelectFormMain").cityLocations.value=="select")
		{
			errorMessage+="Please Enter a Location or a Postcode\n\n";	
			if(status)
			{
				document.getElementById("restSelectFormMain").searchPostcode.focus();
				status=false;							
			 }			
		}
		if( document.getElementById("restSelectFormMain").searchPostcode.value!="")
		{
			var pcode=document.getElementById("restSelectFormMain").searchPostcode.value; 
			var plength=pcode.length;
			if(plength<2)
			errorMessage+="Please Enter atleast 2 (PostCode)Characters to Search\n";
			else
			status=false;	
			if(status)
			{
				if(document.getElementById("restSelectFormMain").formType!=null)
				{
					document.getElementById("restSelectFormMain").searchPostcode.focus();
					status=false;
				}
			 }
		}

	if(errorMessage!=errorMessage1)
	  {
		alert(errorMessage);
	  }
	else 
	{
		//mu160909 start
		var lucene="";

		if(postcodeLucene=="postcode"){
			lucene="";
			postcodeLucene="";
		}
        else{
      	lucene="lucene";
        }
		//mu160909 end
		document.forms["RestaurantSelectionForm"].luceneSearch.value=lucene;
		
		searchType="allSearch";
		searchValue ="allSearch";			
		country='IND';
		//document.forms["restSelectFormMain"].cityId.value=$("citiesID").value;		
		postcode=document.getElementById("restSelectFormMain").searchPostcode.value;
		restparamsjson=new restparamsObj(searchValue,'success',searchType,formType,timezone,cuisine,dish,rstp,restName,country,postcode,$("citiesID").value,"");		
		restparams=Object.toJSON(restparamsjson);
		document.forms["RestaurantSelectionForm"].restParamJson.value=restparams;
		//var strURL = 'RestaurantSelection.do?searchValue='+searchValue+'&msg=success&searchType='+searchType+'&formType='+formType+'&timezone='+timezone+'&cuisineType='+cuisine+'&dishValue='+dish+'&restaurantsType='+rstp+'&searchRest='+restName+'&searchCountry='+country+"&searchPostcode="+postcode;
//		var strURL = 'RestaurantSelection.do?lucene='+lucene;	
		var strURL = 'RestaurantSelection.do';			
		document.getElementById("restSelectFormMain").action=strURL;
   		document.getElementById("restSelectFormMain").submit();		
	}
}
 function getSearchDetails()
	{
			if(document.forms["RestaurantSelectionForm"].shipLocName!=null)
			  {
				 sloc=document.forms["RestaurantSelectionForm"].shipLocName.value;
			  }
			  //anvesh july 30 2008
			 if(document.forms["RestaurantSelectionForm"].shipLocName!=null)
			  {
				 dishnew=document.forms["RestaurantSelectionForm"].dishValue.value;
			  } 
		var strURL='SearchCriteria.do?shippingLoc='+sloc+'&dishnew='+dishnew+'&param=shippingLocation';
			new Ajax.Updater("searchCriteria",strURL,{onComplete:function(){displayShippingLocations()},asynchronous:false, evalScripts:true});
	}
	
  
function searchNew(a,parameter)
{
	var formType="restaurantForm";
	var rstp="";
	var message="";
	var word="";
	var searchWith="";
	var postcode="";
	var place="";
	var dish="";
	var country="";
	var restName="";
	var searchlocations="";
	errorMessage1="";
	errorMessage=errorMessage1;
	var	status=true;
	var visitortime = new Date();
    var timezone=(visitortime.getTimezoneOffset()/60)*(-1);
	var cuisine= document.getElementById("restSelectFormMain").cuisineTypes.value;
	if(document.getElementById("restSelectFormMain").dishValue.value!="" && document.getElementById("restSelectFormMain").dishValue.value!=null)
		{dish= document.getElementById("restSelectFormMain").dishValue.value.replace(/'/,"");}
	if(document.getElementById("restSelectFormMain").searchRest.value!="" && document.getElementById("restSelectFormMain").searchRest.value!=null)
	   {restName= document.getElementById("restSelectFormMain").searchRest.value;}
	if(document.getElementById("restSelectFormMain").takeaway1.checked==true){
			rstp =1+"|";
			 document.getElementById("takeaway1").value=true;
	}
	else{
		 document.getElementById("takeaway1").value=false;}
	
	if( document.getElementById("restSelectFormMain").delivery1.checked==true){
			rstp +=2+"|";
		 document.getElementById("delivery1").value=true;
	}
	else{
		 document.getElementById("delivery1").value=false;}

	if( document.getElementById("restSelectFormMain").restaurant1.checked==true)
	{		rstp +=3+"|";
	 document.getElementById("restaurant1").value=true;
	}
	else{
		 document.getElementById("restaurant1").value=false;}
		if(document.getElementById("restSelectFormMain").onlinedel1!=null && document.getElementById("restSelectFormMain").onlinedel1.checked==true){
				rstp +=4+"|";
				document.getElementById("onlinedel1").value=true;
		}
		else
			{ document.getElementById("onlinedel1").value=false;}
			 
        if(document.getElementById("restSelectFormMain").onlinecol1!=null && document.getElementById("restSelectFormMain").onlinecol1.checked==true){
				rstp +=5+"|";
				document.getElementById("onlinecol1").value=true;
		}
		else
			{ document.getElementById("onlinecol1").value=false;}
	if(rstp==""){
		rstp='1|2|3|4';}
	if(document.getElementById("restSelectFormMain").restaurantsType!=null)
	    document.getElementById("restSelectFormMain").restaurantsType.value=rstp;
	if(document.getElementById("restSelectFormMain").shipLocName!=null)
	{
		country= document.getElementById("restSelectFormMain").shipLocName.value;
    searchlocations = document.getElementById("restSelectFormMain").shipLocName.value;
     }
    
      if(document.getElementById("restSelectFormMain").searchCountry!=null)
    {
   	if( document.getElementById("restSelectFormMain").searchCountry[ document.getElementById("restSelectFormMain").searchCountry.selectedIndex].text=='Select')
		{
			errorMessage+="please select the country\n";	
		  if(status)
					{
						status=false;
					}
		  message="not valid";
		}
		else
		country= document.getElementById("restSelectFormMain").searchCountry.value;
      }
		if( document.getElementById("restSelectFormMain").searchPostcode.value=="" &&  document.getElementById("restSelectFormMain").stateCityTown.value=="")
		{
			errorMessage+="Please Enter a Postcode or a Place\n\n";	
				if(status)
					{
					if(document.getElementById("restSelectFormMain").formType!=null)
						{
							if(document.getElementById("restSelectFormMain").formType.value!="cuisineForm")
							{	
								document.getElementById("restSelectFormMain").searchPostcode.focus();
								status=false;
							}
						}else
						{
							document.getElementById("restSelectFormMain").searchPostcode.focus();
							status=false;
						}
					}
			message="not valid";
		}
		if( document.getElementById("restSelectFormMain").searchPostcode.value!="")
		{
			var pcode=document.getElementById("restSelectFormMain").searchPostcode.value; 
			pcode = pcode.toUpperCase();
			document.getElementById("restSelectFormMain").searchPostcode.value = pcode;
			var plength=pcode.length;
			if(plength<2)
			errorMessage+="Please Enter atleast 2 (PostCode)Characters to Search\n";	
			if(status)
					{
						if(document.getElementById("restSelectFormMain").formType!=null)
						{
							if(document.getElementById("restSelectFormMain").formType.value!="cuisineForm")
							{
							document.getElementById("restSelectFormMain").searchPostcode.focus();
							status=false;
							}
						}else
						{
							document.getElementById("restSelectFormMain").searchPostcode.focus();
							status=false;
						}
					}
		}
		if( document.getElementsByName("searchPostcode")[0].value!="" &&  document.getElementsByName("stateCityTown")[0].value!="")
		{
			alert("Please Search by One Parameter either postcode or place");
				message='not valid';
		}
	var stateCityTown="";
		if(message=="")
		{
			if( document.getElementById("restSelectFormMain").stateCityTown.value!="" && document.getElementById("restSelectFormMain").stateCityTown.value!=null)
			{
				place= document.getElementById("restSelectFormMain").stateCityTown.value;
				 searchWith='place';
				 stateCityTown=document.getElementById("restSelectFormMain").stateCityTown.value;
			}
			if( document.getElementById("restSelectFormMain").searchPostcode.value!="" )
			{
				postcode= document.getElementById("restSelectFormMain").searchPostcode.value;
				searchWith='postcode';
			}
		}
	if(errorMessage!=errorMessage1)
		  {
			alert(errorMessage);
		  }
	else if(message=="" && searchWith!="")
	{
		//mu160909 start
		var lucene="";

		if(postcodeLucene=="postcode"){
			lucene="";
			postcodeLucene="";
		}
        else{
      	lucene="lucene";
        }
		//mu160909 end
		document.forms["RestaurantSelectionForm"].luceneSearch.value=lucene;
		
		searchType="allSearch";
		searchValue ="allSearch";
		//var strURL = 'RestaurantSelection.do?stateCityTown='+stateCityTown+'&searchValue='+searchValue+'&msg=success&searchType='+searchType+'&formType='+formType+'&timezone='+timezone+'&cuisineType='+cuisine+'&dishValue='+dish+'&restaurantsType='+rstp+'&searchRest='+restName+'&searchCountry='+country+"&searchPostcode="+postcode;
		restparamsjson=new restparamsObj(searchValue,'success',searchType,formType,timezone,cuisine,dish,rstp,restName,country,postcode,"","");		
		restparams=Object.toJSON(restparamsjson);
		document.forms["RestaurantSelectionForm"].restParamJson.value=restparams;
		var strURL = 'RestaurantSelection.do?view='+parameter;
		if(document.getElementById("mainPage") != null) 
		{
			document.getElementById("restSelectFormMain").action=strURL;
	   		document.getElementById("restSelectFormMain").submit();
		}
		else 
			{
				if(document.getElementById("restaurants")!=null) 
				 {
				 	viewModule("searchCriteria");
//			      	new Ajax.Updater("restaurants",strURL+"&pageType=half&nextValnew=0&billShipAddressId="+searchlocations,{onComplete:function(){fillCartBlock(document.getElementById("topRestsFlag").value);setOpenDays("restaurantDesc");},parameters:Form.serialize(document.forms[1]),asynchronous:false, evalScripts:true});
					document.getElementById("restSelectFormMain").action=strURL;
  		   			document.getElementById("restSelectFormMain").submit();
			     }
				 else
				 {
				 	document.getElementById("restSelectFormMain").action=strURL;
  		   			document.getElementById("restSelectFormMain").submit();
				 }
			}
	}
}

function ChangeLocation()
{
	if(document.forms["RestaurantSelectionForm"].shipLocName!=null)
		document.forms["RestaurantSelectionForm"].shipLocName.value="select";
}
function onEnter(param,e)
		{
			var keyNo="";
			if(window.event)
				keyNo=window.event.keyCode;
			else
				keyNo=e.which
				
				
			if(keyNo==13)
			{
				if(valueIsFrom==1)  //mu080310 for domain flow in auto suggest
				{
					if(currentDomain != "")
					{
						var protocol=window.location.protocol;       
					    var host=window.location.hostname;
					    var port=window.location.port;
					    var pathname=window.location.pathname;
					    var pname=pathname.split("/");
					    var domainurl="";
					    domainurl=protocol+"//"+host+":"+port+"/"+pname[1];
	//for local			var finalDomainName = domainurl+"/RestaurantSelection.do?formType=cuisineForm&domain="+currentDomain
						var finalDomainName = "http://www."+currentDomain+".bringmefood.com";
						document.location.href = finalDomainName;
						return;
					}
				}
				
							  	
				if(param=='allSearch')
				{
					   searchNew(param);
					   postcodeLucene="postcode";  //mu160909
					   ChangeLocation();
				}
				if(param=='allSearchnew')
				{	
						document.forms["RestaurantSelectionForm"].searchCountry.value=document.getElementById("searchCountrynew").value;
						document.forms["RestaurantSelectionForm"].searchPostcode.value=document.getElementById("middlesearchPostcode").value;				   
					
				    searchNew('allSearch');
				    ChangeLocation();
				}
				if(param=='allSearchnew1')
				{
						document.forms["RestaurantSelectionForm"].searchCountry.value=document.getElementById("searchCountrynew1").value;
						document.forms["RestaurantSelectionForm"].searchPostcode.value=document.getElementById("middlesearchPostcode1").value;
						searchNew('allSearch');
				    	ChangeLocation();
				}
			   if(param=='sortItems')
					getSortedItems();
			   if(param=='allSearchInd')
			   {
				   searchNewInd('allSearch');					  
			    }		
    		}else
    		{
//mu021009		getautoValues();
    		}
		 
    	//anvesh nov 3
		}	
		
		/* postcodes popup for com only */
function postcodespopup()
	{
		  //alert("hi");
		  var cntry=document.forms["RestaurantSelectionForm"].searchCountry.value;
		  new Ajax.Updater("postcodepopup","RestaurantSelection.do?paramx=availablepostcodes&searchCountry="+cntry,{onComplete:function(){},asynchronous:false, evalScripts:true});
		  Popup.showModal('postcodepopup'); 
	}	
function setpopuppostcode(postcode)
{
	Popup.hide('postcodepopup');
	$("searchPostcode").value=postcode;
}
function getPagesHtml(pgno)
{
	if(parseInt(pgno)>1)
	{
		var links = "<div class=\"pagesTag\">Pages : </div>";
		for(var p=0;p<pgno;p++)
		{
			var disp = parseInt(p)+1;
			links+="<div class=\"pageNo\" onclick=\"javascript:getThisPage("+p+")\">"+disp+"</div>"; 		
		}
		document.getElementById("pages").innerHTML=links;
	}
}
function getThisPage(pno)
{
	new Ajax.Updater("restaurants","RestaurantSelection.do?pageNo="+pno,{onComplete:function(){},asynchronous:false, evalScripts:true});
}
function getItemsPagesHtml(pgno)
{
	if(parseInt(pgno)>1)
	{
		var links = "<div class=\"pagesTag\">Pages : </div>";
		for(var p=0;p<pgno;p++)
		{
			var disp = parseInt(p)+1;
			links+="<div class=\"pageNo\" onclick=\"javascript:getThisPageItems("+p+")\">"+disp+"</div>"; 		
		}
		document.getElementById("itemspages").innerHTML=links;
	}
}
function getThisPageItems(pgno)
{
   var protocol=window.location.protocol;       
   var host=window.location.hostname;
   var port=window.location.port;
   var pathname=window.location.pathname;
   var pname=pathname.split("/");
   var htmlurl="";
   var restId=document.forms["RestaurantSelectionForm"].restaurantName.value; 
   var cuisine=document.forms["RestaurantSelectionForm"].searchCuisine.value;
   var category=document.forms["RestaurantSelectionForm"].ccategory.value;
   var subcat=document.forms["restSelectFormMain"].scategory.value;
   var orderfor="";
   orderfor=GetCookie("OrderFor");
   if(orderfor==null || orderfor=="" || typeof(orderfor)=="object" || orderfor=="[object Object]")
   orderfor="Delivery";
   
   if(subcat!=null && subcat!="" && subcat!="undefined")
       {
	 // for local		htmlurl=protocol+"//"+host+":"+port+"/"+pname[1]+"/templocations/"+restId+"/"+orderfor+"/Page"+pgno+"/catlist_"+cuisine+"_"+category+"_"+subcat+".html";
		      	 	htmlurl=protocol+"//"+host+":"+port+"/templocations/"+restId+"/"+orderfor+"/Page"+pgno+"/catlist_"+cuisine+"_"+category+"_"+subcat+".html";
       }else
       {
       	// for local	htmlurl=protocol+"//"+host+":"+port+"/"+pname[1]+"/templocations/"+restId+"/"+orderfor+"/Page"+pgno+"/catlist_"+cuisine+"_"+category+".html";
		      	 	htmlurl=protocol+"//"+host+":"+port+"/templocations/"+restId+"/"+orderfor+"/Page"+pgno+"/catlist_"+cuisine+"_"+category+".html";
       }
       new Ajax.Updater("restaurants",htmlurl,{onComplete:function(){},asynchronous:false, evalScripts:true});
	
}
function SuggestDomainFlow()
{
	if(valueIsFrom==1)  //mu220310 for domain flow in auto suggest
	{
		if(currentDomain != "")
		{
			var protocol=window.location.protocol;       
		    var host=window.location.hostname;
		    var port=window.location.port;
		    var pathname=window.location.pathname;
		    var pname=pathname.split("/");
		    var domainurl="";
		    domainurl=protocol+"//"+host+":"+port+"/"+pname[1];
//for local			var finalDomainName = domainurl+"/RestaurantSelection.do?formType=cuisineForm&domain="+currentDomain
			var finalDomainName = "http://www."+currentDomain+".bringmefood.com";
			document.location.href = finalDomainName;
			return;
		}
	}
}
