//************* Registeration function********************/
function doRegister(form)
{
	form.action="register.php";
	form.submit();
}
function doReset()
{
	$('title').value = "";
	$('fname').value = "";
	$('lname').value = "";
	$('company_name').value="";
	$('building_name_number').value = "";
	$('street_address').value = "";

	$('country').value= 0;
	$('county').value = 0;
	$('town').value= 0;
	$('postcode').value = "";
	$('telephone').value= "";

	$('mobile').valu="";
	$('fax').value="";
	$('email_id').value="";
	$('password').value="";
	$('password_conf').value="";
	$('hidRegister').value="";

	$('address2').value="";
	$('agreed_rate_pounds').value="";

	return true;
}
function process_regstr(form)
{
	if(typeof($F('country'))=='undefined' || $F('country')==null) {
    	var country = '';
	} else {
	    var country = trim($F('country'));
	}
	if(typeof($F('county'))=='undefined' || $F('county')==null) {
    	var county = '';
	} else {
	    var county = trim($F('county'));
	}
	if(typeof($F('town'))=='undefined' || $F('town')==null) {
    	var town = '';
	} else {
	    var town = trim($F('town'));
	}

	if(typeof($F('local'))=='undefined' || $F('local')==null) {
    	var local = '';
	} else {
	    var local = trim($F('local'));
	}
	
	var email_id = trim( $F('email_id') );
	var confirm_email_id = trim( $F('confirm_email_id'));

	var password = trim( $F('password') );
	var password_conf = trim( $F('password_conf'));

		
	 if ($F('title') == '')  
	 { 
		 
		  alert('Please select the title'); 
		  $('title').focus(); 
		  return false; 
	 }
	 
	 if($F('fname') == '')  
	 { 
		  alert('Please enter the first name'); 
		  $('fname').focus(); 
		  return false; 
	 }

	 if($F('lname') == '')  
	 { 
		  alert('Please enter the last name'); 
		  $('lname').focus(); 
		  return false; 
	 } 

	 if($F('building_name_number') == '')  
	 { 
		  alert('Please enter the building name/number'); 
		  $('building_name_number').focus(); 
		  return false; 
	 }
	
	 if($F('street_address') == '')  
	 { 
		  alert('Please enter the street address'); 
		  $('street_address').focus(); 
		  return false; 
	 }
	
	if($F('country') == 0)
	{
		alert("Please select the country");
		$('country').focus();
		return false;
	}
	
	if($F('county') == 0)
	{
		var varCounty	=	$('county');
		var varCountyLen	=	varCounty.options.length;
		if(varCountyLen > 1)
		{
			alert("Please select the county");
			$('county').focus();
			return false;
		}
	}
	
	if($F('town') == 0)
	{
		var varTown		=	$('town');
		var varTownLen	=	varTown.options.length;
		if(varTownLen > 1)
		{
			alert("Please select the town");
			$('town').focus();
			return false;
		}
	}
	if(($F('postcode') == '')||($F('postcode') ==null))
	{
		alert("Please enter the postcode");
		$('postcode').focus();
		return false;
	}
	if($F('postcode') != '')
	{
		if(check_postcode($F('postcode')))
		{
			alert('Please complete the Post Code in the following format:\n\n"LLN NLL" or "LLNN NLL" or "LN NLL" or "LNN NLL" where L=letter and N=number');
			$('postcode').focus();
			$('postcode').select();
			return false;
		}
	}
	
 	if($F('telephone') == '')  
	{ 
		alert('Please enter the telephone');
		$('telephone').focus();
		return false;
	}
	
	if(email_id == '')
	{
		alert("Please enter the email id");
		$('email_id').focus();
		return false;
	}
	
	if(checkEmail( email_id ))
	{
		alert("Please enter valid email id");
		$('email_id').focus();
		$('email_id').select();
		return false;
	}

	if(confirm_email_id == '')
	{
		alert("Please enter the confirm email id");
		$('confirm_email_id').focus();
		return false;
	}
	else if(checkEmail(confirm_email_id))
	{
		alert("Please enter valid confirm email id");
		$('confirm_email_id').focus();
		$('confirm_email_id').select();
		return false;
	}

	if( email_id != confirm_email_id)
	{
		alert("The emails ids are not matcing ");
		$('confirm_email_id').focus();
		$('confirm_email_id').select();
		return false;
	}

	if(password == '')
	{
		alert("Please enter the password");
		$('password').focus();
		return false;
	}

	if(password_conf == '')
	{
		alert("Please enter the confirm password");
		$('password_conf').focus();
		return false;
	}

	if( password != password_conf)
	{
		alert("The passwords are not matcing ");
		$('password_conf').focus();
		$('password_conf').select();
		return false;
	}
		
	if(email_id != "")
	{
		
		if($('ck_avl').value == 0)
		{
			alert("This email id already present , please enter new id");
			$('email_id').focus();
			$('email_id').select();
			return false;
		}
	}


	$('hidRegister').value	=	"Register";
	$('frmRegister').submit();
	return true;

/*
	//ajx code for registeration
			var url = "register.php";
			par_post = "eid="+escape(email_id)+"&pwd="+escape(password)+"&title="+escape(title)+"&fname="+escape(fname)+"&lname="+escape(lname)+"&company_name="+escape(company_name)+"&customer_type="+customer_type+"&building_name_number="+escape(building_name_number)+"&street_address="+escape(street_address)+"&address2="+escape(address2)+"&country="+escape(country)+"&county="+escape(county)+"&town="+escape(town)+"&local="+escape(local)+"&postcode="+escape(postcode)+"&telephone="+escape(telephone)+"&mobile="+escape(mobile)+"&fax="+escape(fax)+"&agreed_rate_pounds="+escape(agreed_rate_pounds);
			//alert(par_post)
			
			//$('divregisteration').style.display = "none";
			$('regDiv').style.display = 'block';
			$('regDiv').innerHTML = '<span style="text-align:center;"><img src="images/wait.gif" border="0"></span>';
			var ajax = new Ajax.Request(url , {
					method : 'post',
					postBody : par_post,
					onSuccess : function(req){
						var result = req.responseText;
						//alert(result)
						if(result == 1)
						{
							alert('Thanks for registering with us . We have emailed you the login details , you can login now from the login form');
							//form.reset();
							//$('regDiv').innerHTML = '';
			                //$('regDiv').style.display = "none";
							//$('divregisteration').style.display = "none";
							//$('divregisteration_thanks').style.display = "block";
							document.location.href='login.php';
						} else {
						 alert(req.responseText);
						}
					},
					onFailure:showerror
					//	onFailure: function(req) { alert(req.responseText); }

				}
			
			)
	
	return false;*/
}

