// JavaScript Document

function LoadDropBox(container,CurrPage,CatID,key,s){
	var URL = "load_dropdown.php";
	new Ajax.Updater(container, URL, {
					 	method : 'post',
						parameters : { CurrPage : CurrPage, CatID : CatID, key : key, s : s }
					 });
}

function LoadCategory(container){
	var URL = "LoadCat.php";
	new Ajax.Updater(container, URL, {
					 	method : 'post'
					 });
}

//setting up cookie 


function optionPriceRange(CategoryID,MinPrice,MaxPrice,RoomNo,CarType)
{
	var MinRange = parseInt(MinPrice);
	var MaxRange = parseInt(MaxPrice);
	//alert(CategoryID);
	//alert(MinRange);
	//alert(MaxRange);
	//$('ButtonSearch').show();
	$('searchInputButton').show();
	if(CategoryID == 28 || CategoryID == 31 || CategoryID == 35 || CategoryID == 36)
	{
		//if((MinPrice==0) && (MaxPrice==0))
		if((MaxPrice==0) || (MaxPrice==-1))
		{
			$('searchInputButton').hide();
			$('searchInput').show();
			//$('SEARCH').disabled=true;
		}
	}
	$('LoadPriceRange').innerHTML = "<img src='images/icons/loading_small.gif' alt='loading' />";
	var URL = "LoadPriceRange.php";
	//alert(URL);
	new Ajax.Request(URL, 
					 {
					 	method : 'post',
						parameters : 
						{ 
							CategoryID : CategoryID,
							MinPrice : MinPrice,
							MaxPrice : MaxPrice,
							RoomNo : RoomNo,
							CarType : CarType
						},
							onSuccess : function( transport )
										{
											var res = transport.responseText;
											//alert(res);
											if(res )
											{
												//alert(res);
												$('LoadPriceRange').innerHTML = res;
												//$('ImageDiv').hide();
											}
											else
											{
												$('LoadPriceRange').innerHTML = "";
												//$('ImageDiv').hide();
											}
										},
							onFailure : function()
										{
											$('ShowMsg').innerHTML = "Server Error !! Insertion Fail";
											//$('ImageDiv').hide();
										}
					   
					   }
					 );
	
	
}

function RangeCheck(categoryId)
{
	//var MinRange = parseInt($('dbdMinPriceRange').value);
	var MinRange = parseInt($('dbdMinPriceRange').value);
	var MaxRange = parseInt($('dbdMaxPriceRange').value);
	/*alert(categoryId);
	alert(MinRange);
	alert(MaxRange);*/
	//$('SEARCH').disabled=false;
	$('searchInputButton').show();
	$('searchInput').hide();
	
	if(categoryId == 28 || categoryId == 31 || categoryId == 35 || categoryId == 36)
	{
		if((MaxRange==-1 || MaxRange==0))
		{
			alert("Please Select The Price Range");
			$('dbdMinPriceRange').focus();
			$('searchInputButton').hide();
			$('searchInput').show();
			//$('SEARCH').disabled=true;
			return false;
		}
		else
		{
			if(MaxRange>0)
			{
				if(MinRange>MaxRange)
				{
					alert("Max Price cannot be lower than Min");
					$('dbdMinPriceRange').focus();
					$('searchInputButton').hide();
					$('searchInput').show();
					//$('SEARCH').disabled=true;
					return false;
				}
				else
				{ 
					$('searchInputButton').show();
					$('searchInput').hide();
					//$('SEARCH').disabled=false;
					return true;
				}
			}
		}
	}
	else
	{
		if((MaxRange>0))
		{
			if(MinRange>MaxRange)
			{
				alert("Max Price cannot be lower than Min");
				$('dbdMinPriceRange').focus();
				$('searchInputButton').hide();
				$('searchInput').show();
				//$('SEARCH').disabled=true;
				return false;
			}
			else
			{ 
				//$('ButtonSearch').show();
				$('searchInputButton').show();
				$('searchInput').hide();
				return true;
			}
		}
	}
}


function CheckOnButton()
{
	alert('Please Select Your Price Range')
}


