// JavaScript Document

function Validator(theForm)
{
	 
 if(theForm.name.value == "")
  {
    alert("Please enter Name");
    theForm.name.focus();
    return (false);
  }
  if (theForm.tel.value == "")
  {
    alert("Please enter Phone number");
    theForm.tel.focus();
    return (false);
  }
  
  if(theForm.mobile.value == "")
  {
    alert("Please enter mobile number");
    theForm.mobile.focus();
    return (false);
  }
  
   if (theForm.email.value == "")
  {
    alert("Please enter your Email Address");
    theForm.email.focus();
    return (false);
  }
  if  (theForm.email.value != "")
  {   
	var eresult
	var str=theForm.email.value
  	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(str))
	  {
		alert("Please enter a valid Email address!")
		theForm.email.focus();
		eresult=false;
		return (eresult);
  	  }
  
  }  
  
  if (theForm.spam.value == "")
  {
    alert("Please Enter Security Code");
    theForm.spam.focus();
    return (false);
  }
  
   /* if (theForm.calculate_two.value == "")
  {
    alert("Please Calculate Calculation");
    theForm.calculate_two.focus();
    return (false);
  }
  
   if (theForm.calculate_two.value != "")
  {
      if (theForm.calculate_two.value != theForm.calculate_one.value)
     {
		 alert("Please Calculate properly");
		 theForm.calculate_two.value = "";
         theForm.calculate_two.focus();
         return (false);
	 }
  }*/
  
   if (theForm.location.value == "Select")
  {
    alert("Please Select Location");
    theForm.location.focus();
    return (false);
  }
  
  
   if (theForm.property_type.value == "Select")
  {
    alert("Please Select property type");
    theForm.property_type.focus();
    return (false);
  }
  
}


function Validator_contact(theForm)
{
	
	if(theForm.name.value == "")
  {
    alert("Please enter Name");
    theForm.name.focus();
    return (false);
  }

  if(theForm.mobile.value == "")
  {
    alert("Please enter mobile number");
    theForm.mobile.focus();
    return (false);
  }
  
   if (theForm.email.value == "")
  {
    alert("Please enter your Email Address");
    theForm.email.focus();
    return (false);
  }
  if  (theForm.email.value != "")
  {   
	var eresult
	var str=theForm.email.value
  	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(str))
	  {
		alert("Please enter a valid Email address!")
		theForm.email.focus();
		eresult=false;
		return (eresult);
  	  }
  
  }  
  
    if(theForm.message.value == "")
  {
    alert("Please enter message");
    theForm.message.focus();
    return (false);
  }
  
  
  if (theForm.spam.value == "")
  {
    alert("Please Enter Security Code");
    theForm.spam.focus();
    return (false);
  }
  
  return true;
  
}

function MailFrndValidator(theForm) 
{
	
	if(theForm.frdname.value == "")
  {
    alert("Please enter Friends Name");
    theForm.frdname.focus();
    return (false);
  }
  
   if (theForm.frdemail.value == "")
  {
    alert("Please enter Friends Email Address");
    theForm.frdemail.focus();
    return (false);
  }
  if  (theForm.frdemail.value != "")
  {   
	var eresult
	var str=theForm.frdemail.value
  	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(str))
	  {
		alert("Please enter a valid Friends Email address!")
		theForm.frdemail.focus();
		eresult=false;
		return (eresult);
  	  }
  
  }
  
  return true;
	
}


function getXMLHTTP() { 
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
}
	
	
function getXML() { 
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
}
	
function getCity(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('citydiv').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
}


function miniSearch(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('minisearch').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
}


function getProperty(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('citydiv_two').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
			
	
			
		}
				
}


function showHideWindow(o)
{
	
 		var obj = document.getElementById(o);
        if(obj.style.display == 'block')
          obj.style.display = 'none';
        else
	     obj.style.display = 'block';
}
  
  
function hideDiv() { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById('hideshow').style.visibility = 'hidden'; 
	} 
	else { 
		if (document.layers) { // Netscape 4 
			document.hideshow.visibility = 'hidden'; 
		} 
		else { // IE 4 
			document.all.hideshow.style.visibility = 'hidden'; 
		} 
	} 
}

