var onLoadFunctions = [];
var callbacks = [];

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } //trim func
String.prototype.isEmpty = function(){
	return (this.trim()==null ||this.trim() ==''||this.trim()=='undefined'||this.length == 0 );	
}
Array.prototype.isEmpty = function(){
	return (this.length==0)
}
function addOnLoadFunction(func) {
   onLoadFunctions.push(func);
}

function insertOnLoadFunction(func) {
   onLoadFunctions.unshift(func);
}

function processOnLoad(func) {
   while ((func = onLoadFunctions.shift())) {
      eval(func);
   }
}

window.onload = processOnLoad;

function showDef(keyword) {
	win = window.open(contextPath + 'keywords.html?type=def&keyword=' + keyword, 'definitions', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=515,height=300');
	win.focus();
}

function showQuestion(keyword) {
	win = window.open(contextPath + 'keywords.html?type=que&keyword=' + keyword, 'definitions', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=515,height=300');
	win.focus();
}

function showTermsAndConditions(brandDelegateId) {
	win = window.open(contextPath + 'keywords.html?type=tnc&keyword=tnc_' + brandDelegateId, 'termsAndConditions', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=550,height=500');
	win.focus();
}

function newWindow(url, w, h) {
	win = window.open( url, '', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=' + w + ',height=' + h);
	win.focus();
}

function toggleRecurringLineItemPricing(classStr, arrowImgStr) {
	arrowImg = $(arrowImgStr);
	
	$$(classStr).each(function(elem) {
		elem.toggle();
		elemVisible = elem.visible();
	});
	
	if (arrowImg && typeof elemVisible != "undefined") {
		if (elemVisible) {
			arrowImg.writeAttribute('src', '/shared/img/icons/arows/down_arrow.jpg');
		} else {
			arrowImg.writeAttribute('src', '/shared/img/icons/arows/right_arrow.jpg');
		}
	}
}

function addLibrary(path, type){	
	var x = 0;
	var elem = null;
	if (type=="js"){ 
		elem = document.createElement("script");
		elem.setAttribute("type","text/javascript");
		elem.setAttribute("src", path);
	}
	else if (type=="css"){ 
		elem = document.createElement("link");
		elem.setAttribute("rel", "stylesheet");
		elem.setAttribute("type", "text/css");
		elem.setAttribute("href", path);
	}	
	if(elem != null && (typeof elem!='undefined')){
		document.getElementsByTagName("head")[0].appendChild(elem);
	}
	
	return elem;
}

function setCookie(cName,cValue,cDays){
	var exp = "";
	if (cDays) {
		var date = new Date();
		date.setTime(date.getTime()+(cDays*24*60*60*1000));
		var exp = "; expires="+date.toGMTString();
	}	 
	document.cookie = cName+"="+(cValue+"")+exp+"; path=/";
}

function getCookie(cName){
	var tmp = cName+"=";
	var x = 0;
	var cookies = document.cookie.split(';');
	for(x = 0;x < cookies.length;x++) {
		var cookie = cookies[x].trim();	
		if (cookie.indexOf(tmp) == 0){
			return cookie.substring(tmp.length,cookie.length);
		}		
	}
	return null;
}

function removeCookie(cName) {
	setCookie(cName,"",-1);
}

function JSONRequest(url, options) {
	index = callbacks.length;
	if (url.indexOf('?') < 0) {
		url += '?callback=' + index;
	} else {
		url += '&callback=' + index;
	}
	if (options) {
		var params = Object.clone(options.parameters);
		if (params = Object.toQueryString(params)) {
			url += '&' + params;
		}
	}
	url += '&timecode=' + (new Date()).getTime();
	elem = document.createElement("script");
	elem.setAttribute("type","text/javascript");
	elem.setAttribute("src", url);
	
	options.scriptTag = elem;
	callbacks[index] = options;
	
	document.getElementsByTagName("body")[0].appendChild(elem);
}

function executeCallback(index) {
	Element.remove(callbacks[index].scriptTag);
	callbacks[index].onSuccess(eval('jsonResponse' + index));
}

var DynamicUrls = Class.create({
	initialize: function(options) {
	    this.options = {}
	    Object.extend(this.options, options || { });
	},

	resolveUrl: function(url) {
		if (this.options.data.urls[url]) {
			dUrl = this.options.data.urls[url];
			result = '';
			if (dUrl.secure) {
				if (dUrl.secure == 'HTTPS') {
					result += 'https://';
				} else if (dUrl.secure == 'HTTP') {
					result += 'http://';
				} else {
					result += urlScheme;
				}
			} else {
				result += urlScheme;
			}
	
			if (dUrl.hostname) {
				result += dUrl.hostname;
			} else {
				result += urlHostname;
			}
	
			if (dUrl.port) {
				result += ":" + dUrl.port;
			} else if (!dUrl.hostname) {
				if (urlPort) {
					result += ":" + urlPort;
				}
			}
	
			result += url;
	
			return result;	
		}

		return url;
	}
});