// Javascript validation functions

// http://www.designplace.org/



//function to check empty field

function isEmpty(str) {

	

    if (str == "" || str == null || str.charAt(0) == ' ')

    {

    return false;

    }

    return true;

}



function validate(id){

	var elm = document.getElementById(id);

	//alert(elm.value);

	return isEmpty(elm.value);	

}



//function to check valid email address

function isValidEmail(strEmail){

  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches

    if (strEmail.search(validRegExp) == -1){

      alert('A valid e-mail address is required.\nPlease amend and retry');

      return false;

    } 

    return true; 

}



var http = false;



if(navigator.appName == "Microsoft Internet Explorer") {

  http = new ActiveXObject("Microsoft.XMLHTTP");

} else {

  http = new XMLHttpRequest();

}

function location_changed(value){

  //remove old items;

  var selectbox = document.getElementById('district');

  var count;

  for (count = selectbox.options.length-1; count >=0 ; count--){

  	 selectbox.remove(count);

  }

  //add the zoro item

  var opt = document.createElement("option");

  selectbox.options.add(opt);

  opt.text = "All districts";

  opt.value = "";

  //alert(value);

  if(value=='') return;

  http.abort();

  http.open("GET", "ajax/ajax.php?lo=" + encodeURIComponent(value), true);

  http.onreadystatechange=function() {

    if(http.readyState == 4) {

    	

    	var opts_str= http.responseText;

    	for (count = selectbox.options.length-1; count >=0 ; count--){

  		 selectbox.remove(count);

  		}

		  //add the zoro item

		  var opt = document.createElement("option");

		  selectbox.options.add(opt);

		  opt.text = "All districts";

		  opt.value = "";

		//add the new items

		//alert(http.responseText);

    	var opts = opts_str.split(',');

    	var op;

    	for(op in opts){

    		var op_value=opts[op];

    		if(op_value=='') continue;

    		opt = document.createElement("option");

        	selectbox.options.add(opt);

        	opt.text = op_value;

        	opt.value = op_value;			

		}

    }

  }

  http.send(null);

}



function resetall(){

  var primibox = document.getElementById('price_min');

  primibox.selectedIndex = 0 ;

  

  var primabox = document.getElementById('price_max');

  primabox.selectedIndex = 0 ;

  

  var aremibox = document.getElementById('area_min');

  aremibox.selectedIndex = 0 ;

  

  var aremabox = document.getElementById('area_max');

  aremabox.selectedIndex = 0 ;

  

  var roomibox = document.getElementById('roomi');

  roomibox.selectedIndex = 0 ;  

  

  var roomabox = document.getElementById('rooma');

  roomabox.selectedIndex = 0 ;

  

  var dt = document.getElementById('dt');

  dt.selectedIndex = 0 ;

  

  var batmi = document.getElementById('batmi');

  batmi.selectedIndex = 0 ;

  

  var batma = document.getElementById('batma');

  batma.selectedIndex = 0 ;

  

  var sor = document.getElementById('sor');

  sor.selectedIndex = 0 ;

  

  var ord = document.getElementById('ord');

  ord.selectedIndex = 0 ;

  

  return false;

}



function echeck(str) {

		var at="@"

		var dot="."

		var lat=str.indexOf(at)

		var lstr=str.length

		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){

		   return false

		}



		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		   return false

		}



		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		    return false

		}



		 if (str.indexOf(at,(lat+1))!=-1){

		    return false

		 }



		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		    return false

		 }



		 if (str.indexOf(dot,(lat+2))==-1){

		    return false

		 }

		

		 if (str.indexOf(" ")!=-1){

		    return false

		 }



 		 return true					

	}



