
var checkflag="false";

function addvalue(name, value){
    var input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", name);
    input.setAttribute("value", "" + value);
    document.form1.appendChild(input);
}

function getvalue(id){
    el = document.getElementById(id);
    if (typeof(el) != "undefined")
        return el.value;
}

function getRadioValue(items)
{
        var item = null;
        j=items.length;
	    for (var i=0; i<j; i++){
            item = items[i];
		    if(item.checked) return item.value;
        }
        return null;
}

function gobutton(xxx) {
    window.document.form1.request.value=xxx;
    window.document.form1.submit();
    return true;
}

function gorequest(req, parm){
    if (typeof(parm) != "undefined")
        document.form1.parameters.value = parm;

    gobutton(req);
}

function goprocess(procname, procargs, reqname, parmparms) {
    if(typeof(procargs)=="undefined")
        procargs = "";

    if(typeof(reqname)=="undefined")
        reqname = "";

    if(typeof(reqparms)=="undefined")
        reqparms = "";
   
    document.form1.process.value = procname;
    document.form1.processargs.value = procargs;

    if (reqname != "")
        document.form1.request.value = reqname;

    if (reqparms != "")
        document.form1.parameters.value = reqparms;

    submitForm(document.form1);
}

function submitForm(frm){
    if (frm.onsubmit == null){
        frm.submit();
    }
    else{
         if(frm.onsubmit()){
            frm.submit();
         }
    }
    return false;
}

function sortcol(req,sort)
{
    document.form1.request.value = req;
    document.form1.sortcol.value = sort;
    window.document.form1.submit();
    return true;
}

function sync() {
    var menufrm = false;
    try {
        menufrm = window.parent.frames['topmenu'].document.forms['frmMenu'];
    }
    catch(err) {
        menufrm = false;
    }
    if (menufrm) {
        document.setbanner.submit();
    }
}

function placeFocus() {
    if (document.forms.length > 0) {
        var field = document.form1;
        for (i = 0; i < field.length; i++) {
            if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
                document.form1.elements[i].focus();
                break;
            }
         }
     }
}

function selcust(yyy) {
    window.document.form1.customer.value=yyy;
    window.document.form1.request.value='chgcust';
    window.document.form1.submit();
    return true;
}

function selrouts(zzz) {
    window.document.form1.zipcod.value=zzz;
    window.document.form1.request.value='selrouts';
    window.document.form1.submit();
    return true;
}

function rtselbtn(zzz) {
    window.document.form1.detroute.value=zzz;
    window.document.form1.request.value='detroute';
    window.document.form1.submit();
    return true;
}

function rtsegbtn(zzz) {
    window.document.form1.detroute.value=zzz;
    window.document.form1.request.value=' ';
    window.document.form1.submit();
    return true;
}

function deletebtn(){
    if(confirm("Delete selected?")){
        document.form1.request.value='DELETE';
        document.form1.submit();
    }
}

function funseg(xxx,yyy,zzz) {
    window.document.form1.selord.value=xxx;
    window.document.form1.selseg.value=yyy;
    window.document.form1.request.value=zzz;
    window.document.form1.submit();
    return true;
}

function orddtl(job,cmd)
{
    window.document.form1.selord.value=job;
    window.document.form1.request.value=cmd;
    window.document.form1.submit();
}

function pickbook(){
    s = window.document.form1.session.value;
    window.location = "/BRLCGIP/c36access.pgm?session=" + s + "&company=036";
}

function filter(){

    if (isDate(document.form1.startdate.value) && isDate(document.form1.enddate.value))
    {
        document.form1.request.value='filter';
        document.form1.submit();
    }

}

function setdate(obj){
    var sdtm = obj.value;

    //only validate if date isn't empty
    if(Trim(sdtm) != ""){
        if (isDate(sdtm))
        {
            var sdtm = fixdate(sdtm);
            obj.value = sdtm;
        }
    }
    else{
        obj.value = "";
    }
}

function fixdate(dateStr){
    //arr = str.split("/");

    var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat);

    var m = matchArray[1];
    var d = matchArray[3];
    var y = matchArray[5];

    var strM = padBefore(m,2,"0");
    var strD = padBefore(d,2,"0");

    return strM + "/" + strD + "/" + y;

}

function openWin1(html) {
    MyWin=window.open(html,'','width=600,height=500,scrollbars=1,resizable,nostatus,nomenubar');
    MyWin.focus();
    //return;
}

function openTsWin(html) {
    MyWin=window.open(html+"?ts="+TS(),'','scrollbars,resizable,nostatus');
    MyWin.focus();
    //return true;
}

