function styleTextFields(){$("input:text").addClass("textfield_plain").focus(function(){$(this).removeClass("textfield_plain").addClass("textfield_select");}).blur(function(){$(this).removeClass("textfield_select").addClass("textfield_plain");});}
function setAjaxMessage(message, div_id) {$("#"+div_id).html(message).slideDown("fast");setTimeout(function() {$("#"+div_id).slideUp("slow");} ,10000);}
function getJTemplate(ttype, name, textarea){$("#"+textarea).load("/search/actions/load_template.php", {template: name, type: ttype});}
function copyAreas(){	
	$("#successdiv").load("../actions/addeditareas.php", {action : "copy", id : $("#copy_office").val(), copy_ids : $("input[id='area_check']:checked").val()}, function() {
		$("#successdiv").fadeIn("fast");
		setTimeout(function() {$("#successdiv").fadeOut("slow");} ,10000);
	} );	
}
function go(url){
	location.href=url;
}

function functionTracker(f_name)
{       
    $.post("/jquery/functiontracker.php",{key : f_name});
}

function textCounter(field,cntfield,maxlimit) {
  if ($('#'+field).val().length > maxlimit) // if too long...trim it!
        $('#'+field).val($('#'+field).val().substring(0, maxlimit)).trigger('change');
  
  
  $('#'+cntfield).html($('#'+field).val().length+'');
        
}

function calcSMSSize(sms_message,sms_return)
{
	var msg_o = document.getElementById(sms_message);
	var message = msg_o.value;
	var msg_return = document.getElementById(sms_return);	
	var sms = Math.ceil(message.length/154);
	var return_message = sms+" sms";
	if (sms > 1) return_message = return_message+"'s";
	return_message = return_message+" ("+message.length+")";
	msg_return.innerHTML = return_message;
}

function changeToBusy(id, message)
  {
  	$("#"+id).empty().append("<img src='/images/icons/status.gif'>&nbsp;&nbsp;"+message);
  	return true;
}

