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 popup_to_box() {
    var win = window.open("/projectman/Mail/www_find_recipient/?popup=1","recipient","width=500,height=250,menubar=0,resizeable=0,scrollbars=0,toolbar=0,location=0");
}

function selelect_all_to() {
    var selidx = document.sendmailmsg.to.length;
    if (selidx) {
        for (var i=0; i < selidx; i++) {
            document.sendmailmsg.to.options[i].selected = true;    
        }
        if (document.sendmailmsg.subject.value.length == 0) {
            var subject = prompt("NO SUBJECT SPECIFIED:\n\n Would you like to Specify a subject? ");
            document.sendmailmsg.subject.value = subject;
            return selidx; 
        }
        return(selidx);
    } else {
        if (!(document.sendmailmsg.totext.value.indexOf("@",0))) {
            alert("You must first select a recipient");
        } else {
            if (document.sendmailmsg.subject.value.length == 0) {
                var subject = prompt("NO SUBJECT SPECIFIED:\n\n Would you like to Specify a subject? ");
                document.sendmailmsg.subject.value = subject;
            }
            return (1);
        }
    }

    


}
function delete_to () {
    var selidx = document.sendmailmsg.to.selectedIndex;
    var lenoptions= document.sendmailmsg.to.length;
    if (selidx) {
        document.sendmailmsg.to.options[selidx] = null;
    }
}
function attach_to(selectname) {
    var selidx = selectname.selectedIndex;
    var seltxt = selectname.options[selidx].text;
    var selval = selectname.name + "-" + selectname.options[selidx].value;



    var curidx = document.sendmailmsg.to.length;

    document.sendmailmsg.to.options[curidx] = new Option(seltxt,selval);

}

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) { // GK added (used in Unit Quick View)
    window.open(str,'popup',"height=400,width=450,resizable,toolbar,status,scrollbars,menubar");
    return false;
}
function showByPosition(object,x,y,e) { // GK added (used in Unit Quick View)
    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 + document.body.scrollLeft))
            x = (document.body.clientWidth + document.body.scrollLeft) - document.all[object].clientWidth;
        else
            x = e.x + x;

        if ((e.y + y + document.all[object].clientHeight) > (document.body.clientHeight + document.body.scrollTop))
            y = e.y - (y * 4);
        else
            y = e.y + y;

        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){
    var layer='l_'+ind;
    if (gImages[ind] != "") parent.image.document.modelimg.src=gImages[ind];
    else parent.image.document.modelimg.src="/images/Elevation_None.jpg";
    showByPosition(layer,0,25);
}

function clear_info(ind){
    parent.image.document.modelimg.src="/images/Elevation_None.jpg";
    var layer='l_'+ind;
    hide(layer);
}

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;
            }
        }else if((e.type=="select-one") && e.required){
            if (e.value=="0") {
               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 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;
}