function checkCheckBox(xxx){
    if (document.form1.agree.checked == false ){
        alert('Please check "I accept" box to continue.');
        return false;
    }
    else{
        window.document.form1.request.value=xxx;
        window.document.form1.submit();
        return true;
    }
}

function TS() {
    RightNow = new Date();
    hours=RightNow.getHours();
    minutes=RightNow.getMinutes();
    seconds=RightNow.getSeconds();
    TimeStamp="ts"+hours+minutes+seconds;
    return TimeStamp;
}

function retimer() {
    tim = setTimeout("gobutton('REFRESH')",900000);
}

function check(field) {
    if (checkflag == "false") {
        for (i = 0; i < field.length; i++) {
            field[i].checked = true;
            highlightRow(field[i]);
        }
        checkflag = "true";
        return "Uncheck All";
    }
    else {
        for (i = 0; i < field.length; i++) {
            field[i].checked = false;
            highlightRow(field[i]);
        }
        checkflag = "false";
        return "Check All";
    }
}

function confirmAll(field){
    var el = null;
    for (i = 0; i < form1.elements.length; i++){
        el = form1.elements[i];
        if (el.name == "CONFIRMED")
            el.selectedIndex = 0;

    }
}

function getTimestamp(){
    var dtm=new Date();
    var y = dtm.getYear();
    var m = pad(dtm.getMonth()+1);
    var d = pad(dtm.getDate());
    var h = pad(dtm.getHours());
    var n = pad(dtm.getMinutes());
    var s = pad(dtm.getSeconds());

    var ts = y + "-" + m + "-" + d + "-" + h + "." + n + "." + s;

    return ts;
}

function showmenu(){
    if (document.form1.customer)
    {
        if(document.form1.customer.value != ""){
            document.getElementById("oemenu").style.display="block";
        }
        else{
            document.getElementById("oemenu").style.display="none";
        }
    }
    if(document.form1.selord){
        if (document.form1.selord.value != ""){
            if(document.form1.selseg)
            {
                dispSegMenu(document.form1.selord.value );
            }
            else if(document.form1.segno)
            {
                dispSegMenu(document.form1.selord.value );
            }
        }
        else{
            dispSegMenu("");
        }

    }
}


function dispSegMenu(segno)
{
    var prop = "";

    if (navigator.appName.toUpperCase().match(/MICROSOFT INTERNET EXPLORER/) == null)
        prop = "table-cell";
    else
        prop = "inline";

    if (segno != "")
    {
        if (document.getElementById("newZone"))
            document.getElementById("newZone").style.display=prop;
        if (document.getElementById("newZone_spacer"))
            document.getElementById("newZone_spacer").style.display=prop;
        document.getElementById("orderSummary").style.display=prop;
        document.getElementById("orderSummary_spacer").style.display=prop;
        document.getElementById("editRoutes").style.display=prop;
        document.getElementById("editRoutes_spacer").style.display=prop;
        document.getElementById("mapIt").style.display=prop;
        document.getElementById("mapIt_spacer").style.display=prop;
    }
}


/***************************************************************************************
*  Supporting Scripts-
****************************************************************************************
*   These are scripts that have been written or downloaded to add useful functions to
*   Javascript.  These can easily be copied and used in other programs.
***************************************************************************************/

function pad(str){
    str = str + ""; //<< convert to string trick
    if(str.length < 2){
        return "0" + str;
    }
    else{
        return str;
    }
}

function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
        return"";
    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if(TRIM_VALUE==""){
        return "";
    }
    else{
        return TRIM_VALUE;
    }
} //End Function

function RTrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0){
        return"";
    }


    var iTemp = v_length -1;

    while(iTemp > -1){
        if(VALUE.charAt(iTemp) == w_space){}
        else{
            strTemp = VALUE.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;

    } //End While
    return strTemp;

} //End Function

function LTrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";

    var iTemp = 0;

    while(iTemp < v_length){
        if(VALUE.charAt(iTemp) == w_space){}
        else{
            strTemp = VALUE.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
} //End Function

function isDate(dateStr){
    var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (dateStr != ""){
        if (matchArray == null) {
            alert("Please enter date as either mm/dd/yyyy.");
            return false;
        }

        month = matchArray[1]; // parse date into variables
        day = matchArray[3];
        year = matchArray[5];

        if (month < 1 || month > 12) { // check month range
            alert("Month must be between 1 and 12.");
            return false;
        }

        if (day < 1 || day > 31) {
            alert("Day must be between 1 and 31.");
            return false;
        }

        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
            alert("Month "+month+" doesn't have 31 days!")
            return false;
        }

        if (month == 2) { // check for february 29th
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day==29 && !isleap)) {
                alert("February " + year + " doesn't have " + day + " days!");
                return false;
            }
        }
    }

    return true; // date is valid
}