﻿// JavaScript Document
/*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\																												 \\\\\
\\\\\	Custom Transaction System Document																			 \\\\\
\\\\\																												 \\\\\
\\\\\	Coded & Maintained by: Scott Crowley																		 \\\\\
\\\\\				  Contact: scott@scottcrowley.com																 \\\\\
\\\\\																												 \\\\\
\\\\\	All code & concepts 2010 SC Designs																			 \\\\\
\\\\\																												 \\\\\
\\\\\	Version: 1.0																								 \\\\\
\\\\\																												 \\\\\
\\\\\ Document Revision History:																					 \\\\\
\\\\\ 11/23/08 18:45 Document Created																				 \\\\\
\\\\\																												 \\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
//whether or not to use a debug div in the page. if true, info can be placed in div with TS_JQ('#ts_debugDiv').append('msg');
var ts_debug = false;
//Assign jQuery to variable for use in other scripts
if (jQuery !== null && jQuery !== undefined) var TS_JQ = jQuery.noConflict();
//various message & functional containers that can be used and place in a page using jquery
var ts_loadingContainer = '<div id="ts_loadingContainer"><div class="ts_loadingMed"></div></div></div>';
var ts_debugContainer = '<div id="ts_debugDiv" style="background-color:#eeeeee; border:2px solid #333333; width:350px; margin:20px auto; padding:10px;"></div>';
var alertContainerID = 'ts_modalAlert';
var alertWindowClass = 'ts_modalAlertWindow';
var alertTitleClass = 'ts_modalAlertTitle';
var alertContentClass = 'ts_modalAlertContent';
var confirmContainerID = 'ts_modalConfirm';
var confirmWindowClass = 'ts_modalConfirmWindow';
var confirmTitleClass = 'ts_modalConfirmTitle';
var confirmContentClass = 'ts_modalConfirmContent';
var confirmContentMsgClass = 'ts_modalConfirmMsg';
var ts_confirmContainer = '<div class="'+confirmContainerID+'" id="'+confirmContainerID+'"><div class="'+confirmWindowClass+'"><div class="'+confirmTitleClass+' clearfix"><h1>Please Confirm?</h1><a href="#" class="jqmClose"><em>Close</em></a></div><div class="'+confirmContentClass+'"><p class="'+confirmContentMsgClass+'">Are you sure you want to perform this action?</p><p>Continue?</p></div><input type="submit" id="ts_confirmNoBtn" value="no" /><input type="submit" id="ts_confirmYesBtn" value="yes" /></div></div>';
var ts_alertContainer = '<div class="'+alertContainerID+'" id="'+alertContainerID+'"><div class="'+alertWindowClass+'"><div class="'+alertTitleClass+' clearfix"><h1>Alert!</h1><a href="#" class="jqmClose"><em>Close</em></a></div><div class="'+alertContentClass+'"></div></div>';

if (TS_JQ) {
	TS_JQ(function() {
		if (ts_debug === true) TS_JQ(document.body).prepend(ts_debugContainer);
	});
	//allows you to write debug info to the ts_debugDiv
	function ts_debugMsg(msg) {
		if (ts_debug === false || msg == '') return null;
		TS_JQ('#ts_debugDiv').append(msg);
	}
	//---Override Javascript's Alert Dialog
	function ts_alert(msg) {
		var hasCls = TS_JQ('#'+alertContainerID).hasClass(alertContainerID);
		if (hasCls === false) {
			TS_JQ(document.body).append(ts_alertContainer);
			TS_JQ('#'+alertContainerID).jqm();
		}
		TS_JQ('#'+alertContainerID).jqmShow().find('.'+alertContentClass).html(msg);
	}
	//---Override Javascript's Confirm Dialog
	//NOTE; A callback must be passed. It is executed on "continue". This differs from the standard confirm() function, which returns only true or false! If the callback is a string, it will be considered a "URL", and followed. If the callback is a function, it will be executed.
	function ts_confirm(msg,callback) {
		var hasCls = TS_JQ('#'+confirmContainerID).hasClass(confirmContainerID);
		if (hasCls === false) {
			TS_JQ(document.body).append(ts_confirmContainer);
			TS_JQ('#'+confirmContainerID).jqm({modal:true});
		}
		var fnc = ''
		if (typeof callback == 'string') {
			if (callback.substr(0,2) == '::') {
				fnc = callback.substr(2);
				if (fnc.substr(-1) != ';') fnc += ';';
			}
		}
		TS_JQ('#'+confirmContainerID).jqmShow().find('.'+confirmContentMsgClass).html(msg).end().find(':submit:visible').unbind('click').bind('click',function(){
			if(this.value == 'yes') {
				if (typeof callback == 'string') {
					if (fnc == '' && callback != '') window.location.href = callback;
					else if (fnc != '') eval(fnc);
				} else if (callback != '') {
					callback();
				}
			}
			TS_JQ('#'+confirmContainerID).jqmHide();
		});
	}
}
//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;
}
//standard jump menu function
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);
	}
}
//display confirmation message before performing a redirect
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;
}
//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;
	var elVal = (el !== null && el !== undefined) ? el.value : '';
	if (val != '' && ((sel) || (ckBox) || (elVal != '' && override === true) || (elVal == '' && !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);
			if (fileNameEl !== null && fileNameEl !== undefined) fileNameEl.innerHTML = val;
		} else {
			if (el !== null && el !== undefined) 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;
}
//used to cycle through various images using prev & next buttons. used with the TS_ListObjectGenerator class
//params: dir=direction(prev or next), prefx=container prefix, row=container row id, par=parent element images are located under
function imgcyle(dir,prefx,row,par,el) {
	var tmpID = '#'+prefx+row+' .'+par+' '+el;
	var disp = dispCnt = 0;
	var imgs = TS_JQ(tmpID);
	var total = imgs.length;
	var type = 'block';
	if (total < 2) return false;
	TS_JQ(imgs).each(function(idx) {
		var curDisp = (TS_JQ(this).css('display') == 'none') ? false : true;
		if (curDisp === true) {
			type = TS_JQ(this).css('display');
			disp = idx;
			TS_JQ(this).css('display','none');
			dispCnt ++;
		}
	});
	if (dispCnt == total && disp == total - 1) dispNxt = (dir == 'next') ? 1 : total - 1;
	else if (dir == 'prev' && disp == 0) dispNxt = total - 1;
	else if (dir == 'next' && disp == (total - 1)) dispNxt = 0;
	else dispNxt = (dir == 'next') ? disp + 1 : disp - 1;
	TS_JQ(imgs[dispNxt]).css('display',type);
}
//used to show a lightbox pertaining to a certain row of data.
//params: row=id for the current row (inc "#"), par=parent element class or id (inc "#" or "."), id=id of the clicked element
function dispImgLB(row,par,id) {
	var curSrc = TS_JQ('#'+id).attr('rel'); //current clicked image src
	var allImgs = TS_JQ(row+' '+par+' img'); //all images within parent
	var imgCnt = allImgs.length;
	var cur = 0; //current clicked image
	var imgArr = new Array();
	if (imgCnt > 0) {
		for(i=0;i<imgCnt;i++) {
			var tmpSrc = TS_JQ(allImgs[i]).parent().attr('rel');
			var tmpTitle = TS_JQ(allImgs[i]).parent().attr('title');
			if (tmpSrc == curSrc) cur = i;
			var tmpArr = new Array();
			tmpArr[0] = tmpSrc;
			tmpArr[1] = tmpTitle;
			imgArr[i] = tmpArr;
		}
	}
	TS_JQ.slimbox(imgArr,cur,{ overlayOpacity:overlayOpacity, fadeDur:fadeDur, resizeDuration:resizeDuration, initialWidth:initialWidth, initialHeight:initialHeight, imageFadeDuration:imageFadeDuration, captionAnimationDuration:captionAnimationDuration, counterText:counterText, loop:loop});
}
//Uses jQuery to dynamically add alternating table rows based on the passed arguments.
//params: tbl=table class to apply to, e=even row class name, o=odd row class name, h=hover class name, sf=boolean to skip 1st row, sl=boolean to skip last row, uh=boolean to use hover class
function altTableRows(tbl,e,o,h,sf,sl,uh) {
	var odd = (TS_JQ('.'+tbl+' tr').length%2 != 0) ? true : false;
	var el = (odd === false && sl === true) ? ':not(:last)' : '';
	var of = (sf === true) ? ':gt(0)' : '';
	var ol = (odd === true && sl === true) ? ':not(:last)' : '';
	if (uh === true) {
		var tf = (sf === true) ? ':gt(0)' : '';
		var tl = (sl === true) ? ':not(:last)' : '';
		TS_JQ('.'+tbl+' tr'+tf+tl).mouseover(function(){ TS_JQ(this).addClass(h); }).mouseout(function(){ TS_JQ(this).removeClass(h); });
	}
	TS_JQ('.'+tbl+' tr:nth-child(even)'+el).addClass(e);
	TS_JQ('.'+tbl+' tr:nth-child(odd)'+of+ol).addClass(o);
}
//Updates a pager display div with the current page information. uses jQuery
//params: id=the element id that contains the pager display, cp=current page, lp=total pages, size=row disp size, trc=total number of rows
function updatePagerDisplay(parentid,contClass,cp,lp,size,trc) {
	if (trc > 0) {
		var fpr = ((cp-1) * size)+1;
		var lpr = (fpr+size >= trc) ? trc : (fpr + size)-1;
		TS_JQ(parentid+' '+contClass+' .pgr_cp').text(cp);
		TS_JQ(parentid+' '+contClass+' .pgr_lp').text(lp);
		TS_JQ(parentid+' '+contClass+' .pgr_fpr').text(fpr);
		TS_JQ(parentid+' '+contClass+' .pgr_lpr').text(lpr);
		TS_JQ(parentid+' '+contClass+' .pgr_trc').text(trc);
	}
}
//Updates a pager button div with the current btn active status. switches class names depending which page the list is on. uses jQuery
//params: id=the element id that contains the pager btns,first btn class, prev btn class, next btn class, last btn class, cp=current page, lp=total pages
function updatePagerBtnStatus(parentid,contClass,fCls,pCls,nCls,lCls,cp,lp) {
	if (cp == 1) {
		TS_JQ(parentid+' '+contClass+' '+fCls).removeClass(fCls.substr(1)).addClass(fCls.substr(1)+'_dis').css('cursor','default');
		TS_JQ(parentid+' '+contClass+' '+pCls).removeClass(pCls.substr(1)).addClass(pCls.substr(1)+'_dis').css('cursor','default');
	} else {
		TS_JQ(parentid+' '+contClass+' '+fCls+'_dis').removeClass(fCls.substr(1)+'_dis').addClass(fCls.substr(1)).css('cursor','pointer');
		TS_JQ(parentid+' '+contClass+' '+pCls+'_dis').removeClass(pCls.substr(1)+'_dis').addClass(pCls.substr(1)).css('cursor','pointer');
	}
	if (cp == lp) {
		TS_JQ(parentid+' '+contClass+' '+nCls).removeClass(nCls.substr(1)).addClass(nCls.substr(1)+'_dis').css('cursor','default');
		TS_JQ(parentid+' '+contClass+' '+lCls).removeClass(lCls.substr(1)).addClass(lCls.substr(1)+'_dis').css('cursor','default');
	} else {
		TS_JQ(parentid+' '+contClass+' '+nCls+'_dis').removeClass(nCls.substr(1)+'_dis').addClass(nCls.substr(1)).css('cursor','pointer');
		TS_JQ(parentid+' '+contClass+' '+lCls+'_dis').removeClass(lCls.substr(1)+'_dis').addClass(lCls.substr(1)).css('cursor','pointer');
	}
}