function popup(url,width,height)
{   
    window.open(url,'','width='+width+',height='+height+',menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
}

function swapClass(obj,strClassName) {
	obj.className = strClassName;
}
function checkLogin(){
	var msg = ' ';
	if(document.getElementById('username').value == '' ){
		msg = 'Please enter your username.\n';
	}
	if(document.getElementById('password').value == '' ){
		msg = msg + 'A valid password is required.';
	}
	if (msg != ' '){
		alert(msg);
		return false;	
	}else{	
		document.getElementById('Login').value = "Please wait...";
		document.getElementById('Login').disabled = true;
		document.getElementById('loginform').submit();
	}
}
function checkBirthday(){
	if(document.getElementById('bd_month').value =="00" || document.getElementById('bd_day').value =="00"){
		alert("Please first set the birthday of this client. (Year is not required)");	
		document.getElementById('client_bday_reminder').checked = false;
	}
}

function checkAreaSuburb(){
	if(document.getElementById('area_listRight').value=="" && document.getElementById('suburb_listRight').value==""){
		alert("Please select at least one area or suburb.");		
		return false;
	}		
	document.getElementById('Next').value = "Please wait...";
	document.getElementById('Next').disabled = true;
	return true;
	
}

function checkReminder(){
	if(document.getElementById('bd_month').value =="00" || document.getElementById('bd_day').value =="00"){	
		document.getElementById('bday_reminder').checked = false;
	}
}

function setOverride(){ //used for adding clients
	document.getElementById('Name').disabled = false;
	document.getElementById('Surname').disabled = false;
	document.getElementById('Cellphone').disabled = false;
	document.getElementById('override').value = "yes";	
	document.getElementById('Next').value = "Please wait...";
	document.getElementById('Next').disabled = true;
	//document.getElementById('addclientform').submit();
	document.getElementById('addclientform').submit();
}

function setFinished(){ //used for adding sellers
	document.getElementById('finished').value = "yes";	
	document.getElementById('Next').value = "Please wait...";
	document.getElementById('Next').disabled = true;
	document.getElementById('addsellerform').submit();
}

function activateForm(){
	if(document.getElementById('Change').value == 'Change Details'){
		document.getElementById('Next').disabled = true;
		document.getElementById('ButtonDiv').style.visibility = "hidden";
		document.getElementById('Name').disabled = false;
		document.getElementById('Surname').disabled = false;
		document.getElementById('Cellphone').disabled = false;
		document.getElementById('Change').value = 'Next';
	}else{
		if(validateForm('Name','','R','Surname','','R','Cellphone','','RisNum')){
			document.getElementById('Change').value = "Please wait...";
			document.getElementById('Change').disabled = true;
			document.getElementById('addclientform').submit();
		}
	}
}

function validateForm() {
  var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { 
  	test=args[i+2]; 
  	val=MM_findObj(args[i]);
    if (val) { 
    	nm=val.name;if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) {p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+= nm+' must contain an e-mail address.\n';
      } else if (test!='R') { 
      		num = parseFloat(val);
		    if (isNaN(val)) errors+= nm+' may only contain digits.\n';
    	    if (test.indexOf('inRange') != -1) { 
      	  		p=test.indexOf(':');
	            min=test.substring(8,p);max=test.substring(p+1);
       		    if (num<min || max<num) errors+= nm+' must contain a number between '+min+' and '+max+'.\n';
  		     } 
  	  } 
  	  if (nm.indexOf('Cellphone')>-1) {       		
  	  		if (val.length < 10){
       		    errors+= 'Cellphone must be at least 10 characters long.\n';
  		     } 
  	  } 
  } else if (test.charAt(0) == 'R') errors += nm+' is required.\n';}
  }if (errors){
	  alert(errors);
  	}	
	if(!errors){
		document.getElementById('Next').value = "Please wait...";
		document.getElementById('Next').disabled = true;
		return true;
	}else{	
		return false;	
	}
}

function setPleaseWait(theform,obj){
		document.getElementById(obj).value = "Please wait...";
		document.getElementById(obj).disabled = true;
		document.getElementById(theform).submit();
}

function menuAction(theid,pic,strUrl) {
    loc = (parent.mainFrame.location)+"";
    //alert(loc);
    if (  loc.indexOf("popup_news")>0 && loc.indexOf("date")>0) {
        alert("Please read the news below and click on the button at the bottom once you have completed it");
    } else {
	$("#home").attr("src","/images/menu/home_1.gif");
	$('#myclients').attr("src","/images/menu/myclients_1.gif");
	$('#search').attr("src","/images/menu/search_1.gif");
	$('#addclient').attr("src","/images/menu/addclient_1.gif");
	$('#report_viewer').attr("src","/images/menu/reports_1.gif");
	$('#report_manager').attr("src","/images/menu/reports_1.gif");
	$('#resources_menu').attr("src","/images/menu/resources_1.gif");
    $('#documents_menu').attr("src","/images/menu/documents_1.gif");
    $('#details').attr("src","/images/menu/details_1.gif");
    $('#sales').attr("src","/images/menu/sales_1.gif");
   
	$('#admin').attr("src","/images/menu/admin_1.gif");
	$("#"+theid).attr("src","/images/menu/"+pic);
	parent.mainFrame.location = strUrl;
    }
}
function gotoLocation(strUrl) {
	location = strUrl;
}
function gotoLogout(strUrl) {
	$('#home').attr("src","/images/menu/home_1.gif");
	$('#myclients').attr("src","/images/menu/myclients_1.gif");
	$('#search').attr("src","/images/menu/search_1.gif");
	$('#addclient').attr("src","/images/menu/addclient_1.gif");
	$('#report_viewer').attr("src","/images/menu/reports_1.gif");
	$('#report_manager').attr("src","/images/menu/reports_1.gif");
	$('#resources_menu').attr("src","/images/menu/resources_1.gif");
	$('#admin').attr("src","/images/menu/admin_1.gif");
	parent.location = strUrl;
}

