// 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.1.1.2 2006/05/23 06:11:47 c920621 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; }



// write out copy for the buttons. needed because we have to have 
// separate div id's to get both top and bottom bars to work independently
function writeButtonCopy(location, navigationText)
{
    location = capitalize(location);
    var sHtml = "";
    var bBottom = location == "Bottom" ? true : false;
    sHtml += "<table border=\"0\"><tr><td>";
    sHtml += "<input type=\"image\" class=\"button\" id=\"mail.list.compose.button." + location + "\" src=\"/webedge/images/bigpond/bt_compose.gif\"";
    sHtml += " value=\"" + composeButtonValue + "\" width=\"72\" height=\"19\"";
    sHtml += " onclick=\"document.location.href='" + composeLocation + "'\"";
    sHtml += " onmouseover=\"return hint('" + composeHint + "')\"";
    sHtml += " onmouseout=\"return hint()\" title=\"" + composeHint + "\"";
    sHtml += " style=\"border:none\"></td>";
    sHtml += "<td>";
    sHtml += "<input type=\"image\" class=\"contentbutton\" id=\"mail.list.delete.button." + location + "\" src=\"/webedge/images/bigpond/bt_delete.gif\"";
    sHtml += " value=\"" + deleteButtonValue + "\" width=\"71\" height=\"19\"";
    sHtml += " onclick=\"doDelete('list')\"";
    sHtml += " onmouseover=\"return hint('" + deleteHint + "')\"";
    sHtml += " onmouseout=\"return hint()\" title=\"" + deleteHint + "\"";
    sHtml += " style=\"border:none\"></td>";
    sHtml += "<td><a href=\"#\" onmouseover=\"showDropdown('markMail" + location + "', " + bBottom + ")\" onmouseout=\"hideDropdown('markMail" + location + "')\" class=\"ddButton\"><img name=\"img_markMail" + location + "\" id=\"img_markMail" + location + "\" src=\"/webedge/images/bigpond/bt_mark_mail.gif\" alt=\"Mark Mail\" width=\"125\" height=\"19\" border=\"0\"></a></td>";
    sHtml += "<td><a href=\"#\" onmouseover=\"showDropdown('moveToFolder" + location + "', " + bBottom + ")\" onmouseout=\"hideDropdown('moveToFolder" + location + "')\" class=\"ddButton\"><img name=\"img_moveToFolder" + location + "\" id=\"img_moveToFolder" + location + "\" src=\"/webedge/images/bigpond/bt_move_folder.gif\" alt=\"Move to Folder\" width=\"125\" height=\"19\" border=\"0\"></a></td>";

    <!-- Forward As Spam -->
    

    sHtml += "</tr>";
    sHtml += "</table>";
	sHtml += "<div id=\"markMail" + location + "\" class=\"dropDown" + location + "\" onmouseover=\"dropdownMouseOver(this)\" onmouseout=\"dropdownMouseOut(this)\">";
    sHtml += "	<a href=\"#\" onclick=\"doMarkRead()\" onmouseover=\"return hint('" + readHint + "')\" onmouseout=\"return hint()\">Read</a>";
    sHtml += "	<a href=\"#\" onclick=\"doMarkUnread()\" onmouseover=\"return hint('" + unreadHint + "')\" onmouseout=\"return hint()\">Unread</a>";
    sHtml += "</div>";
    sHtml += "<div id=\"moveToFolder" + location + "\" class=\"dropDown" + location + "\" onmouseover=\"dropdownMouseOver(this)\" onmouseout=\"dropdownMouseOut(this)\">";
    for(var i = 0; i < aFolders.length; i++)
    {
        sHtml += "<a href=\"#\" onmouseover=\"return hint('move to " + escape(aFolders[i][1]) + "')\" onmouseout=\"return hint()\" onclick=\"document.TheForm.destfid.value='" + escape(aFolders[i][0]) + "';doMoveTop('list')\">" + aFolders[i][1] + "</a>";
    }
    sHtml += "</div>";
    sElementName = "buttons_" + location;
	document.getElementById(sElementName).innerHTML = sHtml; 
	}


