var tabs = new Array();
var current_tab='ad_summary';
tabs[0] = 'ad_summary';
tabs[1] = 'contact';
tabs[5] = 'details';
tabs[2] = 'map';
tabs[3] = 'local_services';
tabs[4] = 'pdf';
tabs[6] = 'local_business';
var w=1;

if (typeof Array.prototype.indexOf == "undefined") {
  Array.prototype.indexOf = function(value) {
    for (var i = 0; i < this.length; i++){
      if (this[i] == value) return i;
    }
    return -1;
  }
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


function in_array(value,array){  for (var i = 0; i < array.length; i++) if (array[i] == value) return true;
  return false;}


function showTab(tab){
	current_tab=tab;	for(var i=0;i<tabs.length;i++){
	  //alert(tab);		if(tabs[i]==tab){
	    	document.getElementById(tabs[i]).style.display = "block";
	    	document.getElementById(tabs[i]).style.visibility = "visible";
	    }else{
	    	document.getElementById(tabs[i]).style.display = "none";
	    	document.getElementById(tabs[i]).style.visibility = "hidden";	    }
    }
    if(tab==tabs[0]){      document.getElementById(tabs[5]).style.display = "block";
	    document.getElementById(tabs[5]).style.visibility = "visible";    }
	  document.getElementById('tellafriend').style.display = "none";
    document.getElementById('tellafriend').style.visibility = "hidden";
    document.getElementById("is_sendtofriend").innerHTML='';}

function Check_form(form){
  if(form.email.value=='' || form.passwd.value==''){
    alert('Enter your email and password please.');
    form.email.focus();
    return false;
  }
  return true;
}

function goToAgentContact(url){	createCookie('gotoagent','1',1);	location.href=url;}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function setContactTab(){
	if(readCookie('gotoagent')=='1'){
		showTab('contact');
		eraseCookie('gotoagent');
		return true;
	}
	return false;
}

function contactAgent(vid){

          var myimg=document.getElementById('img_' + vid);
          var vidi=document.getElementById('open_'+ vid);
          if (vidi.style.display == "block")
           {
              vidi.style.display = "none";
              vidi.style.visibility = "hidden";
              myimg.src="http://ineedproperty.co.uk/images/plus.gif";
              }
          else
          {
                    vidi.style.display = "block";
                    vidi.style.visibility = "visible";
                    myimg.src="http://ineedproperty.co.uk/images/minus.gif";

             }

}

var http_request = false;
function makePOSTRequest(url, parameters) {
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   http_request.onreadystatechange = alertContents;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}

function alertContents() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {         request_result=http_request.responseText.trim();
         if(request_result){           document.getElementById('tellafriend').style.display = "none";
           document.getElementById('tellafriend').style.visibility = "hidden";
      	   document.getElementById('is_sendtofriend').innerHTML='<center><br><font color="red">Your message has been sent to your friend.</font></center>';
      	   setTimeout('showTab(current_tab)',4000);         }else{           document.getElementById("my_code_er").style.display = "block";         }
      }else{
        alert('There was a problem with the request.');
      }
   }
}

function sendToFriend(obj){   var error = 0;
   var fr_mail = document.getElementById("fr_mail").value;
   var my_mail = document.getElementById("my_mail").value;
   var snd_copy = document.getElementById("snd_copy").checked;
   var my_msg = document.getElementById("my_msg").value;
   var link_to_friend = document.getElementById("link_to_friend").value;
   var code = document.getElementById("my_code").value;

   document.getElementById("fr_mail_er").style.display = "none";
   document.getElementById("er_firstname").style.display = "none";
   document.getElementById("er_lastname").style.display = "none";
   document.getElementById("er_pref").style.display = "none";
   document.getElementById("my_code_er").style.display = "none";
   document.getElementById("my_mail_er").style.display = "none";


   if(!isValidEmail(fr_mail)){
     document.getElementById("fr_mail_er").style.display = "block"; error=1;
   }
   if(!isValidEmail(my_mail)){
     document.getElementById("my_mail_er").style.display = "block"; error=1;
   }
   if(code==''){
     document.getElementById("my_code_er").style.display = "block"; error=1;
   }

   if(error==0){
	   var poststr = "fr_mail=" + encodeURI(fr_mail) +
				     "&my_mail=" + encodeURI(my_mail) +
				     "&snd_copy=" + encodeURI(snd_copy) +
				     "&link_to_friend=" + encodeURI(link_to_friend) +
				     "&code=" + encodeURI(code) +
				     "&my_msg=" + encodeURI(my_msg);
	   makePOSTRequest('/send_details.php', poststr);
   }}