//***************************************************Checking uid availablity
function check_uid()
{
	var email_id = trim($F('email_id'));
	
	if( checkEmail( email_id ) )
	{
		$('ava_div').innerHTML = "Invalid email id";
	}
	else
	{
		var url = 'check-availablity.php';
		var params = 'email_id=' +email_id ;
			//alert(params)
			var ajax = new Ajax.Request(url , {
					method : 'get',
					parameters : params,
					onSuccess : function(req){
						var result = req.responseText;
						//alert(result)
						if(result == 1)
						{
							//alert(result)
							$('ava_div').innerHTML = "You can choose this as your user id";
							$('ck_avl').value = "1";
						}
						else if(result == 0)
						{
							$('ava_div').innerHTML = "This user id is already present";
							$('ck_avl').value = "0";
						}
					}
				}
			
			)
	}
}
 
function dispCounties(getFieldValues)
{
	//ajx code for registeration
	
	if($('country').value!= 0)
	{ 
		if(getFieldValues=="county")
		{
			var url = "ajax_common.php";
			par_post = "get="+getFieldValues+"&countryid="+$('country').value;
			var ajax = new Ajax.Request(url , 
				{
					method : 'get',
					parameters : par_post,
					onSuccess : function(req)
					{
						var result = req.responseText;
						if(result != "")
							$('county_col').innerHTML	=	req.responseText;
						else
							alert(req.responseText);
					},
					onFailure:showerror
				}
			)//new Ajax.Request
		}//if(getFieldValues=="county")
	}//if($('country').value!= 0)
	return false;
}//function dispFields(getFieldValues)