function writeButtonMessage(location)
{
    location = capitalize(location);
    var sHtml = "";
    var bBottom = location == "Bottom" ? true : false;
    sHtml += "<a style=\"padding-left: 20px;\"href=\"#\" onmouseover=\"showDropdown('moveToFolder" + location + "', " + bBottom + ")\" onmouseout=\"hideDropdown('moveToFolder" + location + "')\" class=\"ddButton\"><img name=\"img_moveToFolder" + location + "\" id=\"img_moveToFolder" + location + "\" src=\"/webedge/images/bigpond/bt_move_folder.gif\" alt=\"Move to Folder\" width=\"125\" height=\"19\" border=\"0\"></a>";
    sHtml += "<div id=\"moveToFolder" + location + "\" class=\"dropDown" + location + "\" onmouseover=\"dropdownMouseOver(this)\" onmouseout=\"dropdownMouseOut(this)\">";
    for(var i = 0; i < aFolders.length; i++)
    {
        sHtml += "<a href=\"#\" onmouseover=\"return hint('move to " + escape(aFolders[i][1]) + "')\" onmouseout=\"return hint()\" onclick=\"document.TheForm.destfid.value='" + escape(aFolders[i][0]) + "';doMoveTop('msg')\">" + aFolders[i][1] + "</a>";
    }
    sHtml += "</div>";
    sElementName = "buttons_" + location;
    
    document.getElementById(sElementName).innerHTML = sHtml;
}


// 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;
        }
    }*/

		document.TheForm.destfid.value=unescape(document.TheForm.destfid.value);
	
    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 = '/webedge/do/mail/message/move;jsessionid=a4ziWZH0Ojtb?update=true&ref=list&l=en-AU&v=bigpond';
            document.TheForm.submit();
        }
    } else {
        document.TheForm.action = '/webedge/do/mail/message/move;jsessionid=a4ziWZH0Ojtb?update=true&ref=msg&l=en-AU&v=bigpond';
        document.TheForm.submit();
    }
}

function confirmDelete(page) {
    if (page == "list") {
        if (isMessageSelected()) {
            if (confirm('Are you sure you want to delete the selected messages?') ) {
                doDelete(page);
            }
        }
    } else {
        if (confirm('Are you sure you want to delete this message?') ) {
            doDelete(page); 
        }
    }
}

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

function confirmForwardAsSpam(page) {
    if (page == "list") {
        if (isMessageSelected()) {
            
            if (confirm('Do you want to report this email as spam?') ) {
                doForwardAsSpam(page);
            }
            
        }
    } else {
        
        if (confirm('Do you want to report this email as spam?') ) {
            doForwardAsSpam(page);
        }
        
    }
}