function GetLinks(CatID)
{
	//alert(CatID);
	if(!CatID)
	{
		CategoryID = 0 ;	
	}
	else
	{
		CategoryID = CatID ;
	}
	$('LinkPagiShow').hide();
	var AjaxURL = 'LoadLink.php?CategoryID='+CategoryID;
	var AjaxParams = 'PageIndex=1&RecordPerPage=10';
	var ListingDiv = 'linkDisplay';
	var PagiShowDiv = 'LinkPagiShow';
	AjaxLinkPagging(AjaxURL,AjaxParams,ListingDiv,PagiShowDiv);
	
	
}


function AjaxLinkPagging(AjaxURL,AjaxParams,ListingDiv,PagiShowDiv)
{
	//alert(AjaxURL);
	//alert(AjaxParams);
	//alert(ListingDiv);
	//alert(PagiShowDiv);
	
	if (getRef(PagiShowDiv).style.display=='none') 
	{ 
		showDiv(PagiShowDiv);
	}
	var FirstPagiDiv = PagiShowDiv;
	//alert(FirstPagiDiv);
	$('LinkPagiShow').hide();
	$('linkDisplay').hide();
	$('loadImage').show();
	$('loadImage').innerHTML = "<img src='images/icons/ajax-loader (1).gif' alt='loading' />";
	
	var URL = AjaxURL;
	var splitURL = AjaxURL.split("?");
	var URL = splitURL[0];
	//alert(URL);
	var extraParam = splitURL[1];
	var Params = AjaxParams+"&"+extraParam;
	//alert(Params);
	new Ajax.Request(URL, 
					 {
					 	method : 'post',
						parameters : Params,
							onSuccess : function( transport )
										{
											var res = transport.responseText;
											//alert(res);
											if(res )
											{
												var splitRes = res.split("|");
												$(ListingDiv).show();
												$(FirstPagiDiv).show();
												$(FirstPagiDiv).innerHTML = splitRes[1];
												$(ListingDiv).innerHTML = splitRes[0];
												$('loadImage').hide();
											}
											else
											{
												$('linkDisplay').innerHTML = "";
											}
										},
							onFailure : function()
										{
											$('linkDisplay').innerHTML = "Server Error !! ";
										}
					   
					   }
					 );
	
}


function LoadRightNav(container,CatID,CityID,PageName)
{
	var URL = "LoadRightNav.php";
	new Ajax.PeriodicalUpdater(container, URL, {
					 	method : 'post',
						parameters : { CatID : CatID, CityID : CityID, CurrentPageName : PageName},
						frequency : 30,
						decay : 1
					 });
}

function MySavedAdds(advID,CatID,checked,loadingObj,outputClt,doIt)
{
	//alert('Ananya MySavedAdds Called');
	//alert(advID);
	//alert(CatID);
	//alert(checked);
	//loadingObj=outputClt;
	///loadingObj.innerHTML="";
	
	Ajax.Responders.register(
	{
		onCreate: function()
		{
			var agt=navigator.userAgent.toLowerCase();
			if (agt.indexOf('msie')!=-1 && document.all && agt.indexOf('opera')==-1 && agt.indexOf('mac')==-1) 
			{
				eval('var c=(agt.indexOf("msie 5")!=-1)?"Microsoft.XMLHTTP":"Msxml2.XMLHTTP";try{new ActiveXObject(c);}catch(e){alert("ActiveX Sees To Be Disabled!");}');
			}					
		}, 
		onUninitialized: function()
		{
			loadingObj.innerHTML='YOUR REQUEST HAS NOT BEEN INITIALISED!';
		}, 
		onComplete: function(){	}
	}); //Ajax.Responders.register
				
	loadingObj.innerHTML="<span style='padding:0px;color:#4C636E'><img src='images/icons/loading_small.gif' width='16' height='16' border='0'  align='absmiddle'>Loading Please Wait....</span>";
	
	/////GIVE THE EMBEDED PAGE NAME WHERE YOUR PHP CODE RECIDES( THE GHOST's HAND ) parameters:  == YOUR QUERY STRING		
	new Ajax.Request('ajaxserver/MySavedAdds.php',
	{
		method:'get',
		onSuccess: function(transport)
		{
			outputClt.innerHTML=transport.responseText;
			loadingObj.innerHTML="";////THIS WILL CLEAR LODING...			
		},
		parameters: 
		{
			task:'insert',
			advID:advID,
			CatID:CatID,
			checked:checked,
			doIt:doIt		
		},
		
		onFailure: function(transport)
		{ 
			 if (404 == transport.status)
			 {
				loadingObj.innerHTML="Sorry, Resource Is Unavailable";
			 }
			  else if (500 == transport.status)
			 {
				loadingObj.innerHTML="Sorry, Internal Error. Please Contact Administrator.";
				
			 }
			 else
			 {
			   loadingObj.innerHTML="Sorry, Unexpected Response Status: " +transport.status ; 
			 }
		}
  }); //new Ajax.Request

}

