var objAjax = new ajaxFunction();

function do_trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function do_ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function do_rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function disappear(t) {
     document.getElementById(t).style.visibility='hidden';
}
function appear(t) {
     document.getElementById(t).style.visibility='visible';
}
function showinfo(t){
document.getElementById(t).style.display="inline";
}

function hideinfo(t){
document.getElementById(t).style.display="none";
}

function cannot_edit(){
alert("Sorry, Rejected building information cannot be updated!");
}


function submit_paging(start_val) {
//alert(start_val);
     document.getElementById('id_start').value=start_val;
     document.getElementById('id_paging').submit();
}

function ValidateEmail(theinput){
	var s=theinput;

	if(s.search)	{
		return (s.search(new RegExp('^([-!#$%&\'*+./0-9=?A-Z^_`a-z{|}~'+unescape('%7F')+'])+@([-!#$%&\'*+/0-9=?A-Z^_`a-z{|}~'+unescape('%7F')+']+\\.)+[a-zA-Z]{2,6}$','gi'))>=0)
	}
	if(s.indexOf)	{
		var at_character=s.indexOf('@')
		if(at_character<=0 || at_character+4>s.length)
			return false
	}
	if(s.length<6)
		return false
	else
		return true
}
function Validatecheckbox(theinput){
	if(theinput.checked == true){
		document.registration.ChkTerms.value="yes";
	}else{
		document.registration.ChkTerms.value="";
	}
}

