var link_num=new Array();
var current_link='';
var current_page='view.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 brokenImage(imageObject)
{
var name=imageObject.src;
imageObject.src=name;
}

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='view.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(str) 
{ 
	$.unblockUI();		
	var st='';
	//document.getElementById('im_'+current_link).innerHTML="";
	document.getElementById('compare_content').innerHTML = str;
	if (str!='') {
		alert('Property added successfully to compare list');
			Set_Cookie( 'flag_child', '1', '', '/', '', '' );
		document.getElementById('link_'+current_link).style.display='none';
		showHideLinks(totrec,'');
		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(str) { 
	if (xmlhttp.readyState==4){ 
		$.unblockUI();				
		var str = xmlhttp.responseText;
		var st='';
		document.getElementById('compare_content').innerHTML = str;
		if (str!='') {
			alert('Property added successfully to compare list');	
			document.getElementById('link_compare').style.display='none';
			if (window.opener != null && !window.opener.closed) {
				if(window.opener.document.URL.match("view.php") != 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;
				}
			}
		}
		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(str) { 
	if (xmlhttp.readyState==4){ 
		$.unblockUI();		
		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("view") != null)
			{
				window.opener.document.getElementById('compare_content').innerHTML = str;
				var sublen = window.opener.document.ff.prp.length;
			}
		}
		else if(document.URL.match("view.php") != null)
		{
			var sublen = document.ff.prp.length;
		}
		if (str!='') {
			document.getElementById('sort_order').style.display='';
			alert('Property removed successfully from compare list');	
			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("view") != 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("view.php") != 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(str) { 
		$.unblockUI();
		if (str==1) {
			alert('Properties added successfully');		
		}
		else if (str==2){
			alert('Login to save property');	
		}
		else {
			alert('One or more selected Property already saved');	
		}
}

///save 
/// add to comparelist
function savesingle_ajaxCall(url) {
	if(xmlhttp){
		xmlhttp.open('POST',url,true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.onreadystatechange = savesingle_stateChanged;
    	xmlhttp.send(null);
  }
} 
function savesingle_stateChanged() 
{ 
//	$.unblockUI();
if (xmlhttp.readyState==4){ 
	$.unblockUI();
	var str =xmlhttp.responseText;
	if (str==1) {
		alert('Property added successfully');		
	}
	else if (str==2){
		alert('Login to save property');
		//closeparent();
		//location.href='login.php?url='+current_page;
	}
	else if (str=="Property Already Exists") {
		alert('Property already saved');	
	}
	//document.getElementById('im_'+current_link).innerHTML="";
	showHideLinks(totrec,'');
 }
}

function menuDetails(id){
	if (document.getElementById(id+'_val').value==1) {
		document.getElementById(id+'_flag').className='view_default';
		document.getElementById(id+'_arrow').className='arrow';
		document.getElementById(id+'_val').value=0;
		switch (id){
			case 'location':
					//getState();
					//document.getElementById('state_cntrl').className='button-table';
					getCities();
					document.getElementById('city_cntrl').className='button-table';
				break;
			case 'price':	
					getPrice();
				break;
			case 'lifestyle':
					getLifeStyle();
				break;
			case 'propertytype':
					getPropertyType();
				break;
			case 'bedrooms':
					getBedRooms();
				break;
			case 'bathrooms':
					getBathRooms();
				break;	
			case 'amenities':
					getAmenities();
				break;
			case 'propertystyles':
					getPropertyStyles();
				break;
			case 'listingtypes':
					getListingTypes();
				break;
			case 'save_search':
					var inputctrl = "Name this search:<br /><input type='text' name='savedsearch' id='savedsearch' class='input' maxlenght=30 />";
					var cb    = "<input type='checkbox' name='watchlst' id='watchlst' checked='checked' />Email new listings that match this search";
					document.getElementById('save_search_cntrl').innerHTML=inputctrl+"<br />"+cb;	
					document.getElementById('save_search_cntrl').className='button-table';
				break;
		 }
	}
	else {
		 document.getElementById(id+'_flag').className='view_content';
		 document.getElementById(id+'_arrow').className='arrow-changed';
		 document.getElementById(id+'_val').value=1;
		 switch (id){
			case 'location':
					//getState();
					//document.getElementById('state_cntrl').className='button-table';
					getCities();
					document.getElementById('city_cntrl').className='button-table';
				break;
			case 'price':	
					getPrice();
				break;
			case 'lifestyle':
					getLifeStyle();
				break;
			case 'propertytype':
					getPropertyType();
				break;
			case 'bedrooms':
					getBedRooms();
				break;
			case 'bathrooms':
					getBathRooms();
				break;	
			case 'amenities':
					getAmenities();
				break;
			case 'propertystyles':
					getPropertyStyles();
				break;
			case 'listingtypes':
					getListingTypes();
				break;	
			case 'save_search':
				var inputctrl = "Name this search:<br /><input type='text' name='savedsearch' id='savedsearch'   class='input' maxlength='30' />";
				var cb    = "<input type='checkbox' name='watchlst' id='watchlst' checked='checked' />Email new listings that match this search";
				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;
	}
}
if (window.XMLHttpRequest){

          // If IE7, Mozilla, Safari, etc: Use native object
          var xmlhttp = new XMLHttpRequest();

}
else
{
if (window.ActiveXObject){

          // ...otherwise, use the ActiveX control for IE5.x and IE6
          var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }

}
//var xmlhttp = new XMLHttpRequest();
var mytext;
function searchByID()
{
	textfield	= (trim(document.fd.textfield.value) == 'Search Location or Property ID') ? '' : document.fd.textfield.value;
	if(textfield.length>0) {
		document.fd.submit();
	}
	else {
		alert("Please enter Location or Property 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;
	//-------For Rental Modified By Ajoydhas---------------//
	var Rentalurl=window.location.href;
	var Rentalarrsplitted = Rentalurl.split('/');
	split_url             = Rentalarrsplitted[Rentalarrsplitted.length-1];
	split_page            = split_url.split('?');
	if(split_page[0] == 'rentals.php')
			url = "viewrental_inc.php?pst=1&page=1&sort_order="+sort_order;
		else
			url = "view_inc.php?pst=1&page=1&sort_order="+sort_order;
	//-------For Rental Modified By Ajoydhas---------------//
	//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 searchCriteriaRental(page_no)
{
	window.scrollTo(300,100);
	var Rentalurl=window.location.href;
	var Rentalarrsplitted=Rentalurl.split('/');
	//document.getElementById('spinner_go').innerHTML="<img src='images/common/loading.gif'>";	
	//document.getElementById('spinner_go').style.display = '';
	//document.getElementById('spinner_sort').style.display = '';
	go_click 			= document.ff.go_click.value;
	var ssrch           = document.getElementById('ssrch_val').value;
	arrElements			= document.ff.elements;
	textfield			= document.fd.textfield.value;
	find_property		= document.fd.find_property.value;
	if(textfield == 'Search Location or Property ID') {
		textfield ='';
	}
	if(find_property == 'Tuxedo Park')
		rcity = 'Tuxedo Park';	
	else if(find_property == 'Orange County')
		rcity = 'Orange County';
	else if(find_property == 'Sterlington')
		rcity = 'Sterlington';
	else if(find_property == 'Sullivan County')
		rcity = 'Sullivan County';
	else
		rcity				= (document.ff.location_val.value == 1 && document.ff.state.value != '') ? document.ff.rcity.value : '';	

	sort_order			= document.ff.sort_order.value;
	state				= (document.ff.location_val.value == 1) ? document.ff.state.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 : '';

	var Rentalurl=window.location.href;
	var Rentalarrsplitted=Rentalurl.split('/');
	split_url   = Rentalarrsplitted[Rentalarrsplitted.length-1];
	split_page  = split_url.split('?');
	if (split_page[0] == 'rentals.php')
		Page = "viewrental_inc.php";
	else
		Page = "view_inc.php";
		
	if(!isValidSearchName(searchname) && searchname.length>0) {
		alert('Invalid search name');		
		 document.getElementById('spinner_go').innerHTML="";	
	}
	else{
		srch_id = document.getElementById('srch_id').value;
		
	if(page_no>=1)
	{
		url = "pgn=1&page="+page_no+"&sort_order="+sort_order;
	}
	else
	{
		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;
	}
	if (ssrch >0 && go_click!=1) {
		url +="&ssrch=1&pst=1&search_id="+srch_id;
	}

	$.blockUI({ backgroundColor: ''});
	var response = new $.ajax(
	{
			'type': 'post',
			'url': 'viewrental_inc.php',			
			'data': url,
			success: function(msg){
				httpstatechange(msg);
			}
	}); 
}
	if(page_no<1)
	{
		return false;
	}	
}
function searchCriteria(page_no)
{
	window.scrollTo(300,100);
	//document.getElementById('spinner_go').innerHTML="<img src='images/common/loading.gif'>";	
	//document.getElementById('spinner_go').style.display = '';
	//document.getElementById('spinner_sort').style.display = '';
	go_click 			= document.ff.go_click.value;
	var ssrch           = document.getElementById('ssrch_val').value;
	arrElements			= document.ff.elements;
	textfield			= document.fd.textfield.value;	
	find_property		= document.fd.find_property.value;
	if(textfield == 'Search Location or Property ID') 
	{
		textfield ='';
	}
	if(find_property == 'Tuxedo Park')
		rcity = 'Tuxedo Park';
	else if(find_property == 'Orange County')
		rcity = 'Orange County';
	else if(find_property == 'Sullivan County')
		rcity = 'Sullivan County';
	else if(find_property == 'Sterlington')
		rcity = 'Sterlington';
	else
		rcity				= (document.ff.location_val.value == 1 && document.ff.state.value != '') ? document.ff.rcity.value : '';	

	sort_order			= document.ff.sort_order.value;
	state				= (document.ff.location_val.value == 1) ? document.ff.state.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(!isValidSearchName(searchname) && searchname.length>0) {
		alert('Invalid search name');		
		 document.getElementById('spinner_go').innerHTML="";	
	}
	else{
		srch_id = document.getElementById('srch_id').value;
		if(page_no>=1)
		{
			url = "pgn=1&page="+page_no+"&sort_order="+sort_order+"&rcity="+rcity;
		}
		else
		{
			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;	
		}
		if (ssrch >0 && go_click!=1) {
			url +="&ssrch=1&pst=1&search_id="+srch_id;
		}

		$.blockUI({ backgroundColor: ''});
		var response = new $.ajax(
		{
				'type': 'post',
				'url': 'view_inc.php',
				'data': url,
				success: function(msg){
					httpstatechange(msg);
				}
		}); 
	}
	if(page_no<1)
	{
		return false;
	}	
}
function sortControl()
{
	var Rentalurl=window.location.href;
	var Rentalarrsplitted=Rentalurl.split('/');
	split_url   = Rentalarrsplitted[Rentalarrsplitted.length-1];
	split_page  = split_url.split('?');
	if (split_page[0] == 'rentals.php')
		document.getElementById('sort_order').value = 'city-asc';	
}
function httpstatechange(mytext)
{
	$.unblockUI();		
	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="";
}

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='Invalid Name';
		document.getElementById('username_msg').style.color='red';
		flag=false;
	}
	if (!isValidEmail(email) || email.length<5){
		document.getElementById('email_msg').innerHTML='Invalid Email';
		document.getElementById('email_msg').style.color='red';
		flag=false;
	}
	if (bttn=='add') { 
		passflag=1 ;
	}
	else 
	if (bttn=='Update') { 
		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='Password should have min 6 char(s)';
				document.getElementById('old_password_msg').style.color='red';
			}
		}
		else {
			passflag=0;	
		}
	}
	if (password.length<6  && passflag==1){
		document.getElementById('password_msg').innerHTML='Password should have min 6 char(s)';
		document.getElementById('password_msg').style.color='red';
		flag=false;
	}
	if (confirm_password!=password){
		document.getElementById('confirm_password_msg').innerHTML='Confirmed password does not match with password';
		document.getElementById('confirm_password_msg').style.color='red';
		flag=false;
	}
	
	if (country.length<1 ) {
		document.getElementById('country_msg').innerHTML='Select a country';
		document.getElementById('country_msg').style.color='red';
		flag=false;
	}
	if (!isValidName(state) || state.length<3){
		document.getElementById('state_msg').innerHTML='Invalid State Name';
		document.getElementById('state_msg').style.color='red';
		flag=false;
	}
	if (!isValidName(city) || city.length<3){
		document.getElementById('city_msg').innerHTML='Invalid City name';
		document.getElementById('city_msg').style.color='red';
		flag=false;
	}
	if (address.length<1) {
		document.getElementById('address_msg').innerHTML='Address cannot be empty';
		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') {
			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)'> No need to change password </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=' Invalid Email';
		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';
		document.getElementById('sort_order').style.display='none';
		document.getElementById('compareBtn').innerHTML="<img src='images/buttons/btn_compareproperties_dn.gif' name='compareImage' id='compareImage' width=129 height=16 alt='' onClick='showHideCompare(0);'>";
	}
	else {
		document.getElementById('compareTool').style.display='none';
		document.getElementById('sort_order').style.display='';
		document.getElementById('compareBtn').innerHTML="<img src='images/buttons/btn_compareproperties_up.gif' name='compareImage' id='compareImage' width=129 height=16 alt='' onClick='showHideCompare(1);'>";	
	}
}

/*function compareList(prpid,option){
	querystring ="prpid="+prpid;
	if (option=='add'){
		document.getElementById('link'+prpid).style.display='none';		
		//Element.show('spinner_sort');
	}
	compare_ajaxCall('comparelist.php?option='+option+'&'+querystring);
}
*/
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/loading.gif'>";
			showHideLinks(totrec,'none');
		}
		//Element.show('spinner_sort');
		
		$.blockUI({ backgroundColor: ''});
		var response = new $.ajax(
		{
				'type': 'post',
				'url' : 'comparelist.php',
				'data': 'option='+option+'&'+querystring,
				success: function(msg){
					compare_stateChanged(msg);
				}
		}); 		
}

function compareSingleList(prpid,nm){
	current_link = nm;
	querystring ="prpid="+prpid;
	$.blockUI({ backgroundColor: ''});
	compareSingle_ajaxCall('comparelist.php?option=add&'+querystring);
/*	var response = new $.ajax(
	{
			'type': 'post',
			'url' : 'comparelist.php',
			'data': 'option=add&'+querystring,
			success: function(msg){
				alert(msg);
				compareSingle_stateChanged(msg);
			}
	}); 		
*/
}




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;
			$.blockUI({ backgroundColor: ''});
			/*	var response = new $.ajax(
				{
					'type': 'post',
					'url' : 'comparelist.php',
					'data': 'option=delete&'+querystring,
					success: function(msg){
						remove_stateChanged(msg);
					}
				}); 			
			*/
			remove_ajaxCall('comparelist.php?option=delete&'+querystring);
			current_link = lnk;
			//str.split(',');
		}
		else {
			alert('You must select atleast one property to remove');
			document.getElementById('remove').disabled=false;
			
		}
   }
   else {
			alert('You must select atleast one property to remove');
			document.getElementById('remove').disabled=false;
		}
		return false;
}

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;
      val = trim(val);
    if (val.search(validRegExp) == -1) {
      return false;
    } 
    return true; 	
}

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('You must select atleast two properties to compare');
	}
	else 
	if (count >4 ) {
		alert('Maximum four properties can be compared at a time');
	}
	else {
		var win_url = "compare_details.php?prpid="+str;
		newPopUpWindow(win_url,'compare Properties',716,780,'yes');
	}
	return false;
}

