/**********************************************************************************************************************
This code has been developed by : Coddo Net Technologies Pvt. Ltd.
Project Name 	: FHP
Program Name 	: common.js
Purpose 		: common functions
Creation Date 	: 28/12/2005 2:57PM
***********************************************************************************************************************/

/**********************************************************************************************************************
Following function changes records per page in the CMS
**********************************************************************************************************************/
function ChangePageRecs(ele)
{
	recs=ele.options[ele.selectedIndex].value;
	url=location.href;
	
	if(url.indexOf("recs_page")!=-1)
	{
		replaced="&recs_page="+recs;
		
		if(url.match(/&recs_page=[0-9]+/)!=null)
		{
			url=url.replace(/&recs_page=[0-9]+/,replaced);
		}
	}	
	else
	{
		url=url+"&recs_page="+recs;
	}
	
	location.href=url;
}

/**********************************************************************************************************************
 Following function changes records per page in the CMS for team specific
***********************************************************************************************************************/

function ChangePageRecsteam(ele)
{
	recs=ele.options[ele.selectedIndex].value;
	url=location.href;
	
	if(url.indexOf("teamrecs_page")!=-1)
	{
		replaced="&teamrecs_page="+recs;
		
		if(url.match(/&teamrecs_page=[0-9]+/)!=null)
		{
			url=url.replace(/&teamrecs_page=[0-9]+/,replaced);
		}
	}	
	else
	{
		url=url+"&teamrecs_page="+recs;
	}
	
	location.href=url;
}

/**********************************************************************************************************************
Following function changes the page in CMS
**********************************************************************************************************************/
function ChangePage(dir)
{
	url=location.href;
	//alert (url);
	cur_page=parseInt(document.frmpage.page.value,10);
	num_pages=parseInt(document.frmpage.num_pages.value,10);
	
	if(dir==1)
	{
		if((cur_page+1)==num_pages)
		{
			alert("Last Page. Cannot move forward");
			return;
		}
	
		cur_page++;
	}
	else if(dir==0)
	{
		if(cur_page==0)
		{
			alert("First Page. Cannot move backwards");
			return;
		}
		cur_page--;
	}

	if(url.indexOf("curpage")!=-1)
	{
		replaced="&curpage="+cur_page;

		if(url.match(/&curpage=[0-9]+/)!=null)
		{
			url=url.replace(/&curpage=[0-9]+/,replaced);
		}
	}
	else
	{
		url=url+"&curpage="+cur_page;
	}

	location.href=url;
}

/**********************************************************************************************************************
Following function changes the page in CMS
**********************************************************************************************************************/
function ChangePageTeam(dir)
{
	url=location.href;
	//alert (url);
	cur_page1=parseInt(document.frmpage.page1.value,10);
	num_pages1=parseInt(document.frmpage.num_pages1.value,10);
	
	if(dir==1)
	{
		if((cur_page1+1)==num_pages1)
		{
			alert("Last Page. Cannot move forward");
			return;
		}
	
		cur_page1++;
	}
	else if(dir==0)
	{
		if(cur_page1==0)
		{
			alert("First Page. Cannot move backwards");
			return;
		}
		cur_page1--;
	}

	if(url.indexOf("curpage1")!=-1)
	{
		replaced="&curpage1="+cur_page1;

		if(url.match(/&curpage1=[0-9]+/)!=null)
		{
			url=url.replace(/&curpage1=[0-9]+/,replaced);
		}
	}
	else
	{
		url=url+"&curpage1="+cur_page1;
	}

	location.href=url;
}

/**********************************************************************************************************************
Following function returns results returned by query with ajax and fills city combo
***********************************************************************************************************************/

function ShowSearchCount(url,ele)
{
	//alert(url+"="+ele.id);
	var xmlhttp=false;
	propcmb=document.frmsearch.prop_type;
	citycmb=document.frmsearch.city;
	pcodecmb=document.frmsearch.postcode;

	if(ele.id=="city")
	{
		FillPostCodes(pcodecmb);
	}

	propval=propcmb.options[propcmb.selectedIndex].value;
	cityval=citycmb.options[citycmb.selectedIndex].value;
	pcode=pcodecmb.options[pcodecmb.selectedIndex].value;
	
	//------------------------------------------------------
	//Detect browser and initialize browser specific objects
	//------------------------------------------------------
	
	browser=navigator.userAgent.toLowerCase();
	
	if(browser.indexOf("opera")!=-1)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else if(browser.indexOf("msie")!=-1)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		xmlhttp=new XMLHttpRequest();
	}
	
	url=url+"?prop_type="+propval+"&city="+cityval+"&postcode="+pcode;
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			var result=xmlhttp.responseText;
			if(result!=0)
			{
				document.getElementById("total").innerHTML="";
				document.getElementById("total").innerHTML="<p><strong style=\"color:red;font-size:1em;\">"+result+"</strong> properties match your query <br /><br /></p>";	
				document.getElementById("submit_search").disabled=false;
			}
			else
			{
				document.getElementById("total").innerHTML="";
				document.getElementById("total").innerHTML="<p>There are no matches to your query, please widen your search or <a href=\"#\">contact us</a> to discuss your requirements further.<br /><br /></p>";
				document.getElementById("submit_search").disabled=true;
			}
		}
	}
}

