var
  testid=0

function getResizeWin(winWidth, winHeight) {
  var leftpos=0
  var toppos=0

 if (winWidth==0) {
    winWidth=screen.width-10;
 winHeight=screen.height-15;
  }
  else
  {
    leftpos=(screen.width/2)-(winWidth/2);
 toppos=(screen.height/2)-(winHeight/2);
  }

  self.resizeTo(winWidth, winHeight);
};

function getnewcore(winName, winWidth, winHeight, blScroll, blMsg, blUnique) {
  var leftpos=0
  var toppos=0
  var ie=0;
  var winNameO=winName;
  var winscroll="no";

  if (document.all) {
    ie=1
  }

  var qry=winName.indexOf("?");

  if (qry==-1) {
    winName=winName + '?ie=' + ie
  }
  else if (qry==winName.length - 1) {
    winName=winName + 'ie=' + ie
 winNameO=winNameO.substr(0,qry-1)
  }
  else {
    winName=winName + '&ie=' + ie
    winNameO=winNameO.substr(0,qry-1)
  }

  if (winWidth==0) {
    winWidth=screen.width-10;
 winHeight=screen.height-15;
  }
  else
  {
    leftpos=(screen.width/2)-(winWidth/2);
 toppos=(screen.height/2)-(winHeight/2);
  }

  if (blScroll) {
    winscroll="yes"
  }

  if (blUnique) {
    testid=testid+1;
    winNameO=winNameO + testid;
  }

  var winDef='toolbar=no,location=no,scrollbars=' + winscroll + ',resizable=no,width=' + winWidth + ',height=' + winHeight + ',left=' + leftpos + ',top=' + toppos;

  if (blMsg) {
    var msgWin = window.open("", winNameO, winDef);
 msgWin.document.writeln("<html><head></head><title>Message Window</title><body bgcolor=#FFCC66>");
    msgWin.document.writeln("<h2><i>Please Wait....</i></h2>");
 msgWin.document.writeln("</body></html>");
 msgWin.location=winName;
  }
  else
  {  window.open(winName,
              winNameO,
     'toolbar=no,location=no,scrollbars=' + winscroll + ',resizable=yes,width=' + winWidth + ',height=' + winHeight + ',left=' + leftpos + ',top=' + toppos);
  }
};

function getnew(winName, winWidth, winHeight) {
  getnewcore(winName, winWidth, winHeight, false, false, false)
};

function getnewS(winName, winWidth, winHeight) {
  getnewcore(winName, winWidth, winHeight, true, false, false)
};

function getnewClose(winName, winWidth, winHeight) {
  getnew(winName, winWidth, winHeight)
  parent.close()
};

function getMsgDlg(winName) {
  getnewcore(winName, 500, 10, false, true, false)
};

function getnewUnique(winName, winWidth, winHeight) {
  getnewcore(winName, winWidth, winHeight, true, false, true)
};

function getWindow (winHeight) {
  var leftpos=0;
  var winwidth=screen.width-15;
  var toppos=(screen.height/2)-(winHeight/2);
  var winDef="location=no,status=no,titlebar=no,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes,width=" + winwidth + ",height=" + winHeight + ",left=" + leftpos + ",top=" + toppos;

  return( window.open("","",winDef) );
};

function wcLogin(ID,pass) {
  getnew("loginVal?ID=" + ID + "&pass=" + pass,0,0)
};

function showDSSModalDialog(aURL,aFieldName) {
  return (window.showModalDialog(aURL,aFieldName,"dialogHeight: 450px; dialogWidth: 750px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;"));
};

function showDSSModalDialog2(aURL,aFieldName) {
  return (window.showModalDialog(aURL,aFieldName,"dialogHeight: 300px; dialogWidth: 650px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;"));
};

var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.") >= 0) ? 1 : 0;

function getWindowWidth() {
  if (isMinIE4)
    return document.body.clientWidth;
  return -1;
};

function getWindowHeight() {
  if (isMinIE4)
    return document.body.clientHeight;
  return -1;
};

function getPageWidth() {
  if (isMinIE4)
    return document.body.scrollWidth;
  return -1;
};

function getPageHeight() {
  if (isMinIE4)
    return document.body.scrollHeight;
  return -1;
};

function getPageScrollX() {
  if (isMinIE4)
    return document.body.scrollLeft;
  return -1;
};

function getPageScrollY() {
  if (isMinIE4)
    return document.body.scrollTop;
  return -1;
};

function boolToStr(bool,strTrue,strFalse) {
  if (bool) {return strTrue}
  else {return strFalse}
};

function intCheck(iValue,rStart,rEnd) {
  if (isNaN(iValue)) {return 1}
  else if (!((rStart<=iValue)&&(iValue<=rEnd))) {return 2}
  else {return 0}
};

function intVal(iValue,rStart,rEnd) {
   var intR = intCheck(iValue,rStart,rEnd);
   if (intR!=0) {
     if (intR==1) {alert("Not a number")}
     else {alert("invalid range")};
  return false
   }
   else {return true}
};

function trim(word) {
var fl=0;
 ll=word.length;
 while (word.charAt(fl)==" ") {fl++};
 while (word.charAt(ll)==" ") {ll--};
 return word.substr(fl,ll)
};

