/**
 * $Header: /export/cvsroot/EBS_CRM/html/xxssi_ibeCButton.js,v 1.35 2011/03/30 16:29:12 nagarn97 Exp $
 */

// Temporary hack until styles are included in global CSS.
(function(){
	var link = document.createElement("link");
	link.type = "text/css";
	link.rel = "stylesheet";
	link.href = "/OA_HTML/xxssi_buttongen.css";
	document.getElementsByTagName("head")[0].appendChild(link);
})();

(function(){
	var iStore = namespace("com.schoolspecialty.ebs.iStore");
	
	iStore.getUniqueId = (function(){
		var id = 0;
		return function(){
			return "xxssi_id-" + id++;
		};
	})();
	
	var getEl = function(el){
		if(typeof(el) == "string"){
			el = document.getElementById(el);
		}
		return el;
	};
	
	var disabledRegExp = /(^|\s)xxssi_buttonDisabled(\s|$)/;
	
	window.buttonGen = function(config, href, linkTitle){
		var linkId, linkText, jsAction, outputElement, type, disabled, onClickAction, s, classType, setTimeOut;
		if(typeof(config) == "string"){
			linkText = config;
		}else{
			linkId = config.linkId;
			linkText = config.linkText;
			linkTitle = config.linkTitle || linkTitle;
			jsAction = config.jsAction;
			href = config.href || href;
			outputElement = config.outputElement;
			type = config.type; // "button" or "link"
			disabled = config.disabled || config.elementDisabled; // boolean
			setTimeOut = config.setTimeOut || true;
		}
		if(!linkId){
			linkId = com.schoolspecialty.ebs.iStore.getUniqueId();
			if(typeof(config) != "string"){
				config.linkId = linkId;
			}
		}
		if(href == "disabled"){
			disabled = true;
			href = null;
		}else if(href != null && href.indexOf("javascript:") == 0){
			jsAction = href.substring("javascript:".length);
			href = null;
		}
		if(href == null){
			href = "javascript:";
		}
		//SetTimeOut is needed for IE6
		var func = (setTimeOut == true) ? ", function(){setTimeout( function(){" + jsAction + "}, 0)}" : ", function(){" + jsAction + "}";
		onClickAction = "return com.schoolspecialty.ebs.iStore.checkLink(this"
			+ (jsAction != null ? func : "") + ");";
		if(type == "button"){
			classType = "xxssi_button";
		}
		if(disabled){
			if(classType){
				classType += " ";
			}else{
				classType = "";
			}
			classType += "xxssi_buttonDisabled";
		}
		s = "<a id=\"" + linkId + "\""
			+ (classType ? " class=\"" + classType + "\"" : "")
			+ " onclick=\"" + onClickAction + "\" href=\"" + href + "\""
			+ (linkTitle != null ? " title=\"" + linkTitle : "") + "\">"
			+ linkText + "</a>";
		outputElement = getEl(outputElement);
		if(outputElement){
			outputElement.innerHTML = s;
		}else{
			document.writeln(s);
		}
		return linkId;
	};
	
	iStore.checkLink = function(linkId, enabledFunction){
		if(disabledRegExp.test(linkId.className)){
			return false;
		}else if(enabledFunction){
			return enabledFunction();
		}else{
			return true;
		}
	};
	iStore.enableLink = function(linkId){
		var linkIdObj = getEl(linkId);
		linkIdObj.className = linkIdObj.className.replace(disabledRegExp, " ");
	};
	iStore.disableLink = function(linkId){
		var linkIdObj = getEl(linkId);
		linkIdObj.className += " xxssi_buttonDisabled";
	};
})();
var submitForm = function(event, formName){
	var windowForm = document.forms[formName];
	capitalizeForm(windowForm);
	windowForm.event.name = event;
	var delaySubmit = function(){
		windowForm.submit();
	};
	//Required for IE6
	setTimeout(delaySubmit, 0);
	return true;
};
var capitalizeForm = function(form){
	var i, field;
	for(i = 0; i<form.elements.length; i++){
		field = form.elements[i];
		if(field.type == "text"){
			field.value = field.value.toUpperCase();
		}
	}
};
// DO NOT USE
var putNbsp = function(){
	document.writeln("&nbsp;");
};

