/* functions.js */
var isIE  = (document.all) ? true : false;
var isDOM = (document.getElementById) ? true : false;

function subVal(Val,Msg)
{
	if (Val!=="")
	{ 
		if (typeof(Msg)!="undefined") { jsAlert.show({trans:true,close:false,type:'text',text:Msg}); }
		if (!$('FormStatus')) { addFormStatus(); }
		$('FormStatus').value = Val;
		document.forms[0].submit();
	}
}

function addFormStatus()
{
	var form 		= document.forms[0];
	fStatus 		= document.createElement('input');
	fStatus.type	= 'hidden';
	fStatus.id		= 'FormStatus';
	fStatus.name	= 'FormStatus';
	form.appendChild(fStatus);
}

var klGeneric = {
	  defBusyMsg 		: 'Processing, Please Wait...'
	, busyMsg 			: ''
	, invokeField 		: ''
	, invokeFunction 	: ''
	
	, Goto : function (Url) { self.location.href = Url; }
	
	, Cancel : function (Url) {
		if (top.lightbox) { top.lightbox.hide(); }
		else { Goto(Url); }
	}
	
	, SelectState : false
	, SelectAll : function(Prefix, parentObj) 
	{ 
		var pObj = (typeof(parentObj!='undefined')) ? $(parentObj) : document.forms[0];
		var Chks = Form.getInputs(pObj,'checkbox'); 
		klGeneric.SelectState = !klGeneric.SelectState; 
		if (typeof(Prefix)=="undefined") { Prefix="Dl_"; }
		Chks.each( function(idx) { 
			ChkTxt=idx.name.substring(0,Prefix.length);
			if (ChkTxt==Prefix && !idx.disabled) { idx.checked = klGeneric.SelectState; }
		});
		if (klGeneric.SelectState) { klGeneric.allSelected = true; }
	}
	
	, SelectClass : function(className, parentObj) 
	{ 
		var pObj = (typeof(parentObj!='undefined')) ? $(parentObj) : document.forms[0];
		var Chks = document.getElementsByClassName(className, pObj); 
		klGeneric.SelectState =! klGeneric.SelectState; 
		Chks.each( function(idx) { 
			if (!idx.disabled) { idx.checked = klGeneric.SelectState; }
		});
		if (klGeneric.SelectState) { klGeneric.allSelected = true; }
	}
	
	, allSelected : false
	, markChk : function(Obj) {
		if (!Obj.checked) { klGeneric.allSelected = false; }
	}
	
	, getList : function (Prefix) {
		var itemList=[];
		var Elmnts=[];
		Chks = Form.getInputs(document.forms[0],'checkbox');
		Hidd = Form.getInputs(document.forms[0],'hidden');
		Elmnts = Chks.concat(Hidd);
		if (typeof(Prefix)=="undefined") { Prefix="Dl_"; }
		Elmnts.each( function(idx) { SubName=idx.name.substring(0,Prefix.length); 
		if ((idx.checked||String(idx.getAttribute('rel'))=='checked') && SubName==Prefix) { 
			itemList[itemList.length]=idx.name;
		}});
		return itemList;
	}
	
	, getClassList : function (parent, className) {
		
	}
	
	, getClassType : function (parent, tagName, Prefix) {
		var itemList = [];
		//var Elmnts = $(parent).getElementsByTagName(tagName);
		var Elmnts = $(parent).getElementsBySelector(tagName);
		if (Elmnts) {
			$A(Elmnts).each( function(idx) { SubName=idx.name.substring(0,Prefix.length); 
			if ((idx.checked||String(idx.getAttribute('rel'))=='checked') && SubName==Prefix) { 
				itemList[itemList.length]=idx.name;
			}});
			return itemList;
		}
	}

	, fileBrowse : function (folderType, path, title_str, fillField) {
		if (fillField!='undefined' && fillField!=='') { klGeneric.invokeField = fillField; }
		klGeneric.invokeFunction = 'fillField';
		klGeneric.overlay(true, function() { if (jsAlertMsg) { jsAlertMsg.closeHoverBox(); } klGeneric.overlay(false,null,0); },1);
		inlineFramePop(title_str, 'x_files.php?path='+path+'&local_Title='+title_str, 'FileBrowser', 700, 600, '');
	}
	
	, flvPrev : function (fileName, dispType, title_str, w, h) {
		var w 	= (w!='undefined' || w!=='') ? w : 640;
		var h 	= (h!='undefined' || h!=='') ? h : 480;
		jsAlert.show({trans:false, type:'iframe', width:w, height:h, src:'video.php?filename='+fileName, title:title_str});
	}
	
	, picPrev : function (Obj) {
		if (!$(Obj.field) || $F(Obj.field)==='')
		{
			jsAlert.show({trans:true,text:'No image selected!', title:'Error Found!', timer:2500});
		}
		else
		{
			var imgSrc = Obj.path+$F(Obj.field);
			var preloadImg = new Image();
			preloadImg.src = imgSrc;
			Event.observe(preloadImg, 'load', function() { 
				imgWidth 	= preloadImg.width;
				imgHeight	= preloadImg.height;
				jsAlert.show({trans:true, type:'img', width:imgWidth, height:imgHeight, src:imgSrc, objW:imgWidth, objH:imgHeight, title:'Preview'});
			}, false);
		}
	}
	
	, overID 		: 'overlay'
	, overZ			: 500
	, overlay 		: function (Obj) {
		var objID	= (Obj.ObjID)					? Obj.ObjID		: klGeneric.overID;
		var animate = (Obj.animate!='undefined') 	? Obj.animate 	: 1;
		var state 	= (Obj.state!='undefined') 		? Obj.state 	: 1;
		var func 	= (Obj.func)					? Obj.func 		: (state==0) ? ($(objID)) ? $(objID).remove() : null : null;
		var afterFunc = (Obj.afterFinish)			? Obj.afterFinish : null;
		
		if (state!=0)
		{
			if (!$(objID)) {
				var objBody 	= document.getElementsByTagName("body").item(0);
				var objOverlay 	= Builder.node('div',{id:objID, className:'overlay', style:'display:none;'});
				objOverlay.onclick = func;
				objBody.appendChild(objOverlay);
			}
			
			var arrayPageSize = klGeneric.getPageSize();
			$(objID).setStyle({height:arrayPageSize[1]+'px'});
			if (animate && Effect!=false) { new Effect.Appear(objID, { duration: 0.5, from: 0.0, to: 0.4, afterFinish:afterFunc }); }
			else { $(objID).show(); } 
		}
		else { 
			Try.these(afterFunc);
		}
	}
	
	, hideSelects: function(viz) {
		if (Prototype.Browser.IE) {
			selects = $A(document.getElementsByTagName('select'));
			selects.each(function(Obj) { Obj.setStyle({visibility:(viz) ? 'hidden' : 'visible'}); });
		}
	}
	
	, pageRefresh : function () {
		self.location.href = self.location.href;
	}
	
	, getPageSize : function () {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) { 
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		var pageHeight = 0;
		if(yScroll<windowHeight) 	{ pageHeight = windowHeight; } else { pageHeight = yScroll; }
		var pageWidth = 0;
		if(xScroll<windowWidth) 	{ pageWidth = windowWidth; } else { pageWidth = xScroll; }
		var arrayPageSize = [pageWidth,pageHeight,windowWidth,windowHeight]; 
		return arrayPageSize;
	}
	
	, getPageScroll : function() {
		var yScroll;
		if (self.pageYOffset) { yScroll = self.pageYOffset; }
		else { 
			if (document.documentElement && document.documentElement.scrollTop) { yScroll = document.documentElement.scrollTop; } 
			else { if (document.body) { yScroll = document.body.scrollTop; } } 
		}
		var arrayPageScroll = ['',yScroll];
		return arrayPageScroll;
	}

	, childInvoke : function (Val) {
		if (this.invokeFunction!=='')
		{
			switch (this.invokeFunction) {
				case "fillField":
					if (this.invokeField!=='') {
						$(this.invokeField).value = Val;
						if (jsAlertMsg) { jsAlertMsg.closeHoverBox(); }
					}
					break;
			}
		}
	}
	
	, reZebra : function (ObjID) {
		var ColorClass = '';
		var ObjList = $(ObjID).getElementsBySelector('li');
		ObjList.each( function(node) {
			ColorClass = (ColorClass!="odd") ? "odd" : "even";
			node.className = ColorClass;
			if (ColorClass=="odd") { $(node).observe("mouseout", function() { this.className = "odd"; }); }
			else { $(node).observe("mouseout", function() { this.className = "even"; }); }
		});
	}
	
	,cancelForm : function(divID, alertMsg) {
		function showAlert() { if (alertMsg) { inlineAlertMsg(alertMsg.title, alertMsg.msg, alertMsg.timer); } }
		Effect.SlideUp(divID, {duration:1.5, queue:'end', afterFinish:showAlert });
	}
	
	, inAction : function (txt, state) {
		var inActionTxt = '';
		if ($('inAction'))
		{
			switch (state)
			{
				case 0:
			 		new Effect.Fade('inAction', {duration:1.0});
					break;
				case 1:
					if (txt==='') { inActionTxt = 'Processing, Please Wait...'; }
					else { inActionTxt = txt; }
					$('inAction').innerHTML = inActionTxt;
					$('inAction').className = 'activity';
					$('inAction').show();
					break;
			}
		}
	}

	, fixedAction : function (txt, state) {
		var fixedActionTxt = '';
		if (!$('fixedAction'))
		{
			var divElmnt = Builder.node('div', {id:'fixedAction', className:'noactivity'});
			var objBody = document.getElementsByTagName("body").item(0);
			objBody.appendChild(divElmnt);
		}
		if (state==1)
		{
			if (txt==='') { inActionTxt = 'Processing, Please Wait...'; }
			else { inActionTxt = txt; }
			$('fixedAction').innerHTML = inActionTxt;
			$('fixedAction').className = 'fixedactivity';
		}
		else
		{
			$('fixedAction').className = 'noactivity';
		}
	}
	
	, toggleChk : function (Obj) {
		if ($(Obj).hasClassName('chk')) { $(Obj).removeClassName('chk'); }
		else { $(Obj).addClassName('chk'); }
	}
	
	, toggleAll : function (parentObj, togObj) {
		var Elmnts = $(parentObj).getElementsByClassName('member');
		if (togObj.hasClassName('togOn')) { 
			Elmnts.each(function(idx){ if (!$(idx).hasClassName('chk')) { $(idx).addClassName('chk'); } }); 
			$(togObj).className = 'togOff';
			$(togObj).innerHTML = 'Remove assignment from all groups';
		}
		else { 
			Elmnts.each(function(idx){ $(idx).removeClassName('chk'); }); 
			$(togObj).className = 'togOn';
			$(togObj).innerHTML = 'Assign to all groups';
		}
	}
	
	, getMemberIDs : function (parentObj) {
		var members = [];
		var Elmnts = $(parentObj).getElementsByClassName('member');
		Elmnts.each(function(idx) { if ($(idx).hasClassName('chk')) { members.push(idx.id.substring(1)); }});
		if (members.length>0) return members.join(",");
	}
	
	, hideAlerts : function (parentObj) {
		var alertClasses = ['info', 'alert'];
		alertClasses.each( function (className) {
			$(parentObj).getElementsByClassName(className).each(function (idx){ $(idx).hide(); });
		});
	}

	, switchTab : function (ObjID, parentID) {
		var Tabs = $A($(parentID).getElementsByTagName("li"));
		Tabs.each( function (node) { 
			if ($(node).className=='selected' && node.id!=("tab"+ObjID)) { 
				$(node).className = '';
				$($(node).id.substring(3)).setStyle({display:"none"}); 
			}
		});
		$("tab"+ObjID).className = 'selected';
		$(ObjID).setStyle({display:"block"}); 
	}
	
	, cookieSet : function(name,value,seconds) {
		if (seconds) {
			d = new Date();
			d.setTime(d.getTime() + (seconds * 1000));
			expiry = '; expires=' + d.toGMTString();
		} else { expiry = ''; }
		document.cookie = name + "=" + value + expiry + "; path=/";
	}
	
	, cookieGet : function(name) { 
		nameEQ = name + "=";
		ca = document.cookie.split(';');
		for (i=0; i<ca.length; i++) 
		{
			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
	}
	
	, cookieUnset: function(name) { Cookie.set(name,'',-1); }
}

//(C)webreflection.blogspot.com
function onContent(f){
var a,b=navigator.userAgent,d=document,w=window,
c="__onContent__",e="addEventListener",o="opera",r="readyState",
s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,"()}'></scr","ipt>");
w[c]=(function(o){return function(){w[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)f(o?o():o)}})(w[c]);
if(d[e])d[e]("DOMContentLoaded",w[c],false);
if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))
(function(){/loaded|complete/.test(d[r])?w[c]():setTimeout(arguments.callee,1)})();
else if(/MSIE/i.test(b))d.write(s);
};

try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

Ajax.Responders.register({
	onCreate : function() {
		Ajax.activeRequestCount++;
		var procTxt = (klGeneric.busyMsg.empty()) ? klGeneric.defBusyMsg : klGeneric.busyMsg;
		klGeneric.inAction(procTxt, 1);
	}
	
  , onComplete : function() {
		Ajax.activeRequestCount--;
		klGeneric.inAction('', 0);
  }
});


function toPage(page) {
	location.href = page;
}

function downRollover(which) {
	document.getElementById(which).style.backgroundPosition = "0 -28px";
}

function upRollout(which) {
	document.getElementById(which).style.backgroundPosition = "0 0";
}

function centerWindow(page,sWidth,sHeight){
		popWindow = window.open(page,'','toolbar=no,menubar=no,status=no,scrollbars=no,resizeable=no,width='+sWidth+',height='+sHeight);		
		popWindow.moveTo(((screen.width/2)-(sWidth/2)),((screen.height/2)-((sHeight/2)+20)))
		}