
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

stdBrowser = (document.getElementById) ? true : false

function popUp(evt,currElem) {
    popUpWin = (stdBrowser) ? document.getElementById(currElem).style : eval("document." + currElem)
    if (document.all) {
        popUpWin.pixelTop = parseInt(evt.y)+10
        popUpWin.pixelLeft = Math.max(2,parseInt(evt.x)+5)
    }
    else {
        if (stdBrowser) {
            popUpWin.top = parseInt(evt.pageY)+10 + "px"
            popUpWin.left = Math.max(2,parseInt(evt.pageX)+5) + "px"
        }
        else {
            popUpWin.top = parseInt(evt.pageY)+10
            popUpWin.left = Math.max(2,parseInt(evt.pageX)+5)
        }
    }
    popUpWin.visibility = "visible"
}

function popUpWindow(strURL, strType, strHeight, strWidth) {
   var strOptions = "";
   if (strType == "console") {
      strOptions = "resizable=yes,height=" + strHeight + ",width=" + strWidth;
   }
   else if (strType == "fixed") {
      strOptions = "status=yes,height=" + strHeight + ",width=" + strWidth;
   }
   else if (strType == "elastic") {
      strOptions = "toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,height=" + strHeight + ",width=" + strWidth;
   }
   else {  // default
      strOptions = "scrollbars=yes,resizable=yes,height=" + strHeight + ",width=" + strWidth;
   }
   var newWindow = window.open(strURL, 'newWindow', strOptions);
   newWindow.focus();
}

function popDown(currElem) {
    popUpWin = (stdBrowser) ? document.getElementById(currElem).style : eval("document." + currElem)
    popUpWin.visibility = "hidden"
}

function noop() {}

function setCookie(name, value, expires, domain, path) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((path) ? "; path=" + path : "");
  document.cookie = curCookie;
}


function unsetCookie(name) {
    expireDate = new Date();
    expireDate.setDate(expireDate.getDate() - 1);
    setCookie(name, "false", expireDate);
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) {
    date.setTime(date.getTime() - skew);
  }
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// Check if a cookie with the supplied name is present
// This code handles the case where the cookie name is used as a different cookie value.
function hasCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    // First check for all *but* the first.
    var found = dc.indexOf("; " + prefix);
    if (found == -1) {
        // Not there - see if it's the first (must be at pos 0)
        found = dc.indexOf(prefix);
        if (found != 0) {
            return false;
        }
    }
    return true;
}

// is the browser IE?
function isIE()  {
  var navString = navigator.userAgent.toLowerCase();
  return (navString.indexOf("msie") != -1)
}

// Make a table-row element visible
function showTableRow(tableRowElement) {
    if (tableRowElement) {
        // Get the style-display attribute for a table row 
        // on IE this is "inline" 
        // on CSS2 browsers it is "table-row"
        displayValue = isIE() ? "inline" : "table-row";
        setDisplay(tableRowElement, displayValue);
    }
}

// Make a table-row element (or any other) invisible
function hideTableRow(element) {
    if (element) {
        setDisplay(element, "none");
    }
}

// Make a label element visible
function showLabel(element) {
    if (element) {
        setDisplay(element, "block");
    }
}

function setDisplay(element, displayValue) {
    if (element && element.style) {
        element.style.display = displayValue;
    }
}

function setDisplayForElementsByTagNameAndClass(elementName, elementClass, displayValue) {
    var foundElements = document.getElementsByTagName(elementName);
    for (var i = 0; i < foundElements.length; i++)
    {
        if (foundElements[i].className == elementClass) {
           setDisplay(foundElements[i], displayValue);
        }
    }
}

/**
 * set Field focus.
 * @param fieldId - element id.
 */
function setFieldFocus(fieldId) {
    var element = getElementWithIdOrName(fieldId);
    if (element) {
        if (element.focus) {
            element.focus()
        }

        // Detect if the element supports select (for IE)
        if (element.select) {
            element.select();
        }
    }
}

function getElementWithIdOrName(identifier) {
    var element = document.getElementById(identifier);
    if (!element) {
        var namedElements = document.getElementsByName(identifier);
        if (namedElements) {
            element = namedElements[0];
        }
    }
    return element;
}

/**
* @param formName to clear.
* sets the processAction of the form to 'clear' and submits the form.
*/
function clearForm(formName) {
    var currentForm = document.forms[formName];
    if (currentForm) {
        currentForm.processAction.value='clear';
        currentForm.submit();
    }
}


/**
 * Clear the value for the given text field.
 */
function clearTextField(textField) {
    textField.value='';
}

/**
 * Removes the first option in a Select if that option is blank,
 */
function removeBlankSelectOption(id) {
    var selectElement = document.getElementById(id);
    if (selectElement && !selectElement.options[0].text) {
        selectElement.remove(0);
    }
}

function enterKeyPressed (e) {
    return requiredKeyPressed(e, 13);
}

function requiredKeyPressed (e, requiredKey) {
    var keycode = 0;

    if (window.event) {
        keycode = window.event.keyCode;
    }
    else if (e) {
        keycode = e.which;
    }

    return (keycode == requiredKey);
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function displaySingleCurrencyConverter(countryId) {
    var popup = window.open('/CurrencyPopUp.jsp?countryId=' + countryId, 'CurrencyPopUp','width=350,height=130,screenX=100,screenY=0,left=100,top=0,scrollbars=0,statusbar=0');
    window.onfocus = function() {
        if (popup) {
            popup.close();
        }
    }
}

function displaySingleCurrencyConverter(baseUrl, countryId) {
    var popup = window.open(baseUrl + '/CurrencyPopUp.jsp?countryId=' + countryId, 'CurrencyPopUp','width=350,height=130,screenX=100,screenY=0,left=100,top=0,scrollbars=0,statusbar=0');
    window.onfocus = function() {
        if (popup) {
            popup.close();
        }
    }
}

/**
 * This function takes an url and will return the value of the
 * specified parameter from the query string. 
 */
function getUrlParameter(name) {
    var regex = new RegExp("[\\?&]"+name+"=([^&#]*)");
    var results = regex.exec(window.location.href);
    if (results == null) {
        return "";
    }
    return results[1];
}

/**
 * This function takes an 'url' and a 'param name', where paramName
 * is a parameter in the current request url. This parameter and value
 * will be attached to the url passed to this function.
 * Note that this will not remove any existing parameters in 'url'.
 */
function appendUrlParam(url, paramName) {
    var paramValue = getUrlParameter(paramName);
    if (url.indexOf("?") < 0) {
        return url + "?" + paramName + "=" + paramValue;
    }
    return url + "&" + paramName + "=" + paramValue;
}

/**
 * Convert String ISO Date to Date Object 
 */
function parseDate(xmlDate) {
    if (!/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}/.test(xmlDate)) {
        throw new RangeError("xmlDate must be in ISO-8601 format YYYY-MM-DD.");
    }
    return new Date(xmlDate.substring(0,4), xmlDate.substring(5,7)-1, xmlDate.substring(8,10));
}