function dispTowns(getFieldValues)
{
	if($('county').value!= 0)
	{
		if(getFieldValues=="town")
		{
			var url = "ajax_common.php";
			par_post = "get="+getFieldValues+"&countyid="+$('county').value;
			var ajax = new Ajax.Request(url , 
			{
				method : 'get',
				parameters : par_post,
				onSuccess : function(req)
				{
					var result = req.responseText;
					if(result != "")
						$('town_col').innerHTML	=	req.responseText;
					else
						alert(req.responseText);
				},
				onFailure:showerror
			}
			)//new Ajax.Request
		}//if(getFieldValues=="town")
	}//if($('county').value!= 0)
	return false;
}//function dispTowns(getFieldValues)

function dispLocals(getFieldValues)
{
	if($('town').value!= 0)
	{	
		if(getFieldValues=="local")
		{
			var url = "ajax_common.php";
			par_post = "get="+getFieldValues+"&townid="+$('town').value;
			var ajax = new Ajax.Request(url , 
			{
				method : 'get',
				parameters : par_post,
				onSuccess : function(req)
				{
					var result = req.responseText;
					if(result != "")
						$('local_col').innerHTML	=	req.responseText;
					else
						alert(req.responseText);
				},
				onFailure:showerror
			}
			)//new Ajax.Request
		}//if(getFieldValues=="local")
	}//if($('county').value!= 0)
	return false;
}//function dispLocals(getFieldValues)

function showerror(req)
{
	alert("Error has occured");
	$('divregisteration_thanks').style.display = "block";
}


//***************************************Login functionality

function process_login(form)
{

	if($F('txtEmailId')== '')
	{
		alert("Please enter the email id");
		$('txtEmailId').focus();
		return false;
	}

	if($F('txtPassword')=="")
	{
		alert("Please enter the password");
		$('txtPassword').focus();
		return false;
	}
	if($F('txtEmailId')!= '')
	{	
		var logType =	"";
		 
		if($('radHip').checked==true)
			logType	=	$('radHip').value;

		else if ($('radHipAgent').checked==true)
			logType	=	$('radHipAgent').value; 
		 
		$('hidLoginType').value=logType;
		//Ajx code
			var url = "ajax_common.php";
			par_post = "get=login&logType="+$('hidLoginType').value+"&eid="+escape($F('txtEmailId'))+"&pwd="+escape($F('txtPassword'));
			//alert(par_post)
			
			var ajax = new Ajax.Request(url , {
					method : 'get',
					parameters : par_post,
					onSuccess : function(req){
						var result = req.responseText;
					 
						if(result == 1)
						{
							$('hidLoginResult').value = "Success"; 
							form.submit();
						}
						else if(result == 0)
						{
							alert(" Invalid login details ");
						}
					}
				}
			)
			
			return false;
	}
}

/************************************************ forgot password function**************/

function doForgot()
{
	if($('txtEmailId').value == "")
	{
		alert("Please enter the email id");
		$('txtEmailId').focus();
		return false;
	}
	else
	{
		var url = "ajax_common.php";
		var par_post = "get=forgotPwd&uid="+$('txtEmailId').value;
		
		var ajax = new Ajax.Request(url , {
				method	  : 'get',
				parameters: par_post,
				onSuccess : function(req)
				{
					var result = req.responseText; 

					if(result == 1)
					{ 
						alert("Your password has been mailed to your email address");
						$('divForgotPassword').style.display = "none";
					}
					else if(result == 0)
					{
						alert("This Email Id is not present in our database");
						$('email_id_fgt_pwd').focus();
						$('email_id_fgt_pwd').select();
					}//if(result == 1)
				}//function(req)
			}//new Ajax.Request
		)//var ajax = new Ajax.Request(url , 
	}
	return false;
}