function get(obj) {   var error = 0;
   var email = document.getElementById("email").value;
   var title = document.getElementById("title").value;
   var firstname = document.getElementById("firstname").value;
   var lastname = document.getElementById("lastname").value;
   var preferred_contact_email = document.getElementById("preferred_contact_email").value;
   var preferred_contact_phone = document.getElementById("preferred_contact_phone").value;
   var phone = document.getElementById("phone").value;
   var preferredtime = document.getElementById("preferredtime").value;
   var town = document.getElementById("town").value;
   var country = document.getElementById("country").value;
   var postcode = document.getElementById("postcode").value;
   var notes = document.getElementById("notes").value;
   var property_id = document.getElementById("property_id").value;
   var address2 = document.getElementById("address2").value;
   var address1 = document.getElementById("address1").value;

   document.getElementById("er_email").style.display = "none";
   document.getElementById("er_firstname").style.display = "none";
   document.getElementById("er_lastname").style.display = "none";
   document.getElementById("er_pref").style.display = "none";


   if(!isValidEmail(email)){
   		 document.getElementById("er_email").style.display = "block"; error=1;
   }
   if(firstname==''){
   		 document.getElementById("er_firstname").style.display = "block"; error=1;
   }
   if(lastname==''){
   		 document.getElementById("er_lastname").style.display = "block"; error=1;
   }
   if(document.getElementById("preferred_contact_email").checked==false && document.getElementById("preferred_contact_phone").checked==false){
   		 document.getElementById("er_pref").style.display = "block"; error=1;
   }

   if(error==0)
   {
	   var poststr = "email=" + encodeURI(email) +
				     "&title=" + encodeURI(title) +
				     "&firstname=" + encodeURI(firstname) +
				     "&lastname=" + encodeURI(lastname) +
				     "&preferred_contact_email=" + encodeURI(preferred_contact_email) +
				     "&preferred_contact_phone=" + encodeURI(preferred_contact_phone) +
				     "&phone=" + encodeURI(phone) +
				     "&preferredtime=" + encodeURI(preferredtime) +
				     "&town=" + encodeURI(town) +
				     "&country=" + encodeURI(country) +
				     "&postcode=" + encodeURI(postcode) +
				     "&notes=" + encodeURI(notes) +
				     "&property_id=" + encodeURI(property_id) +
				     "&address2=" + encodeURI(address2) +
	                 "&address1=" + encodeURI(address1);
	   makePOSTRequest('/send_details.php', poststr);
	   document.getElementById("open_contact_agent").innerHTML='<font color="red">Your message has been sent to the agent.</font>';
   }
}

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  if (strEmail.search(validRegExp) == -1) return false;
  return true;
}

function openVideo(url){
	window.open(url,"videowindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=300,height=250");
}
function sms(url){  //createCookie('gotoagent','1',1);
	//location.href=url;
	window.open(url,"smswindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=300,height=250");
}
function login_sms(id){  createCookie('property_id',id,1);  window.location.href='/login/';}
function Question(type){  if(type=='agency') type1='agency1'; else type1='customer';	document.getElementById('question').style.display = "none";
	document.getElementById('question').style.visibility = "hidden";
	document.getElementById(type1).style.display = "block";
    document.getElementById(type1).style.visibility = "visible";
    document.getElementById('sbmt').style.display = "block";
    document.getElementById('sbmt').style.visibility = "visible";
    document.getElementById('membership_type').value = type;
}

function clearHistory(){    document.getElementById('last_viewed').style.display = "none";
	document.getElementById('last_viewed').style.visibility = "hidden";
	createCookie('is_clear_history','1',1);
}

function clearLastSearches(){
    document.getElementById('last_searches').style.display = "none";
	document.getElementById('last_searches').style.visibility = "hidden";
	createCookie('is_clear_last_searches','1',1);
}

function tellAFriend(){	for(var i=0;i<tabs.length;i++){
    	document.getElementById(tabs[i]).style.display = "none";
	   	document.getElementById(tabs[i]).style.visibility = "hidden";
    }
	document.getElementById('tellafriend').style.display = "block";
    document.getElementById('tellafriend').style.visibility = "visible";
    document.getElementById("is_sendtofriend").innerHTML='';}

function calculate(property_price){	window.open('/calculators/calc-monthlypayment.php?property_price='+property_price, 'calc','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=450');}

function setBanner(bn_code){
    document.getElementById("bn_code").innerHTML=bn_code;
    document.getElementById("bn_code_noscr").innerHTML=bn_code;
    document.getElementById("bn_code_noscr1").innerHTML=bn_code;
    document.getElementById("bn"+bn_code).checked=true;
}

function setSite(bn_sid){
    document.getElementById("bn_sid").innerHTML=bn_sid;
    document.getElementById("bn_sid1").innerHTML=bn_sid;
}

var pos=0;
function showPictures(property_id){  var tp='plan';  if(w==1) tp='image';  window.open('/show_pictures.php?property_id='+property_id+'&num='+pos+'&type='+tp, '_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=680');
}

function tubeStation(station_id){
	window.open('/tubestationpopup.php?station_id='+station_id, '_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=520,height=400');
}

function schools(education_id){
	window.open('/educationpopup.php?education_id='+education_id, '_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=520,height=400');
}

