function getForm(formname){
    var tfrm = document.getElementById
        ? document.getElementById(formname)
        : document.forms[formname];
    if (!tfrm){
        for (i = 0; i < document.forms.length; i++){
            if (document.forms[i].name == formname){
                tfrm = document.forms[i];
                break;
            }
        }
    }
    return tfrm;
}

function getIt(aname, formNum){
    // TODO: Eliminate the very poor coding of having the formNum in here.
    var data = document.getElementById(aname);
    if (data == null){
        var fnum = 0;
        if (formNum != null){
            fnum = formNum;
        }
        data = document.forms[fnum][aname];
        if (data == null){
            eval('data = document.forms[' + fnum + '].' + aname + ';');
        }
    }
    return data;
}

function getByID(someID, theDocument){

    if (theDocument == null){
        theDocument = document;
    }

    var theItem = null;
    if(!theDocument.all && theDocument.getElementById){ // browser="NN6+ or IE5+ if you're willing to dump the !document.all stuff";
        theItem = theDocument.getElementById(someID);
    }
    else if(theDocument.layers){ // browser="NN4";
        theItem = theDocument.layers[someID];
    }
    else if(theDocument.all){ // browser="IE";
        eval("theItem = theDocument.all." + someID + ";");
    }
    if (theItem == null){
        alert('Could not find item ' + someID);
    }
    return theItem;
}

function getFromForm(aname, formname){

    if(aname == 'name'){
        alert('Use of the field name "name" in the form "formname" will cause browser issues.');
    }

    var frm = getForm(formname);

    if (frm == null){
        alert('Did not find ' + formname + ': ' + aname);
    }

    var item = null;
    if (frm.elements) {
        item = frm.elements[aname];
    } else {
        item = exec('frm.' + aname);
    }
    return item;
}

function toggleDiv(dname){
    d = getIt(dname);
    (d.style.display == "block")?
        d.style.display = "none":
        d.style.display = "block";
}

document.toggleDiv = toggleDiv;

function validateInt(iString) {
    // no leading 0s allowed
    return (("" + parseInt(iString)) == iString);
}

function showHelpWin(helplink, helpdiv, xshift, yshift){
    var parent = getIt(helplink, 0);
    var child = getIt(helpdiv, 0);
    var xs = 0;
    var ys = 0;
    if (xshift != null){
        xs = xshift;
    }
    if (yshift != null){
        ys = yshift;
    }
    parent.getRealLeft = getRealLeft;
    parent.getRealTop = getRealTop;
    pleft = parent.getRealLeft();
    ptop = parent.getRealTop();
    toggleDiv(helpdiv);
    child.style.left = (pleft + xs) + "px";
    child.style.top = (ptop + 10 + ys) + "px";
}

function moveDiv(obstacleDiv, dependentDiv){
    var obstacle = getIt(obstacleDiv);
    var dependent = getIt(dependentDiv);
    var height = 0;
    try{
        top = document.defaultView.getComputedStyle(obstacle,"").getPropertyValue("top");
        height = document.defaultView.getComputedStyle(obstacle,"").getPropertyValue("height");
    } catch (err) {
        //alert(obstacle.style.height);
    }
    //obstacle.getRealLeft = getRealLeft;
    obstacle.getRealTop = getRealTop;
    //pleft = obstacle.getRealLeft();
    ptop = obstacle.getRealTop();
    //dependent.style.left = pleft + "px";
    if (height != 0){
        dependent.style.top = (abs(parseInt(bottom)) + 25) + "px";
    }
}

function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit){
        field.value = field.value.substring(0, maxlimit);
    } else {
        countfield.value = maxlimit - field.value.length;
    }
}

function checkValidDate(day, month, year){
    var tDay = parseInt(day);
    var tMonth = parseInt(month);
    var tYear = parseInt(day);
    var dateCheck = true;
    if (tDay > 28){
        if ((tMonth == 2) ||
                (tMonth == 4) ||
                (tMonth == 6) ||
                (tMonth == 9) ||
                (tMonth == 11)){
            if (tMonth == 2){
                // TODO-L: Implement careful leapyear checks
                dateCheck = (tDay <= 29);
            } else {
                dateCheck = (tDay < 31);
            }
        }
    }
    return dateCheck;
}

