var uniqueId = 0;

function NewWindow(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2 +30 ;
    var wint = (screen.height - h) / 2 +30 ;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
    win = window.open(mypage, myname, winprops);
    win.focus();
    //if(parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function ReportWindow(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2 +30 ;
    var wint = (screen.height - h) / 2 +30 ;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar=yes,menubar=yes,resizable';
    win = window.open(mypage, myname + uniqueId, winprops);
    uniqueId++;
    win.focus();
    //if(parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function UrlNewWindow(mypage, myname, w, h) {
    var winl = (screen.width - w) / 2 +30 ;
    var wint = (screen.height - h) / 2 +30 ;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,location=yes,toolbar=yes,menubar=yes,resizable';
    win = window.open(mypage, myname + uniqueId, winprops);
    uniqueId++;
    win.focus();
}

function FileNewWindow(mypage, myname, w, h, scroll) {
    ReportWindow(mypage, myname, w, h, scroll);
}

function handleCheckAll(checkedInput, checkName) {
    if(checkedInput.checked == true) {
        checkAll(checkName);
    } else {
        uncheckAll(checkName);
    }
}

function handleSingleCheckBox(checkedInput, checkName) {
    if(checkedInput.checked == true) {
        checkName.checked = true;
    } else {
        checkName.checked = false;
    }
}

function isNumeric(num, dec) {

    //Set This Variable//
    //This represents the MAX number of 
    //decimal places that will be allowed...
    var numDec = dec;
    var theNum = num.value;

    //Don't Change These Variables//
    var theLen = theNum.length;
    var dotPos = theNum.indexOf(".");
    var thePos = theLen - (dotPos+1);

    //Conditionals//
    var isNum = (isNaN(theNum)) ? false:true;
    var isAt = (thePos <= numDec && thePos != 0) ? true:false;
    var isOne = (theNum.indexOf(".",dotPos+1) == -1) ? true:false;

    //Run The Checks//
    if((isNum) && (isAt) && (isOne)) {
        return true;
    }
    else {
        alert("Must be a valid number.");
        num.value = theNum;
        num.focus();
        return false;
    }
}

function checkAll(field)
{
    for (i = 0; i < field.length; i++)
        field[i].checked = true;
}

function uncheckAll(field)
{
    for (i = 0; i < field.length; i++)
        field[i].checked = false;
}

function checkField(formRef) {
    var val = formRef.value;
    while(''+val.charAt(val.length-1)==' ') val=val.substring(0,val.length-1);
    formRef.value = val;
    if(val == '') return false;
    else return true;
}

function alertBlankField(formRef,fieldName) {
    alert(fieldName);
    formRef.value='';
    formRef.focus();
    return false;
}

function validSpellCheck(formRef, inputValue, inputName) {
    formRef.validSpell.value = "yes";
    formRef.vSpell.value = inputValue;
    formRef.types.value = inputName;
	if (document['spellCheckIcon']) {
            document['spellCheckIcon'].src = '../images/icon_spellcheck.gif';
        }
}

function invalidSpellCheck(formRef) {
    formRef.validSpell.value = "no";
    formRef.vSpell.value = "";
    formRef.types.value = "";
		if (document['spellCheckIcon']) {
            document['spellCheckIcon'].src = '../images/icon_spellcheck_gray.gif';
        }
}

function onSpell(formRef, formName) {
    if(formRef.validSpell.value=="yes") {
        if(!(formRef.vSpell.value =="")) {
            newWinUrl = "spellCheck.jsp?name=" + formName;
            NewWindow(newWinUrl,"SpellChecker","550","300","no");
        }
        else alert("The selected field has no data to spell check.");
    }
}

function onSort(formRef,field) {
    if(formRef.fieldName.value==field) {
        if(formRef.ascDesc.value=="desc") formRef.ascDesc.value="asc";
        else formRef.ascDesc.value="desc";
    }
    formRef.fieldName.value=field;
    formRef.submit();
}

function onSelectAll(formRef) {
    for (var i=0;i < formRef.length ; i++) {
        var e = formRef.elements[i];
        if(e.type == "checkbox") {
            if(formRef.relateAll.checked == true) e.checked = true;
            else e.checked = false;
        }
    }
}

function onFileURLChange(disableField, enableField, val, ext) {
    if(val == '0') disableField.value='';
    else enableField.value='';
    if(ext) {
        if(val == '0') ext.disabled=false;
        else ext.disabled=true;
    }
    disableField.disabled=true;
    swapFormReadOnly(disableField.name);
    enableField.disabled=false;
    swapFormOff(enableField.name);
    enableField.focus();
}

function addHTTP(formRef) {
    if(checkField(formRef)) {
        if(formRef.value.substring(0,4) != "http") 
        formRef.value="http://"+formRef.value;
        else formRef.value=formRef.value;
    }
}

// To check the entered value does not contain any decimals.
function checkNumeric(field) {
    checkField(field);
    if(field.value=='') return true;
    var s = field.value;
    var bag = "0123456789";
    var i;
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}

// This function checks for the length of text entered in 
// for a field with the maximum length allowed for the field.
// Parameters: 
//  i)  document.<formName>.<fieldName>
//  ii) actual fieldName (ex: Mission Statement, Vision Statement)
//  iii)maxLength allowed
function checkTALength(ta_field, ta_fieldName, maxLength){
    var ta_fieldLength = 0;
    var val = ta_field.value;

    if(checkField(ta_field)) {
        for(var i=0; i < val.length; i++) {
            if(val.substring(i, i + 1) == '\n' && navigator.appName.indexOf("Netscape") != -1) ta_fieldLength++;
            ta_fieldLength++;
        }

        if(ta_fieldLength > maxLength) {
            var msg = "The text that you have entered for the " + ta_fieldName + " is too long.\n" +
                    "The maximum length for the " + ta_fieldName + " is " + maxLength + " characters.\n" +
                    "You have entered " + ta_fieldLength + "characters. Please reduce the amount of text that you are entering.";
            alert(msg);
            ta_field.focus();
            return false;
        }
        else {
            return true;
        }
    }
    return true;
}

// This function checks for the dates entered.
// The startDate should be always less tha the endDate.
// Parameters: 
//  i)  document.<formName>.<startDate>
//  ii)  document.<formName>.<endDate>
function compareDates(ta_startDate, ta_endDate){
    if(checkField(ta_startDate) && checkField(ta_endDate)) {
        var x = new Date(ta_startDate.value);
        var y = new Date(ta_endDate.value);
        var z = parseInt((y.valueOf() - x.valueOf())/(24*60*60*1000));
        if(z < 0) {
            alert("End Date should be after Start Date.");
            ta_endDate.focus();
            return false;
        }
    }
    return true;
}

// This function checks for the selection in multiple select box.
// Parameters: 
//  i)  document.<formName>.<select-multiple name>
function multiSelected(obj){
    var counter = 0;
    for(var i=0;i<obj.options.length;i++){
        if(obj.options[i].selected) counter++
    }
    if(counter>0) return true;
    else return false;
}