/**********************************************************************************************************************
Following function fills the postalcodes combo
**********************************************************************************************************************/
function FillPostCodes(ele)
{
	var citycmb=document.frmsearch.city;
	var selcity=citycmb.selectedIndex;
	var result="";

	//clear the postcode combo first
	while(ele.options.length>1)
	{
		ele.options[0]=null;
	}

	/*if(selcity==0)
	{
		var newopt=new Option("All Postcodes");
		newopt.value="%";
		ele.options[0]=newopt;
	}
	if(selcity==1)
	{
		result=document.frmsearch.npostcodes.value;
	}
	else if(selcity==2)
	{
		result=document.frmsearch.dpostcodes.value;
	}
	else if(selcity==3)
	{
		result=document.frmsearch.lpostcodes.value;
	}*/
	
	if(document.getElementById(citycmb.value))
       result=document.getElementById(citycmb.value).value;

	if(result!="" && result!=null)
	{
		//codes="..~%|PO123~PO123|PO124~PO124|PO125~PO125|PO126~PO126"; //Format of the response
		var codes=result;
		var codelist=codes.split("|");
		i=0;

		while(ele.options.length<codelist.length)
		{
			var entry=codelist[i].split("~");
			var text=entry[0];
			var textarray=text.split(/\s/);
			var outcode=textarray[0];
			var val=entry[1];
			var newopt=new Option(outcode);
			newopt.value=val;
			ele.options[i]=newopt;
			i++;
		}
		//ele.options[ele.options.length]=newopt;
	}
	else
	{
        newopt=new Option("All Postcodes");
		newopt.value="%";
		ele.options[0]=newopt;
	}
}

/**********************************************************************************************************************
Following function adds the selected property to download cart
**********************************************************************************************************************/
function AddToDownloads(seldiv,address,ref_code)
{
	target_div=document.getElementById("download_items");
	newele=document.createElement("P");
	newele_id="cart_"+seldiv;
	newele.id=newele_id;
	newele.innerHTML="<b>Ref Code "+ref_code+"</b><br>"+address;
	target_div.appendChild(newele);
	document.frmdownloads.submit.style.visibility="visible";
	//AddHiddenElement(seldiv,address);
	AddDownloadToSession(seldiv,address);
}

/**********************************************************************************************************************
Following function removes the selected property from download cart
**********************************************************************************************************************/
function RemoveFromDownloads(seldiv)
{
	target_div=document.getElementById("download_items");
	var item="cart_"+seldiv;
	if(document.getElementById(item))
	{
		item=document.getElementById(item);
		target_div.removeChild(item);
	}
	//RemoveHiddenElement(seldiv);
	RemoveDownloadFromSession(seldiv);
}

/**********************************************************************************************************************
Following function adds hidden values to the form for downloads
**********************************************************************************************************************/
function AddHiddenElement(seldiv,address)
{
	newele_id="hcart_"+seldiv;
	target_form=document.getElementById("frmdownloads");
	
	if(document.getElementById(newele_id)==null)
	{
		newele=document.createElement("INPUT");
		newele.id=newele_id;
		newele.type="hidden";
		newele.name=newele_id;
		newele.value=seldiv;
		AddDownloadToSession(newele.value,address);
		target_form.appendChild(newele);
	}
}

/**********************************************************************************************************************
Following function removes hidden values from the form for downloads
**********************************************************************************************************************/
function RemoveHiddenElement(seldiv)
{
	target_form=document.getElementById("frmdownloads");
	item_id="hcart_"+seldiv;
	
	if(document.getElementById(item_id))
	{
		item=document.getElementById(item_id);
		target_form.removeChild(item);
		RemoveDownloadFromSession(seldiv);
	}
}