function showDiv() { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById('hideshow').style.visibility = 'visible'; 
	} 
	else { 
		if (document.layers) { // Netscape 4 
			document.hideshow.visibility = 'visible'; 
		} 
		else { // IE 4 
			document.all.hideshow.style.visibility = 'visible'; 
		} 
	} 
} 


/******************************REGISTER*******************************************/

function ValidateReg(formName)
{
	
	if(formName.name.value == '')
	{
		alert('Please Enter The Name');
		formName.name.focus();
		return false;
	}
	
	if(formName.email.value == '')
	{
		alert('Please Enter The Email');
		formName.email.focus();
		return false;
	}
	else if(formName.email.value != '')
	  {   
		var eresult
		var str=formName.email.value
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (!filter.test(str))
		  {
			alert('Please Enter a Valid Email address!')
			formName.email.focus();
			eresult=false;
			return (eresult);
		  }  
	  }
	  
	  if(formName.password.value == '')
	{
		alert('Please Enter The Password');
		formName.password.focus();
		return false;
	}
	
	if(formName.confirmpassword.value == '')
	{
		alert('Please Enter The Confirm Password');
		formName.confirmpassword.focus();
		return false;
	}
	
	if(formName.confirmpassword.value != formName.password.value)
	{
		alert('Password and Confirm Password must be same');
		formName.confirmpassword.focus();
		return false;
	}
	
	if(formName.mobile.value == '')
	{
		alert('Please Enter The Mobile Number');
		formName.mobile.focus();
		return false;
	}
    else if(formName.mobile.value.length<10) 
	{
		alert("Please Enter 10 digit Mobile Number");
    	formName.mobile.focus();
    	return (false);
    }
  
	if(formName.property_type.selectedIndex == 0)
	{
		alert("Please select property type.");
		return (false);
	}
	
	if(formName.propertysubtype.selectedIndex == 0)
	{
		alert("Please select property sub type.");
		return (false);
	}
	
	if(formName.propertysale[0].checked) {
		
		if(formName.budget.selectedIndex == 0)
		{
			alert("Please select budget.");
			return (false);
		}
	}
	
	if(formName.propertysale[1].checked) {
		
		if(formName.deposit.value == "")
		{
			alert("Please enter deposit.");
			formName.deposit.focus();
			return (false);
		}
		
		if(formName.rent.value == "")
		{
			alert("Please enter rent.");
			formName.rent.focus();
			return (false);
		}
	}	
	
	
	if (formName.seccode.value == "")
	{
		alert("Please Enter Security Code");
		formName.seccode.focus();
		return (false);
	}
	
	if(formName.location.selectedIndex == 0)
	{
		alert("Please select location.");
		return (false);
	}
	
	return true;
  
}

function ValidateLogin(formName)
{
	if(formName.email.value == '')
	{
		alert('Please Enter The Email');
		formName.email.focus();
		return false;
	}
	else if(formName.email.value != '')
	{   
		var eresult
		var str=formName.email.value
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (!filter.test(str))
		{
			alert('Please Enter a Valid Email address!')
			formName.email.focus();
			eresult=false;
			return eresult;
		}
		//else { return true; }
	}
	
	if(formName.password.value == '')
	{
		alert('Please Enter The Password');
		formName.password.focus();
		return false;
	}
}