function doForwardAsSpam(page) {
    if (page == "list") {
        if (isMessageSelected()) {
            document.TheForm.action = '/webedge/do/mail/message/forwardAsSpam;jsessionid=a4ziWZH0Ojtb?update=true&ref=list&l=en-AU&v=bigpond';
            document.TheForm.submit();
        }
    } else {
        document.TheForm.action = '/webedge/do/mail/message/forwardAsSpam;jsessionid=a4ziWZH0Ojtb?update=true&ref=msg&l=en-AU&v=bigpond';
        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 selectAllEnabledCheckboxes(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) {
                if (document.forms[form].elements[i].disabled == false) {
                    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) {
                if (document.forms[form].elements[i].disabled == false) {
                    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();
} 


    var _newwin;
    function openFAQwin()
    {
		
		var _sw = 800;
		var _sh = 600;
		if(screen)
		{
			var _sh = screen.height;
			var _sw = screen.width;
		}
        var width = _sw * .8;
        var height = _sh * .75;
        var top = 50;
        var left = _sw - width - 50;
        _newwin = window.open("/webedge/do/help/userIndex;jsessionid=a4ziWZH0Ojtb?l=en-AU&v=bigpond","HELP","height=" + height + ",width=" + width + ",scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no,top=" + top + ",left=" + left + ",screenX=" + left + ",screenY=" + top)
        if(_newwin)
            if(_newwin.focus)
                _newwin.focus();
        if(_newwin != null)
        {
            setTimeout("if(!_newwin.closed)_newwin.focus()", 5000)
        }
    }

// Drop downs JS


function getPosition(obj, mode, bIsBottom) {
	var buttonHeight = obj.height;
	iPos=0; macValue=0;
	iPos = (mode) ? parseInt(obj.offsetLeft) : parseInt(obj.offsetTop);
	while(obj.offsetParent != null)
	{
		iPos += (mode) ? parseInt(obj.offsetParent.offsetLeft) : parseInt(obj.offsetParent.offsetTop);
		obj = obj.offsetParent;
	}

	if(navigator.userAgent.indexOf("Mac") > -1 && document.all) {
		if (mode) {
			macValue = 7;
		} else {
			var messages = document.getElementById("messagesDiv");
			if (messages != null) {
				if(bIsBottom) {
					macValue = parseInt(messages.height) + buttonHeight - 5;
				} else {
					macValue = parseInt(messages.height) + 13;
				}
			} else {
				macValue = 0;
			}
		}
	}
	return iPos - macValue;
}

var oDropdownVis = {};
var sCurrentID;
var nCurrentFocusedIndex;

function showDropdown(sID, bIsBottom){
	//alert("showDropdown(" + sID + ")");
    var oDropDown = document.getElementById(sID);
    //var sImageSuffix = (bIsBottom) ? 'Bott' : '';
    var oImage = document.images['img_' + sID];
		oDropdownVis[sID] = true;
	var dropDownBottom = 0;
	if (bIsBottom)
	{
		dropDownBottom = getPosition(oImage, 0, bIsBottom) - oDropDown.offsetHeight + 1
		if (dropDownBottom < 0)
			dropDownBottom = 0;
	}
	oDropDown.style.top = (bIsBottom) ? dropDownBottom : getPosition(oImage, 0, bIsBottom) + oImage.height-2 ;
	oDropDown.style.left = getPosition(oImage, 1, bIsBottom);
	oDropDown.className = (bIsBottom) ? 'dropDownBottom' : 'dropDownTop';
    oDropDown.style.visibility = 'visible';
    document.onkeydown = captureKeyPress;
    sCurrentID = sID;
    nCurrentFocusedIndex = null;
}


function hideDropdown(sID){
//alert("hideDropdown(" + sID + ")");
	oDropdownVis[sID] = false;
	setTimeout('hideAllDropdowns()', 100);
}

function hideAllDropdowns()
{
//alert('hideAllDropdowns()')
	for(var i in oDropdownVis){
		if(!oDropdownVis[i]){
			var oDropDown = document.getElementById(i);
			oDropDown.style.visibility = 'hidden';
			if(sCurrentID == i){
				sCurrentID = null;
			}
		}
	}
}

function dropdownMouseOver(oID)
{
    //alert("dropdownMouseOver(" + oID.id + ")");
    var sID = oID.id;
    oDropdownVis[sID] = true;
}

function dropdownMouseOut(oID)
{
    //alert("dropdownMouseOut(" + oID.id + ")");
    var sID = oID.id;
    oDropdownVis[sID] = false;
    hideDropdown(sID);
}


function setDropdownVisAfterBlur()
{
//alert("setDropdownVisAfterBlur()");
    oDropdownVis[sCurrentID] = true;
}

function doSwitchFolderBigpond(update, fid, isCancel) {
   var theAction = isCancel == "true" ? "/webedge/do/mail/closeSearch/folder/view;jsessionid=a4ziWZH0Ojtb?l=en-AU&v=bigpond" : "/webedge/do/mail/folder/view;jsessionid=a4ziWZH0Ojtb?l=en-AU&v=bigpond";
   document.folderForm.action=theAction;
   document.folderForm.fid.value=fid;
   document.folderForm.update.value=update
   document.folderForm.submit();
}


function captureKeyPress(){
	var e = (e == null) ? event : arguments[0];
	if(sCurrentID != null && (e.keyCode == 38 || e.keyCode == 40)){
		var oDropDown = document.getElementById(sCurrentID);
		if(oDropdownVis[sCurrentID]){
			nCurrentFocusedIndex = (nCurrentFocusedIndex == null || nCurrentFocusedIndex >= oDropDown.childNodes.length - 2) ? 0 : nCurrentFocusedIndex + 2;
			oDropDown.childNodes[nCurrentFocusedIndex].focus();
			setTimeout(setDropdownVisAfterBlur, 50);
		}
	}
}

