// ************************
// layer utility routines *
// ************************

var agent=navigator.userAgent.toLowerCase();
function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
  styleObject.left = newXCoordinate;
  styleObject.top = newYCoordinate;
  return true;
    } else {
  // we couldn't find the object, so we can't very well move it
  return false;
    }
} // moveObject

function switchIfDone(the_form, this_div, next_div) {

  var complete = true;
  for (var loop=0; loop < the_form.elements.length; loop++) {
    if (the_form.elements[loop].value == "") {
      complete = false;
    }
  }
  if ((complete == true) && (next_div == "finished")) {
    submitTheInfo();
  } 
  else if (complete == true) {
    switchDiv(this_div, next_div);
  } else {
    alert('please complete the form before moving on');
  }
}

function switchDiv(this_div, next_div) {
  if (getStyleObject(this_div) && getStyleObject(next_div)) {
    setVisDisp(this_div, false);
    setVisDisp(next_div, true);
  }
}

function escapeChars(theString) {
   var regexp = /&/g ;
   var regexp2 = /#/g ;
   var regexp3 = /\r\n/g ;
   var regexp4 = /\n/g ;
   var regexp5 = /\"/g ;
   theString = theString.replace(regexp, "%^");
   theString = theString.replace(regexp2, "%*");
   theString = theString.replace(regexp3, "<br>");
   theString = theString.replace(regexp4, "<br>");
   theString = theString.replace(regexp5, "%)");
   return theString;
}

function submitTheInfo() 
{
  var submission_string="";
  var check=true;  
  
   //check if the property owner's name is filled in         
   if (getFormObject('inspectrpt', 'ownerName').value == "") 
   {
     setVisDisp("nameErr", true);
     getFormObject('inspectrpt', 'ownerName').focus();
     
     check=false;
   }

   else
   	setVisDisp("nameErr", false);

      
     
 if (check == true)     
 {    
  for (var form_loop=0; form_loop<document.forms.length; form_loop++) 
  { 
     if(document.forms[form_loop].name != "hiddenform") 
     {
       var el = document.forms[form_loop].elements;

       for (var elems=0; elems<document.forms[form_loop].length;elems++) 
       {
         if (document.forms[form_loop].elements[elems].name != "")            
         {
           if ( document.forms[form_loop].elements[elems].type == "checkbox") 
           {
             submission_string += document.forms[form_loop].elements[elems].name + "=" +
             document.forms[form_loop].elements[elems].checked + "&";
           } 
           
           else if( document.forms[form_loop].elements[elems].type == "radio") 
           { 
             var radiogroup = el[el[elems].name];
            
             for (var j=0; j<radiogroup.length; ++j) 
             {
               if(radiogroup[j].checked) 
               {
                 submission_string += el[elems].name + "=" +
                 document.forms[form_loop].elements[elems + j].value + "&";
                 elems = elems + radiogroup.length - 1;
               }
             }
           } 
           
           else 
           {
             submission_string += document.forms[form_loop].elements[elems].name + "=" +
             escapeChars(document.forms[form_loop].elements[elems].value) + "&";
           }
         }
         
        }
     }
  }

  getFormObject("hiddenform","variablestring").value = submission_string;

  // the next two lines are written for debugging - 
  // to put the script into action
  // comment out the setVisDisp() line
  // and uncomment the document.hidden.form.submit() line
  //

  getForm("hiddenform").submit(); 
  //setVisDisp("hiddenstuff",true);
   }
}


function hideAll(hidden_div) {
  setVisDisp("hidden_div",false);
}


function checkboxcheck(entry,HiddenDiv) { 
  if(entry) {
     if (entry.checked) {
       setVisDisp(HiddenDiv,true);
     }
     else {
       setVisDisp(HiddenDiv,false);
     }
  }
}


function isNumber(inputVal) {
  oneDecimal = false
  inputStr = inputVal.toString()
  for (var i=0; i<inputStr.length; i++) {
    var oneChar =inputStr.charAt(i)
    if (oneChar == "." && !oneDecimal) {
      oneDecimal = true
      continue
    }
    if (oneChar < "0" || oneChar > "9")  {
      if (oneChar != ",") {
        alert("Please make sure entries are numbers.")
        return false
      }
    }
  }
  return true
}

function checkIt(inputvar) {
   inputStr = inputvar.value
   if (isNumber(inputStr)) {
   } else {
     inputvar.focus()
     inputvar.select()
   }
}  

function funcChangeDiv(inputvar,selectedvalue,HiddenDiv) { 
   if(inputvar) {
      if (inputvar.selectedIndex == selectedvalue) {
         setVisDisp(HiddenDiv,true);
      } else {
         setVisDisp(HiddenDiv,false);
      }
   }
}

function funcradChangeDiv(selectedvalue,HiddenDiv) { 
   if (selectedvalue == "true") {
      setVisDisp(HiddenDiv,true);
   } else {
      setVisDisp(HiddenDiv,false);
   }
}

function funcNotChangeDiv(inputvar,selectedvalue,HiddenDiv) { 
   if(inputvar) {
      if (inputvar.selectedIndex != selectedvalue) {
        setVisDisp(HiddenDiv,true);
      } else {
        setVisDisp(HiddenDiv,false);
      }
   }
}

function redisplay(db,openingornot,divname) {
   for (var i=1; i<db.length; i++) {
      setVisDisp(db[i].divname, false);
   }
   if (openingornot == 'true') {
      setVisDisp(db[1].divname, true);
   }  
   setVisDisp(divname, true);
}


function formatCurrency(num,dollarSign) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num)){
      num="0";
      //alert("Oops!  That does not appear to be a valid number.  Please try again."); 
      return (num);
   } else {
      sign = (num == (num = Math.abs(num)));
      num = Math.floor(num*100+0.50000000001);
      cents = num%100;
      num = Math.floor(num/100).toString();
      if(cents<10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
         num = num.substring(0,num.length-(4*i+3))+','+
               num.substring(num.length-(4*i+3));
      }
      if (dollarSign == true)	{
        return (((sign)?'':'-') + '$' + num + '.' + cents);
      } else {
         return (((sign)?'':'-') + num + '.' + cents);
      }
   }
}
function setStatus(msg) {
  status = msg;
}
function bookmark(){
   /***** Compatibility? *********/
   window.external.AddFavorite(window.location.href,window.document.title);
}
function printDocument() {
   if(window.print) {
      window.print();
   } else if(agent.indexOf("mac") != -1) {
      alert("Press Cmd-p to print your document.");
   } else {
      alert("Press Ctrl-p to print your document.");
   }
}
