function addfaxoption(textval,valval) {
   var hiddenval = document.send_fax.ccuserlist.value;
   var myoption = new Option(textval,valval,true,true);
   
   document.send_fax.ccuserid.options[document.send_fax.ccuserid.options.length] = myoption;
   if (hiddenval.length) {
      document.send_fax.ccuserlist.value = hiddenval + "," + valval;
   } else {
      document.send_fax.ccuserlist.value = valval;
   }
}
function addemailoption(textval,valval) {
   var hiddenval = document.send_email.ccuserlist.value;
   var myoption = new Option(textval,valval,true,true);
   
   document.send_email.ccuserid.options[document.send_email.ccuserid.options.length] = myoption;
   if (hiddenval.length) {
      document.send_email.ccuserlist.value = hiddenval + "," + valval;
   } else {
      document.send_email.ccuserlist.value = valval;
   }
}
function addoption(formname,field,id,textvalue) {
    var addopt = new Option(textvalue,id,true,false);
    var optlength = eval("document." + formname + "." + field + ".options.length");
    var selfield = eval("document." + formname + "." + field);
    selfield.options[optlength] = addopt;
    selfield.options[optlength].selected = true;
}
function quickselect(userform,userselect) {
    var strDiv;
    var strItem;
    var strCompany;
    var strLast;
    var tmpval;
    var startindex = 1;
    var selsize = userselect.options.length;
    var results = new Array(selsize);
    var matchoption = 0;
    var matchtotal = 0;

    strDiv = userform.txtDiv.value;
    strItem = userform.txtItem.value;
    strCompany = userform.txtCompany.value;
    strLast = userform.txtLast.value;
    var cmpare = new RegExp();

    for (var x = startindex; x < selsize; x++) {
        tmpval = userselect.options[x].text;

        var tmparr = tmpval.split(/:/);
        var total = 0;


        //userform.txtCompany.value = arrfoo[0];
        if (strDiv.length) {
            cmpare.compile(strDiv);
            if (tmparr[0].match(cmpare)) {
                total += tmparr[0].match(cmpare).length;
            } 

                    
        }
        if (strItem.length) {
            cmpare.compile(strItem);
            if (tmparr[1].match(cmpare)) {
                total += tmparr[1].match(cmpare).length;
            } 
        }
        if (strCompany.length) {
            cmpare.compile(strCompany,"i");
            if (tmparr[2].match(cmpare)) {
                total += tmparr[2].match(cmpare).length;
            } 
            cmpare.compile(strCompany);
            if (tmparr[2].match(cmpare)) {
                total += tmparr[2].match(cmpare).length;
            }         
        }
        if (strLast.length) {
            cmpare.compile(strLast,"i");
            if (tmparr[3]) {
                if (tmparr[3].match(cmpare)) {
                    total += tmparr[3].match(cmpare).length;
                }
            }
//            cmpare.compile(strLast);
//            if (tmparr[3].match(cmpare)) {
//                total += tmparr[3].match(cmpare).length;
//            }         
        }
        if (total > matchtotal) {
            matchtotal = total;
            matchoption = x;
        }
        

    }
    userselect.selectedIndex = matchoption;
}
function openPopup(str,name,w,h) { // GK added (used in Unit Quick View)
    if (!name) {
        var date=new Date();
        name=date.getTime();
    }
    if (!w) {
        w=600;
    }
    if (!h) {
        h=400;
    }
    window.open(str,name,"height="+h+",width="+w+",resizable,scrollbars=yes");
    return false;
}
function fill_info(e,ind,x,y){
  if (documentLoaded==true) {
    if(!x) x=15;
    if(!y) y=25;
    var name='l_'+ind;
    var obj=new getObj(name);
    showByPosition(obj,x,y,e);
  }
}
function showByPosition(obj,x,y,e) { // GK added (used in Unit Quick View)
    
    if (documentLoaded==true) {

        var posx = 0;
    	var posy = 0;
        //var tempx=x;
        //var tempy=y;
    
        if (!e) var e = window.event;
    	
        if (e.pageX || e.pageY){
            //Firefox
    		posx = e.pageX;
    		posy = e.pageY;
    	}else if (e.clientX || e.clientY){
            //IE
    		posx = e.clientX + document.body.scrollLeft;
    		posy = e.clientY + document.body.scrollTop;
    	}
    
        if (window.innerWidth || window.innerHeight) {
           //Firefox
            var winx=(window.pageXOffset + window.innerWidth);
            var winy=(window.pageYOffset + window.innerHeight);

            if ((posx + x + obj.obj.offsetWidth) > window.innerWidth)
                x = winx - obj.obj.offsetWidth - 15;
            else
                x = posx + x;

            if ((posy + y + obj.obj.offsetHeight) > winy)
                y = posy - obj.obj.offsetHeight - 15;
            else
                y = posy + y;

            //alert('Mouse X: \t'+posx+'\nOffset X: \t'+tempx+'\nElem Width: \t'+obj.obj.offsetWidth+'\nWin Width: \t'+window.innerWidth+'\nScroll Width: \t'+window.pageXOffset+'\nTotal Width: \t'+winx+'\nFINAL X: \t'+x+'\n\nMouse Y: \t'+posy+'\nOffset Y: \t'+tempy+'\nElement Height: \t'+obj.obj.offsetHeight+'\nWin Height: \t'+window.innerHeight+'\nScroll Height: \t'+window.pageYOffset+'\nTotal Height: \t'+winy+'\nFINAL Y: \t'+y);

        }else{
            //IE
            if ((posx + x + obj.obj.clientWidth) > (document.body.clientWidth + document.body.scrollLeft)){
                x = posx - x - obj.obj.clientWidth;
            }else{
                x = posx + x;
            }

            if ((posy + y + obj.obj.clientHeight) > (document.body.clientHeight + document.body.scrollTop)){
              y = posy - y - obj.obj.clientHeight;
            }else{
              y = posy + y;
            }
            var winx='undef';
            var winy='undef';
                //alert('Else Mouse X: \t'+posx+'\nOffset X: \t'+tempx+'\nElem Width: \t'+obj.obj.offsetWidth+'\nWin Width: \t'+window.innerWidth+'\nScroll Width: \t'+document.body.scrollLeft+'\nTotal Width: \t'+winx+'\nFINAL X: \t'+x+'\n\nMouse Y: \t'+posy+'\nOffset Y: \t'+tempy+'\nElement Height: \t'+obj.obj.offsetHeight+'\nWin Height: \t'+window.innerHeight+'\nScroll Height: \t'+document.body.scrollTop+'\nTotal Height: \t'+winy+'\nFINAL Y: \t'+y);
        }
        //alert('div clientHeight: '+obj.obj.clientHeight+', offsetParent: '+obj.obj.offsetParent+', offsetHeight: '+obj.obj.offsetHeight+', innerHeight: '+obj.obj.innerHeight+"\n"+', outerHeight: '+obj.obj.outerHeight+', marginHeight: '+obj.obj.marginHeight+', scrollHeight: '+obj.obj.scrollHeight+', id: '+obj.obj.id);
        //if y is negative, it will not display everything. Set it to 0 plus scroll offset.
        if(y < (0+document.body.scrollTop)){
          y=0+document.body.scrollTop;
          //alert('y reset: '+y);
        }
        obj.style.top=y;
        obj.style.left=x;
        obj.style.visibility='visible';
    }
}
function show_popup(e,ind,x,y){
  if (documentLoaded==true) {
    if(!x) x=15;
    if(!y) y=25;
    var name='l_'+ind;
    var obj=new getObj(name);
    showLayer(obj,x,y,e);
  }
}
function showLayer(obj,x,y,e) { // GK added (for use OUTSIDE of Unit Quick View)
    
    if(window.event + "" == "undefined"){

        if ((e.x + x + 50 + obj.obj.offsetWidth) > (window.pageXOffset + window.innerWidth))
            x = x - obj.obj.offsetWidth;

        if ((e.y + y + 50 + obj.obj.offsetHeight) > (window.pageYOffset + window.innerHeight))
            y *= -4;

        obj.style.left = e.x + x;
        obj.style.top = e.y + y;
        obj.style.visibility = 'visible';

    }else{
        e = window.event;

        if ((e.x + x + obj.obj.clientWidth) > (document.body.clientWidth))
            x = (document.body.clientWidth + document.body.scrollLeft) - obj.obj.clientWidth;
        else
            x = e.x + x + document.body.scrollLeft;

        if ((e.y + y + obj.obj.clientHeight) > (document.body.clientHeight)){
            y = e.y - obj.obj.clientHeight - 15 + document.body.scrollTop;
        }
        else{
            y = e.y + y + document.body.scrollTop;
        }
        obj.style.posLeft = x;
        obj.style.posTop = y;
        obj.style.visibility = 'visible';
    }
}
function hide(name) { // GK added (used in Unit Quick View)
  if (documentLoaded==true) {
    var obj=new getObj(name);
    obj.style.visibility='hidden';
  }
}
function getObj(name){
  if (document.getElementById){
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }else if (document.all){
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }else if (document.layers){
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}
function change_text_value (sel,text){
    if (change_text_array.length>0) {
        text.value=change_text_array[sel.selectedIndex];
    }
}
function check_required (f){
   //needs checking for radio buttons
    var empties="";
    var emptym2m="";
    var focuselement;
    var msg="";
    var chk=true;

    var m2m = new Array();
    for (var i=0; i<f.length; i++) {
        var e=f.elements[i];
        
        if ((e.type=="text" || e.type=="textarea" || e.type=="password") && (e.bprequired || e.required_switch)) {

            if (e.bprequired || parseInt(e.required_switch.options[e.required_switch.options.selectedIndex].value)==parseInt(e.required_case)) {
                //alert(e.name+" type="+e.type+" val="+e.value);
                if (is_blank(e.value)) {
                    empties += "\n     " + e.label;
                    if (chk) {
                        focuselement=e;
                        chk=false;
                    }
                    continue;
                }
            }
        }
        
        if ((e.type=="file") && (e.bprequired || e.required_switch)) {

            if (e.bprequired || parseInt(e.required_switch.options[e.required_switch.options.selectedIndex].value)==parseInt(e.required_case)) {
                var del = document.getElementsByName("_delete_"+e.name)[0];
                var old = document.getElementsByName("oldfilename_"+e.name)[0];
                if(del && is_blank(e.value)) {
                    if(del.checked) {
                        empties += "\n     " + e.label;
                        if (chk) {
                            focuselement=e;
                            chk=false;
                        }
                        continue;
                    }
                } else if (is_blank(e.value) && (!old || !old.value)) {
                    empties += "\n     " + e.label;
                    if (chk) {
                        focuselement=e;
                        chk=false;
                    }
                    continue;
                }
            }
        }
                        
        if((e.type=="select-one") && (e.bprequired))
        {
            if((e.value==0) || (e.value=="NULL")) {
                empties += "\n     " + e.label;
               if (chk) {
                   focuselement=e;
                   chk=false;
               }
               continue;
            }
        }

        if(e.type=="checkbox" && e.getAttribute('bprequired')==1) {
            var label = e.getAttribute('namelabel');
            if(e.checked) {
                m2m[label] = 1;
            } else if (!m2m[label]) {
                focuselement=e;
                m2m[label] = 0;
            }
        }
    }
    for (var i in m2m) {
        if(m2m[i] == 0) {
            emptym2m += "\n     " + i;
        }        
    }

    if(empties) {
        msg = "You must fill in the following required fields:\n" + empties;
    }
    if(emptym2m) {
        if(msg) {
            msg += "\n\n";
        }
        msg += "You must select one or more items in the following categories:\n" + emptym2m;
    }
    if (msg.length) {
        alert (msg);
        focuselement.focus();
        return false;
    }else{
        return true;
    }
}
function check_pattern (f){

    var empties="";
    var focuselement;
    var chk=true;

    for (var i=0; i<f.length; i++) {
        var e=f.elements[i];
        
        if (e.has_pattern) {
            if (is_blank(e.value)) {
                continue;
            } else if (e.content_invalid) {
                var invalid = e.content_invalid(e.value);
                if (invalid != null) {
                    empties += "\n     " + e.label + " - " + invalid;
                    if (chk) {
                        focuselement=e;
                        chk=false;
                    }
                }
                continue;
            }
        }
    }
    if (empties) {
        var msg = "The following fields are invalid:\n" + empties;
        alert (msg);
        focuselement.focus();
        return false;
    }else{
        return true;
    }
}
function is_blank(s){
    if (s == null) return true;
    if (s == "") return true;
    for (var i=0; i<s.length; i++) {
        var c = s.charAt(i);
        if ((c!=' ') && (c!='\n') && (c!='\t')) return false;
    }
    return true;
}
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_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_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n);return x;
}
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_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) {v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v;}
}
function MM_showHideLayersDisplay() { //DC added to use display none/block instead of visibility.
  var i,p,v,obj,args=MM_showHideLayersDisplay.arguments;
  for (i=0; i<(args.length-2); i+=3){
       if ((obj=MM_findObj(args[i]))!=null) {
           v=args[i+2];
           if (obj.style) { 
               obj=obj.style; 
               v=(v=='show')?'block':(v='hide')?'none':v; 
           }
           obj.display=v;
       }
  }
}
function check_query (f){

    var XYZ = new Array ('X','Y','Z');
    var ok = true;
    var errmsg = "";
    var firstobj;
    var XYZselected = new Array();

    // make sure no X, Y or Z are the same
    for (var i=0; i<XYZ.length; i++) {
        var sel = eval ("f."+XYZ[i]+"axis");
        if (sel) {
            for (var j=0; j<XYZselected.length; j++) {
                if (sel.options[sel.selectedIndex].value == XYZselected[j]) {
                    alert ("Cannot select " + sel.options[sel.selectedIndex].text + " as multiple axes!");
                    sel.focus();
                    return false;
                }
            }
            XYZselected[i] = sel.options[sel.selectedIndex].value;
        }
    }

    // check selected X, Y, Z (if present) axes
    if (Selectors.length) {
        for (var i=0; i<Selectors.length; i++) {
            var obj = eval ("f."+Selectors[i]);
            //alert (obj.name);
            var isAxis=true;
            /*
            for (var x=0; x<XYZ.length; x++) {
                var sel = eval ("f."+XYZ[x]+"axis");
                if (sel.options[sel.selectedIndex].value == obj.name) {
                    isAxis=true;
                    break;
                }
            }
            */
            var selects=0;
            for (var j=0; j<obj.options.length; j++) {
                if (obj.options[j].selected) selects++;
            }
            if (isAxis) {
                // make sure we have at least one selected
                if (selects < 1) {
                    ok=false;
                    errmsg += "You must select at least one "+SLabels[i]+"\n\n";
                    if(!firstobj) firstobj = obj;
                }
            }else{
                // make sure we have one and ONLY one selected
                if (selects != 1) {
                    ok=false;
                    errmsg += "You must select ONLY one "+SLabels[i]+", \nor select "+SLabels[i]+" as your X, Y or Z axis.\n\n";
                    if(!firstobj) firstobj = obj;
                }
            }
        }
    }
    if (!ok) {
        alert(errmsg);
        firstobj.focus();
    }
    return ok;
}
function check_survey_responses (f){
    //params: current form object
    var empties="";
    var focuselement;
    var missing=false;
    var processed = new Array();
    
    for (var i=0; i<f.length; i++) {
        var e=f.elements[i];
        if (e.type=="checkbox" || e.type=="select-one" || e.type=="radio") {
            if (!processed[e.name] && !has_response(f,e)) {
                //alert(e.name+" type="+e.type+" val="+e.value);
                missing=true;
                if (!focuselement) {
                    focuselement=e;
                }
            }
            processed[e.name]=1;
        }
        //alert (processed);
    }
    if (missing) {
        alert ("Please make sure you respond to each question before continuing.");
        focuselement.focus();
        return false;
    }else{
        return true;
    }
}
function has_response (f,e){
    // params: current form element
    
    switch (e.type) {
    case "checkbox":
        var obj = eval("f."+e.name); // this retrieves the array of all checkboxes with same name
        //alert ("checking "+obj.name+":"+obj.length);
        if (obj.length) {
            for (var i=0; i<obj.length; i++) {
                if (obj[i].checked) {
                    //alert (e.name+" has a response");
                    return true;
                }
            }
            return false;
        }else return true;
        break;
    case "select-one":
        //alert ("checking "+e.name+":"+e.selectedIndex);
        if (e.selectedIndex>=0) return true;
        else return false;
        break;
    case "radio":
        var obj = eval("f."+e.name); // this retrieves the array of all radio buttons with same name
        //alert ("checking "+obj.name+":"+obj.length);
        if (obj.length) {
            for (var i=0; i<obj.length; i++) {
                if (obj[i].checked) {
                    //alert (e.name+" has a response");
                    return true;
                }
            }
            return false;
        }else return true;
        break;
    default:
        return true;
        break;
    }
}
function applymask(e, patternarr, maskarr, errorid) { 
    for(i=0; i<patternarr.length; i++) {        
        if(e.value.match(patternarr[i])) {            
            e.value = e.value.replace(patternarr[i], maskarr[i]);
            document.getElementById(errorid).style.display='none';
            return true;
        }
    }

    if(e.value != "") {
        e.value = "";
        document.getElementById(errorid).style.display='inline';
    }

    return false;
}
function textlimit(field,counter,limit) {
  if (field.value.length > limit)
    field.value = field.value.substring(0, limit);
  else
    counter.value = (limit - field.value.length) + " char. left";
}

// To be deprecated - no longer used
function add_list_item(listid,str){
    var eItem = document.createElement("li");
    eItem.appendChild(document.createTextNode(str));
    document.getElementById(listid).appendChild(eItem);
}
function find_target(){
    var targ;
    if (self.opener == null) {
        targ='_self';
    }else if(self.opener.parent.name=='main'){
        //alert("My opener's parent's name is " + self.opener.parent.name);
        targ=self.opener.parent.name;
    }else if (self.opener.name=='main') {
        //alert("My opener's name is " + self.opener.name);
        targ=self.opener.name;
    }else{
        targ='_self';
    }
    return targ;
}


