var link_num=new Array();
var current_link='';
var current_page='view.php';
var listings_page = 'imatlistings.php';

var totrec = 0;

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

/// ajax script ////
var xmlhttp = new XMLHttpRequest();
var str='';
function ajaxCall(url) {
	if(xmlhttp){
		xmlhttp.open('POST',url,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.onreadystatechange = stateChanged;
    	xmlhttp.send(null);
  }
} 

function closeparent() {
	if (window.opener != null && !window.opener.closed) {
		window.open('','main','');
		window.name="main";
		if(document.all) {
			window.opener.opener = '';
		}
		window.opener.close();
	}
}		
function stateChanged() { 
	if (xmlhttp.readyState==1){ 
		document.getElementById("output").innerHTML='Processing...';
		document.getElementById("output").className='ajax-success';
	}
	if (xmlhttp.readyState==4){ 
			var str =xmlhttp.responseText;
			if (str.match('.php')){
					location.href = str;
			}
			else {
			switch (str) {
				case '1':	
					location.href='message.php?page=profile';	
				break;
				case '2':
					location.href='message.php?page=editprofile';	
				break;
				case '3':
					location.href=listings_page;//"imatlistings.php"
				break;
				case '4':
					document.getElementById("output").style.display='inline';
				break;
				default:
					document.getElementById("output").innerHTML=str;
					document.getElementById("output").className='ajax-failure';
				break;	
			}
			}
			str='';
	}
}
/// add to comparelist
function compare_ajaxCall(url) {
	if(xmlhttp){
		xmlhttp.open('POST',url,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.onreadystatechange = compare_stateChanged;
    	xmlhttp.send(null);
  }
} 
function compare_stateChanged() { 
	
	if (xmlhttp.readyState==4){ 
		var str = xmlhttp.responseText;
		var st='';
		document.getElementById('im_'+current_link).innerHTML="";
		document.getElementById('compare_content').innerHTML = str;
		if (str!='') {
			//alert('Property added successfully to compare list');
			alert('PROPRIETE AJOUTEE AVEC SUCCES A LA LISTE DE COMPARAISON');
				Set_Cookie( 'flag_child', '1', '', '/', '', '' );
			document.getElementById('link_'+current_link).style.display='none';
			showHideLinks(totrec,'');
			if(document.getElementById('sort_order') != null)
				document.getElementById('sort_order').style.display='';
		}
		str='';
	}
}


/// add to comparelist
function compareSingle_ajaxCall(url) {
	if(xmlhttp){
		xmlhttp.open('POST',url,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.onreadystatechange = compareSingle_stateChanged;
    	xmlhttp.send(null);
  }
} 
function compareSingle_stateChanged() { 
	if (xmlhttp.readyState==4){ 
		var str = xmlhttp.responseText;
		var st='';
		document.getElementById('compare_content').innerHTML = str;
		if (str!='') {
			//alert('Propriété ajoutée avec succès à la liste de comparaison');	
			alert('PROPRIETE AJOUTEE AVEC SUCCES A LA LISTE DE COPARAISON');
			if (window.opener != null && !window.opener.closed) {
				if(window.opener.document.URL.match("view") != null) {
					var sublen = window.opener.document.ff.prp.length;
					for (j=0;j<sublen;j++){
						if (window.opener.document.ff.prp[j].value==current_link) {
							window.opener.document.getElementById('link_'+current_link).style.display='none';	
						}
					}
					window.opener.document.getElementById('compare_content').innerHTML = str;
				}
			}
			document.getElementById('im_1').innerHTML="";			
		}
		str='';
	}
}
/// remove from savelist
/// add to comparelist
function remove_ajaxCall(url) {
	if(xmlhttp){
		xmlhttp.open('POST',url,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.onreadystatechange = remove_stateChanged;
    	xmlhttp.send(null);
  }
} 
function remove_stateChanged() { 
	if (xmlhttp.readyState==4){ 
		var str = xmlhttp.responseText;
		document.getElementById('compare_content').innerHTML = str;
		if (window.opener != null && !window.opener.closed) {
			if(document.URL.match("detail") != null && window.opener.document.URL.match("imatlistings.php") != null)
			{
				window.opener.document.getElementById('compare_content').innerHTML = str;
				var sublen = window.opener.document.ff.prp.length;
			}
		}
		else if(document.URL.match(listings_page) != null)
		{
			var sublen = document.ff.prp.length;
		}
		if (str!='') {
			if(document.getElementById('sort_order') != null)
				document.getElementById('sort_order').style.display='';
				
			//alert('Propriété supprimée avec succès de la liste de comparaison');			
			alert('PROPRIETE SUPPRIMEE AVEC SUCCES DE LA LISTE DE COMPARAISON');
			rcurrent_link = current_link.split(',');
			for(i=0;i<rcurrent_link.length;i++){
				if (document.URL.match("detail") != null) {
					if (document.getElementById('prid').value==rcurrent_link[i]){
						document.getElementById('link_compare').style.display='';	
					}
				}
				for (j=0;j<sublen;j++) 
				{
					
					if (window.opener != null && !window.opener.closed) {
						if (document.URL.match("detail") != null && window.opener.document.URL.match(listings_page) != null) {
							if (window.opener.document.ff.prp[j].value==rcurrent_link[i]) {		
								window.opener.document.getElementById('link_'+rcurrent_link[i]).style.display='';
							}
						}
					}
					else if (document.URL.match(listings_page) != null){
						if(document.ff.prp[j].value==rcurrent_link[i])
							document.getElementById('link_'+rcurrent_link[i]).style.display='';
							Set_Cookie( 'flag_child', '1', '', '/', '', '' );
					}
				}
			}
		}
		str='';
	}
}

///save 
/// add to comparelist
function save_ajaxCall(url) {
	if(xmlhttp){
		xmlhttp.open('POST',url,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.onreadystatechange = save_stateChanged;
    	xmlhttp.send(null);
  }
} 
function save_stateChanged() { 
	if (xmlhttp.readyState==4){ 
		var str =xmlhttp.responseText;
		alert(str);
		document.getElementById('CompareTool_m_ibSave').disabled = false;
	}
}

function savesingle_ajaxCall(rurl) {
	if(xmlhttp){
		xmlhttp.open('POST',rurl,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.onreadystatechange = savesingle_stateChanged;
    	xmlhttp.send(null);
  }
} 
///save 
/// add to comparelist
function savesingle_ajaxCall_sini(rurl) {
	$.ajax({
			  type: "POST",
			  url: rurl,
			  success: function(msg){
			  str= msg;
			  	if(isNaN(msg) == true)
				{
					alert(msg);
				}
				else if (str==1) {
					alert('PROPRIETE AJOUTEE AVEC SUCCES');		
				}
				else if (str==2){
					alert('IDENTIFIEZ-VOUS POUR SAUVER DES BIENS');
					closeparent();
					location.href='login.php?url='+current_page;
				}
				else {
					alert('PROPRIETE DEJA SAUVE');	
				}
				document.getElementById('im_'+current_link).innerHTML="";
				showHideLinks(totrec,'');
						}
					 });
} 

function savesingle_stateChanged() { 
	if (xmlhttp.readyState==4){ 
	//alert(str);
	
		var str =xmlhttp.responseText;
		if(isNaN(str) == true && str != "Proerty Already Exists"){
			alert(str);
		}
		else if (str==1) {
			//alert('Property added successfully');
			alert('PROPRIETE AJOUTE AVEC SUCCES');
		}
		else if (str==2){
			alert('IDENTIFIEZ-VOUS POUR SAUVER DES BIENS');
			closeparent();
			location.href='login.php?url='+current_page;
		}
		else if(str == "Proerty Already Exists"){
					alert('PROPRIETE DEJA SAUVE');	
		}
		document.getElementById('im_'+current_link).innerHTML="";
		showHideLinks(totrec,'');
	}
}

function menuDetails(id){
	if (document.getElementById(id+'_val').value==1) {
		$('#'+id+'_flag').hide('slow');
		//document.getElementById(id+'_arrow').className='arrow';
		document.getElementById(id+'_val').value=0;
		switch (id){
			case 'save_search':
					var inputctrl = "<label style ='color:#FFFFFF'>Name this search:</label><br /><input type='text' name='savedsearch' id='savedsearch' class='input' maxlenght=30 />";
					var cb    = "<input type='checkbox' name='watchlst' id='watchlst' checked='checked' /><label style ='color:#FFFFFF'>Email new listings that match this search</label>";
					document.getElementById('save_search_cntrl').innerHTML=inputctrl+"<br />"+cb;	
					document.getElementById('save_search_cntrl').className='button-table';
				break;
		 }
	}
	else {
		 $('#'+id+'_flag').show('slow');
		 //document.getElementById(id+'_arrow').className='arrow-changed';
		 document.getElementById(id+'_val').value=1;
		 switch (id){
			case 'save_search':
				var inputctrl = "<label style ='color:#FFFFFF'>Name this search:</label><br /><input type='text' name='savedsearch' id='savedsearch'   class='input' maxlength='30' />";
				var cb    = "<input type='checkbox' name='watchlst' id='watchlst' checked='checked' /><label style ='color:#FFFFFF'>Email new listings that match this search</label>";
				document.getElementById('save_search_cntrl').innerHTML=inputctrl+"<br />"+cb;	document.getElementById('save_search_cntrl').className='button-table';
			break;
		 }
	}	
}

function chkExists(arr, val)
{
	flag = 0;
	for (i in arr)
	{
		if (val == arr[i].name)
		{
			flag = '1'
		}
		return flag;
	}
}
var mytext;
function searchByID()
{
	textfield	= (document.fd.textfield.value == 'Search Location Property ID or MLS ID') ? '' : document.fd.textfield.value;
	if(textfield.length>0) {
		document.fd.submit();
	}
	else {
		alert("Please enter Location or Property ID or MLS ID");
		return false;
	}
}
function sortCriteria()
{
	document.getElementById('spinner_go').innerHTML="<img src='images/common/loading.gif'>";
	sort_order			= document.ff.sort_order.value;
	var ssrch           = document.getElementById('ssrch_val').value;
	srch_id = document.getElementById('srch_id').value;
	url = "view_inc.php?pst=1&page=1&sort_order="+sort_order;
	if (ssrch >0) {
			url +="&ssrch=1&pst=1&search_id="+srch_id;
		}
	if(xmlhttp){
		xmlhttp.open('POST',url,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		xmlhttp.onreadystatechange = httpstatechange;
		xmlhttp.send(null);
	}
}


function searchCriteria(page_no){
	
	document.getElementById('spinner_go').innerHTML="<img src='images/common/loading.gif'>";	
	
	go_click 			= document.ff.go_click.value;
	
	var ssrch           = document.getElementById('ssrch_val').value;

	arrElements			= document.ff.elements;
	
	
	var textfield = '';
	var country='';
	var state='';
	var rcity='';
	var price='';
	var lifestyle='';
	var propertytype='';
	var bedrooms='';
	var bathrooms='';
	var amenities='';
	var propertystyles='';
	var searchname='';
	var email_flag='';
	
	/* Altered by Srinivvasan K the field has been removed which in the top search form*/
	if(document.getElementById('textfield') != null)
	{
		textfield			= (document.getElementById('textfield').value == 'Search Location Property ID or MLS ID') ? '' : document.getElementById('textfield').value;
	}
	//alert(page_no);
	sort_order			= document.ff.sort_order.value;
	//country             = (document.ff.location_val.value == 1) ? document.ff.country.value : ''; 
	/*state				= (document.ff.location_val.value == 1  && document.ff.country.value != '') ? document.ff.state.value : '';
	rcity				= (document.ff.location_val.value == 1 && document.ff.state.value != '') ? document.getElementById('city').value : '';*/
	country             = (document.ff.location_val.value == 1) ? document.ff.country.value : ''; 
	if(country!="") {
	state				= (document.ff.location_val.value == 1  && document.ff.country.value != '') ? document.ff.state.value : '';
	}
	if(country!="") {
	rcity				= (document.ff.location_val.value == 1 && document.ff.state.value != '') ? document.getElementById('city').value : '';
	}
	price				= (document.ff.price_val.value == 1) ? document.ff.price.value : '';
	lifestyle			= (document.ff.lifestyle_val.value == 1) ? document.ff.lifestyle.value : '';
	propertytype		= (document.ff.propertytype_val.value == 1) ? document.ff.propertytype.value : '';
	bedrooms			= (document.ff.bedrooms_val.value == 1) ? document.ff.bedrooms.value : '';
	bathrooms			= (document.ff.bathrooms_val.value == 1) ? document.ff.bathrooms.value : '';
	amenities			= (document.ff.amenities_val.value == 1) ? document.ff.amenities.value : '';
	propertystyles		= (document.ff.propertystyles_val.value == 1) ? document.ff.propertystyles.value : '';
	searchname		    = (document.getElementById('save_search_val').value == 1) ? document.getElementById('savedsearch').value : '';
	email_flag          = (document.getElementById('save_search_val').value == 1) ? document.getElementById('watchlst').checked : '';
	if(state=='' && rcity=='' && price=='' && lifestyle=='' && propertytype=='' && bedrooms=='' && bathrooms=='' && amenities=='' && propertystyles=='' && searchname=='' && email_flag=='') {
		document.getElementById("txtSID").value = 1;
		} else {
		document.getElementById("txtSID").value = 0;
	}
	
	
	var strQueryPrice = "";
	if(price!="") {
		strQueryPrice = "";
	}

	if(!isValidSearchName(searchname) && searchname.length>0) {
		alert('RECHERCHE NON VALIDE NOM');		
		 document.getElementById('spinner_go').innerHTML="";	
	}
	else{
		srch_id = document.getElementById('srch_id').value;
		intSID = document.getElementById("txtSID").value;
		if(page_no>=1)
		{			
			search_url = "pgn=1&page="+page_no+"&sort_order="+sort_order+"&txtSID="+intSID+strQueryPrice;//"&spur=N";
		}
		else
		{
			search_url = "textfield="+textfield+"&sort_order="+sort_order+"&state="+state+"&rcity="+rcity+"&price="+price+"&lifestyle="+lifestyle+"&propertytype="+propertytype+"&bedrooms="+bedrooms+"&bathrooms="+bathrooms+"&amenities="+amenities+"&propertystyles="+propertystyles+"&searchname="+searchname+"&email_flag="+email_flag+"&country="+country+"&txtSID="+intSID+strQueryPrice; }//"&spur=N";		}
	
		if (ssrch >0 && go_click!=1) {
			search_url +="&ssrch=1&pst=1&search_id="+srch_id+"&txtSID="+intSID+strQueryPrice;//"&spur=N";
		}
		//alert(search_url);
			$.ajax({
		   type: "POST",
		   url: "view_inc.php",
		   data: search_url,
		   success: function(mytext){
				document.getElementById('spinner_go').innerHTML="";	
				var msg = mytext.split('~!^zz');
				var teststr = msg[0];
				var msgstr ='';
				var cont ='';
				if (teststr.match('<table')){
					cont = msg[0];			
					document.getElementById('savename_content').innerHTML='';
					document.getElementById('savename_flag').style.display='none';
				}
				else{
					msgstr=msg[0];
					cont = msg[1];
					document.getElementById('savename_content').innerHTML=msgstr;
					document.getElementById('savename_flag').style.display='';
					
				}
				
				
		    	var objMore = document.getElementById("listingResults");
		    	objMore.innerHTML = cont;
		    	mytext="";
		   }
		 });
	}
	if(page_no<1)
	{
		return false;
	}	
 }


function clearTextBox(){
	alert(document.getElementById('textfield').value);
	//document.getElementById('textfield').value='';
}

function isValidEmail(strEmail){
  validRegExp = /^[\w\.-]+@[a-z,A-Z,0-9-]+[\.]{1}[a-zA-Z]{2,}[[\.]?[a-zA-Z]{0,2}$/i;
   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) {
      return false;
    } 
    return true; 
}
function isValidSearchName(searchname){
 validRegExp = /^[\w]+$/;
    if (searchname.match(validRegExp) == null) {
      return false;
    } 
    return true;
}
// user - add/ modify/delete
function addUser(){
	
	document.getElementById('username_msg').innerHTML='';
	document.getElementById('email_msg').innerHTML='';
	document.getElementById('password_msg').innerHTML='';
	document.getElementById('confirm_password_msg').innerHTML='';
	document.getElementById('state_msg').innerHTML='';
	document.getElementById('city_msg').innerHTML='';
	document.getElementById('address_msg').innerHTML='';
	document.getElementById('country_msg').innerHTML='';
	
	var username =trim( document.getElementById('username').value);
	var email    = trim(document.getElementById('email').value);
	var password = trim(document.getElementById('password').value);
	var confirm_password = trim(document.getElementById('confirm_password').value);
	var country  = trim(document.getElementById('country').value);
	var state    = trim(document.getElementById('state').value);
	var city     = trim(document.getElementById('city').value);
	var address  = escape(trim(document.getElementById('address').value));
	var bttn     = document.getElementById('reg_button').value;
	var passflag=1;
	var flag=true;
	var old_password ='';
	if (!isValidUserName(username) || username.length<3){
		document.getElementById('username_msg').innerHTML='Nom incorrect';
		document.getElementById('username_msg').style.color='red';
		flag=false;
	}
	if (!isValidEmail(email) || email.length<5){
		document.getElementById('email_msg').innerHTML='Email incorrect';
		document.getElementById('email_msg').style.color='red';
		flag=false;
	}
	if (bttn=='add') { 
		passflag=1 ;
	}
	else 
	if (bttn=='Update' || bttn=='Mettre à jour') { 
		if (document.getElementById('pass_group1').style.display=='') {
			old_password = trim(document.getElementById('old_password').value);
			document.getElementById('old_password_msg').innerHTML='';
			if (old_password.length<6){
				document.getElementById('old_password_msg').innerHTML='Le mot de passe doit être composé d’au moins 6 caractères';
				document.getElementById('old_password_msg').style.color='red';
			}
		}
		else {
			passflag=0;	
		}
	}
	if (password.length<6  && passflag==1){
		document.getElementById('password_msg').innerHTML='Le mot de passe doit être composé d’au moins 6 caractères';
		document.getElementById('password_msg').style.color='red';
		flag=false;
	}
	if (confirm_password!=password){
		document.getElementById('confirm_password_msg').innerHTML='Confirm&eacute; le mot de passe ne correspond pas avec le mot de passe';
		document.getElementById('confirm_password_msg').style.color='red';
		flag=false;
	}
	
	if (country.length<1 ) {
		document.getElementById('country_msg').innerHTML='sélectionner un pays';
		document.getElementById('country_msg').style.color='red';
		flag=false;
	}
	if (!isValidName(state) || state.length<2){
		document.getElementById('state_msg').innerHTML='&Eacute;tat non valide nom';
		document.getElementById('state_msg').style.color='red';
		flag=false;
	}
	if (!isValidName(city) || city.length<2){
		document.getElementById('city_msg').innerHTML='Nom de ville incorrect';
		document.getElementById('city_msg').style.color='red';
		flag=false;
	}
	if (address.length<1) {
		document.getElementById('address_msg').innerHTML='Le champ « adresse » ne peut être vide';
		document.getElementById('address_msg').style.color='red';
		flag=false;
	}
	else 
	if(!isValidAddress(address) && address.length>1){
		document.getElementById('address_msg').innerHTML='Invalid Address';
		document.getElementById('address_msg').style.color='red';
		flag=false;	
	}
	
	if (flag) {
		querystring ="username="+username+"&password="+password+"&email="+email+"&country="+country+"&state="+state+"&city="+city+"&address="+address+"&old_password="+old_password;	
		if (bttn=='Update' ||  bttn=='Mettre à jour') {
			option='update&';	
		}
		else {
			option='adduser&';		
		}
		ajaxCall('profile.php?option='+option+querystring);
		return false;
	}
	else {
		return flag;
	}
}


// validate uset
function validateUser(){
	var email = trim(document.getElementById('emailx').value);
	var password = trim(document.getElementById('passwordx').value);
	var url = trim(document.getElementById('strurl').value);
	
	var flag=true;
	if (!isValidEmail(email) || email.length<5){
		flag=false;
	}
	if (password.length<6){
		flag=false;
	}
	if (flag) {
		querystring ="email="+email+"&password="+password+"&strurl="+url;	
		ajaxCall('login.php?option=validate&'+querystring);
		return false;
	}
	else {
		document.getElementById('output').innerHTML='Invalid Login';
		document.getElementById('output').className='ajax-failure';
		return false;
	}
}

function changePassword(flag){
	//document.getElementById('pass_link').style.display='table-row';
	if (flag) {
		document.getElementById('link_content').innerHTML="<a href='javascript:changePassword(0)'> Pas besoin de changer le mot de passe </a>";
		document.getElementById('pass_group1').style.display='';
		document.getElementById('pass_group2').style.display='';
		document.getElementById('pass_group3').style.display='';
	}
	else {
		document.getElementById('link_content').innerHTML="<a href='javascript:changePassword(1)'> Click here to change password </a>";
		document.getElementById('pass_group1').style.display='none';
		document.getElementById('pass_group2').style.display='none';
		document.getElementById('pass_group3').style.display='none';	
	}
	
}
function sendPassword(){
	var email = document.getElementById('email').value;
	document.getElementById('email_msg').innerHTML='';
	var flag=true;
	if (!isValidEmail(email) || email.length<3) {
		document.getElementById('email_msg').innerHTML=' Courriel invalide';
		document.getElementById('email_msg').style.color='red';
		flag=false;
	}
	if (flag) {
		querystring ="email="+email;
		ajaxCall('profile.php?option=resetpassword&'+querystring);			
	}
	return false;
}

function showHideCompare(flag){
	if (flag==1) {
		document.getElementById('compareTool').style.display='inline';
		if(document.getElementById('sort_order') != null)
		{
				document.getElementById('sort_order').style.display='none';
		}
		document.getElementById('compareBtn').innerHTML="<img src='images/buttons/btn_compareproperties_dn.gif' name='compareImage' id='compareImage' alt='' onClick='showHideCompare(0);'>";
	}
	else {
		document.getElementById('compareTool').style.display='none';
		if(document.getElementById('sort_order') != null)
		{
			document.getElementById('sort_order').style.display='';
		}		
		document.getElementById('compareBtn').innerHTML="<img src='images/buttons/btn_compareproperties_up.gif' name='compareImage' id='compareImage' alt='' onClick='showHideCompare(1);'>";	
	}
}

function showHideLinks(total,display){
	for (i=1;i<=total;i++){
		document.getElementById('sidx_'+i).style.display=display;	
	}

}
function compareList(prpid,option,nm,totalrec){
	querystring ="prpid="+prpid;
	current_link = nm;
    totrec= totalrec;
	var st='';
	if (option=='add'){
		//for (i=1;i<=totrec;i++) {
			//if(document.getElementById('link_'+i).style.display=='none'){
				//link_num[i] = 'none';
				//st +=link_num[i]+',';
		//	}
			//else {
				//link_num[i] ='';	
				//st +=link_num[i]+',';
				//document.getElementById('link_'+nm).style.display='none';
			//}
			document.getElementById('link_'+nm).style.display='none';
			document.getElementById('im_'+current_link).innerHTML="<img src='images/common/loading1.gif'>";
			showHideLinks(totrec,'none');
		}
		
		//Element.show('spinner_sort');
	compare_ajaxCall('comparelist.php?option='+option+'&'+querystring);
}

function compareSingleList(prpid,nm){
	current_link = nm;
	querystring ="prpid="+prpid;
	compareSingle_ajaxCall('comparelist.php?option=add&'+querystring);
	document.getElementById('link_compare').style.display='none';
	document.getElementById('im_1').innerHTML="<img src='images/common/loading1.gif'>";
}




function removeList(tot){
	document.getElementById('remove').disabled = true;
	var str='';
	var lnk='';
	var count=0;
	if (tot>0) {
		flag = document.getElementById('cbx1');
		if (flag.checked) {
			var st = document.getElementById('cbx1').value;
			st = st.split('|');
			str = st[0];
			lnk = st[1];
			count++;
		}
		for (i=2;i<=tot;i++){
			flag = document.getElementById('cbx'+i);
			if (flag.checked) {
				if (str!='') {	
					str += ',';
					lnk += ',';
				}
				var st = document.getElementById('cbx'+i).value;
				st = st.split('|');
				str += st[0];
				lnk += st[1];
			}
		}
		if (str!='') {	
			querystring ="prpid="+str;
			remove_ajaxCall('comparelist.php?option=delete&'+querystring);
			current_link = lnk;
			//str.split(',');
		}
		else {
			alert('VOUS DEVEZ SELECTIONNER AU MOINS UN ETABLISSEMENT POUR EN ENLEVER');
			document.getElementById('remove').disabled = false;
		}
   }
   else {
			alert('VOUS DEVEZ SELECTIONNER AU MOINS UN ETABLISSEMENT POUR EN ENLEVER');
			document.getElementById('remove').disabled = false;
		}
		return false;
}

function compareProperties(tot){
	var str ='';
	var count=0;
	
	if (tot>0) {
		flag = document.getElementById('cbx1');
		if (flag.checked) {
			var st = document.getElementById('cbx1').value;
			st = st.split('|');
			str = st[0]; 
			count++;
		}
		for (i=2;i<=tot;i++){
			flag = document.getElementById('cbx'+i);
			if (flag.checked) {
				count++;
				if (str!='') {	
					str += ',';
				}
				var st = document.getElementById('cbx'+i).value;
				st = st.split('|');
				str += st[0];
			}
		}
	}
	if (count <2 || tot <1 ) {
		alert('VOUS DEVEZ CHOISIR AU MOINS 2 PROPRIETES POUR POUVOIR LES COMPARER');
		//alert('You must select atleast two properties to compare');
	}
	else 
	if (count >4 ) {
		//alert('Maximum four properties can be compared at a time');
		alert('UN MAXIMUM DE 4 PROPRIETES PEUT ETRE COMPARE EN MEME TEMPS');
	}
	else {
		var win_url = "compare_details.php?prpid="+str;
		newPopUpWindow(win_url,'compare Properties',716,780,'yes');
	}
	return false;
}

function saveProperty(tot){
	var str ='';
	//alert(tot);
	var count=0;
	if (tot>0) {
		flag = document.getElementById('cbx1');
		if (flag.checked) {
			var st = document.getElementById('cbx1').value;
			st = st.split('|');
			str = st[1]; 
			count++;
		}
		for (i=2;i<=tot;i++){
			flag = document.getElementById('cbx'+i);
			if (flag.checked) {
				count++;
				if (str!='') {	
					str += ',';
				}
				var st = document.getElementById('cbx'+i).value;
				st = st.split('|');
				str += st[1];
			}
		}
	}
	if (count <1 || tot <1 ) {
		alert("VOUS DEVEZ SELECTIONNER AU MOINS UN ETALISSEMENT POUR SAUVER");
		//alert('You must select atleast one property to save');			
	}
	else {
		document.getElementById('CompareTool_m_ibSave').disabled = true;
		querystring = "property_id="+str;
		//alert(querystring);
		save_ajaxCall('save_property.php?option=save&'+querystring);
	}
	return false;
}

function saveSingleProperty(propertyid,nm,page,total){
	totrec=total;
	current_link = nm;
	current_page = page;
	pid  = propertyid;
	querystring = "option=save&property_id="+propertyid;
	document.getElementById('im_'+current_link).innerHTML="<img src='images/common/loading1.gif'>";
	savesingle_ajaxCall('save_property.php?option=save&'+querystring);
	showHideLinks(totrec,'none');
}

function isValidUserName(val) {
	 validRegExp = /^[a-zA-Z. ]+$/i;
   // search email text for regular exp matches
   val = trim(val);
    if (val.search(validRegExp) == -1) {
      return false;
    } 
    return true; 	
}

function isValidName(val) {
	 validRegExp = /^[a-zA-Z ]+$/i;
   // search email text for regular exp matches
   val = trim(val);
    if (val.search(validRegExp) == -1) {
      return false;
    } 
    return true; 	
}

function newPopUpWindow(page, name, w, h, scroll) { 
	name = "foo";
	page = page.split(" ").join("%20");
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	//var resizable = false;
	winprop  = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes';
	win = window.open(page, name, winprop);
    win.focus();
 }
function  isValidAddress(addr){
	//validRegExp = /^[0-9a-zA-Z\s\,./-]+$/;
	validRegExp = /^[0-9a-zA-Z \s,.\!\@\#\$\%\^\&\*\(\)\?\_\'\"/-]+$/;
    if (addr.match(validRegExp) == null) {
      return false;
    } 
    return true;
}
function  isValidComment(commt){
	validRegExp = /^[0-9a-zA-Z \s,.\!\@\#\$\%\^\&\*\(\)\?\_\'\"/-]+$/;
    if (commt.match(validRegExp) == null) {
      return false;
    } 
    return true;
}
function isValidPhone(val) {
	 validRegExp = /^[0-9 -]+$/;
   // search email text for regular exp matches
   val = trim(val);
    if (val.match(validRegExp) == null) {
      return false;
    } 
    return true; 	
}
function validateContact(){
	var result_ajax = '';
	var email 	 = document.contact.email.value;
	var fname 	 = document.contact.fname.value;
	var lname 	 = document.contact.lname.value;
	var hphone 	 = document.contact.hphone.value;
	var comments = escape(document.contact.comments.value);
	var flag = true;
	document.getElementById('email_err').innerHTML ='';
	document.getElementById('fname_err').innerHTML ='';
	document.getElementById('lname_err').innerHTML ='';
	document.getElementById('hphone_err').innerHTML ='';
	document.getElementById('comments_err').innerHTML ='';
	if (!isValidEmail(email) || email.length<3) {
		document.getElementById('email_err').innerHTML='L\'Invalid Adresse e-mail';
		flag = false;
	}
	if (!isValidName(fname) && fname.length>0){
		document.getElementById('fname_err').innerHTML='L\'Invalide Nom';
		flag = false;
	}
	if (!isValidName(lname) && lname.length>0){
		document.getElementById('lname_err').innerHTML='L\'Invalid Prénom';
		flag = false;
	}
	if (hphone.length>1){
		if(hphone.length<6 || !isValidPhone(hphone)) {
		document.getElementById('hphone_err').innerHTML='L\'Invalid Téléphone';
		flag = false;
		}
	}
	if (!isValidComment(comments) && comments.length>0){
		document.getElementById('comments_err').innerHTML='L\'Invalid Message';
		flag = false;
	}
	if (flag) {
		querystring ="email="+email+"&fname="+fname+"&lname"+lname+"&hphone="+hphone+"&comments="+comments;
		url= 'contactus.php?option=contactus&'+querystring;	
		if(xmlhttp){
			xmlhttp.open('POST',url,true);
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			xmlhttp.onreadystatechange = httpcontactchange;
			xmlhttp.send(null);
		}
		return false;
	}
	return false;
}
function httpcontactchange(){
	if (xmlhttp.readyState==1){
			document.getElementById('spinner').innerHTML = "<img src='images/common/loading.gif' alt='Loading...'>";
	}
		
	if (xmlhttp.readyState == 4){
		mytext = xmlhttp.responseText;
		document.getElementById("contact_us").innerHTML = mytext;
		mytext="";
	}
}

function lgx(flag){
	if (flag){	
	     var fileLoadingImage = "images/loading.gif";
		 var objBody = document.getElementsByTagName("body").item(0);
		 var objOverlay = document.createElement("div");
  		 objOverlay.setAttribute('id','overlay');
	     objBody.appendChild(objOverlay);
		  var objLoadingImage = document.createElement("img");
		  objLoadingImage.setAttribute('src', fileLoadingImage);
		  objLoadingImage.setAttribute('vspace', "278");
		  objOverlay.appendChild(objLoadingImage);
		  document.getElementById('overlay').style.top = window.screen.height-500;
	      document.getElementById('overlay').style.height=window.screen.height;
	}
	
	else {
		     document.getElementById('overlay').style.display='none';
	  	 }
}

function idsearch(){
		//alert('test');
    var val = trim(document.getElementById('textfield').value);
	closeparent();
    if (val=='' || val=='Search Location Property ID or MLS ID') {
        alert('Invalid search parameter');
		return false;
    }
    else {
        document.fd.action='view.php?pst=1&textfield='+val;
        document.fd.submit()
		return true;
    }
}
function deleteSearch(id) {
	if(confirm("Are you sure you want to delete?")) {
		location.href="mysavedsearches.php?option=delete&id="+id;
	}
}


function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function OpenPopUpWindow(url){
	var popup_window = window.open(url,'','status=1,scrollbars=1,width=780,height=700');
		popup_window.focus();
}


function propertyDetails(){
	var details_window = window.open('details.php','Property details','status=1,scrollbars=0,width=600,height=600');
}
function sendToFriend(){
	var email_window = window.open('email.php','Send to friend','status=1,scrollbars=1,width=600,height=600');
}
function printDetails(url){
	var print_window = window.open(url,'print','status=1,scrollbars=1,width=790,height=600');
		print_window.focus();
}

function resetCriteria(){
	//alert("Test");
	//document.ff.reset();
	//document.getElementById('location_flag').className='view_default';
//	document.getElementById('price_flag').className='view_default';
//	document.getElementById('lifestyle_flag').className='view_default';
//	document.getElementById('propertytype_flag').className='view_default';
//	document.getElementById('bedrooms_flag').className='view_default';
//	document.getElementById('bathrooms_flag').className='view_default';
//	document.getElementById('amenities_flag').className='view_default';
//	document.getElementById('propertystyles_flag').className='view_default';
	//document.ff.action = "view.php?pst=1";
	//document.ff.action = "view_new.php?pst=1";
	window.location = listings_page;
	//document.ff.target="_self";
	//document.ff.submit();
}	
function doNull()
{
	return;
	exit;
}