function checkValidEmail(data){
    var ce = true;
    ce = ce && (data.indexOf('@') > 0);
    ce = ce && (data.indexOf('.') > 0);
    return ce;
}

var alphanum = 'abcdefghijklmnopqrstuvwxyz0123456789_-ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isAlphaNum(data, spacesOk, oc){
    //Check to see if values are alphanumeric or - or _, but with no spaces
    var checkAN = true;
    var allChars = alphanum;

    if (oc != null){
        allChars = alphanum + oc;
    }

    if (spacesOk == null){
        spacesOk = false;
    }
    if ((data == null) || (data == '')){
        checkAN = false;
    } else {
        var t = '';
        for (ii = 0; ii < data.length; ii++){
            t = data.charAt(ii);
            if (allChars.indexOf(t) < 0){
                if ((!spacesOk) || (t != ' ')){
                    checkAN = false;
                    break;
                }
            }
        }
    }
    return checkAN;
}

function isNumeric(elementID){
    var obj = getByID(elementID);
    var checkIN = false;
    try{
        var a = parseInt(obj.value);
        checkIN = !isNaN(a);
        if (checkIN){
			checkIN = (obj.value == (a + ''));
		}
    } catch (e) {
    }
    return checkIN;
}

// Convert a string to a safer string with special characters and spaces removed
function convertToSafeChars(data){
    var cdata1 = new String(data);
    var cdata2 = '';
    while (cdata1.indexOf(' ') > -1){
        cdata1 = cdata1.replace(' ', '-');
    }
    for (cci = 0; cci < cdata1.length; cci++){
            t = cdata1.charAt(cci);
            if (alphanum.indexOf(t) >= 0){
                cdata2 += t;
            }
    }
    return cdata2;
}

function validateContactForm(contactPrefix){
    var sel = getFromForm('subject', 'sendComment');
    var itm = sel.options[sel.selectedIndex];

    var vcCheck = true;

    var vcobj = getFromForm('fromName', 'sendComment');
    vcCheck = vcCheck && (vcobj.value != '');
    if (!vcCheck){
        alert('You must enter a name.');
        vcobj.focus();
    } else if (!checkValidEmail(getFromForm('fromEmail', 'sendComment').value)){
        vcCheck = false;
        alert('You must enter a valid email.');
        getFromForm('fromEmail', 'sendComment').focus();
    } else {
        vcobj = getFromForm('content', 'sendComment');
        vcCheck = vcCheck && (vcobj.value != '');
        if (!vcCheck){
            alert('You must enter a message.');
            vcobj.focus();
        }
    }

    if (vcCheck){
        itm.value = contactPrefix + itm.value;
    }

    return vcCheck;
}

//Function to remove a value from a select
function removeFromSelect(selName, index){
  var sel = getIt(selName);
  if (sel.length > index){
    sel.remove(index);
  }
}

//Firefox safe version... pass in the actual select
function removeFromSelect2(theSelect, index){
  if (theSelect.length > index){
    theSelect.remove(index);
  }
}

//Function to set a specific selected value in a select
function setSelected(selName, val, clearOthers){
    var sel = getIt(selName);
    for (i = 0; i < sel.length; i++){
        if (sel.options[i].value == val){
            sel.options[i].selected = true;
        } else if (clearOthers){
            sel.options[i].selected = false;
        }
    }
}

//Firefox safe version... pass in the actual select
function setSelected2(theSelect, val, clearOthers){
    if (theSelect != null){
        for (iSS = 0; iSS < theSelect.length; iSS++){

            if (theSelect.options[iSS].value == String(val)){
                theSelect.options[iSS].selected = true;
            } else if (clearOthers){
                theSelect.options[iSS].selected = false;
            }
        }
    }
}

