/**
 * @author MHavard
 * Deprecated code from common.js and scripts.js
 */
/* TODO: Make this go away, some methods can be implimented in Compass style and put in a utils.js */

/* Dean Edwards/Matthias Miller/John Resig
 * Fix image caching for CSS in IE 
 */
try {
    document.execCommand("BackgroundImageCache", false, true);
} 
catch (err) {
    /* do nothing */
}
function set (sSize){
    var fontMax = 82.5;
    var fontMin = 42.5;
    var fontsize = document.body.style.fontSize;
    var currentTime = new Date();
    if (sSize == 'decrease') {
        if (fontsize) {
            fontsize = parseFloat(fontsize.replace("%", "")) - 10;
            if (fontsize < fontMin) {
                fontsize = fontMin;
            }
        }
        else {
            fontsize = 52.5;
        }
        document.body.style.fontSize = fontsize + "%";
    }
    else {
        if (fontsize) {
            fontsize = parseFloat(fontsize.replace("%", "")) + 10;
            if (fontsize > fontMax) {
                fontsize = fontMax;
            }
        }
        else {
            fontsize = 72.5;
        }
        document.body.style.fontSize = fontsize + "%";
    }
    setCookie('fontSize', fontsize, '');
}
com.express_scripts.legacy = {
	init : function(){
		this.fontSize.init();
	},
	fontSize : {
		init : function(){
			var fontsize = getCookie('fontSize');
		    if (fontsize) {
		        document.body.style.fontSize = fontsize + "%";
		    }
		    
		    $("#increase").click(function () { 
		    	set("increase"); 
		    });
		    $("#decrease").click(function () { 
		    	set("decrease");  
		      });
		    
		    
		    
			/*var oIncrease = document.getElementById("increase");
			var oDecrease = document.getElementById("decrease");
			bindings.bind(oIncrease, "onclick", this, "increase");
			bindings.bind(oDecrease, "onclick", this, "decrease");*/
		}
		
		
	}
};


/* ***********************
 ** Validation Methods  **
 *********************** */
  
	/**
	 * Create validation namespace (automatically created when API loads).
	 * @class  Namespace used to handle basic form and data validation. More complex
	 * data validation will occur on the server side.
	 * @alias com.express_scripts.validation
	 * @constructor
	 * @extends com.express_scripts
	 */
 	com.express_scripts.validation = {
 	
	 	/**
	 	 * Limits the number of characters that can be entered into a given textarea either
	 	 * by pasting or by manual key entry.
	 	 * @alias com.express_scripts.validation.limitTextArea
	 	 */
	 	limitTextArea : function(event)
	 	{
	 		var oText = com.express_scripts.htmlUtils.getEventTarget(event)
	 		var sText = oText.value;
	 		var iMax = oText.maxlength || com.express_scripts.htmlUtils.getClassProperty(oText, "maxLength");
	 		/*var sLabel = oText.parentNode.previousSibling.firstChild.innerText.replace(/:/,"");*/
	 		if(iMax !== null && sText.length > iMax-1)
	 		{
	 			/** TODO: This should change to use a notify method instead of an alert */
	 			alert("You have entered the maximum number of characters allowed (" + iMax + " characters). ");
	 			oText.value = sText.substring(0, iMax);
			}
	 	},
	 
	 	/**
	 	 * Initializes all textareas that have a maxlength defined and attaches the appropriate
	 	 * bindings needed to limit the input into those fields.
	 	 * @alias com.express_scripts.validation.initLimitTextArea
	 	 */
	 	initLimitTextArea : function()
	 	{
	 		var aTextAreas = document.getElementsByTagName("textarea");
	 		for(var i = 0, iLen = aTextAreas.length; i < iLen; i++)
	 		{
	 			if(aTextAreas[i].maxlength || com.express_scripts.htmlUtils.getClassProperty(aTextAreas[i], "maxLength")!=null)
	 			{
		 			if(aTextAreas[i]["onkeyup__original__"] == undefined)
		 			{				
		 				bindings.bind(aTextAreas[i], "onkeyup", com.express_scripts.validation, "limitTextArea");
		 			}
					
		 		}
	 		}
	 	}
 	};
	

