// 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));
}
//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) 
{
	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)
	{
	var $j = jQuery.noConflict();
	$j.blockUI('<h1><img src="images/busy.gif" align="absmiddle" class="processing_request" />Processing your request...</h1>');
		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 '5':
					location.href='search.php';
				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/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();	
}

var block_ui_content='<div id="processing_request" align="middle"><h1 class="processing_request"><img src="images/busy.gif" align="absmiddle" class="processing_request" /> &nbsp;Processing your request...</h1></div>';

function ValidateContactus()
{
	document.getElementById('name_error').innerHTML='';		
	document.getElementById('email_error').innerHTML='';	
	document.getElementById('additional_error').innerHTML='';
	
	var name =document.getElementById('txtName');
	var phone =document.getElementById('txtPhone');
	var email =document.getElementById('txtEmail');
	var addComments =document.getElementById('txtAdditionalComments');
	var flag=true;
	if(!isValidUserName(name.value) || (name.value).length<3) {
		document.getElementById('name_error').innerHTML='Invalid Name';
		document.getElementById('name_error').className='error';
		flag=false;
	}
	if(!isValidEmail(email.value) || (email.value).length<5) {
		document.getElementById('email_error').innerHTML='Invalid Email Address';
		document.getElementById('email_error').className='error';
		flag=false;
	}
	if((addComments.value).length<1) {
		document.getElementById('additional_error').innerHTML='Invalid Comments';
		document.getElementById('additional_error').className='error';
		flag=false;	
	}
	if(flag)
	{
		$.blockUI(block_ui_content);
		$.post('contactus.php',{name:name.value,phone:phone.value, email:email.value, addComments:addComments.value},function(data){
			if(data=='success')
				window.location.href='contactusthankyou.html';
			if(data=='failure')
				document.getElementById('emailError').innerHTML='Error - Unable To Send Mail';
			});
		$.unblockUI(block_ui_content);	
	}
	
	return false;
	
}
	
// 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    = trim(document.getElementById('state').value);
/*	if(document.getElementById('state_def').style.display =='none')
	{
		var state  = $j("#select_state1").val();
		//alert(document.getElementById('select_state').options[0].value);
		err_msg = "Invalid State";
		document.getElementById('select_state').value=document.getElementById('select_state').options[0].value;
		document.getElementById('state_new').style.display='none';

	}
	else
	{
		var state  = $j("#select_state").val();
		err_msg = "Please Select State Name";
		document.getElementById('select_state1').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').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) || state.length<3){
		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'; 
		}
	}
	if (!isValidUserName(city) || city.length<3){
		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="+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;
	}
}

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;	
	}

}

/*function state_text(val)
{
	if(val=='United States')
	{
	document.getElementById('state_def').style.display='';
	document.getElementById('state_new').style.display='none';
	document.getElementById('state_msg').innerHTML='';
	}
	else
	{	
	document.getElementById('state_new').style.display='';
	document.getElementById('state_def').style.display='none';
	document.getElementById('state_msg').innerHTML='';
	//document.getElementById('select_state').value=document.getElementById('select_state').options[0].value;
	}
//return true
}*/
