// Copyright (c) 2002 Openwave Systems Inc. All rights reserved.
// 
// The copyright to the computer software herein is the property of
// Openwave Systems Inc. The software may be used and/or copied only
// with the written permission of Openwave Systems Inc. or in accordance
// with the terms and conditions stipulated in the agreement/contract
// under which the software has been supplied.
// 
// $Id: common_js.jsp,v 1.4 2004/06/28 23:08:09 matthew Exp $






// Temporary functions for disabled client side validation


function validateLoginForm(form) { return true; }

function validateMailSettingsBlockingForm(form) { return true; }

function validateComposeForm(form) { return true; }

function validateDestinationForm(form) { return true; }

function validateMailSettingsListExternalForm(form) { return true; }

function validateFamilyMboxForm(form) { return true; }

function validateFilterForm(form) { return true; }

function validateFilterListForm(form) { return true; }

function validateAddEditFolderForm(form) { return true; }

function validateMailSettingsForwardingForm(form) { return true; }

function validateMailSettingsJunkMailForm(form) { return true; }

function validateMailSettingsAliasForm(form) { return true; }

function validateMailSettingsExternalForm(form) { return true; }

function validateMailSettingsPreferencesForm(form) { return true; }

function validateMessageSearchForm(form) { return true; }

function validateMailSettingsSignatureForm(form) { return true; }

function validateMailSettingsVacationForm(form) { return true; }

function validateNotificationForm(form) { return true; }

function validateMailSettingsReturnReceiptForm(form) { return true; }

function validateGeneralSettingsGreetingsForm(form) { return true; }

function validateGeneralSettingsPasswordForm(form) { return true; }

function validateGeneralSettingsPinForm(form) { return true; }

function validateGeneralSettingsTimeZoneForm(form) { return true; }

function validateMailSettingsSendersControlForm(form) { return true; }

function validateNotificationForm(form) { return true; }

function validateDestinationForm(form) { return true; }



// Displays a hint in the status line.
function hint(s) {
    window.status = (s ? s : '');
    return true;
}

// Move message(s) to folder
// page: specifies if the move operation is started from messagelist page ("list")
//       or from the message page ("msg")
function doMoveTop(page) {
    for (var i=0; i < document.topbar.top_folderid.length; i++) {
        if (document.topbar.top_folderid[i].selected) {
            document.TheForm.destfid.value = 
                document.topbar.top_folderid[i].value;
            i = document.topbar.top_folderid.length;
        }
    }

    if (document.TheForm.destfid.value.length == 0) {
        return;
    }
    document.TheForm.inbox_create_folder.value = 0;
    var newFolder = '[New Folder]';
    if(document.TheForm.destfid.value == newFolder){
        document.TheForm.inbox_create_folder.value = 1;
    }
    if (page == "list") {
        if (isMessageSelected()) {
            document.TheForm.action = '/do/mail/message/move?update=true&ref=list&l=en-US&v=att';
            document.TheForm.submit();
        }
    } else {
        document.TheForm.action = '/do/mail/message/move?update=true&ref=msg&l=en-US&v=att';
        document.TheForm.submit();
    }
}

function doDelete(page) {
    if (page == "list") {
        if (isMessageSelected()) {
            document.TheForm.action = '/do/mail/message/delete?update=true&ref=list&l=en-US&v=att';
            document.TheForm.submit();
        }
    } else {
        document.TheForm.action = '/do/mail/message/delete?update=true&ref=msg&l=en-US&v=att';
        document.TheForm.submit();
    }
}

function selectAllCheckboxes(form, selection, checkboxname) {
    var length = document.forms[form].elements.length;
    var i = 0;
    if (document.forms[form].elements[selection].checked == false) {
        document.forms[form].elements[selection].checked = false;
        for (i=0; i < length; i++) {
            if (document.forms[form].elements[i].name == checkboxname) {
                document.forms[form].elements[i].checked = false;
            }
        }
    } else {
        document.forms[form].elements[selection].checked = true;
        for (i=0; i < length; i++) {
            if (document.forms[form].elements[i].name == checkboxname) {
                document.forms[form].elements[i].checked = true;
            }
        }
    }
}

function capitalize(s) {
    initialCap = "";
    rest = "";

    if (s.length > 0) {
        initialCap = s.substr(0, 1).toUpperCase();
    }
    if (s.length > 1) {
        rest = s.substr(1);
    }
    
    return initialCap + rest;
}

// TODO: Remove below functions
function validateData(formObject, validationCommand, errorString) { 
    var equalPosition = validationCommand.search("="); 
    var command  = ""; 
    var commandValue = ""; 
    if(equalPosition >= 0) { 
     command  = validationCommand.substring(0,equalPosition); 
     commandValue = validationCommand.substr(equalPosition+1); 
    } else { 
     command = validationCommand; 
    } 

    switch(command) { 
        case "required": {       
            if (formObject.value.length == 0) { 
                alert(errorString); 
                return false; 
            }
            break;             
        }
        case "greaterthan": { 
            if(formObject.value.length > commandValue) {
                alert(errorString); 
                return false;                 
            }     
            break; 
        }
        case "greaterthanvalue": { 
            if(parseInt(formObject.value) > commandValue) {
                alert(errorString); 
                return false;                 
            }     
            break; 
        }
        case "lessthanvalue": { 
            if(parseInt(formObject.value) < commandValue) {
                alert(errorString); 
                return false;                 
            }     
            break; 
        }
    }
}


// This is a generic function that checks specified fields in a form (using 
// parameters from validationRules defined in each page's javascript.jsp), and
// either displays an error message or submits the form if there are no errors.
// validationRules provides the name of the field, the type of checking to be 
// done or a function name, and the error string.
function verifyAndSubmit(form, validationRules) { 
    for(var object=0; object < validationRules.length; object++) { 
        for (var rule=0; rule < validationRules[object].length; rule++) {
            var valFunction = validationRules[object][rule][1];
            if (typeof valFunction == "function") {
                if (valFunction(form) == false) {
                    return;
                }
            } else {
                if (validateData(document.forms[form].elements[validationRules[object][rule][0]], 
                    validationRules[object][rule][1], 
                    validationRules[object][rule][2]) == false) { 
                    document.forms[form].elements[validationRules[object][rule][0]].focus();
                    return; 
                }
            }
        }
    }
    document.forms[form].submit();
} 

function SwitchFolder(update, fid) {
   document.folderForm.action='/do/mail/folder/switch?l=en-US&v=att';
   document.folderForm.fid.value=fid;
   document.folderForm.update.value=update
   document.folderForm.submit();
}

function openpopup(url, name) {
  mywindow=window.open(url,name,'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=600,height=420,screenX=125,screenY=30,left=125,top=30');
  mywindow.location.href = url;
  if (mywindow.opener == null) mywindow.opener = self;
}
