// JavaScript Document
/*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\																												 \\\\\
\\\\\	Custom Transaction System Document																			 \\\\\
\\\\\																												 \\\\\
\\\\\	Coded & Maintained by: Scott Crowley																		 \\\\\
\\\\\				  Contact: scott@scottcrowley.com																 \\\\\
\\\\\																												 \\\\\
\\\\\	All code & concepts ©2008 SC Designs																		 \\\\\
\\\\\																												 \\\\\
\\\\\	Version: 1.0																								 \\\\\
\\\\\																												 \\\\\
\\\\\ Document Revision History:																					 \\\\\
\\\\\ 11/23/08 18:45 Document Created																				 \\\\\
\\\\\																												 \\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/

//simple redirect function. params: url to redirect to, type of redirect (parent, blank, self, etc). If no type is provided then 'parent' is used
//syntax to call function: TS_goToURL('index.html','blank');
function TS_goToURL() {
	var args = TS_goToURL.arguments;
	if (args.length == 1 || args[1] == '') {
		var url = args[0];
		var type = 'parent';
	} else {
		var url = args[0];
		var type = args[1];
	}
	if (type !='' && url != '') {
		if (type == 'blank') {
			eval("window.open('"+url+"','_blank')");
		} else {
			eval(type+".location='"+url+"'");
		}
	} else {
		return false;
	}
}

function TS_jumpMenu(url,urlID,val) {
	if (val != '' && url != '' && urlID != '') {
		if (strpos(url,'?') !== false) var sp = '&';
		else var sp = '?';
		var gotoURL = url + sp + urlID + '=' + val;
		TS_goToURL(gotoURL);
	}
}

function confirmBeforeRedirect() {
	var args = confirmBeforeRedirect.arguments;
	if (args.length < 2) return false;
	var msg = args[0];
	var url = args[1];
	var type = (args.length > 2 && args[2] != '') ? args[2] : '';
	if (msg != '' && url != '') {
		var ans = confirm(msg);
		if (ans === true) {
			TS_goToURL(url,type);
		}
	}
	return false;
}

//converts array of arguments received by another function and converts to a list of paramaters
function argToParam(argArr,offst) {
	var params = '';
	for (i=offst;i<argArr.length;i++) {
		if (typeof(argArr[i]) == 'object') {
			params += argArr[i];
		} else {
			params += "'"+argArr[i]+"'";
		}
		if (i+1 != argArr.length) params += ',';
	}
	return params;
}

//function to check to see if a page element is loaded yet so it can start to be used
//Params: id=id of element to check, fnc=function to run if ready to use, xxx=any other params are sent as arguments to the fnc function
var pageLoaded = false;
var intValID = new Array();
window.onload = function() { pageLoaded = true; }

function elLoaded(id,fnc) {
	var args = elLoaded.arguments;	
	if (args.length > 2) {
		var params = argToParam(args,2);
	}
	if (document.getElementById(id) != null) {
		clearTimeout(intValID[id]);
		var fncName = (args.length > 2) ? fnc+"('"+id+"',"+params+")" : fnc+"('"+id+"')";
		eval(fncName);
	} else if (!pageLoaded) {
		var fncName = "elLoaded('"+id+"','"+fnc+"',"+params+")";
		if (args.length > 2) {
			intValID[id] = setTimeout("elLoaded('"+id+"','"+fnc+"',"+params+")",20);
		} else {
			intValID[id] = setTimeout("elLoaded('"+id+"','"+fnc+"')",20);
		}
	}
}

//adds an event to an element
//Params: id=id of element to attach to or the actual element itself that you want to attach to, fnc=function to run when event is initiated, evnt=type of event to add to element(i.e. click,keyup,keydown,mouseover,mouseout,etc), xxx=any other params are sent as arguments to the fnc function
function addEvntHndlr() {
	var args = addEvntHndlr.arguments;
	if (args.length > 0) {
		var id = args[0];
		var fnc = args[1];
		var evnt = args[2];
		var params = argToParam(args,3);
		params = params.replace(/'this.value'/gi,'this.value');
		params = params.replace(/'this'/gi,'this');
	}
	if (typeof(id) == 'object') {
		var el = id;
	} else {
		var el = document.getElementById(id);
	}
	var fncName = fnc+"("+params+")";
	evntFnc = new Function(fncName);
	if (navigator.appName != 'Microsoft Internet Explorer') el.addEventListener(evnt,evntFnc,false);
	else el.attachEvent(evnt,evntFnc);
}

//adds or updates the value of a form element. won't change if element already has a value, unless override = true
//Params: id=id of element id to set value to, val=value to set
function setElementValue(id,val,type,override) {
	var el = document.getElementById(id);
	var ckBox = (type=='CHECKBOX_YN_TYPE' || type=='CHECKBOX_1_0_TYPE' || type=='CHECKBOX_-1_0_TYPE' || type=='CHECKBOX_TF_TYPE' || type=='CHECKBOX_VALUE_NULL_TYPE') ? true : false;
	var sel = (type=='SELECT_NUMERIC_TYPE' || type=='SELECT_STRING_TYPE') ? true : false;
	if (val != '' && ((sel) || (ckBox) || (el.value != '' && override === true) || (el.value == '' && !sel && !ckBox))) {
		if (ckBox) {
			var op = (type=='CHECKBOX_YN_TYPE') ? 'Y' : ((type=='CHECKBOX_1_0_TYPE') ? '1' : ((type=='CHECKBOX_-1_0_TYPE') ? '-1' : ((type=='CHECKBOX_TF_TYPE') ? 'T' : ((type=='CHECKBOX_VALUE_NULL_TYPE' && val != '') ? val : false))));
			if (val == op || (type=='CHECKBOX_1_0_TYPE' && val > 0) || (type=='1_0_TYPE' && val < 0)) el.checked = true;
		} else if (sel) {
			var selVal = (type=='SELECT_NUMERIC_TYPE') ? parseInt(val) : val;
			el.value = selVal;
		} else if (type == 'FILE_TYPE') {
			var spanName = id+'_display';
			var fileNameEl = document.getElementById(spanName);
			fileNameEl.innerHTML = val;
		} else {
			el.value = val;
		}
	}
}

//disables a form element
//Params: id=id of element id to disable
function disableFormElement(id) {
	var el = document.getElementById(id);
	el.disabled = true;
}