/* *************************
 ** HTML Utility Methods  **
 ************************* */

	
	com.express_scripts.htmlUtils = {
				/**
		 * Adds a class to an existing element.
		 * @alias com.express_scripts.htmlUtils.addClass
		 * @param {Object} oObject Element/Object to add the class string to.
		 * @param {String} sClass String to be added to the class. 
		 */		
		addClass : function(oObject, sClass)
		{
			var sCurrentClass = oObject.className || "";
			/* test to see that className is not undefined and that the class doesn't exist on the object */
			if(typeof sCurrentClass != "undefined" && 
				!com.express_scripts.htmlUtils.hasClass(oObject, sClass))
			{
				oObject.className += ((sCurrentClass)?" " : "") + sClass;	
			}
		},
		
		/**
		 * Removes a class from an existing element.
		 * @alias com.express_scripts.htmlUtils.removeClass
		 * @param {Object} oObject Element/Object to remove the class string from.
		 * @param {String} sClass String to be removed from the class. 
		 */		
		removeClass : function(oObject, sClass)
		{
			var sCurrentClass = oObject.className || "";
			oObject.className = sCurrentClass.replace(new RegExp("(^|\\s)" + sClass + "(\\s|$)"),"$1");
		},
		/**
		 * Replace one class with another class
		 * @alias com.express_scripts.htmlUtils.replaceClass
		 * @param {Object} oObject Element/Object to replace the class string on.
		 * @param {String} sClass String to be removed from the class.
		 * @param {String} sReplacement String to add to the class.
		 */
		replaceClass : function(oObject, sClass, sReplacement)
		{
			com.express_scripts.htmlUtils.removeClass(oObject, sClass);
			com.express_scripts.htmlUtils.addClass(oObject, sReplacement);	
		},
		/**
		 * Tests whether the string exists within the class
		 * @alias com.express_scripts.htmlUtils.hasClass
		 * @param {Object} oObject Element/Object to test against.
		 * @param {String} sClass String to be tested for.
		 * @return {Boolean}
		 */
		hasClass : function(oObject, sClass)
		{
			return new RegExp("(^|\\s)" + sClass + "(\\s|$)").test(oObject.className);
		},
		
		/**
		 * Gets a concatenated property for an object based on one portion of the class
		 * EXAMPLE: getClass(oTable, "pageablerows") returns "6" from <table class="pageablerows:6">.
		 * @alias com.express_scripts.htmlUtils.getClass
		 * @param {Object} oObject Element/Object to test against.
		 * @param {String} sClass String to be tested for.
		 * @return {String}
		 */
		getClassProperty : function(oObject, sClass)
		{
			var sCurrentClass = oObject.className;
			if(sCurrentClass.match(new RegExp("(^|\\s)" + sClass + "([^ ]+)")))
			{
				return RegExp.$2;
			}
			return null;
		},
		
		/**
		 * Gets the target of an event in a crossbrowser manner.
		 * @alias com.express_scripts.htmlUtils.getEventTarget
		 * @param {Object} event Event to source.
		 * @return {Object}
		 */
		getEventTarget: function(event)
		{
			var target = (event.target) ? event.target : event.srcElement;
			if (target.nodeType == 3)
			{
				target = target.parentNode;
			}
			return target;
		}
	};










/* alias old method call to new method */
window.setFontSize = com.express_scripts.legacy.fontSize.set;

$(document).ready(function() {
	com.express_scripts.legacy.init();
});

/*bindings.bind(window, "onload", com.express_scripts.legacy, "init", true);
bindings.bind(window, "onload", com.express_scripts.validation, "initLimitTextArea");*/


/* Unwrapped legacy methods, some of these exist in Compass code
 * We should migrate to use those instead
 */
function setCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
        if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
    }
    return null;
}

function eraseCookie(name){
    setCookie(name, "", -1);
}

//sees if Alert cookie is set
function getCookieBool(cookieName){
    noAlert = getCookie(cookieName);
    if (noAlert != null && noAlert == 'true') {
        return true;
    }
    else {
        return false;
    }
}

function getElementsByClassName(oElm, strTagName, strClassName){
    if (oElm == null) {
        return false;
    }
    var arrElements = (strTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for (var i = 0; i < arrElements.length; i++) {
        oElement = arrElements[i];
        if (oRegExp.test(oElement.className)) {
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function createCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = ";expires=" + date.toGMTString();
    }
    else 
        expires = "";
    document.cookie = name + "=" + value + expires + ";domain=.express-scripts.com;path=/;";
}

function readCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') 
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

/* 
 * Due to the potential need for optional parameters passed to this function, 
 * I had to forego the usual method of declaring it in order to make it work in
 * Javascript properly. When calling this function, consider it declared as follows:
 * 
 *    function PopupWindow(url, height, width, name)
 *    
 * Name is an optional parameter whose default value is "choicePopup".
 */
function PopupWindow(){
    // Mandatory arguments
	var url = arguments[0];
	var height = arguments[1];
	var width = arguments[2];                  
    
    // Optional argument
	var name;
	if(arguments < 4) {
		name = "choicePopup" ; // Default value
	}
	else {
		name = arguments[3];
	}
		
	var features = "status=yes,scrollbars=yes,resizable=yes,width=" + width + ",height=" + 
		height + ",left=30,top=30";

	var childWindow = open(url, name, features);
	childWindow.focus();
}

function toggleExtraInfo(el){
    if (/Show/.test(el.innerHTML)) {
        el.innerHTML = el.innerHTML.replace(/Show/, "Hide");
        el.nextSibling.style.display = "block";
    }
    else {
        el.innerHTML = el.innerHTML.replace(/Hide/, "Show");
        el.nextSibling.style.display = "none";
    }
}

function alertLeave() {
	return confirm("You are about to leave Express Scripts Web Site");
}