function validateform() {
  var s = document.getElementById('s');
  if (s.value=='place or postcode') {
    alert('Please enter a placename or postcode');
    s.focus();
    return false;
  }

  if (s.value.length==0 || s.value.length<3) {
    alert('Please enter a placename or postcode');
    s.focus();
    return false;
  }

  return true;
}

var currentRow=-1;
var currentArea='';
function SelectRow(idRow){
  for(var i=1;i<16;++i){
    var cell=document.getElementById('m_'+idRow);
    currentArea=cell.innerHTML;
    cell.style.background='#e3e3e3';
    if(currentRow!=-1){
      var cell=document.getElementById('m_'+currentRow);
      cell.style.background='#f5f5f5';
    }
  }
  currentRow=idRow;
}

function IsSelected(){
  return currentRow==-1 ? false : true;
}

function GetSelectedRow(){
  return currentRow;
}

function pub(val){
  if(val=='banner'){    document.getElementById('banner').style.display='inline';
    document.getElementById('banner').style.visibility = 'visible';
    document.getElementById('note').innerHTML='<b>Note:</b>&nbsp;'+note[0];
  }else{    document.getElementById('banner').style.display='none';
    document.getElementById('banner').style.visibility = 'hidden';
  }
  if(val=='paid'){    document.getElementById('paid').style.display='inline';
    document.getElementById('paid').style.visibility = 'visible';
    document.getElementById('note').innerHTML='<b>Note:</b>&nbsp;'+note[1];
  }else{    document.getElementById('paid').style.display='none';
    document.getElementById('paid').style.visibility = 'hidden';
  }

  if(val=='unpaid') document.getElementById('note').innerHTML='<b>Note:</b>&nbsp;'+note[2];
}

function set_location(idx){  //alert(arr_location.indexOf(idx));  if(!in_array(idx,arr_location)){    document.getElementById('tr_location').style.display='none';
    document.getElementById('tr_location').style.visibility = 'hidden';
  }else{
    document.getElementById('tr_location').style.display='inline';
    document.getElementById('tr_location').style.visibility = 'visible';
  }}

function dsp(vid){
  var myimg=document.getElementById('img' + vid);
  var vidi=document.getElementById('top'+ vid);
  if (vidi.style.display == "block"){
    vidi.style.display = "none";
    vidi.style.visibility = "hidden";
    myimg.src="/images/plus.gif";
  }else{
    vidi.style.display = "block";
    vidi.style.visibility = "visible";
    myimg.src="/images/minus.gif";
  }
}

function uni_dsp(vid){
  var vidi=document.getElementById(vid);
  if (vidi.style.display == "block"){
    vidi.style.display = "none";
    vidi.style.visibility = "hidden";
  }else{
    vidi.style.display = "block";
    vidi.style.visibility = "visible";
  }
}


var receiveReq = getXmlHttpRequestObject();

function getXmlHttpRequestObject(){
  if (window.XMLHttpRequest){
    return new XMLHttpRequest();
  }else if(window.ActiveXObject){
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function getBusiness(business_type_id) {  document.getElementById('local_busss').innerHTML='Loading...';
  receiveReq.open("GET",'/getBusiness.php?business_type_id='+business_type_id+'&postcode='+postcode, true);
  receiveReq.onreadystatechange = handleReceiveBusiness;
  receiveReq.send(null);
}

function handleReceiveBusiness() {
  if (receiveReq.readyState==4){
    //alert(receiveReq.responseText);
    document.getElementById('local_busss').innerHTML=receiveReq.responseText;
  }
}

function openBusinessBanner(business_url,business_id){	window.open(business_url);
  receiveReq.open("GET",'/business_banner_click.php?business_id='+business_id, true);
  receiveReq.onreadystatechange = handleReceiveBannerClick;
  receiveReq.send(null);
}

function handleReceiveBannerClick() {
  if (receiveReq.readyState==4){
    //alert(receiveReq.responseText);
  }
}

function location_limit(obj){  var checked_count = 0;
  for(i=0;i<document.locfrm['location[]'].length;i++){    if(document.locfrm['location[]'][i].checked) checked_count++;  }
  if(checked_count>5 ){    obj.checked=false;
    alert('You can search for maximum 5 locations at a time.');
  }
}

function change_action(obj){  var f = obj.form;
  var action;
  if(obj.value=='sale') action='/buy/list/';
  else if(obj.value=='rent') action='/rent/list/';
  f.setAttribute('action',action);
  //alert(f.action);
}

function preloadProperty(imagePath,content,conteiner){  imageObj = new Image(300,300);
  imageObj.src = imagePath;
  imageObj.onload=function(){    try{      document.getElementById(conteiner).innerHTML=content;
      document.images['img'+conteiner].src=imagePath;
    }catch(err){}
  };
  if(imageObj.readyState=='complete') document.getElementById(conteiner).innerHTML=content;
}