/*
	E-commerce site version 4.0.1
	-----------------------------
	2008-09-22: Cross-Updated Common Javascript Files. -AJ
	2008-01-24: added a bit of code to ow() for version 1.01
*/
var _COMMON_VERSION_=4.00;
var _COMMON_INSTANCE=1;

//----------------------- functions ----------------------------
function g(x){ return document.getElementById(x); }
function jsEval(x){
	try{
		eval(x);
	}catch(e){
		var v1=''; var v2=''; var v3=''; var str=''; var descr=false;
		for(j in e){
			if(j=='stack' || j=='number')continue;
			if((j=='message' || j=='description') && descr)continue
			if(j=='description' || j=='message')descr=true;
			str+=(j+': '+e[j])+"\n";
		}
		if(str) alert(str);
	}
	return false;
}
function gCookie(ck){
	var cVal = document.cookie;
	var cStart = cVal.indexOf(" " + ck + "=");
	if(cStart==-1)	cStart = cVal.indexOf(ck + "=");
	if(cStart == -1){
		cVal = null;
	}else{
		cStart = cVal.indexOf("=", cStart) + 1;
		var cEnd = cVal.indexOf(';', cStart);
		if(cEnd==-1) cEnd=cVal.length;
		cVal = unescape(cVal.substring(cStart,cEnd));
	}
	return cVal;
}
function sCookie(cName,cVal,cExp,cPath){
	cVal = escape(cVal);
	if(typeof cExp == 'undefined'){
		var nw = new Date();
		nw.setMonth(nw.getMonth() + 6);
		var expiry= ";expires="+nw.toGMTString();
	}else if(cExp==0){
		var expiry='';
	}else{
		var expiry=';expires='+cExp;
	}
	if(typeof cPath == 'undefined'){
		var path=';Path=/';
	}else{
		var path = ";Path="+cPath;
	}
	//if(testmode==1) alert(cName + "="+cVal + expiry+path);
	document.cookie = cName + "="+cVal + expiry+path;
}
function ow(href,w,p,rand){
	/* currently v1.01 2008-01-24
	   work to do:
	determine if the window is open and if there is unsaved data in it
	indicate in open window that a new url is coming up (gray out over window and text, moving icon pending)
	remember positions only if it works
	
	v1.01 - added the ability to add a random variable to the query (&ResourceToken) for setting quasi resource for a new object
	*/
	var params;
	if(typeof w=='undefined'){
		//develop - this is a "new" and distinct object
	}
	var reg=/^[0-9]+,[0-9]+$/;
	if(p.match(reg)){
		var a=p.split(',');
		params='width='+a[0]+',height='+a[1]+',resizable,scrollbars,menubar,statusbar';
	}else{
		params=p;
	}

	//for new objects, creates a unique resource token for generating a "quasi resource" in the database, before the object is saved and recognized as an actual object - this allows for example to add a resource and associate sub-resources with it before the resource has an "official" ID number
	if(typeof rand!=='undefined'){
		rnd= ( parseInt(rand)>8000 ? rand : generate_date()+generate_rand(5) );
		href+=(href.indexOf('?')==-1 ? '?' : '&')+'ResourceToken='+rnd;
		//by default right now, we open this window in an absolutely new window each time - override the value of w
		w=rnd;
	}
	wins[w]=window.open(href,w,params);
	wins[w].focus();
	return false;
}
function in_array(needle,haystack){//about the same as the function in PHP
	strCase=1; //insensitive (2=sensitive)
	loc=10; //value (11=key)
	for(i=2;i<arguments.length;i++){
		arguments[i]=='sensitive' || arguments[i]==2?strCase=2:'';
		arguments[i]==11 || arguments[i]=='key'?loc=11:'';
	}
	//find the value
	for(j in haystack){
		if(strCase==1){
			if(loc==10 && (haystack[j]+'').toLowerCase()==(needle+'').toLowerCase()){
				return true;
			}else if(loc==11 && (j+'').toLowerCase()==(needle+'').toLowerCase()){
				return true;
			}
		}else{
			if((loc==10 && haystack[j]==needle) || (loc==11 && j==needle)){
				return true;
			}
		}
	}
	return false;
}
function delete_element(array, n){//removes an element from an array by key value
  //delete the nth element of array
  var length = array.length;
  if (n >= length || n<0)
    return;
  for (var i=n; i<length-1; i++)
    array[i] = array[i+1];
  array.length--;
}
function sizeof(x){//this gets count of elements in an array
	var f=0;for(var g in x){f++;}return f;}