//Function to add a value to a select
function addToSelect(selName, val, txt){
  var optNew = new Option();
  optNew.text = txt;
  optNew.value = val;
  var sel = getIt(selName);

  try {
    sel.add(optNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    sel.add(optNew); // IE only
  }
}

//Firefox safe version... pass in the actual select
function addToSelect2(theSelect, val, txt){
  var optNew = new Option();
  optNew.text = txt;
  optNew.value = val;

  try {
    theSelect.add(optNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    theSelect.add(optNew); // IE only
  }
}

function clearSelect(selName){
    var sel2 = getIt(selName);
    if ((sel2 != null) && (sel2.length > 0)){
        while(sel2.length > 0){
            sel2.remove(sel2.length - 1);
        }
    }
}

//Firefox safe version... pass in the actual select
function clearSelect2(theSelect){
    if ((theSelect != null) && (theSelect.length > 0)){
        while(theSelect.length > 0){
            theSelect.remove(theSelect.length - 1);
        }
    }
}

function inArray(item, list){
    var checkInArray = false;
    for (arI = 0; arI < list.length; arI++){
        if (item == list[arI]){
            checkInArray = true;
            break;
        }
    }
    return checkInArray;
}

// Checks to see if a text input has a value
function checkHasValue(elemName, formName, errorMessage){
    var txtIn = getFromForm(elemName, formName);
    var chkTxtVal = ((txtIn.value != null) && (txtIn.value != ''));
    if ((!chkTxtVal) && (errorMessage != null)){
        alert(errorMessage);
        // Thanks to http://mrmx.blogspot.com for the hint for this to fix FireFox
        setTimeout('getFromForm(\'' + elemName + '\',\'' + formName + '\').focus();',1);
        setTimeout('getFromForm(\'' + elemName + '\',\'' + formName + '\').select();',1);
    }
    return checkHasValue;
}

// Checks to see if a text input has an integer value
function checkHasInt(elemName, formName, errorMessage, forcePositive){
    var txtInt = getFromForm(elemName, formName);
    var chkIntVal = checkHasValue(elemName, formName);

    if (chkIntVal){
        var pi = parseInt(txtInt.value);
        chkIntVal = !isNaN(pi);
        if (chkIntVal && forcePositive){
            chkIntVal = (pi > 0);
        }
        if (chkIntVal) txtInt.value = Math.round(pi);
    }

    if ((!chkIntVal) && (errorMessage != null)){
        alert(errorMessage);
        // Thanks to http://mrmx.blogspot.com for the hint for this to fix FireFox
        setTimeout('getFromForm(\'' + elemName + '\',\'' + formName + '\').focus();',1);
        setTimeout('getFromForm(\'' + elemName + '\',\'' + formName + '\').select();',1);
        txtInt.value = '';
    }
    return chkIntVal;
}

// Function to popup a simple message window
function popupHelpBrowser(thePage, theMessage){
    var winName = 'popupMessageWindow';
    if ((thePage == null) || (thePage == '')){
        thePage = 'about:blank';
    }
    var myMessageWindow = open(thePage, winName,
        'menubar=no,toolbar=no,scrollbars=yes,resizable=no,left=50,top=120,width=490,height=300');
    if ((theMessage == null) || (theMessage == '')){
        var msgDoc = myMessageWindow.document;
        msgDoc.open();
        msgDoc.write(theMessage);
        msgDoc.close();
    }
    return false;
}

function writeMessageDiv(divID, message, textColor, bgColor){
    var dobj = getByID(divID);
    if (dobj != null){
        //if (textColor != null) dobj.style.color = textColor;
        //if (bgColor != null) dobj.style.backgroundColor = bgColor;
        dobj.innerHTML = message;
        dobj.className = 'flashMessageOn';
    }
}

function hideMessageDiv(divID){
    var dobj = getByID(divID);
    if (dobj != null){
        dobj.className = 'flashMessageOff';
    }
}