/**
 * This file provides JavaScript utility functions used in the Sony-Ericcson
 * OTA Site.
 *
 * @author Rubens Gomes
 * @version $Id: OTAUtils.js 11185 2008-06-26 20:06:26Z rgomes $
 */

function OTAUtils() {
    return this;
}

OTAUtils.prototype.showBlockElement = function(elementId) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((elementId == undefined) || (elementId == null)
            || (typeof (elementId) != "string")
            || (window.document.getElementById(elementId) == null)) {
        throw ("DOM element for ID [" + elementId + "] not found.");
    }

    window.document.getElementById(elementId).style.display = 'block';
    window.document.getElementById(elementId).style.visibility = 'visible';
    return false;
}

OTAUtils.prototype.toggleBlockElement = function(elementId) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((elementId == undefined) || (elementId == null)
            || (typeof (elementId) != "string")
            || (window.document.getElementById(elementId) == null)) {
        throw ("DOM element for ID [" + elementId + "] not found.");
    }

    if (window.document.getElementById(elementId).style.display == 'none') {
        window.document.getElementById(elementId).style.display = 'block';
        window.document.getElementById(elementId).style.visibility = 'visible';
    } else {
        window.document.getElementById(elementId).style.display = 'none';
        window.document.getElementById(elementId).style.visibility = 'hidden';
    }
    return false;
}

OTAUtils.prototype.toggleBlockElementAndHideOthers = function(elementIdToToggle, elementsToHide) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((elementIdToToggle == undefined) || (elementIdToToggle == null)
            || (typeof (elementIdToToggle) != "string")
            || (window.document.getElementById(elementIdToToggle) == null)) {
        throw ("DOM element for ID [" + elementIdToToggle + "] not found.");
    }

    if (window.document.getElementById(elementIdToToggle).style.display == 'none') {
        window.document.getElementById(elementIdToToggle).style.display = 'block';
        window.document.getElementById(elementIdToToggle).style.visibility = 'visible';
    } else {
        window.document.getElementById(elementIdToToggle).style.display = 'none';
        window.document.getElementById(elementIdToToggle).style.visibility = 'hidden';
    }

    if((elementsToHide == undefined) || (elementsToHide == null))
    {
        return false;
    }

    for(var i=0; i<elementsToHide.length; i++)
    {
        if(elementsToHide[i]!=null && (typeof (elementsToHide[i]) == "string"))
        {
            window.document.getElementById(elementsToHide[i]).style.display = 'none';
            window.document.getElementById(elementsToHide[i]).style.visibility = 'hidden';
        }
        else
        {
            throw ("elementsToHide contains an invalid entry.");
        }
    }
    return false;
}

OTAUtils.prototype.togglePlusMinusImgElement = function(elementId, imgElementId,
        plusSrcImgUrl, minusSrcImgUrl) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((elementId == undefined) || (elementId == null)
            || (typeof (elementId) != "string")
            || (window.document.getElementById(elementId) == null)) {
        throw ("DOM element for ID [" + elementId + "] not found.");
    }

    if ((imgElementId == undefined) || (imgElementId == null)
            || (typeof (imgElementId) != "string")
            || (window.document.getElementById(imgElementId) == null)) {
        throw ("DOM img element for ID [" + elementId + "] not found.");
    }

    if ((plusSrcImgUrl == undefined) || (plusSrcImgUrl == null)
            || (typeof (plusSrcImgUrl) != "string")) {
        throw ("The Plus Image Src URL [" + plusSrcImgUrl + "] is not valid.");
    }

    if ((minusSrcImgUrl == undefined) || (minusSrcImgUrl == null)
            || (typeof (minusSrcImgUrl) != "string")) {
        throw ("The Minus Image Src URL [" + minusSrcImgUrl + "] is not valid.");
    }

    if ((window.document.getElementById(imgElementId).tagName == null)
            || (window.document.getElementById(imgElementId).tagName.toUpperCase() != "IMG")) {
        throw ("DOM img element for ID [" + elementId + "] is a ["
                + window.document.getElementById(imgElementId).tagName + "] which is not a valid IMG tag");
    }

    if (window.document.getElementById(elementId).style.display == 'none') {
        window.document.getElementById(elementId).style.display = 'block';
        window.document.getElementById(imgElementId).src = minusSrcImgUrl;
        window.document.getElementById(imgElementId).alt = 'Close the menu';
    } else {
        window.document.getElementById(elementId).style.display = 'none';
        window.document.getElementById(imgElementId).src = plusSrcImgUrl;
        window.document.getElementById(imgElementId).alt = 'Open the menu';
    }
    return false;
}