function process_FgtPwd()
{
	if($('txtEmailId').value == "")
	{
		alert("Please enter the email id");
		$('txtEmailId').focus();
		return false;
	}
	else
	{
		var url = "ajax_common.php";
		var par_post = "get=forgotPwd&uid="+$('txtEmailId').value;
		
		var ajax = new Ajax.Request(url , {
				method	  : 'get',
				parameters: par_post,
				onSuccess : function(req)
				{
					var result = req.responseText;
					//alert(result)
					if(result == 1)
					{
						//alert(result)
						alert("Your password has been mailed to your email address");
						$('divForgotPassword').style.display = "none";
						return true;
					}
					else if(result == 0)
					{
						alert("This Email Id is not present in our database");
						$('email_id_fgt_pwd').focus();
						$('email_id_fgt_pwd').select();
						return false;
					}
				}
			}
		)
	}//if($('txtEmailId').value != "")
}

function process_property(form,propId)
{
	
	if($F('lstBedRooms') == 0)
	{
		alert("Please select no. of bedrooms");
		$('lstBedRooms').focus();
		return false;
	}

	if($F('lstPropType') == 0)
	{
		alert("Please select the property type ");
		$('lstPropType').focus();
		return false;
	}

	if($F('txtStreet') == 0)
	{
		alert("Please enter the street address ");
		$('txtStreet').focus();
		return false;
	}

	if($F('lstTowns') == 0)
	{
		alert("Please select the town");
		$('lstTowns').focus();
		return false;
	}
	if($F('lstPricePrefix') == '0.00')
	{
		alert("Please select the price prefix ");
		$('lstPricePrefix').focus();
		return false;
	}
	if($F('lstPricePrefix') == '')
	{
		alert("Please enter the price greater than 0.00");
		$('txtPrice').focus();
		return false;
	}
	if($F('txtPostCode') == 0)
	{
		alert("Please enter the postal code");
		$('txtPostCode').focus();
		return false;
	}
	if($F('txtPostCode') != '')
	{
		if(validate_pcode($F('txtPostCode')))
		{
			alert('Please complete the Post Code in the following format:\n\n"LLN NLL" or "LLNN NLL" or "LN NLL" or "LNN NLL" where L=letter and N=number');
			$('txtPostCode').focus();
			$('txtPostCode').select();
			return false;
		}
	}

	if($F('txtSmsDesc') == 0)
	{
		alert("Please enter the SMS description");
		$('txtSmsDesc').focus();
		return false;
	}
	if($F('txtRefNo') == 0)
	{
		alert("Please enter the reference number");
		$('txtRefNo').focus();
		return false;
	}

	if(form.name=="frmEditPropDtls")
	{
		$('hidPropDtls').value	=	"editPropertyDtls";
		$('hidPropId').value	=	propId;
		$('frmEditPropDtls').submit();
	}
	else
	{
		$('hidPropDtls').value	=	"propertyDtls";
		$('frmPropDtls').submit();
	}
	return true;
}

function doEdit(PropId)
{
	var params = "pro_id="+PropId;
	var url = "property_edit.php";

	var ajax = new Ajax.Updater(
	{success: 'list_pro_details'},
	url,
	{method: 'get', parameters: params});
}

function doSMS(PropId)
{
	var params = "pro_id="+PropId;
	var url = "property_SMS.php";

	var ajax = new Ajax.Updater(
	{success: 'list_pro_details'},
	url,
	{method: 'get', parameters: params});
}

function showPropertyDetails(pid)
{
	var params = "pro_id="+pid;
	var url = "viewPropInfo.php";

	var ajax = new Ajax.Updater(
	{success: 'list_pro_details'},
	url,
	{method: 'get', parameters: params});
}