//
//  check char from str at pos for spase
//
function isSpace(str, pos) {
    curChar = str.charAt(pos);
    return ( curChar == ' ' || curChar == '\x09' || curChar == '\xA0' ||  // xA0 - 160 - code for &nbsp;
             curChar == '\x0D' || curChar == '\x0A' );
}

//
//  removes leading and trailing spaces/tabs/CRs/LFs from string strToTrim
//
function strTrim(strToTrim) {
    strToTrim = String(strToTrim);
    if (strToTrim == "")
        return "";
    iStart = 0;
    while ((isSpace(strToTrim, iStart)) && 
            iStart < strToTrim.length - 1)
        iStart++;
    iEnd = strToTrim.length - 1;
    while ((isSpace(strToTrim, iStart)) &&
            iEnd >= iStart)
        iEnd--;
    return strToTrim.substr(iStart, iEnd - iStart + 1);
}

//
//  checks if the given string is composed solely of letters and spaces
//
function isAlphabeticString (InString)  {
    if(InString.length==0)
        return (false);
    InString=InString.toLowerCase ()
    RefString="abcdefghijklmnopqrstuvwxyz ";
    for (Count=0; Count < InString.length; Count++)  {
        TempChar= InString.substring(Count, Count+1);
        if (RefString.indexOf(TempChar, 0)==-1) 
            return (false);
    }
    return (true);
}

//
//  popup pickLocation window to pick from tree
//
    function pick(tableName, formField, mode, leafId) {
       width = 400;
       height = 500;

       winLeft = (screen.availWidth - width) / 2;
       winTop  = (screen.availHeight - height) / 2;

       id = 0;
       if (mode == 'node')
         id = leafId;

       pickWnd = window.open('pickFromTree.asp?tableName=' + tableName + '&formField=' + formField +
            '&mode=' + mode + '&id=' + id, 'pickMyWnd',
            'width=' + width + ', height=' + height  + ', left = ' + winLeft +
                ', top = ' + winTop + ', resizable=yes, scrollbars=yes');

       pickWnd.focus();
    }

//
//  call pick function to pick Location from tree
//
    function pickLocation(formField, isNode) {
       pick("Location", formField, isNode);
    }

//
//  call pick function to pick Location from tree
//
    function pickProblemState(formField, isNode) {
       pick("ProblemState", formField, isNode);
    }

//
//  call pick function to pick Location from tree
//
    function pickProblemType(formField, isNode) {
       pick("ProblemType", formField, isNode);
    }

//
//  call pick function to pick Location from tree
//
    function pickProject(formField, isNode) {
       pick("Project", formField, isNode);
    }


//
//  check whether str is number
//
    function isNumber(field, errorMsg) {
        str = new String(field.value);

        if (str.length == 0) {
            alert(errorMsg);
            return false;
        }

        refString="1234567890";

        for (i = 0; i < str.length; i++)  {
            tempChar = str.substring(i, i + 1);
            if (refString.indexOf(tempChar) == -1) {
                alert(errorMsg);
                return false;
            }
        }
        return true;
    }

//
//  check whether str is number
//
    function isFloatNumber(field, errorMsg) {
        str = String(field.value);

        if (str.length == 0) {
            alert(errorMsg);
            return false;
        }

        refString="1234567890.";

        pointCount = 0;
        for (i = 0; i < str.length; i++)  {
            tempChar = str.substring(i, i + 1);
            if (refString.indexOf(tempChar) == -1) {
                alert(errorMsg);
                return false;
            } else
              if (tempChar == '.') {
                pointCount++;
                if (pointCount > 1) {
                  alert(errorMsg);
                  return false;
                }
              } 
            
        }
        return true;
    }


//
//  check whether field is empty and display errorMsg if so
//
    function isEmpty(field, errorMsg) {
        value = field.value;
        if (String(field.type).indexOf('select') != -1)
            if (value == "-1")
                value = "";

        if (strTrim(value) == "") {
            if ((errorMsg != '') && (errorMsg != null))
                alert(errorMsg);
            field.focus();
            return true;
        }
        else
            return false;
    }

//
//  check whether field is empty and display errorMsg if so
//
    function isEmail(field, errorMsg) {
        value = new String(field.value);
        if ((strTrim(value) == "") || (value.indexOf('@') == -1)
             || (value.indexOf('.') == -1) ) {
            if (errorMsg != '')
                alert(errorMsg);
            field.focus();
            return false;
        }
        else 
            return true;
    }