function vaildateFav(usrid,lstid) {


if(lstid!=0) {
			
	var shrtlstid=lstid;
		
	var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {	
						alert("Added to Favorites Successfully");						
						//document.getElementById('favorite').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", "/addtofav.php?memid="+usrid+"&fav="+shrtlstid, true);
			req.send(null);
		}
		
		return true;
}
else {
	var frmFavr=document.frmFav;
    var chks=document.getElementsByName('chkFav[]');
    var isChecked = false;
	var valid=[];
	var indx=0;
	
	for (var i = 0; i < chks.length; i++)
    {
 	   if (chks[i].checked)
	    {
			//alert(chks[i].value);
			valid[indx]=chks[i].value;
        	isChecked = true;
			indx++;
			chks[i].checked=false;
        	//break;
        }
    }

	if (!isChecked)
    {
    	alert("Please select at least one Property.");
    	return false;
    }
	else
	{
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {		
						alert("Added to Favorites Successfully");
						//document.getElementById('favorite').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", "/addtofav.php?memid="+usrid+"&favarray="+valid, true);
			req.send(null);	
			
		}
		//alert(valid);
		return true;
	}
}
    //booSubmit = confirm("Are you sure to add this property list(s)?");
    //if(booSubmit == true)
   		//frmFavr.submit();
      	
}

function removeFav() {
	
	var frmFavr=document.frmFav;
    var chks=document.getElementsByName('chkFav[]');
    var isChecked = false;
	for (var i = 0; i < chks.length; i++)
    {
 	   if (chks[i].checked)
	    {
        	isChecked = true;
        	break;
        }
    }

	if (!isChecked)
    {
    	alert("Please select at least one Property.");
    	return false;
    }
	else
	{
		//alert(valid);
		return true;
	}
	
}

function removeMail() {
	
	var frmMailer=document.frmMail;
    var chks=document.getElementsByName('chkMail[]');
    var isChecked = false;
	for (var i = 0; i < chks.length; i++)
    {
 	   if (chks[i].checked)
	    {
        	isChecked = true;
        	break;
        }
    }

	if (!isChecked)
    {
    	alert("Please select at least one Mail.");
    	return false;
    }
	else
	{
		//alert(valid);
		return true;
	}
	
}

function chkAllInbox(){ 

	var frmFavr=document.frmFav;
	var chkAll = document.getElementById('chkAllMail');
    var chks = document.getElementsByName('chkMail[]');
	var chklenght=chks.length;
	
	for (var i = 0; i < chklenght; i++)
    {
 	   chks[i].checked = chkAll.checked;
    }

}

function unchkAll() {
	
	var frmFavr=document.frmFav;
	var chkAll = document.getElementById('chkAllMail');
    var chks = document.getElementsByName('chkMail[]');
	var chklenght=chks.length;
	var ischk=true;
	
	for (var i = 0; i < chklenght; i++)
    {
		if(chks[i].checked!=true) {
 	   		ischk=false; 
		}
		if(ischk!=false) { ischk=true; }
    }

	chkAll.checked = ischk;
}

function limitSelection()
{
	var ctr = 0;
	var mctr = 5; /* this is the maximum number of options you want selected */
	var selObj = document.getElementById('locations');
	
	for (var i=0; i< selObj.options.length; i++)
	{
	if (selObj.options[i].selected==true)
	ctr++;
	}
	
	if (ctr>mctr)
	{
	alert("You have selected too many locations, only " + mctr + " are permitted.");
	return false;
	}
	
	return true;
}


function limitCheckBox(indx)
{
	
	var textvalues = '';	
		
	var ctr = 0;
	var mctr = 5; 
	var chkloc = document.getElementsByName('location[]');
	//alert(chkloc.length);	
	for (var i=0; i< chkloc.length; i++)
	{
		if (chkloc[i].checked==true) {			
			
			textvalues+="<div class='options' onclick='removechk("+i+");$(this).remove();'>"+$('span.'+chkloc[i].value).text()+"</div>";

			ctr++;
			
		}
	}
	
	
	if (ctr>mctr)
	{
		alert("You have selected too many locations, only " + mctr + " are permitted.");
		chkloc[indx].checked=false;
		//selObj.options[selIndex].selected=false;
		return false;
	}
		
		$('span#selectedoptions').html(textvalues);

	
	return true;
}

function removechk(inx) {

	var unchkloc = document.getElementsByName('location[]');
	unchkloc[inx].checked=false;

}


/*********************************END REGISTER********************************************************/