function colorFocus(aInput,aErrorText) {
  var oldColor=aInput.style.backgroundColor;
  aInput.style.backgroundColor="yellow";
  alert(aErrorText);
  aInput.style.backgroundColor=oldColor;
  aInput.focus();
};

function autoValidateForm(aForm) {
  var aInputList=aForm.getElementsByTagName("INPUT");

  for (i=0;i<aInputList.length;i++) {
    if ((aInputList[i].required=="true")&&(trim(aInputList[i].value)=="")) {
      colorFocus(aInputList[i],"Value Required");
      return(false);
    };

    if (aInputList[i].fieldtype=="money") {
      var vNewStr = (aInputList[i].value.replace("$","")).replace(",","");
      if (isNaN(vNewStr)) {
        colorFocus(aInputList[i],"Invalid Input");
        aInputList[i].value="";
        return(false);
      }
    }
    else if (aInputList[i].fieldtype=="integer") {
      if (isNaN(aInputList[i].value)) {
        colorFocus(aInputList[i],"Invalid Input");
        aInputList[i].value="";
        return(false);
      }
    }
  };
  return(true);
}

function getContent(aForm) {
var aResult="";
 for (i=0;i<aForm.length;i++) {
   if ((aForm[i].type.toLowerCase()=="text")&&(aForm[i].value!=""))
     {aResult += (aForm[i].name+"="+escape(aForm[i].value)+'&')}
   else if (aForm[i].type.toLowerCase()=="select-one")
     {aResult += (aForm[i].name+"="+escape(aForm[i].value)+'&')}
   else if ((aForm[i].type.toLowerCase()=="radio")&&(aForm[i].checked))
     {aResult += (aForm[i].name+"="+escape(aForm[i].value)+'&')}
 };
 return(aResult);
};

function searchObj(aFieldName, aFieldValue) {
  this.fieldname  = aFieldName;
  this.fieldvalue = aFieldValue;
};

function getRecord(aXML, aIndexName, aIndexValue, aFieldName) {
  aXML.recordset.MoveFirst();
  while (!aXML.recordset.EOF) {
    if (aXML.recordset.fields(aIndexName).value==aIndexValue) {
       if (typeof(aFieldName)=="object") {
          for (i=0;i<aFieldName.length;i++) {
             aFieldName[i].fieldvalue = aXML.recordset.fields(aFieldName[i].fieldname).value;
          };
          return(aFieldName);
       }
       else {
          return(aXML.recordset.fields(aFieldName).value);
       }
    };
    aXML.recordset.MoveNext;
  };
  return "";
};

function initArraySize(size) {
 this.length = size;
 for (i=0;i<this.length;i++)
  this[i] = "";
};

function areaPaste(aArea) {
var vClipText    = window.clipboardData.getData('Text');
var vClipLength  = vClipText.length;
var vAreaLength  = aArea.value.length;
var vTotalLength = (vAreaLength+vClipLength) - aArea.maxlength;

  if (vTotalLength > 0) {
    if (vAreaLength < aArea.maxlength) {
      window.clipboardData.setData("Text", vClipText.substr(0, (aArea.maxlength-vAreaLength)))
    }
    else {return false;}
  };
};

function areaKeyPress(aArea) {
  return ( aArea.value.length < aArea.maxlength );
};

function concatUndefined() {
var vResult = "";
  for (var i=0;i<arguments.length;i++) {
    if (typeof(arguments[i])=="undefined") {return("")}
    else (vResult += arguments[i])
  };

  return(vResult);
};

function isUndefined(aString) {
  aString = trim(aString);
  if (aString=="") {
    return(undefined)
  }
  else {
    return(aString)
  };
};

function fnAddress_Cat(a_add1, a_add2, a_add3, a_add4, a_suburb, a_state, a_postcode, a_separator1, a_separator2) {

  a_add1       = isUndefined(a_add1);
  a_add2       = isUndefined(a_add2);
  a_add3       = isUndefined(a_add3);
  a_add4       = isUndefined(a_add4);
  a_suburb     = isUndefined(a_suburb);
  a_state      = isUndefined(a_state);
  a_postcode   = isUndefined(a_postcode);
  var v_result = undefined;

  if (typeof(a_add1)!="undefined") {v_result = a_add1};
  if (typeof(a_add2)!="undefined") {v_result = concatUndefined(v_result, a_separator1) + a_add2};
  if (typeof(a_add3)!="undefined") {v_result = concatUndefined(v_result, a_separator1) + a_add3};
  if (typeof(a_add4)!="undefined") {v_result = concatUndefined(v_result, a_separator1) + a_add4};
  if (typeof(a_suburb)!="undefined") {v_result = concatUndefined(v_result, a_separator1) + a_suburb};
  if (typeof(a_state)!="undefined") {v_result = concatUndefined(v_result, a_separator2) + a_state};
  if (typeof(a_postcode)!="undefined") {v_result = concatUndefined(v_result, a_separator2) + a_postcode};

  return(v_result);
};