//
//  check for valid date (ex: 02.30.00 is invalid)
//
    function checkDate(year, month, day) {
        tmpDate = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
        if (tmpDate.getDate() != day) 
            return false;

        return true;
    }
//
//  check for valid date or leave empty
//
    function isValidDate(yearField, monthField, dayField, errorMsg) {
        year  = yearField.value;
        month = monthField.value;
        day   = dayField.value;

        // valid if all fields are blank
        if ((month == "") && (day == "") && (year == "")) 
            return true;

        // check for empty fields
        if ((month == "") || (day == "") || (year == "")) {
            alert(errorMsg + " or leave blank!")
            monthField.focus();
            return false;
        }

        if (!checkDate(year, month, day)) {
            alert(errorMsg + "!");
            monthField.focus();
            return false;
        }
        return true;

    }

//
//  process Action
//
function processAction(action, sbstnc, form, idField) {
/*
alert('action = ' + action);
alert('sbstnc = ' + sbstnc);
alert('form = ' + form);
*/
    action = String(action).toLowerCase();

    if (action == "")
        action = String(form.defaultAction.value).toLowerCase();

    form.Action.value = action;

    sbstnc = String(sbstnc);//.toLowerCase();

    if (action == "update") {

        return true;
    
    }
    else if (action == "delete") {

        if (idField.type == "hidden")
            Name = strTrim(form.Name.value);
        else
            Name = strTrim(idField[idField.selectedIndex].text);
            
        if (sbstnc == "User" && (pos = String(Name).indexOf(" ")) != -1) {
            if ((Name.substr(0, pos)).toUpperCase() == String('<% = getLogonUser() %>').toUpperCase()) {
                alert("Don't delete yourself, booby...");
                return false;       
            }
            Name = String(Name).substr(pos + 3); // " - " - 3 symbols
        }

        if (Name == '') {
            alert ("Please select a " + sbstnc + " Name to delete");
            idField.focus();
            return false;
        }
        if (confirm ("Do you really want to delete " + sbstnc + " '" + Name + "'?")) {
            form.Name.value = Name; // need to assign to this hidden element 
                                    // since it's then used in AdminUpdate.asp
            return true;
        }
        return false;
    }
}

//
//  open edit window
//
function edit(thisField, name) {
    if (name == "User")
        id = thisField.form.oldTrainID.value;
    else
        id = thisField.form.id.value;
    if ((strTrim(id) == '') || (strTrim(id) == '0')) {
        alert("Please select a " + name + " Name to edit");
        thisField.form.id.focus();
    } else
        document.location.href = 'edit.asp?table=' + name + '&mode=edit&id=' + id;
}

//
//  check password
//
function isPasswordOk(field, field1, errorMsg) {
    if (isEmpty(field, "Please specify Password!"))
        return false;

    if (field.value != field1.value) {
        alert(errorMsg);
        field.focus();
        return false;
   }
   return true;
}

function focusFirstField() {
    if (document.forms.length) {
        for (i = 0; i < document.forms[0].elements.length; i++) {
            fieldType = document.forms[0].elements[i].type;
            if ((fieldType != "hidden") && (fieldType != "button") && (fieldType != "submit")) {
                document.forms[0].elements[i].focus();
                break;
            }
        }
    }
}

function changeStatus(element) {
  element = (element).toLowerCase(element);
  switch (element) {
    case "mainmenu":    status = "Displays Main Menu"; 
                        break;
    case "newproblem":  status='Displays New Problem submission page';
                        break;
    case "problemlist": status='Displays Problem List';
                        break;
    case "admin":       status='Displays Administration Menu';
                        break;
    case "archive":     status='Displays problems that have been deleted';
                        break;
    case "logout":      status='Logs you out';
                        break;
    case "problem":     status='Displays detailed information on the problem';
                        break;
    case "history":     status='Displays history of the problem';
                        break;
    //case "problemtitle": 
    //                  status="Links to the Problem Description Page";
    //                  break;
  }
  return true;
}

function delAlert() {
    return confirm("Do you really want to delete the record?");
}

function setDelMode(field) {
    if (delAlert()) {
        form = field.form;
        form.editMode.value = "delete";
        form.submit();
    }
}

function setMode(field, modeName) {
    form = field.form;
    form.editMode.value = modeName;
}

function runMode(field, modeName) {
    setMode(field, modeName);
    form.submit();
}



