function clickclear(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
    }
}

function clickrecall(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
    }
}

// clear all defaults when form is submitted
function ClearAllOnSubmit(thisForm, employeeName) {
    if(thisForm['wname'].value == employeeName) {
        thisForm['wname'].value = '';
    }
}

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=300,left = 300,top = 200');");
}

function isNumeric(formElement) {
    var isNotInt = true;
    if (parseInt(formElement.value).toString() == formElement.value)
        isNotInt = false;
    else
        isNotInt = true;

    if(isNotInt) {
        var theErrMSG = 'The input is not a number.';
        alert(theErrMSG);
        formElement.focus();
        formElement.select();
        return false;
    }
    return true;
}
   
function initField(elem, value) {
    if(elem.value == value) {
        elem.value = '';
    }
}

function dorequest(req) {
    dorequest2(req, 'login');
}

function dorequest2(req, formname) {
    var frm = document.forms[formname];
    var user = "";
    var url = "/";
    if(frm != null) {
        user = frm.elements['username'];        
        url = frm.elements['url'].value;
    }
    var username = "";
    if(user) { 
        username = user.value;
    }
    if(url == null) {
        url = "/";
    }
    location.href = "/login" + 
        '?login=' + req + 
        '&username=' + username +
        '&url=' + url;
}

function checkAll(theElement) {
    var theForm = theElement.form, z = 0;
    for(z=0; z<theForm.length;z++){
        if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall' && theForm[z].name != 'quoteId'){
            theForm[z].checked = theElement.checked;
        }
    }
}

function checkQuote(theButton) {
    var theForm = theButton.form, z = 0;
    var quoteallowed = false;
    for(z=0; z<theForm.length;z++){
        if(theForm[z].type == 'checkbox'){
            if (theForm[z].checked == true) {
                quoteallowed = true;
            }
        }
    }
    if (quoteallowed == false) {
        alert('Please select a quote!');
    }
    return quoteallowed;
}

function EnableButtons() {
    var bt = returnObjectById('getquote');
    if (bt != null) bt.disabled = false;
    bt = returnObjectById('remove');
    if (bt != null) bt.disabled = false;
    bt = returnObjectById('getOldQuote');
    if (bt != null) bt.disabled = false;
    bt = returnObjectById('savechanges');
    if (bt != null) bt.disabled = false;
}

function checkElementById(myid) {
    var elem = returnObjectById(myid);
    elem.checked = true;
}

function returnObjectById(id)
{
    var returnVar;
    if (document.getElementById)
        returnVar = document.getElementById(id);
    else if (document.all)
        returnVar = document.all[id];
    else if (document.layers)
        returnVar = document.layers[id];
    return returnVar;
}

function chk_kb_srch(frm, fld1, fld2, msg) {
    var box = frm.elements[fld1].value;
    box = box.replace(/^\s+/, '');
    box = box.replace(/\s+$/, '');
    if(box.length == 0) {
        box = frm.elements[fld2].value;
        box = box.replace(/^\s+/, '');
        box = box.replace(/\s+$/, '');
        if (box.length() == 0) {
            alert(msg);
            frm.elements[fld1].focus();
            return false;
        }
    } else {
        frm.elements[fld1].value = box;
    }
    return true;
}

function chk_srch(frm, fld, msg) {
    var box = frm.elements[fld].value;
    box = box.replace(/^\s+/, '');
    box = box.replace(/\s+$/, '');
    if(box.length == 0) {
        alert(msg);
        frm.elements[fld].focus();
        return false;
    }
    frm.elements[fld].value = box;
    return true;
}

function addErrorMessage(details) {
    var errordetails = document.getElementById('errormessage');
    errordetails.innerHTML = details;
}

// used in the footer
function printpage() {
    window.print();  
}

// used in the featured links portlets
function redirectMe(me) {
    window.location = me.options[me.selectedIndex].value;
}

$(document).ready(function() {
    if(pageTracker != null) {
        $("a[href$=.pdf],a[href$=.zip]").click(function(a) {
            pageTracker._trackPageview($(this).attr("href"));
        });
    }
});