OTAUtils.prototype.hideElement = function(elementId) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((elementId == undefined) || (elementId == null)
            || (typeof (elementId) != "string")
            || (window.document.getElementById(elementId) == null)) {
        throw ("DOM element for ID [" + elementId + "] not found.");
    }

    window.document.getElementById(elementId).style.display = 'none';
    window.document.getElementById(elementId).style.visibility = 'hidden';
    return false;
}

OTAUtils.prototype.setElementValue = function(elementName, value) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((elementName == undefined) || (elementName == null) || (typeof (elementName) != "string")) {
        throw ("elementName argument not valid.");
    }

    if ((value == undefined) || (value == null) || (typeof (value) != "string")) {
        throw ("value argument not valid.");
    }

    var elementList = window.document.getElementsByName(elementName);
    if(elementList != null)
    {
        for(var i=0; i<elementList.length; i++)
        {
            elementList[i].value = value;
        }
    }

    return false;
}

OTAUtils.prototype.appendParmToPageLinks = function(/* string */arg) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((arg == undefined) || (arg == null) || (typeof (arg) != "string")) {
        throw ("arg argument not valid.");
    }

    var anchorTags = window.document.getElementsByTagName("a");
    if (anchorTags != null) {
        for ( var i = 0; i < anchorTags.length; i++) {
            if (anchorTags[i].href != null) {
                if (anchorTags[i].href.search(/#/) != -1) {
                    continue;
                }

                if (anchorTags[i].href.search(/mailto:/) != -1) {
                    continue;
                }

                if (anchorTags[i].href.search(/javascript:/) != -1) {
                    continue;
                }

                if (anchorTags[i].href.search(/\?/) == -1) {
                    anchorTags[i].href += "?" + arg;
                } else {
                    anchorTags[i].href += "&" + arg;
                }
            }
        }
    }

    return false;
}

OTAUtils.prototype.historyGoBack = function(/* string */arg) {
    if ((this == undefined) || (this == null)) {
        throw ("OTAUtils object is null.");
    }

    if ((arg == undefined) || (arg == null) || (typeof (arg) != "string")) {
        throw ("arg argument not valid.");
    }

    var historyObj = window.history;
    if (historyObj != null) {
        historyObj.go(-1);
    } else {
        if(window.document.location != null)
        {
            window.document.location.pathname = arg;
        }
        else
        {
            throw ("unexpected error: window.document.location is null");
        }
    }

    return false;
}

OTAUtils.prototype.submitForm = function(/* string */arg) {
  if ((this == undefined) || (this == null))
  {
    throw ("OTAUtils object is null.");
  }

  if ((arg == undefined) || (arg == null) || (typeof (arg) != "string")) {
    throw ("arg argument not valid.");
  }

  var form0 = window.document.forms[0];
  if(form0 == undefined || form0 == null)
  {
    throw ("form 0 is not defined.");
  }

  form0.action=arg;
  form0.submit();

  return false;
}

OTAUtils.prototype.submitFormById = function(formId, formAction) {
    if ((this == undefined) || (this == null))
    {
      throw ("OTAUtils object is null.");
    }

    if ((formId == undefined) || (formId == null) || (typeof (formId) != "string"))
    {
        throw ("formId argument not valid.");
    }

    if ((formAction == undefined) || (formAction == null) || (typeof (formAction) != "string"))
    {
      throw ("formAction argument not valid.");
    }

    var form = window.document.getElementById(formId);
    if((form == undefined) || (form == null))
    {
        throw("formId [" + formId + "] does not exist or is not a form");
    }

    form.action=formAction;
    form.submit();

    return false;
  }