/**********************************************************************************************************************
Following function adds the downloads to the session
**********************************************************************************************************************/
function AddDownloadToSession(value,address)
{
	var xmlhttp=false;

	//------------------------------------------------------
	//Detect browser and initialize browser specific objects
	//------------------------------------------------------
	browser=navigator.userAgent.toLowerCase();
	
	if(browser.indexOf("opera")!=-1)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else if(browser.indexOf("msie")!=-1)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		xmlhttp=new XMLHttpRequest();
	}
	
	url="includes/add_download.php?download=1&dnld_item_val="+value+"&dnld_item_address="+address;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			result=xmlhttp.responseText;
		}
	}
}

/**********************************************************************************************************************
Following function removes the download from session
**********************************************************************************************************************/
function RemoveDownloadFromSession(value)
{
	var xmlhttp=false;

	//------------------------------------------------------
	//Detect browser and initialize browser specific objects
	//------------------------------------------------------
	browser=navigator.userAgent.toLowerCase();
	
	if(browser.indexOf("opera")!=-1)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else if(browser.indexOf("msie")!=-1)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		xmlhttp=new XMLHttpRequest();
	}
	
	url="includes/add_download.php?download=-1&dnld_item_val="+value;
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			result=xmlhttp.responseText;
			
			if(result=="0")
			{
				document.frmdownloads.submit.style.visibility="hidden";
			}
		}
	}
}

/**********************************************************************************************************************
//Following function retrives the cookie by name
**********************************************************************************************************************/
function getCookie(NameOfCookie)
{
	// First we check to see if there is a cookie stored.
	// Otherwise the length of document.cookie would be zero.
	
	if (document.cookie.length > 0)
	{
	
		// Second we check to see if the cookie's name is stored in the
		// "document.cookie" object for the page.
		
		// Since more than one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie" object
		// is not just an empty text.
		// If our cookie name is not present the value -1 is stored
		// in the variable called "begin".
	
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) // Note: != means "is not equal to"
		{
			// Our cookie was set.
			// The value stored in the cookie is returned from the function.
				
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		}
	}
	return null;
	
	// Our cookie was not set.
	// The value "null" is returned from the function.
}