function saveProperty(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[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('You must select atleast one property to save');			
	}
	else {
		querystring = "property_id="+str;

		$.blockUI({ backgroundColor: ''});
		var response = new $.ajax(
		{
				'type': 'post',
				'url': 'save_property.php',
				'data': 'option=save&'+querystring,
				success: function(msg){
					save_stateChanged(msg);
				}
		}); 		
	}
	return false;
}

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

/*	var response = new $.ajax(
	{
			'type': 'post',
			'url': 'save_property.php',
			'data': 'option=save&'+querystring,
			success: function(msg){
				alert(msg);
				savesingle_stateChanged(msg);
			}
	}); 
*/		
	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 = 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 = (val);
    if (val.match(validRegExp) == null) {
      return false;
    } 
    return true; 	
}
function  isValidTelephone(tel){
	validRegExp = /^[0-9 +-]+$/;
    if (tel.match(validRegExp) == null) {
      return false;
    } 
		return true;
}
function validateContact(){
	var result_ajax = '';
	frm = document.contact;
	var fname 	 		= frm.fname.value;
	var lname 	 		= frm.lname.value;
	var phone 	 		= frm.phone.value;
	var email 	 		= frm.email.value;
	var contact_phone 	= frm.contact_phone.value;
	var txtAddress 		= document.contact.txtAddress.value;
	var txtCity 	 	= document.contact.txtCity.value;
	var lstState 	 	= document.contact.lstState.value;
	var txtZip 	 		= document.contact.txtZip.value;
	var contact_phone 	= document.contact.contact_phone.value;
	var contact_way 	= document.contact.contact_way.value;
	var contact_time 	= document.contact.contact_time.value;
	
	var comments = escape(document.contact.comments.value);
	var flag = true;	
	
	document.getElementById('fname_err').innerHTML ='';
	document.getElementById('lname_err').innerHTML ='';
	document.getElementById('lstState_err').innerHTML ='';
	document.getElementById('comments_err').innerHTML ='';
	document.getElementById('email_err').innerHTML ='';
	document.getElementById('phone_err').innerHTML ='';
	document.getElementById('contact_phone_err').innerHTML ='';
	
	if(trim(frm.email.value) != "")
	{
		if (isValidEmail(trim(frm.email.value)) == ""){		
		document.getElementById('email_err').innerHTML='Enter valid Email';
		document.getElementById('email_err').style.color='red';
		flag = false;
	}
	}
	if (isValidName(trim(frm.fname.value))== ""){
		document.getElementById('fname_err').innerHTML='Enter valid First name';
		document.getElementById('fname_err').style.color='red';
		flag = false;
	}
	if (isValidName(trim(frm.lname.value))== ""){
		document.getElementById('lname_err').innerHTML='Enter valid Last name';
		document.getElementById('lname_err').style.color='red';
		flag = false;
	}
	if(trim(frm.lstState.value) != "")
	{
		if (isValidName(trim(frm.lstState.value)) == ""){		
		document.getElementById('lstState_err').innerHTML='Enter valid State name';
		document.getElementById('lstState_err').style.color='red';
		flag = false;
	}
	}
	if(isValidTelephone(phone) == '')
	{
		document.getElementById('phone_err').innerHTML='Enter valid Phone number';
		document.getElementById('phone_err').style.color='red';	
		flag=false;
	}
	if (isValidTelephone(phone) != '' && document.getElementById('contact_phone').value == "")
	{
		document.getElementById('contact_phone_err').innerHTML='Select Phone Type';
		document.getElementById('contact_phone_err').style.color='red';
		flag=false;
	}
	if (document.getElementById('contact_way').value == "Phone")
	{
		if ((!isValidTelephone(phone) && phone.length>0) || (phone.length<6)){	
		document.getElementById('phone_err').innerHTML='Enter valid Phone number';
		document.getElementById('phone_err').style.color='red';
		flag=false;
	}
	}	
	if (comments.length==""){
		document.getElementById('comments_err').innerHTML='Enter valid Comments';
		document.getElementById('comments_err').style.color='red';
		flag = false;
	}	
	/*if(flag==false)
		return false;
	else
		return true;
}*/
	if(flag) {
		querystring ="fname="+fname+"&lname="+lname+"&txtAddress="+txtAddress+"&txtCity="+txtCity+"&lstState="+lstState+"&txtZip="+txtZip+"&phone="+phone+"&contact_phone="+contact_phone+"&email="+email+"&contact_way="+contact_way+"&contact_time="+contact_time+"&comments="+comments;
		url= 'contactus.php?'+querystring;	
		//alert(url);
		if(xmlhttp){
			xmlhttp.open('POST',url,true);
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			xmlhttp.onreadystatechange = mailcontactchange;
			xmlhttp.send(null);
		}
		return false;
	}
	return false;
}
function mailcontactchange()
{
	if (xmlhttp.readyState==1){
			document.getElementById('spinner').innerHTML = "<img src='images/common/loading.gif' alt='Loading...'>";
	}		
	if (xmlhttp.readyState == 4)
	{
		mytext = xmlhttp.responseText;
		if(mytext == "success")
		{
			window.location="thankyou.html?msg=success";
		}
		else if(mytext == "failure")
		{
			window.location="thankyou.html?msg=failure";
		}
		mytext="";
	}
}
function validateReset()
{
	//alert('reset');
	
	frm = document.contact;
	document.getElementById('fname').value ='';
	document.getElementById('lname').value ='';
	document.getElementById('phone').value ='';
	document.getElementById('email').value ='';
	
	document.getElementById('txtAddress').value ='';
	document.getElementById('txtCity').value ='';
	document.getElementById('lstState').value ='';
	document.getElementById('txtZip').value ='';
	document.getElementById('contact_phone').value ='';
	document.getElementById('contact_way').value ='Email';
	document.getElementById('contact_time').value ='Any time';
	document.getElementById('comments').value ='';
	
	
	document.getElementById('fname_err').innerHTML ='';
	document.getElementById('lname_err').innerHTML ='';
	document.getElementById('lstState_err').innerHTML ='';
	document.getElementById('comments_err').innerHTML ='';
	document.getElementById('email_err').innerHTML ='';
	document.getElementById('phone_err').innerHTML ='';
	document.getElementById('contact_phone_err').innerHTML ='';
}
function validateSellers(){
	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 flag = true;
	document.getElementById('email_err').innerHTML ='';
	document.getElementById('fname_err').innerHTML ='';
	document.getElementById('lname_err').innerHTML ='';
	document.getElementById('hphone_err').innerHTML ='';
	if (!isValidEmail(email) || email.length<3) {
		document.getElementById('email_err').innerHTML=' Invalid Email';
		flag = false;
	}
	if (!isValidName(fname) && fname.length>0){
		document.getElementById('fname_err').innerHTML='Invalid First name';
		flag = false;
	}
	if (!isValidName(lname) && lname.length>0){
		document.getElementById('lname_err').innerHTML='Invalid Last name';
		flag = false;
	}
	if (hphone.length>1){
		if(hphone.length<6 || !isValidPhone(hphone)) {
		document.getElementById('hphone_err').innerHTML='Invalid Phone No.';
		flag = false;
		}
	}
	if (flag) {
		querystring ="email="+email+"&fname="+fname+"&lname"+lname+"&hphone="+hphone;
		url= 'sellers.php?option=sellers&'+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(){
     var val = trim(document.getElementById('textfield').value);
	if(val=='Search Location or Property ID')
		val = "";
	/*if(val!='')
		closeparent(); */
		
    if (val=='' || val=='Search Location or Property ID') {
        alert('Invalid search parameter');
		return false;
    }
   else {
		url=window.location.href;
		arrsplitted=url.split('/');
		if (arrsplitted[arrsplitted.length-2]=='agents')
			document.fd.action='../view.php?pst=1&textfield='+val;
		else
			document.fd.action='view.php?pst=1&textfield='+val;
		document.fd.submit()
		return true;
    }
}
function deleteSearch(id) {
	if(confirm("Are you sure 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 ) );
}