function justOne(id){
	var cbitem = id;
		if(cbitem == "seller"){
			document.getElementById('buyer').checked = false;
            document.getElementById('renter').checked = false;
		}else if (cbitem == "buyer"){
			document.getElementById('seller').checked = false;
            document.getElementById('renter').checked = false;
		}else if (cbitem == "renter"){  
            document.getElementById('seller').checked = false;
            document.getElementById('buyer').checked = false;
        }
}

function checkSelected(){
	if ((document.getElementById('buyer').checked == false) 
		&& (document.getElementById('seller').checked == false) 
        && (document.getElementById('renter').checked == false) 
		&& (document.getElementById('vip').checked == false) 
		&& (document.getElementById('investor').checked == false) 
		&& (document.getElementById('developer').checked == false)
		&& (document.getElementById('referral').checked == false)
		){
		alert("You have not selected any criteria.");
		return false;
	}
	document.getElementById('Next').value = "Please wait...";
	document.getElementById('Next').disabled = true;
	return true;
}

function MM_findObj(n, d) {
  var p,i,x;if(!d) d=document;if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);return x;
}

function swapRowStatus( callid, id, swap, swapClassName ) {
	var element = document.getElementById( 'r'+id );
	var caller = document.getElementById( callid );
	if( !swap ) swap = true;
	if( !swapClassName ) swapClassName = 'tableCell';
	if( element.className=='tree_closed' ) {
		element.className = 'tree_expanded';
		if( swap ) swapClass(caller,swapClassName);
		showRow('b'+id);
	} else {
		element.className = 'tree_closed';
		if( swap ) swapClass(caller,swapClassName);
		hideRow('b'+id);
	}
}

function hideRow(id) {
  document.getElementById(id).style.display = 'none';
}

function showRow(id) { 
  document.getElementById(id).style.display = '';
}

function showHideRow(id, pmid, ajax_notify) { 
    ajax_notify = ajax_notify || true;    

	if (document.getElementById(id).style.display == '')
	{
	   document.getElementById(id).style.display = 'none';
	   $("#"+pmid).html("<a href='javascript:showHideRow(\""+id+"\",\""+pmid+"\","+ajax_notify+")'><img src='/images/buttons/expand_down.gif' alt='(+)'  border='0' /></a>");       
	   if (ajax_notify) expandTable(pmid, 0);
	}else
	{
	   document.getElementById(id).style.display = '';
  	   $("#"+pmid).html("<a href='javascript:showHideRow(\""+id+"\",\""+pmid+"\","+ajax_notify+")'><img src='/images/buttons/expand_up.gif' alt='(-)'  border='0' /></a>");       
	   if (ajax_notify) expandTable(pmid, 1);
	}
}

function showHideRowList(id, pmid) {     
    if (document.getElementById(id).style.display == '')
    {
       document.getElementById(id).style.display = 'none';
       $("#"+pmid).html("<a href='javascript:showHideRowList(\""+id+"\",\""+pmid+"\")'><img src='/images/buttons/expand_down.gif' alt='(+)'  border='0' /></a>");       
    }else
    {
       document.getElementById(id).style.display = '';
       $("#"+pmid).html("<a href='javascript:showHideRowList(\""+id+"\",\""+pmid+"\")'><img src='/images/buttons/expand_up.gif' alt='(-)'  border='0' /></a>");       
    }
}

function showDiv(newDiv) {
	document.getElementById(newDiv).className = 'detailsShow';
}

function hideDiv(newDiv) {
	document.getElementById(newDiv).className = 'detailsHide';
}

function isUpdateForm(){
    $("#changecheck").val('1');	
}
function isUpdateSForm(form_id){
	if (document.getElementById(form_id) != null)
	{
		document.getElementById(form_id).value = "1";
	}
}