/**********************************************************************************************************************
//Following function sets the cookie
**********************************************************************************************************************/
function setCookie(NameOfCookie,val,expiredays)
{
	// Three variables are used to set the new cookie.
	// The name of the cookie, the value to be stored,
	// and finally the number of days until the cookie expires.
	// The first lines in the function convert
	// the number of days to a valid date.

	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

	// The next line stores the cookie, simply by assigning
	// the values to the "document.cookie" object.
	// Note the date is converted to Greenwich Mean time using
	// the "toGMTstring()" function.
	
	document.cookie = NameOfCookie + "=" + escape(val) +((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

/**********************************************************************************************************************
//Following function deletes the cookie
**********************************************************************************************************************/
function delCookie (NameOfCookie)
{

// The function simply checks to see if the cookie is set.
// If so, the expiration date is set to Jan. 1st 1970.

	if (getCookie(NameOfCookie)) {
	document.cookie = NameOfCookie + "=" +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/**********************************************************************************************************************
//Following function toggles the advanced options as per the last status
**********************************************************************************************************************/
function ToggleAdvancedOptions()
{
	opt_cookie=getCookie('search_options_on');
	
	if(opt_cookie=="off" || opt_cookie==null)
	{
		opt_cookie="on";
	}
	else if(opt_cookie=="on")
	{
		opt_cookie="off";
	} 
	
	delCookie('search_options_on');
	setCookie('search_options_on',opt_cookie,30);		
}


function ValidateForgotPassword()
{
	if(document.frm_mail.txtusername.value=="")
	{
		alert("Please enter username");
		return false;
	}
	else
	{
		return true;
	}
}

/**********************************************************************************************************************
//Following function shows the email text box for forgot password link
**********************************************************************************************************************/
function ShowEmailBox(url)
{
	target_div=document.getElementById("forgot_pass");
	newdiv=document.createElement("DIV");
	newdiv.id="email";
	newdiv.name="email";
	newdiv.style.position="absolute";
	newdiv.style.height="55px";
	newdiv.style.width="325px";
	newdiv.style.padding="10px";
	newdiv.style.border="1px solid #666666";	
	newdiv.style.MozOpacity=1.0;
	newdiv.style.background="#e3e3e3";
	newdiv.style.display="inline";
	newdiv.style.zIndex=2;
	newform=document.createElement("FORM");
	newform.id="frm_mail";
	newform.name="frm_mail";
	newform.method="post";
	newform.action=url;
	newform.onsubmit= function(){return ValidateForgotPassword();}
	newform.style.height="55px";
	newform.style.margin="0";
	newform.style.padding="0";
	newlabel=document.createElement("LABEL");
	newlabel.name="email";
	newlabel.innerHTML="Enter Username";
	newform.appendChild(newlabel);
	newtext=document.createElement("INPUT");
	newtext.type="text";
	newtext.name="txtusername";
	newtext.style.margin="0";
	newtext.style.padding="3px";
	newsubmit=document.createElement("INPUT");	
	newsubmit.type="submit";
	newsubmit.name="btnsubmit";
	newsubmit.className="submit";
	newsubmit.value="Send";
	newsubmit.style.background="#faf9f5";
	newsubmit.style.margin="5px 21px 0 0";
	newsubmit.style.padding="3px";
	closebtn=document.createElement("INPUT")
	closebtn.type="button";
	closebtn.value="Close";
	closebtn.className="submit";
	closebtn.style.background="#faf9f5";
	closebtn.style.margin="5px 5px 0 0";
	closebtn.style.padding="3px";
	closebtn.onclick=function(){target_div.removeChild(document.getElementById("email"));}
	newform.appendChild(newtext);
	newform.appendChild(newsubmit);
	newform.appendChild(closebtn);
	newdiv.appendChild(newform);
	target_div.appendChild(newdiv);
}

/**********************************************************************************************************************
//Following function toggles the xml path file upload enable/disable
**********************************************************************************************************************/
function ToggleXmlPath()
{
	feature=document.getElementById("feature_path");
	xml_path=document.getElementById("xml_path");
	newval=feature.value.replace(/^(\s)(.+[^\s])(\s)$/,/^(\"\")(.+[^\s])(\"\")$/);

	
	if(newval.match(/^(.+)\.(swf|jpeg|jpg|png|bmp)$/)==null)
	{
		alert("Not a valid image file");
		/*
		//following condition is only for mozilla
		if(document.layers && navigator.javaEnabled())
		{
			netscape.security.PrivilegeManager.enablePrivilege('UniversalFileRead');
			feature.value="";
		}
		*/
		xml_path.disabled=true;
	}
	else if(feature.value.match(/^(.+)\.(swf)$/)!=null)
	{
		xml_path.disabled=false;
	}
	
}

/**********************************************************************************************************************
//Following function validates whether the given file has .xml extension
**********************************************************************************************************************/
function ValidateXml()
{
	xml_path=document.getElementById("xml_path");
	if(xml_path.value.match(/^(.+)\.(xml)$/)==null)
	{
		alert("Invalid file type");
	}
}

/**********************************************************************************************************************
//Following function submits the search form on advanced property search page
**********************************************************************************************************************/
function SubmitSearchForm(url,form)
{
	form.action=url;
	form.submit();
}

function SelectProptype()
{
	office=document.frm_edit.office.value;
	
	typecmb=document.frm_edit.property_type;
	
	if(office!="")
	{
		if(office=="o")
		{
			typecmb.selectedIndex=0;
		}
		else if(office=="i")
		{
			typecmb.selectedIndex=1;
		}
		else if(office=="inv")
		{
			typecmb.selectedIndex=2;
		}
		else if(office=="r")
		{
			typecmb.selectedIndex=3;
		}
		else if(office=="lo")
		{
			typecmb.selectedIndex=4;
		}
		else if(office=="rsr")
		{
			typecmb.selectedIndex=5;
		}
		else if(office=="res")
		{
			typecmb.selectedIndex=6;
		}
	}
}

function EnterOffice()
{
	office=document.frm_edit.office;
	
	typecmb=document.frm_edit.property_type.selectedIndex;
	
	if(typecmb!="")
	{
		if(typecmb=="0")
		{
			office.value="o";
		}
		else if(typecmb=="2")
		{
			office.value="i";
		}
		else if(typecmb=="3")
		{
			office.value="inv";
		}
		else if(typecmb=="4")
		{
			office.value="r";
		}
		else if(typecmb=="5")
		{
			office.value="lo";
		}
		else if(typecmb=="6")
		{
			office.value="rsr";
		}
		else if(typecmb=="7")
		{
			office.value="res";
		}
	}
}


function checkallopt(obj,action)
{
   for (var i=0; i < obj.elements.length; i++) {

	   if (obj.elements[i].type == 'checkbox' && obj.elements[i].name == 'chk_delete[]')
		{
			if(action == 'true')
		     obj.elements[i].checked = true;
			else
			 obj.elements[i].checked = false;		
			
		}
   }

}