function SetAjaxCookieForRightNav(advID,CatID)
{
	getSetChkBoxForRightNav(advID);
	if(document.getElementById('MySavedAdvertsForRightNav'+advID).checked==true)
	{
		///alert('advID = '+advID+'  , CatID = '+CatID+' CHECKED');
		MySavedAdds(advID,CatID,'1',$('AjaxMessage'+advID), $('AjaxShowOutPut'+advID),'0');///REQUEST TO JAVASCRIPT WITH PARAMS ( advID,CatID,$('AjaxMessage'+advID), $('AjaxShowOutPut'+advID) ) TO REQUEST TO EMBEDED PHP PAGE
	}
	else
	{
		///alert('advID = '+advID+'  , CatID = '+CatID+' UNCHECKED');
		MySavedAdds(advID,CatID,'0',$('AjaxMessage'+advID), $('AjaxShowOutPut'+advID),'0');///REQUEST TO JAVASCRIPT WITH PARAMS ( advID,CatID,$('AjaxMessage'+advID), $('AjaxShowOutPut'+advID) ) TO REQUEST TO EMBEDED PHP PAGE	
	}
}

function getSetChkBoxForRightNav(advID)
{
	//////CHECK FOR NOT NULL OBJECT
	if(document.getElementById('MySavedAdvertsForRightNav'+advID)!=null)
	{
		if(document.getElementById('MySavedAdvertsForRightNav'+advID).checked == true)
		{
			//////CHECK FOR NOT NULL OBJECT
			if(document.getElementById('MySavedAdvertsForIndex'+advID)!=null)
			{
				document.getElementById('MySavedAdvertsForIndex'+advID).checked = true;
			}
		}
		else
		{
			//////CHECK FOR NOT NULL OBJECT
			if(document.getElementById('MySavedAdvertsForIndex'+advID)!=null)
			{
				document.getElementById('MySavedAdvertsForIndex'+advID).checked = false;
			}
		}
	}	
}

function change()
{
	if(examp.style.overflow=="auto"){
		examp.style.overflow="hidden";
		knop.value="Set overflow to VISIBLE.";
		txt.innerText="Overflow is set to HIDDEN.";
		return true;
	}
}

function getOptionValNew(opt,page,catID,key,s)
{
	//var data = opt.options[opt.selectedIndex].innerHTML;
	//alert(opt);
	if(catID == ""){
		catID = 1;
	}
	if(s == ""){
		s = 1;
	}
	window.location.href = page+"?CatID="+catID+"&CityID="+opt+"&key="+key+"&s="+s;
}


function preventOpp(objID)
{
	if(navigator.appName.indexOf('Microsoft')!=-1)
	{
		document.getElementById(objID).onkeypress = Filter4Characters;
	}
	else
	{
		document.getElementById(objID).addEventListener("keypress",Filter4Characters, true);
	}
}

function Filter4Characters(evt)
{
	if(navigator.appName.indexOf('Microsoft')!=-1) {
		if(event.keyCode < 46 || event.keyCode > 57) {
			event.returnValue = false;
		}
	} else {
		if(evt.charCode && (evt.charCode < 46 || evt.charCode > 57)) {
			if(evt.preventDefault) {
				evt.preventDefault();
			}
		}
	}
}

function Set_Cookie_old(name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	alert(name);
	alert(value);
	alert(expires);
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
/*	if (expires)
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
*/	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = alert(name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" ));
}

function Set_Cookie(CategoryID)
{
	
	var CategoryID = parseInt(CategoryID);
	//alert(CategoryID);
	var URL = "SetCookie.php";
	new Ajax.Request(URL, 
					 {
					 	method : 'post',
						parameters : 
						{ 
							CategoryID : CategoryID
						},
							onSuccess : function( transport )
										{
											var res = transport.responseText;
										},
							onFailure : function()
										{
											$('ShowMsg').innerHTML = "Server Error !! Insertion Fail";
										}
					   
					   }
					 );
	
	
}