function confirmGo(mesg, url){
	if (confirm(mesg)){
		gotoLocation(url);
	}
}

function mandateStatus(){
	if(document.getElementById('mandate').value=="Open"){
		document.getElementById('mandateRow').className = 'hideRow';
	}else{
		document.getElementById('mandateRow').className = 'showRow';
	}
}

function updatedays(){
	updatedays1('startdate','enddate');
}	

function updatedays1(start_id,end_id){
	if ((document.getElementById(start_id) == null) || (document.getElementById(end_id) == null)) return;
	if((document.getElementById(start_id).value != "") && (document.getElementById(end_id).value != "")){	
	var sdate = new Date(getDateFromFormat(document.getElementById(start_id).value , "yyyy-MM-dd"));
	var edate = new Date(getDateFromFormat(document.getElementById(end_id).value , "yyyy-MM-dd"));
		document.getElementById('totaldays').value = (daysElapsed(edate, sdate)+1);
		if(document.getElementById('totaldays').value < 0){
			alert("Please select an end date that occurs after the start date.");
			document.getElementById(end_id).value = "";
			document.getElementById('totaldays').value = "";
		}
	}
}	

function y2k(number) {return (number < 1000) ? number + 1900 : number;}

function daysElapsed(date1,date2) {
    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}

function titleStatus(){
	if(document.getElementById('property_type').value=="1"){
		document.getElementById('titleRow').className = 'showRow';
	}else{
		document.getElementById('titleRow').className = 'hideRow';
	}
}

function floorShow(){
	var robjValue = getCheckedValue(document.addsellerform.sectionaltitle);
	if(robjValue=="stack" || robjValue=="apartment"){
		document.getElementById('floorRow').className = 'showRow';
		
	}else{
		document.getElementById('floorRow').className = 'hideRow';
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function loadRenterType(select_id, div_id)
{
    $("#"+div_id).toggleClass('hidden');
    $("#renter_loading").toggleClass('hidden');
    selected_id = $("#"+select_id).val();
    //$("#"+div_id).slideUp("fast", function (){
        $("#"+div_id).load("/renter/ajax_property_type.php", {property_type : selected_id}, function(){
            //$("#"+div_id).slideDown("fast");
            $("#"+div_id).toggleClass('hidden');
            $("#renter_loading").toggleClass('hidden');
            makeValidate();
        });         
    //});
}

function watchItem(_monitor_type, _monitor_id, _monitored_type, _monitored_id, _alert_type, _alert_level)
{           
    $.get("/functions/alerts/ajaxalertmonitor.php", {
        monitor_type   : _monitor_type, 
        monitor_id     : _monitor_id, 
        monitored_type : _monitored_type, 
        monitored_id   : _monitored_id,
        alert_type     : _alert_type,
        alert_level    : _alert_level
    });
    if (_alert_level==1)
    {
        $("#watch_status").val(0);
        $("#watch_image").attr("src","/images/icons/watchitemstop.gif");
        $("#watch_image").attr("title","Remove this item from my alerts list");
    }else
    {
        $("#watch_status").val(1);
        $("#watch_image").attr("src","/images/icons/watchitem.gif");
        $("#watch_image").attr("title","Add this item to my alerts list");
    }
}

function changeTitle(title)
    {
        if (title=="sectional") {$("#title_section").show();$("#title_full").hide();}
        else {$("#title_full").show();$("#title_section").hide();}
        
    }
    
function toggleQuotas()
{
    if ($("#enforce_quotas:checked").val()=="1") $("#quotas").attr('class','');
    else $("#quotas").attr('class','invisible');    
}
function onlyNumber(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        status = "This field accepts numbers only."
        return false
    }
    status = ""
    return true
}

function onlyNumberDec(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46  && charCode != 45) {
        status = "This field accepts numbers only."
        return false
    }
    status = ""
    return true
}
