// Removes leading whitespaces

// For save property feature
if(location.host == "192.168.2.39")
	var client_url="http://"+location.host+"/c21vjf";
else if(location.host == "localhost")
	var client_url="http://"+location.host+"/projects/vjf";
else if(location.host == "dev03.bspsrv.net")
	var client_url="http://"+location.host+"/century21vjf.com";
else
	var client_url="http://"+location.host;

/***********************************/

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));
}
//email validation
function isValidEmail(strEmail)
{
	if(strEmail.length<5)
	{
		return false;
	}
  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
   strEmail = trim(strEmail);
    if (strEmail.search(validRegExp) == -1) 
	{
      return false;
    } 
    return true; 
}
//validatating user name
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; 	
}
//validating address
function  isValidAddress(addr){
	//validRegExp = /^[0-9a-zA-Z\s\,./-]+$/;
	validRegExp = /^[0-9a-zA-Z \s,.\!\@\#\$\%\^\&\*\(\)\?\_\'\"/-]+$/;
    if (addr.match(validRegExp) == null) {
      return false;
    } 
    return true;
}
/// ajax script ////
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 str='';
function ajaxCall(url) 
{
	var $j = jQuery.noConflict();
	$j.blockUI('<h1><img src="./images/crest/busy.gif" align="absmiddle" class="processing_request" />Processing your request...</h1>');
	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 stateChanged()
{
	if (xmlhttp.readyState==1)
	{
		document.getElementById('output').value='Processing..';
	}
	if (xmlhttp.readyState==4)
	{
		if(document.getElementById("rf"))
			rf = document.getElementById("rf").value;
			
		str=xmlhttp.responseText;
		if (str.match('.php'))
		{
			location.href = str;
		}
		else
		{
			switch (str)
			{
				case '1':
					location.href='message.php?page=profile&paging='+rf;
				break;
				case '2':
					location.href='message.php?page=editprofile&paging='+rf;	
				break;
				case '5':
					location.href='search.php?rf=login'; //EDITED BY SATHIYAN ON 28-08-2008 ADDED "?request_from=login";
				break;
				default:
					document.getElementById("output").innerHTML=str;
					document.getElementById("output").className='ajax-failure';
				break;
			}
		}
	$j.unblockUI();
	}
}
// validate user
function ValidatUser()
{
	var $j = jQuery.noConflict();
	$j.blockUI('<h1><img src="./images/crest/busy.gif" align="absmiddle" class="processing_request" />Processing your request...</h1>');
	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) 
	{
		if(url!='{strurl}')
		{
			querystring ="email="+email+"&password="+password+"&url="+url;	
			
		}
		if(url=='{strurl}')
		{
			querystring ="email="+email+"&password="+password;	
						
		}
		ajaxCall(client_url+'/login.php?option=validate&'+querystring);
		$j.unblockUI();
		return false;
		
	}
	else 
	{
		document.getElementById('output').innerHTML='Invalid Email Address / Password.';
		//document.getElementById('output').className='ajax-failure';
		document.getElementById('passwordx').value='';
		$j.unblockUI();
		return false;

	}
$j.unblockUI();	
}
// for registration form validation 
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='';	
	if (document.getElementById('country_emsg'))
		document.getElementById('country_emsg').innerHTML='';
	var username =trim( document.getElementById('username').value);
	var email    = $j("#email").val();
	var password = trim(document.getElementById('password').value);
	var confirm_password = trim(document.getElementById('confirm_password').value);
	var country  = $j("#select_country").val();
	var state_name    = trim(document.getElementById('state_name').value);

/*alert(state_name);	
return false;
*/	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').className='error_message';
		flag=false;
	}
	if (!isValidEmail(email) || email.length<5){
		document.getElementById('email_msg').innerHTML='Invalid Email Address';
		document.getElementById('email_msg').className='error_message';
		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').className='error_message';
			}
		}
		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').className='error_message';
		flag=false;
	}
	if (confirm_password!=password){
		document.getElementById('confirm_password_msg').innerHTML='Confirmed password does not match with password';
		document.getElementById('confirm_password_msg').className='error_message';
		flag=false;
	}
	if (!isValidUserName(state_name) || state_name.length<2){
		document.getElementById('state_msg').innerHTML='Invalid State Name';
		document.getElementById('state_msg').className='error_message';
		flag=false;
	}
	if(country == "")
	{
		if (document.getElementById('country_emsg'))
		{
			document.getElementById('country_emsg').innerHTML='Select a country';
			document.getElementById('country_emsg').className='error_message'; 
			flag=false;
		}
	}
	if (!isValidUserName(city) || city.length<2){
		document.getElementById('city_msg').innerHTML='Invalid City Name';
		document.getElementById('city_msg').className='error_message';
		flag=false;
	}
	if (address.length<1) {
		document.getElementById('address_msg').innerHTML='Address cannot be empty';
		document.getElementById('address_msg').className='error_message';
		flag=false;
	}
	else {
	if(!isValidAddress(address) && address.length>1){
		document.getElementById('address_msg').innerHTML='Invalid Address';
		document.getElementById('address_msg').className='error_message';
		flag=false;	
	}
	}
	if (flag) {
		querystring ="username="+username+"&password="+password+"&email="+email+"&country="+country+"&state_name="+state_name+"&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;
	}
}

function changePassword(flag){
	//document.getElementById('pass_link').style.display='table-row';
	if (flag) {
		document.getElementById('link_content').innerHTML="<a href='javascript:changePassword(0)' class='menulink'> 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)' class='menulink'> 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 Address';
		document.getElementById('email_msg').className='error_message';
		flag=false;
		document.getElementById('email').value="";
	}
	if (flag) {
		querystring ="email="+email;
		ajaxCall('profile.php?option=resetpassword&'+querystring);
		document.getElementById('email').value="";
	}
	return false;
}

///save the particular property.......
	function saveSingleProperty(propertyid,nm,page,total){
	//javascript:saveSingleProperty('4079868',4079868,'view.php?pgn=1','10');
	//propertyid = document.getElementById('propertyid').value;
	//nm = document.getElementById('propertyid').value;
	//var $j = jQuery.noConflict();
	//$j.blockUI('<h1><img src="images/busy.gif" align="absmiddle" class="processing_request" />Processing your request...</h1>');
	totrec=total;
	current_link = nm;
	current_page = page;
	querystring = "property_id="+propertyid;
	//document.getElementById('im_'+current_link).innerHTML="<img src='images/common/loading.gif'>";
	savesingle_ajaxCall(client_url+'/save_property.php?option=save&'+querystring);
	//showHideLinks(totrec,'none');
}

function savesingle_ajaxCall(url) {
//alert(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() { 
	if (xmlhttp.readyState==4){ 
		var str =xmlhttp.responseText;
		if (str==1) {
			alert('Property added successfully');		
		}
		else if (str==2){
			alert('Login to save property');
		}
		else {
			alert('Property already saved');	
		}
	}
	//$j.unblockUI(); 
}
//show and hide the particula link

function showHideLinks(total,display)
{
	for (i=1;i<=total;i++){
		document.getElementById('sidx_'+i).style.display=display;	
	}

}