function ValidateForm(urlref){

	var toemail=document.getElementById('toemail');

	if ((toemail.value==null)||(toemail.value=="")){

		alert("Please enter your friend's email")

		toemail.focus()

		return false

	}

	if (echeck(toemail.value)==false){

		alert("Your friend's email is invalid")

		toemail.focus()

		return false

	}

	

	var fromemail=document.getElementById('fromemail');

	if ((fromemail.value==null)||(fromemail.value=="")){

		alert("Please enter your email")

		fromemail.focus()

		return false

	}

	if (echeck(fromemail.value)==false){

		alert("Your email is invalid")

		fromemail.focus()

		return false

	}

	http.abort();
    http.open("GET", "ajax/sharemail.php?youremail="+ encodeURIComponent(fromemail.value) + "&yourfriendmail="+ encodeURIComponent(toemail.value)  + "&urlref="+ encodeURIComponent(urlref), true);
    http.onreadystatechange=function() {
	    if(http.readyState == 4) {
	    		var opts_str= http.responseText;
	    		alert(opts_str);
	    		
	    		$('#div_mail'+idmail).css({display:"none"});
	    }
    }
    http.send(null);
	
	return false	
 }

 

 function ValidateSubmitSite(){

	var firstname=document.getElementById('firstname');

	if ((firstname.value==null)||(firstname.value=="")){

		alert("Please enter your first name")

		firstname.focus()

		return false

	}

	var lastname=document.getElementById('lastname');

	if ((lastname.value==null)||(lastname.value=="")){

		alert("Please enter your last name")

		lastname.focus()

		return false

	}

	

	var email=document.getElementById('email');

	if ((email.value==null)||(email.value=="")){

		alert("Please enter your email")

		email.focus()

		return false

	}

	if (echeck(email.value)==false){

		alert("Your email is invalid")

		email.focus()

		return false

	}

	

	var website=document.getElementById('website');

	if ((website.value==null)||(website.value=="")){

		alert("Please enter your website")

		website.focus()

		return false

	}

	

	var company=document.getElementById('company');

	if ((company.value==null)||(company.value=="")){

		alert("Please enter your company")

		company.focus()

		return false

	}

	

	var comment=document.getElementById('comment');

	if ((comment.value==null)||(comment.value=="")){

		alert("Please enter your comment")

		comment.focus()

		return false

	}

	

	return true

 }

 

 

 function ValidateComment(){

	var com=document.getElementById('name');

	if ((com.value==null)||(com.value=="")){

		alert("Please enter your name")

		com.focus()

		return false

	}

}

 

 function save(id){

 	var label=document.getElementById(id)

	label.innerHTML ='Saving...'

	http.abort();

    http.open("GET", "ajax/save.php?v="+ encodeURIComponent(id), true);

    http.onreadystatechange=function() {

	    if(http.readyState == 4) {	    	

	    	var opts_str= http.responseText;

			label.innerHTML=opts_str;	

	    }

    }

    http.send(null);

	

	return false

	 

}



 function remove(id){

 	var label=document.getElementById(id)

 	var item = 'item'+id;

 	document.getElementById(item).innerHTML='';

	label.innerHTML ='Removing...'

	http.abort();

    http.open("GET", "ajax/save.php?remove="+ encodeURIComponent(id), true);

    http.onreadystatechange=function() {

	    if(http.readyState == 4) {	    	

	    	var opts_str= http.responseText;

			label.innerHTML=opts_str;	

	    }

    }

    http.send(null);

	

	return false

	 

}

var count = "1000";

function limiter(tid,vid){

var texarea=document.getElementById(tid);

var tex = texarea.value;

var len = tex.length;

if(len > count){

        tex = tex.substring(0,count);

        texarea.value =tex;

        return false;

}

var countleft=document.getElementById(vid);

//alert(countleft);

countleft.innerHTML  = count-len;

}

function change(id,im){

	var img=document.getElementById(id);

	img.src="/images/"+im;

}

function sharemail(idmail,urlref){
	
	http.abort();
    http.open("GET", "ajax/sharemail.php?youremail="+ encodeURIComponent($('#yourmail' + idmail).val()) + "&yourfriendmail="+ encodeURIComponent($('#yourfriendmail'+ idmail).val())  + "&urlref="+ encodeURIComponent(urlref), true);
    http.onreadystatechange=function() {
	    if(http.readyState == 4) {
	    		var opts_str= http.responseText;
	    		alert(opts_str);
	    		
	    		$('#div_mail'+idmail).css({display:"none"});
	    }
    }
    http.send(null);
	
	return false
}
