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 addoptionjs(formname,field){
    var newopt=prompt("Enter your new option (max 30 characters):","");
    if (newopt != null && newopt != "") {
        if (newopt.length > 30) {
            newopt = newopt.substring(0,30);
        }
        var id="userdefined_"+newopt;
        addoption(formname,field,id,newopt);
    }
}
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) {
    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=auto");
    return false;
}
function showByPosition(object,x,y,e) { // GK added (used in Unit Quick View)
    
    //var isIE = document.all?true:false;
    //if (!isIE) document.captureEvents(Event.MOUSEMOVE);
    
    if(window.event + "" == "undefined"){ //if (document.layers && document.layers[object]) {

        if ((e.x + x + 50 + document.layers[object].clip.width) > (window.pageXOffset + window.innerWidth))
            x = x - document.layers[object].clip.width;

        if ((e.y + y + 50 + document.layers[object].clip.height) > (window.pageYOffset + window.innerHeight))
            y *= -4;

        document.layers[object].left = e.x + x;
        document.layers[object].top = e.y + y;
        document.layers[object].visibility = 'visible';
    
    }else if (document.all) {
        e = window.event;
        
        if ((e.x + x + document.all[object].clientWidth) > (document.body.clientWidth))
            x = (document.body.clientWidth + document.body.scrollLeft) - document.all[object].clientWidth;
        else
            x = e.x + x + document.body.scrollLeft;

        if ((e.y + y + document.all[object].clientHeight) > (document.body.clientHeight)){
            y = e.y - document.all[object].clientHeight - 15 + document.body.scrollTop;
        }
        else{
            y = e.y + y + document.body.scrollTop;
        }
        document.all[object].style.posLeft = x;
        document.all[object].style.posTop = y;
        document.all[object].style.visibility = 'visible';
    }
}
function hide(object) { // GK added (used in Unit Quick View)
    if (document.layers && document.layers[object])
        document.layers[object].visibility = 'hidden';
    else if (document.all)
        document.all[object].style.visibility = 'hidden';
}
function fill_info(ind,x,y){
    if(!x) x=15;
    if(!y) y=25;
    var layer='l_'+ind;
    showByPosition(layer,15,25);
}
function show_elevation(ind){
    if (gImages[ind] != "") parent.image.document.modelimg.src=gImages[ind];
}
function clear_info(ind){
    var layer='l_'+ind;
    hide(layer);
}
function hide_elevation(){
    parent.image.document.modelimg.src="/images/Elevation_None.jpg";
}
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;
}
function set_colours (obj, id, prefix){
    var menu=eval("obj.form.control_"+id+"_ColorID");
    menu.options.length=0;

    if (prefix=="mat") {
        var idarr=eval("matmfrids_"+id);
        var mfrmenu=eval("obj.form.control_"+id+"_ManufacturerID");
        var match=0;
        if (obj.options[obj.selectedIndex].value!=0) {
           match=idarr[obj.selectedIndex];
        }
        
        for (var i=0; i<mfrmenu.length; i++) {
            if (mfrmenu.options[i].value==match) {
                mfrmenu.selectedIndex=i;
                break;
            }
        }
    }

    if (obj.options[obj.selectedIndex].value > 0) {
        var mfid=obj.options[obj.selectedIndex].value;
        var idarr=eval(prefix+"clrids_"+id+"_"+mfid);
        if (idarr.length) {
            var namearr=eval(prefix+"clrnames_"+id+"_"+mfid);
            var reset_option=eval("reset_option_"+id);
            for (var i=0; i<idarr.length; i++) {
                var bln=(idarr[i]==reset_option[0])?true:false;
                var newopt = new Option(namearr[i],idarr[i],false,bln);
                menu.options[menu.options.length] = newopt;
            }
        }
    }
    menu.options[menu.options.length] = new Option('- select -',0,false,false);
}
function reset_colours (form){
    form.reset();
    var pattern = /control_(\d+)_ColorID/;
    for (var i=0; i<form.elements.length; i++) {
        var result=pattern.exec(form.elements[i].name);
        if (result != null) {
            form.elements[i].options.length=0;
            var Arr=eval("reset_option_"+result[1]);
            form.elements[i].options[form.elements[i].options.length] = new Option(Arr[1],Arr[0],false,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 check_required (f){

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

    for (var i=0; i<f.length; i++) {
        var e=f.elements[i];
        
        if ((e.type=="text" || e.type=="textarea" || e.type=="password") && e.required) {
            if (is_blank(e.value)) {
               empties += "\n     " + e.label;
               if (chk) {
                   focuselement=e;
                   chk=false;
               }
               continue;
            }
        }
    }
    if (empties) {
        var msg = "You must fill in the following required fields:\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 set_range(low,lowobject,highobject){
   lowindex=lowobject.selectedIndex;
   highindex=highobject.selectedIndex;
   if(low == "low"){
      highobject.options[lowindex].selected=true;
   }else{
      if(lowobject.options[lowindex].text=='Lower:'){
         alert("Please choose a low value first.");
         highobject.options[0].selected=true;
         lowobject.options[0].selected=true;
         lowobject.focus();
      }else{
         if (lowindex>0) {
            if(lowobject.options[lowindex].value >= highobject.options[highindex].value){
               highobject.options[lowindex].selected=true;
            }
         }
      }
   }
}