function select_county_one(theform,actionform) {
// alert('sfd');
	if(theform.actionflag){
		if(theform.actionflag.value=='editprop'){
			theform.actionflag.value='showprop';
		}
	}
	///if(theform.State.value){
		theform.action=actionform;
		theform.submit();
///	}
}
function pop_up(page,id) {
	var a =page+id;
	window.open(a,'welcome','width=800,height=600,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
}

function IsAlphaNumericWithunderscore(sText) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789_";
  for (var i=0; i < sText.length; i++) {
    var letter = sText.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function get_field_value(field_id){
	
	try {
    var field = document.getElementById(field_id);
  	//alert("fieldId:"+field_id+" anme :"+field.name+" value:" +field.value+ " type:"+field.type);
  	var field_type = field.type;
  	if (field_type == 'select-one' || field_type == 'text' ||  field_type == 'hidden' || field_type == 'textarea' || field_type == 'radio' || field_type=="file")	{
  		var fieldvalue = field.value;
  		return fieldvalue;
  	}
  	else if (field_type == 'checkbox'){
  		var fieldvalue = field.checked;
  		return fieldvalue;
  	}
  	else if (field_type == 'select-multiple') {
  		var field_length = field.length;
  		var selected_val = new Array();
  		var c=0	;
  		for(var i=0; i< field_length; i++){
  			if (field.options[i].selected){
  				selected_val[c]	= field.options[i].value;
  				c++;
  			}
  		}
  		return selected_val;
  	}
	}
	catch(e) {
	  return true;
  }
	
}

function set_field_value(field_id,value){
	if (!value) {
		document.getElementById(field_id).value = "";
	}
	else {
		document.getElementById(field_id).value = value;
	}
}

function get_style(field_id,style_name) {
	var field = document.getElementById(field_id);
	return field.style[style_name];
}
function get_confirm(msg){
	return confirm(msg);
}

function do_focus(field_id){
	document.getElementById(field_id).focus();
}

function do_convert(val,type){
	if (val != ""){
		if (type == "int"){
			return parseInt(val);
		}
		else if (type == "float"){
			return parseFloat(val);
		}
	}
	return 0;
}

function empty_list(field_id) {
  var field;
  try {
  	field =  document.getElementById(field_id);
  	field.length = 0;
  }
  catch (e) {
    field=0;
  }
}

function do_submit(form_id) {
	document.getElementById(form_id).submit();
}

function add_option (field_id,text,value,append){
	var field; 
  try{
    var field =  document.getElementById(field_id);
  	if (append == "append"){
  		var len = field.length;
  	}
  	else {
  		var len = 0;
  	}
  	len = field.length++;
  	field.options[len].value = value;
  	field.options[len].text = text;
  }
  catch(e) {
    field=0;
  }
}

function select_option(field_id,id)
{
	var field =  document.getElementById(field_id);
	for (var i=0; i < field.length; i++){
		if (field.options[i].value == id){
			field.options[i].selected = true;
		}
	}
}

function find_browser(){
	var browser="";
	browser = navigator.appName;
	return browser;
}

function valid_url(url)
{
	var indexValue = -1;
	if (url.indexOf('http://')==0) {
		indexValue = 0
	}
	else if (url.indexOf('https://')==0) {
		indexValue = 0
	}
	else if (url.indexOf('ftp://')==0) {
		indexValue = 0
	}

	if (indexValue==0)
	return true;
	else
	return false;
}

function is_empty(field_id) {
	var field_value = get_field_value(field_id);
	if (do_trim(field_value) == ""){
		return true;
	}
	else {
		return false;
	}
}

function check_file_type(field_id,type) {
	var file_name = get_field_value(field_id);
	if (file_name != ""){
		var file_name_len = file_name.length - 3;
		var file_type = file_name.substring(file_name_len);
		if (file_type == type) {
			return true;
		}
	}
	return false;
}

function alert_errormessage(message) {
	var html = "";
	html = "<div id='id_errormessage' style='position:absolute; display:block; width:600px; height:100px; top:140px; left:250px; border:red solid 1px;  background:#FFF5D5; padding:3 3 3 3; font-family:Arial; color:red; font-size:12;'><span><a href='javascript:;' onclick=\"hideinfo('id_errormessage');\">Close</a></span><br><p align='center'><span>"+message+"</span></p></div>";
	document.write (html);
}


function popup_div(inner_div) {
     var  div_id = 'form_cover'; 
     document.getElementById(div_id).style.width = '1000px';
     document.getElementById(div_id).style.height = '1100px';
     document.getElementById(div_id).style.display = "block";
     var html = "" 
     html = build_html(inner_div);
     
     document.getElementById('in_form_cover').innerHTML = html;
     do_focus('top_anchor');
     return false
} 

function save_preview(div_id,form_id) {
     close_popup_div(div_id);
     document.getElementById(form_id).submit();
     return true;
}

function edit_preview(div_id) {
     close_popup_div(div_id);
     return false;
}

function close_popup_div(div_id) {
     document.getElementById('form_cover').style.display = "none";
     document.getElementById(div_id).style.display = "none";
}

function form_cover() {
     var  div_id = 'form_cover'; 
     document.getElementById(div_id).style.width = '1000px';
     document.getElementById(div_id).style.height = '1100px';
     document.getElementById(div_id).style.display = "block";
    // document.getElementById('id_OK_message').focus();

}

function do_noting()
{}

function select_county_list(objThis) {

     var html = '';
     var state = objThis.value;
     if (state != '') {
			 empty_list("id_county");
 			 add_option ("id_county",'Processing...','');

       appear('id_message_div')
	     html = '<font>Processing...</font>';
	     inner_html('id_message_div',html);
		 
          var file_url = adminurl+"ajax_serverPage.php?case=county&state_name="+state+"&sid="+Math.random();
          
  		objAjax.open("GET",file_url,true);
  		objAjax.onreadystatechange=function() {
			if (objAjax.readyState==0) {
			  do_submit('id_leftnav_rvb');
			}
      if (objAjax.readyState==3) {
			  html = '<font>Loading...</font>';
			  inner_html('id_message_div',html);
			}
      if(objAjax.readyState==4) {
        //alert(objAjax.responseText);
				result_str = eval ("("+objAjax.responseText+")");
				var county_length = result_str.county.length;
				//countyList =  document.getElementById("id_section");
				empty_list("id_county");
				empty_list("lstCities");
				add_option ("id_county","Select County","");
				add_option ("lstCities","Select City","");
				for (var count=0; count < county_length; count++) {
					text = value = result_str.county[count];
					add_option ("id_county",text,value);
				}
				var html = "&nbsp;";
				inner_html('id_message_div',html);
			}
		}
		objAjax.send(null);
          disappear('id_message_div');          
    }
    else {
				empty_list("id_county");
				add_option ("id_county","Select County","");
    }
}

function select_search_county_list(objThis) {
     var html = '';
     var state = objThis.value;
     if (state != '') {
       appear('id_message_div')
	     html = '<font>Processing...</font>';
	     inner_html('id_message_div',html);
		 
          var file_url = adminurl+"ajax_serverPage.php?case=county&state_name="+state+"&sid="+Math.random();
          
  		objAjax.open("GET",file_url,true);
  		objAjax.onreadystatechange=function() {
			if (objAjax.readyState==0) {
			  do_submit('id_leftnav_rvb');
			}
               if (objAjax.readyState==3) {
			  html = '<font>Loading...</font>';
			  inner_html('id_message_div',html);
			}
      if(objAjax.readyState==4) {
        //alert(objAjax.responseText);
				result_str = eval ("("+objAjax.responseText+")");
				var county_length = result_str.county.length;
				//countyList =  document.getElementById("id_section");
				empty_list("id_county");
				empty_list("lstCities");
				add_option ("id_county","Select County","");
				add_option ("lstCities","Select City","");
				for (var count=0; count < county_length; count++) {
					text = value = result_str.county[count];
					add_option ("id_county",text,value);
				}
				var html = "&nbsp;";
				inner_html('id_message_div',html);
			}
		}
		objAjax.send(null);
          disappear('id_message_div');          
    }
    else {
				empty_list("id_county");
				empty_list("lstCities");
    }
}

function select_city_for_county(objThis) {
     var html =countylist= '';
     var comma='';
     var county = new Array();
     county = objThis;

     for(var i=1; i < county.length; i++) {
        if (county[i].selected) {
        countylist +=comma;
        countylist += county[i].value;
        comma = ",";
        }
     }
     
     var state = document.getElementById('id_state').value;
     if (state != '') {
      var file_url = adminurl+"ajax_serverPage.php?case=city_for_county&county="+countylist+"&state_name="+state+"&sid="+Math.random();
  		objAjax.open("GET",file_url,true);
  		objAjax.onreadystatechange=function() {
			if (objAjax.readyState==0) {
			  do_submit('id_leftnav_rvb');
			}
      if (objAjax.readyState==3) {
        empty_list("lstCities");
        add_option ("lstCities","Loading...","");			
      }
      if(objAjax.readyState==4) {
        //alert(objAjax.responseText);
				result_str = eval ("("+objAjax.responseText+")");
				var city_length = result_str.cities.length;
				//countyList =  document.getElementById("id_section");
				empty_list("lstCities");
				add_option ("lstCities","Select City","");			
				for (var count=0; count < city_length; count++) {
					text = value = result_str.cities[count];
					add_option ("lstCities",text,value);
				}
				var html = "&nbsp;";
				inner_html('id_message_div',html);
			}
		}
		objAjax.send(null);
          disappear('id_message_div');          
    }
    else {
				empty_list("lstCities");
    }
}
function select_city_list_of_state(objThis,countval) {
     var field_id = objThis.id;
     var html ='';
     var comma='';
     var state = objThis.value;
     //alert('state:'+state+"id_city"+countval);
     if (state != '') {
			empty_list("id_city"+countval);
			add_option ("id_city"+countval,"Processing...","");
      var file_url = adminurl+"ajax_serverPage.php?case=state_city_list&state_name="+state+"&sid="+Math.random();
  		objAjax.open("GET",file_url,true);
  		objAjax.onreadystatechange=function() {
      if (objAjax.readyState==3) {
				empty_list("id_city"+countval);
				add_option ("id_city"+countval,"Loading...","");
			}
      if(objAjax.readyState==4) {
        //alert(objAjax.responseText);
				result_str = eval ("("+objAjax.responseText+")");
				var city_length = result_str.cities.length;
				//countyList =  document.getElementById("id_section");
				empty_list("id_city"+countval);
				add_option ("id_city"+countval,"Select City","");
				for (var count=0; count < city_length; count++) {
					text = value = result_str.cities[count];
					add_option ("id_city"+countval,text,value);
				}
			}
		}
		objAjax.send(null);
    }
    else {
				empty_list("id_city"+countval);
				add_option ("id_city"+countval,"Select City","");
    }
}
function select_search_city_list(objThis) {
     var html =countylist= '';
     var comma='';
     var county = new Array();
     county = objThis;
     //alert(objThis.name)
     for(var i=1; i < county.length; i++) {
        if (county[i].selected) {
        countylist +=comma;
        countylist += county[i].value;
        comma = ",";
        }
     }
     var state = document.getElementById('id_state').value;
     if (state != '') {
       appear('id_message_div')
	     html = '<font>Processing...</font>';
	     inner_html('id_message_div',html);
		 
          var file_url = adminurl+"ajax_serverPage.php?case=state_county_city&county="+countylist+"&state_name="+state+"&sid="+Math.random();
          
  		objAjax.open("GET",file_url,true);
  		objAjax.onreadystatechange=function() {
			if (objAjax.readyState==0) {
			  do_submit('id_leftnav_rvb');
			}
               if (objAjax.readyState==3) {
			  html = '<font>Loading...</font>';
			  inner_html('id_message_div',html);
			}
      if(objAjax.readyState==4) {
        //alert(objAjax.responseText);
				result_str = eval ("("+objAjax.responseText+")");
				var city_length = result_str.cities.length;
				//countyList =  document.getElementById("id_section");
				empty_list("lstCities");
				for (var count=0; count < city_length; count++) {
					text = value = result_str.cities[count];
					add_option ("lstCities",text,value);
				}
				var html = "&nbsp;";
				inner_html('id_message_div',html);
			}
		}
		objAjax.send(null);
          disappear('id_message_div');          
    }
    else {
				empty_list("lstCities");
    }
}

function validate_zipcode_for_state_and_city(objThis,countval) {
    var zipcode = objThis.value;
    var city_field_id = "id_city"+countval;
    var state_field_id = "id_state"+countval;
    var city_name = get_field_value(city_field_id);
    var state_name = get_field_value(state_field_id);
    if (zipcode!='' ) {
        if(state_name=='') {
            alert('Please select any state then enter zipcode');
            objThis.focus();
            return false;
        }
        if (city_name=='') {
            alert('Please select any city then enter zipcode');
            objThis.focus();
            return false;
        }
        if (state_name !='' && city_name!='') {
          
            var file_url = adminurl+"ajax_serverPage.php?case=validate_zipcode_for_state_and_city&zipcode="+zipcode+"&selected_state="+state_name+"&selected_city="+city_name+"&sid="+Math.random();
        		objAjax.open("GET",file_url,true);
        		objAjax.onreadystatechange=function() {
              if(objAjax.readyState==4) {
                  //alert(objAjax.responseText);
          				result_str = objAjax.responseText;
            				if (result_str=='invalid'){
              				alert('Please enter valid zipcode');
              				obj.value='';
              				obj.focus();
              				return false;
            		    }
            		    else if (result_str=='valid') {
            		      return true;
                    }
          			}
      		  }
      		  objAjax.send(null);
        }
    }
}

function validate_zipcode(obj) {
    var county = get_field_value('id_county');
    var city = get_field_value('lstCities');
    var zipcode = obj.value;
    var sendurlvalue = '';
    if (zipcode != '') {
        if (county == '') {
            alert('Please select any county then enter zipcode');
            obj.value='';
            obj.focus();
            return false;
        }
        if (city==''){
            alert('Please select any city then enter zipcode');
            obj.value='';
            obj.focus();
            return false;
        }
        if (county !='' && city!=''){
            var file_url = adminurl+"ajax_serverPage.php?case=validate_zipcode_for_city&zipcode="+zipcode+"&selected_county="+county+"&selected_city="+city+"&sid="+Math.random();
        		objAjax.open("GET",file_url,true);
        		objAjax.onreadystatechange=function() {
              if(objAjax.readyState==4) {
                  //alert(objAjax.responseText);
          				result_str = objAjax.responseText;
            				if (result_str=='invalid'){
              				alert('Please enter valid zipcode');
              				obj.value='';
              				obj.focus();
              				return false;
            		    }
            		    else if (result_str=='valid') {
            		      return true;
                    }
          			}
      		  }
      		  objAjax.send(null);
        }
    }
}

function validate_us_phone(Phone_value) {
  var p = do_trim(Phone_value);
  pRegex = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/; // it allows only for (xxx) xxx-xxxx
  pRegex1 = /^\d\d\d\ \d\d\d-\d\d\d\d$/;// it allows only for xxx xxx-xxxx
  pRegex2 = /^\d\d\d-\d\d\d-\d\d\d\d$/;// it allows only for xxx-xxx-xxxx
 if( !p.match( pRegex ) ) {
      if( !p.match( pRegex1 ) ) {
          if( !p.match( pRegex2 ) ) {
              alert('Please fill the phone number in the format \'xxx xxx-xxxx\' or \'(xxx) xxx-xxxx\' ');
              return false;
          }
          else {
              return true;
          }
      }
      else {
           return true;
      }
 }
 else {
      return true;
 }
}

function select_state() {
	var selected_country = get_field_value("id_country");
	var selected_state = get_field_value("id_state");
	if (selected_country != "US") {
		var field = document.getElementById("id_state");
		field[0].selected = true;
		empty_list("id_city");
	}
}

function select_usa() {
	var field = document.getElementById("id_country");
	for (var count=0; count < field.length; count++) {
		if (field[count].value=="US") {
			field[count].selected = true;
		}
	}
}


function popup_cities(div_id,total_div) {

  for (var i=1; i <= total_div; i++) {
    if (i==div_id) {
      display = 'block';
    }
    else {
      display = 'none';
    }
    try {
      document.getElementById(i).style.display = display;
    }
    catch(e) {
    }
  }
}

function select_zip_for_county(objThis) {
     var html =countylist= '';
     var comma='';
     var county = new Array();
     county = objThis;

     for(var i=1; i < county.length; i++) {
        if (county[i].selected) {
        countylist +=comma;
        countylist += county[i].value;
        comma = ",";
        }
     }
     
     var state = document.getElementById('id_state').value;
     if (state != '') {
      var file_url = adminurl+"ajax_serverPage.php?case=zip_for_county&county="+countylist+"&state_name="+state+"&sid="+Math.random();
  		objAjax.open("GET",file_url,true);
  		objAjax.onreadystatechange=function() {
			if (objAjax.readyState==0) {
			  do_submit('id_leftnav_rvb');
			}
      if (objAjax.readyState==3) {
        empty_list("zip_code");
        add_option ("zip_code","Loading...","");			
      }
      if(objAjax.readyState==4) {
        //alert(objAjax.responseText);
				result_str = eval ("("+objAjax.responseText+")");
				var zip_length = result_str.zip_code.length;
				//countyList =  document.getElementById("id_section");
				empty_list("zip_code");
				add_option ("zip_code","Select Zip Code","");			
				for (var count=0; count < zip_length; count++) {
					text = value = result_str.zip_code[count];
					add_option ("zip_code",text,value);
				}
				var html = "&nbsp;";
				inner_html('id_message_div',html);
			}
		}
		objAjax.send(null);
          disappear('id_message_div');          
    }
    else {
				empty_list("zip_code");
    }
}
function updateClicks(table, type, id) {
      var file_url = adminurl+"ajax_serverPage.php?case=update_clicks&t="+table+"&id="+id+"&type="+type+"&sid="+Math.random();
  		objAjax.open("GET",file_url,true);
	  	objAjax.send(null);
}