function exportxmltocsv(aXML, aFieldNameArray, aFieldTitleArray) {
  var vWindow = getWindow(400);

vWindow.document.writeln("<html><head>");
vWindow.document.writeln('<meta http-equiv="charset" content="iso-8859-1">');
vWindow.document.writeln("</head><body>");

  for (i = 0; i < aFieldTitleArray.length; i++) {
    if (i==aFieldNameArray.length-1) {
       vWindow.document.writeln(aFieldTitleArray[i] + "<br />");
    }
    else {
       vWindow.document.write(aFieldTitleArray[i]+",");
    };
  };

  aXML.recordset.MoveFirst();
  while (!aXML.recordset.EOF) {
    for (i=0; i < aFieldNameArray.length; i++) {
      if (i==aFieldNameArray.length-1) {
         vWindow.document.writeln('"' + aXML.recordset.fields(aFieldNameArray[i]).value + '"' + '<br />');
      }
      else {
         vWindow.document.write('"' + aXML.recordset.fields(aFieldNameArray[i]).value + '",');
      };
    };
    aXML.recordset.MoveNext;
  }

vWindow.document.writeln("</body></html>");
};

function nodisplay (aID) {
   aID.style.display="none";
};

function fnDataComplete() {
   ipagenum.innerHTML = xmlsearch.recordset("page") + "&nbsp;" + xmlpage;
};


function PostXML(aURL,aXML){
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST", aURL, false);
xmlhttp.setRequestHeader("content-type","text/xml");
xmlhttp.send(aXML);
alert(xmlhttp.responseText);
};

function showXMLError(aXML) {
  alert(aXML.recordset.fields('message').value)
};

function debtTypeLookup(aID, aDesc) {
   var vDebtTypeArray = new initArraySize(2);

   vDebtTypeArray[0] = new searchObj("dt_code", "");
   vDebtTypeArray[1] = new searchObj("dt_desc", "");

   var vNewDebtTypeArray = showDSSModalDialog("searchdebttypeframe", vDebtTypeArray);

   if (typeof(vNewDebtTypeArray)!="undefined") {

      aID.value=vNewDebtTypeArray[0].fieldvalue;
      if (typeof(aDesc.datafld)!="undefined") {
         aDesc.datafld.value=vNewDebtTypeArray[1].fieldvalue;
      }
      else {
         aDesc.innerText=vNewDebtTypeArray[1].fieldvalue;
      }
   };
};

function fnPostcode(aSuburbCtrl,aStateCtrl,aPostcodeCtrl) {

   xmlsuburb.async = false;

   xmlsuburb.load("xml?origin=postcode&dataset=search&page=1&criteria=" + escape(" and pc_town = '" + aSuburbCtrl.value) + escape("'"));


   if (xmlsuburb.recordset.State!=0) {
      if (xmlsuburb.recordset.recordcount > 1) {

         var PostScrDimensions = "dialogHeight: 200px; dialogWidth: 500px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;";

         var vPostcodeArray = new Array(2);
         vPostcodeArray[0] = new searchObj("pc_id", "");
         vPostcodeArray[1] = new searchObj("pc_state", "");
         vPostcodeArray[2] = new searchObj("pc_postcode", "");


         var frameQueryString = "popup?src=" + escape("searchpostcodebottom?ndbnocount=1&pc_town=" + aSuburbCtrl.value + "&ndbSRadio=2");

         var vPostcodeArray = window.showModalDialog(frameQueryString,vPostcodeArray,PostScrDimensions);

         if (typeof(vPostcodeArray)!="undefined") {
            aStateCtrl.value    = vPostcodeArray[1].fieldvalue;
            aPostcodeCtrl.value = vPostcodeArray[2].fieldvalue;

            if (aStateCtrl.onafterupdate != null) { aStateCtrl.onafterupdate(); };
            if (aPostcodeCtrl.onafterupdate != null) { aPostcodeCtrl.onafterupdate(); };
         }
      }
      else if (xmlsuburb.recordset.recordcount == 1) {
         aStateCtrl.value    = xmlsuburb.recordset.fields("pc_state").value;
         aPostcodeCtrl.value = xmlsuburb.recordset.fields("pc_postcode").value;

         if (aStateCtrl.onafterupdate != null) { aStateCtrl.onafterupdate(); };
         if (aPostcodeCtrl.onafterupdate != null) { aPostcodeCtrl.onafterupdate(); };
      }
   } <!--else alert ("Postcode information could not be retrieved.");-->
};

function checkValid() {
     // Y is the default
     var searchValueRequired = new String("Y");

     try
     {	
       if (document.getElementById('xmlsettings') != null)
       {
         searchValueRequired = xmlsettings.recordset('searchValueRequired');
         searchValueRequired = searchValueRequired.toUpperCase();
       }         
     }
     catch(e)
     {
		// ignore any errors raised
     }
		
     if (searchValueRequired == 'Y')
	 {
       for(i=0; i<validtable.all.length;i++) {
         if (validtable.all.item(i).type == "text") {
           if (trim(validtable.all.item(i).value)!="") {
             return true;
           }
         }
       }
       
       alert('Please enter a value to search on to continue');
       return false;
     }
     else
       return true;
};