function unset(val, arrName){
	//both passed values are strings
	var unset_buffer = new Array();
	eval('var arr = '+arrName);
	for(var x in arr){
		if(x.toLowerCase()!==(val+'').toLowerCase()){
			unset_buffer[x]=arr[x];
		}
	}
	eval(arrName+' = new Array();');
	eval(arrName+' = unset_buffer;');
	return false;
}
function addCartVis(ID,qty){
	for(i in currentProducts){
		if(currentProducts[i]==ID){
			g('prod'+currentProducts[i]).style.backgroundColor='#E1DDDD';
			g('added'+currentProducts[i]).style.visibility='visible';
		}else{
			g('prod'+currentProducts[i]).style.backgroundColor='#FFF';
			g('added'+currentProducts[i]).style.visibility='hidden';
		}
	}
}
function generate_date(x){
	if(typeof x=='undefined') x=3; //range 0,1,2,3
	var y=new Date();
	var l=(y.getYear().toString().length);
	var s=y.getYear().toString().substr(l-1,1);
	s+=str_pad(y.getMonth()+1,2) + str_pad(y.getDate(),2);
	s+=str_pad(y.getHours(),2) + str_pad(y.getMinutes(),2) + str_pad(y.getSeconds(),2);
	return s;
}
function generate_rand(d){
	if(typeof d=='undefined')d=8;
	var b=10;
	var s=Math.random();
	for(i=1;i<=d-1;i++)b=b*10;
	s=str_pad(parseInt(s*b).toString(),d,0,2);
	return s;
}
function str_pad(str, nbr, pad, posn){
	str=str.toString();
	if(str.length>=nbr)return str;
	if(typeof pad=='undefined')pad='0'; //pad left
	if(typeof posn=='undefined')posn=1; //pad left
	var p='';
	for(i=1; i<=nbr-str.length; i++)p+=pad;
	var o=(posn==1 ? p+''+str : str+''+p);
	return o;
}
/* GetSourceElement: find the source element of given event. */
function GetSourceElement(event){
	//---- version 1.0, by Yahav, last edit 2005-01-10
	if ((typeof event == "undefined") || (!event))
		event = window.event;
	
	if (typeof event.srcElement != "undefined")
		return event.srcElement;
	
	var node = event.target;
	while (node.nodeType != node.ELEMENT_NODE)
		node = node.parentNode;
	return node;
}
function GetAbsolutePosition(element, P) {
	//alert(element.nodeName+"\n"+element.offsetLeft+"\n"+element.offsetTop);
	if (typeof P == "undefined")P = new Point(0, 0);
	if (element.nodeName.toLowerCase() == "body") return P;
	if (!((element.nodeName.toLowerCase() == "tr")&&(element.parentNode.nodeName.toLowerCase() == "tbody")))
		P.x += element.offsetLeft;
	if (!((element.nodeName.toLowerCase() == "td")&&(element.parentNode.nodeName.toLowerCase() == "tr")))
		P.y += element.offsetTop;
	return GetAbsolutePosition(element.parentNode, P);
}
function Point(x, y) {
	this.x = x;
	this.y = y;
}
function adminAccessMin(){
	var d=g('amMin').innerHTML;
	if(d=='+'){
		g('amMin').innerHTML='-';
		g('amMin').title='Minimize Admin Toolbar';
		g('amBody').style.display='block';
		sCookie('amBody','block');
	}else{
		g('amMin').innerHTML='+';
		g('amMin').title='Expand Admin Toolbar';
		g('amBody').style.display='none';
		sCookie('amBody','none');
	}
	return false;
}
function switchVersions(loc){
	var from=(loc=='2'?'/gf2b/':'/gf2/');
	var   to=(loc=='2'?'/gf2/':'/gf2b/');
	window.location=g('location').value.replace(from,to);
}
function refreshList(){
	window.location+='';
}
function getPageOffset(point){
	//http://dev.communityserver.com/forums/t/477071.aspx
	if(typeof window.pageXOffset == 'number'){
		//Netscape compliant
		return (point=='left'?window.pageXOffset:window.pageYOffset);
	}else if (document.body && (document.body.scrollLeft || document.body.scrollTop)){
		//DOM compliant
		return (point=='left'?document.body.scrollLeft:document.body.scrollTop);
	}else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
		//IE6 standards compliant mode
		return (point=='left'?document.documentElement.scrollLeft:document.documentElement.scrollTop);
	}
}
function showFeatured(file,node){
	g('Background').innerHTML='/images/'+node+(node?'/':'')+file;
	g('backgroundImage').value=file;
	g('backgroundImageFolder').value=folder;
}
function refreshComponent(id,refreshParams,query){
	/*
	2008-09-19
	Created 2008-09-18 by Samuel - takes either a 2nd parameter of refresh parameters that have to be sent to exe, or a 3 "prepared" querystring.  If neither parameter is passed, the function looks in the object for attribute refreshParams="Events_ID:ID" (e.g.)
	if stored in the data object component, refreshParams is a comma-separated list of parameters that need to be passed to recreate that component in context.  If any param is aliased, enter it as Events_ID:ID - this function will look for ID but pass it as Events_ID.
	the function looks first in any hidden field by that id for the value, then in a global value by that name.  If neither is present the system will return an error - this enforces (I think) good consistent coding
	
	*/
	if((a=g(id))==null) alert('function refreshComponent()[Error 1]: non-existent component id '+id);
	//now get refreshparameters
	if(query){
		//OK - we have an explicit query string	
	}else if(typeof refreshParams!=='undefined'){
		//OK - we must get params from the environment
	}else if((refreshParams=a.getAttribute('refreshparams'))==null){
		alert('function refreshComponent()[Error 2]: non-existent component id '+id);
	}
	if(!query && !refreshParams)return false;
	if(!query){
		var query='';
		var a=refreshParams.split(',');
		for(var j in a){
			//search in hidden field by that id, then global by that name
			if(a[j].match(':')){
				a[j]=a[j].split(':');
				pass=a[j][0];
				field=a[j][1];
			}else{
				pass=a[j];
				field=a[j];
			}
			if(g(field)!==null){
				//build query string
				query+='&'+pass+'='+escape(g(field).value);
			}else{
				test='undefined';
				eval('test=typeof '+field+';');
				if(test!=='undefined'){
					//build query string
					query+='&'+pass+'='+escape(g(field).value);
				}else{
					alert('function refreshComponent()[Error 3]: unable to find refresh parameter "'+field+'"'+(pass!=field?' (will be passed as '+pass+')':''));
					return false;
				}
			}
		}
	}
	if(!query){
		alert('function refreshComponent()[Error 4]: unable to successfully generate query string');
		return false;
	}
	if(query.substring(0,1)!=='&')query='&'+query;
	window.open('/console/resources/bais_01_exe.php?mode=refreshComponent&version=4.0&component='+id+query,'w2');
}
