/////////////////////////////////////////////////////////////////////
//
//  Project: MWasp
//
//	MWasp_Funcs.js
//	03/18/02 Harry Walden - MWare, Inc.
//
//	Client side procedures.
//
//  Revision History
//  -----------------------------------------------------------------
//  20020318  hmw Created initial version
//  20020403  hmw Added the OpenEditRecord function
//  20020518  hmw Added the Focus First Field function
//  20020602  hmw Added the GridSort function
//  20021202  hmw Added the Swap images functions from Dreamweaver for rollovers
//
/////////////////////////////////////////////////////////////////////

// proper case function (JScript 5.5+)
function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|[\s'.-](.)/g, 
          function($1) { return $1.toUpperCase(); });
}

function MM_findObj(n, d) { //v4.01
  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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function onlyonce() {
//
//  Keep the user from pressing submit more than once
//
numberoftimes += 1;

  if (numberoftimes > 1) { 
    var themessage = "Please be patient.  You have already submitted this form.  Pressing submit multiple times will result in your account being billed multiple times.  You will receive a response momentarily.";
    if (numberoftimes == 3) {
      themessage = "DO NOT PRESS SUBMIT MULTIPLE TIMES!!!  YOUR ACCOUNT WILL BE BILLED EACH TIME YOU PRESS SUBMIT!!!  Processing may take up to one minute.";
    }
    alert(themessage);
    return false; 
  } 
  else {
    return true;
  }
}


function ShowDialog(URL) {
//
//  This function opens the page specifed in the URl with a preset
//  window description
//

  window.open(URL,"_blank","left=100,height=600,width=800,scrollbars=yes");
  
}

function GridSort(Section, Field, Direction) {
//
//  this function sets the sort field for the section
//  and tells the form to post
//

  document.frm["SortSection"].value = Section;
  document.frm["OrderBy"].value = Field + " " + Direction;
  document.frm.submit();

  //alert(document.frm["OrderBy"].value);
  return false;
}

function FocusFirstField() {
//
//  Sets focus to the first available field on the form
//
var i;

  for (i=0; i<document.frm.length; i++){
    if (document.frm[i].type!="hidden") {
      if (typeof document.frm[i].disabled != 'undefined') {
        if (!document.frm[i].disabled) {
          document.frm[i].focus(); 
          if (document.frm[i].type=="text") {document.frm[i].select()}
          return;
        }
      }
    }
  }
}

function OpenEditRecord(Features, AllowAddNew, SectionName, RecordIdentifier, RecordIdentifierValue) {
//
//	Open up a page in a new window
//
//if (RecordIdentifierValue == "~") {alert("'" + RecordIdentifierValue+ "'")}

	  if (RecordIdentifierValue == "~") {
	    window.open("../RecordEditor.asp?SectionName=" + SectionName + "&RecordIdentifier=" + RecordIdentifier + "&AllowAddNew=" + AllowAddNew+ "&Features=" + Features, "_blank", Features);
	  }
	  else {
	    window.open("../RecordEditor.asp?SectionName=" + SectionName + "&RecordIdentifier=" + RecordIdentifier + "&RecordIdentifierValue=" + RecordIdentifierValue + "&AllowAddNew=" + AllowAddNew + "&Features=" + Features, "_blank", Features);
	  }
		
	return false;
}

function JiggleTheHandle() {
//
//  This function will refresh the data in the underlying window
//

  //alert(window.opener.location.href);
  window.opener.location.href = window.opener.location.href;
}

function OpenWindow(URL, Target, Features) {
//
//  This function opens the specified URL in a new window and returns false
//

  window.open(URL, Target,Features);
  return false;

}
function CallAsp(URL) {
//
//  This procedure allow the html page to call ans ASP page from the browser
//  If the page spews out anything it is returned to the calling script
//
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

  xmlhttp.open("GET", URL, false);
  xmlhttp.send();
  return xmlhttp.responseText;
}

function GetRsXML(RecordID,Query,FieldList,Options) {
//
//  This function is used for retrieving an XML stream from the server
//  and populating the fields on the data entry form specified in the field list
//
var URL = 'GetRsXML.asp?s=' + SessionID + '&u=' + UserID + '&q=' + Query + '&p1=' + RecordID
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var xmldom = new ActiveXObject("Microsoft.XMLDOM");

	//	Bail if not IE
	//
	if (navigator.userAgent.indexOf("MSIE")==-1) {return true}
	
  //if (Options) {alert(Options);}

  //  20031220 hmw Check to see if we have options passed in for clearing first
  //
  if (Options) {
    if (RecordID=="") {
      var Fields = FieldList.split(",");
      for (i=0; i<Fields.length; i++) {
        //alert(Fields[i]);
        document.forms(0)(Fields[i]).value="";
        //  If the field has an OnChange Event, Fire it
        //
        if (document.forms(0)(Fields[i]).getAttribute("onChange")) {
          document.forms(0)(Fields[i]).onchange();
        }
      }
    }
  }

  //  Early exit if we are clearing a field
  //
  if (RecordID=="") {return true;}
  if (RecordID=="*") {return true;}

  xmlhttp.open("GET", URL, false);
  xmlhttp.setRequestHeader("Content-Type","text/xml");
  xmlhttp.send();
  
  xmldom.loadXML(xmlhttp.responseText);
//	document.write(xmlhttp.responseText);
  //if (UserID=="HWALDEN") {alert(xmlhttp.responseText);}

  if (xmldom.selectSingleNode("//status").text=='0') {
    alert(xmldom.selectSingleNode("//message").text);
    return false;
  }
  else {  

    var Fields = FieldList.split(",");
    var msg = "";
    
    
    //  Loop though the field list and determine if we nee to prompt the user
    //  to overwite and existing information in the fields
    //
    for (i=0; i<Fields.length; i++) {
      if (document.forms(0)(Fields[i])) {
        if (document.forms(0)(Fields[i]).value != "") {
          if (xmldom.selectSingleNode("//" + Fields[i])) {
            if (document.forms(0)(Fields[i]).value != xmldom.selectSingleNode("//" + Fields[i]).text) {
              if (msg != "") {msg += ", "}
              msg += Fields[i];
            }
          } else {
            if (msg != "") {msg += ", "}
            msg += Fields[i];          
          }
        }
      }
    }
    
    //  20031220 hmw Check to see if we have options passed in for no warning
    //
    if (Options) {msg=""}
              

    //  If there are fields that need to be over written then prompt the user to confirm
    //
    if (msg != "") {
      if (!confirm("The following pieces of information are different :\n" + msg + ".\n Do you want to overwrite these EMS values from the external database?")) {
        return true;
      }     
    }
                
    //  Loop thour the field list and if a field exists on the form
    //  set the value to the corresponding value in the XML stream if there is one
    //
    for (i=0; i<Fields.length; i++) {
      if (document.forms(0)(Fields[i])) {
        if (xmldom.selectSingleNode("//" + Fields[i])) {
          document.forms(0)(Fields[i]).value=xmldom.selectSingleNode("//" + Fields[i]).text ;
          
          //  If the field has an OnChange Event, Fire it
          //
          if (document.forms(0)(Fields[i]).getAttribute("onChange")) {
            document.forms(0)(Fields[i]).onchange();
          }
        }
        else {
          document.forms(0)(Fields[i]).value=""
        }
      }
    }
    return true;
  }
  
}
