function returnObjById( id ) { 
	if (document.getElementById)  {
	var returnVar = document.getElementById(id); 
	} else if (document.all) {
	var returnVar = document.all[id];
	} else if (document.layers) {
	var returnVar = document.layers[id]; 
	}
	return returnVar; 
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof(window.onload) != 'function') {
	 window.onload = func;
	//	func();
	} else {
	//	oldonload();
	//	func();
	window.onload = function() {
	if (oldonload) {
	oldonload();
	}
	func();
	}
	}
}
function urlencode(str) {  
	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
} 
function urldecode(str) {
	return decodeURIComponent(str);
}
function alength(a){
	var i=0;
	for (x in a){
	i++;	
	}
	return i;
}
//function urlencode(str) {
//return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
//}
//function urldecode(str) {
//return unescape(str.replace(/\+/g, ' '));
//}


var clearmenu;
function dropIt(x){
	if(x){
	offmenu=0;
	clearTimeout(clearmenu);
	ob=returnObjById(x);
	ob.style.border = "1px solid #ccc";
    ob.style.opacity = 1;
    ob.style.MozOpacity = 1;
    ob.style.KhtmlOpacity = 1;
    ob.style.filter = "alpha(opacity=100)"; 
	returnObjById(x).style.marginLeft="0px";
	}
	return;
}
function liftIt(x,hidex,units){
	if(!hidex){
	hidex=-1999;
	} else {
	hidex=parseFloat(hidex);
	}
	if(!units){
	units='px';	
	}
	if(x){
		offmenu=1;
		//ob=returnObjById(x);
		//var ulen=units.length;
		//var curx=parseFloat(ob.style.marginLeft.substring(0,ob.style.marginLeft.length-ulen));
		returnObjById(x).style.border = "1px solid #FFF";
		clearmenu=setTimeout('fade("'+x+'",1,0,-0.08,5)',300);
		//curx='+curx+';ob=returnObjById("'+x+'");if(onmenu==0){while(curx > '+hidex+'){curx=curx-1;ob.style.marginLeft=curx+"'+units+'";}alert("final: "+ob.style.marginLeft)}',500);
	}
	return;
}

function unfade(el,x){
	if(!is_object(el)){
	ob=returnObjById(el);
	} else {
	ob=el;
	}
	if(x == undefined){
		ob.style.opacity = 1;
		ob.style.MozOpacity = 1;
		ob.style.KhtmlOpacity = 1;
		ob.style.filter = "alpha(opacity=" + 100 + ")"; 
		ob.style.visibility="visible";
	} else {
		ob.style.opacity = 0;
		ob.style.MozOpacity = 0;
		ob.style.KhtmlOpacity = 0;
		ob.style.filter = "alpha(opacity=" + 0 + ")"; 
		ob.style.visibility="hidden";
	}
}
function fade(el,f1,f2,incr,speed){
	
		ob=returnObjById(el);
		if(!incr){
		incr=-1;	
		}
		if(!speed){
			speed=10;
		}

		// if fading
		if(incr < 0){
			// if not faded yet
			if(f1 > f2){
				f1=f1+incr;
			    ob.style.opacity = f1;
			    ob.style.MozOpacity = f1;
			    ob.style.KhtmlOpacity = f1;
			    ob.style.filter = "alpha(opacity=" + f1*100 + ")"; 
				setTimeout('fade("'+el+'",'+f1+','+f2+','+incr+','+speed+')',speed);
			} else {
			    ob.style.opacity = f2;
			    ob.style.MozOpacity = f2;
			    ob.style.KhtmlOpacity = f2;
			    ob.style.filter = "alpha(opacity="+ f2*100 +")";
			    if(f2==0){
				    ob.style.visibility="hidden";
			    }
			}
		} else {
			if(f1 < f2){
				f1=f1+incr;
			    ob.style.opacity = f1;
			    ob.style.MozOpacity = f1;
			    ob.style.KhtmlOpacity = f1;
			    ob.style.filter = "alpha(opacity=" + f1*100 + ")"; 
				setTimeout('fade("'+el+'",'+f1+','+f2+','+incr+','+speed+')',speed);
			} else {
			    ob.style.opacity = f2;
			    ob.style.MozOpacity = f2;
			    ob.style.KhtmlOpacity = f2;
			    ob.style.filter = "alpha(opacity="+(f2*100)+")"; 
			    if(f2==1){
				    ob.style.visibility="visible";
			    }
			}
		}
}
function errpop(el,err){
	var er=returnObjById(el);
	er.innerHTML=err+'<a href="javascript:void(0);" class="bm_close" style="display:block;float:right;" onclick="this.parentNode.style.display=\'none\'">X</a>';
	er.style.display="block";
}

function flyin(fid,html,speed,step,y1,y2,r,cls){
	
	if(!returnObjById(fid)){
		var el = document.createElement('div');
		el.setAttribute("id",fid);
		if(cls == undefined){			
			el.style.position="absolute";
			el.style.zIndex=1000;
			el.style.width="35%";
			el.style.left="33%";
			el.style.border="1px solid #AAA";
			el.style.background="#FFF";
			el.style.textAlign="center";
			el.style.opacity = 0.9;
			el.style.MozOpacity = 0.9;
			el.style.KhtmlOpacity = 0.9;
			el.style.filter = "alpha(opacity=90)";
			el.style.height="90px";
		}
		var h=el.style.height;	
		if(!h){
			el.style.top="-250px";
		} else {
			el.style.top="-"+el.style.height;		
		}
		document.body.appendChild(el);
	} else {
		el=returnObjById(fid);
		el.style.display="block";
		unfade(fid);
	}

	if(cls != undefined){
		el.setAttribute("class",cls);
		el.setAttribute("className",cls);
	}

	el.innerHTML=html+'<a href="javascript:void(0);" class="bm_close" style="display:block;float:right;" onclick="this.parentNode.style.height=\'-250px\';this.parentNode.style.display=\'none\'">X</a>';
	// fade in
	//unfade(el);
	
	if(speed==undefined){
	speed=10;
	}
	if(step==undefined){
		step=-2; // negative means down the page
	}
	// set y1 and y2 if r=t top) else set x1 and x2 if r=s (side)
	if(r==undefined || r=='t'){
		if(y1==undefined){
		y1="-"+el.style.height;
		y1=y1.replace("px","");
		y1=y1.replace("em","");
		y1=y1.replace("%","");
		}
		if(y2==undefined){
		y2=0;
		}
	}
	movey(fid,speed,step,y1,y2);
	/*
	if(!r){
		var x1 = 0;
		var x2 = document.body.offsetWidth/2;
		if(el.style.width){
			el.style.left="-"+el.style.width;
		} else {
			el.style.left=-200;
		}
		el.style.right=\'\';
		el.style.display=\'block\';
		movex(el.id,speed,step,x1,x2,r);
	} else if (r == \'b\') {
		
		var y1 = -200;
		var y2 = 0;

		el.style.top=\'\';
		el.style.bottom="-200px";

		el.style.display=\'block\';
		movey(el.id,speed,step,y1,y2);
		
	} else {
		var x1 = document.body.offsetWidth;
		var x2 = document.body.offsetWidth/5;
		if(GetRight(el)){
			var x2 = GetRight(el);
		}
		el.style.left=\'\';
		el.style.right=0;
		el.style.display=\'block\';
		movex(el.id,speed,step,x1,x2,r);
	}
*/
}

function movex(el,speed,step,x1,x2,r){
	if(!is_object(el)){
	var el=returnObjById(el);
	}
	if(!r){
		if(x1 < x2){
		x1=x1+step;
		el.style.left=x1+"px";
		setTimeout("movex(\'"+el.id+"\',"+speed+","+step+","+x1+","+x2+")",speed);
		} else {
		el.style.left=100*(x1/document.body.offsetWidth)+"%";
		el.style.width='auto';
		el.style.height='auto';
		return false;
		}
	} else {
		if(x1 > x2){
		x1=x1-step;
		el.style.right=x1+"px";
		setTimeout("movex('"+el.id+"',"+speed+","+step+","+x1+","+x2+",1)",speed);
		} else {
		el.style.right=100*(x1/document.body.offsetWidth)+"%";
		el.style.width='auto';
		el.style.height='auto';
		return false;
		}
	}

}
var f=1;
function movey(id,speed,step,y1,y2,d){
	if(d == undefined){
		d='down';
	}
	if(f){
		f=0;
	}
	el=returnObjById(id);
	
	if(d=='down'){
		if(y1 < y2){
			if(y1/y2 == 0.8 && step > 0.1){
				step=step/4;
			}
			y1=y1-step;
			el.style.top=y1+"px";
			setTimeout("movey('"+id+"',"+speed+","+step+","+y1+","+y2+")",speed);
		} else {
		return false;
		}
	} else {
		if(y1 < y2){
			var el=returnObjById(elid);
			if(y1/y2 == 0.8 && step > 0.1){
				step=step/4;
			}
			y1=y1+step;
			el.style.bottom=y1+"px";
			setTimeout("movey('"+id+"',"+speed+","+step+","+y1+","+y2+")",speed);
		} else {
		return false;
		}
	}
}
// makes "iframes" an accessible object
if(document.getElemetsByTagName){
var iframes=document.getElementsByTagName("iframe");
} else if(document.frames) {
var iframes=document.frames;
} else if(window.frames){
var iframes=window.frames;
} else {
var iframes=null;	
}

/***
* Open in New Window
* Standards compliant as opposed to target="_blank". Also nicer to use rel=external rather than onclick attribute.
*	Opens link in new window. 
*	NOTE: "unknown runtime error" may occur when a link is within html comment tags, (e.g. <!-- <a href="" rel="external"> -->
*/
function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href")) {
	   
	if(anchor.getAttribute("class"))
	{
		if(anchor.getAttribute("class").match(/^(.* )?pop( .*)$/)) {
		 anchor.setAttribute("onclick","window.open(\'"+anchor.getAttribute("href")+"\',\'tweet\',\'menubar=1,resizable=1,width=350,height=250\');");
		 anchor.setAttribute("href","javascript:void(0);");
 		}
	}
 	else if(anchor.getAttribute("className"))
	{
		if(anchor.getAttribute("className").match(/^(.* )?pop( .*)$/)) {
		 anchor.setAttribute("onclick","window.open(\'"+anchor.getAttribute("href")+"\',\'tweet\',\'menubar=1,resizable=1,width=350,height=250\');");
		 anchor.setAttribute("href","javascript:void(0);");
 		}
 		
	}
	if(anchor.getAttribute("rel"))
	{
		if(anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "nofollow"){
		// anchor.innerHTML='<span>'+anchor.innerHTML+'</span><span class="pop">Opens in new window</span>';
		 anchor.setAttribute("target","_blank");
		// anchor.setAttribute("className","external");
		// anchor.setAttribute("class","external");
		} else if(anchor.getAttribute("rel").substr(0,3) == "pop") {
		 anchor.setAttribute("onclick","popup1(this,substr(anchor.getAttribute('rel'),4))");
		 anchor.setAttribute("href","javascript:void(0);");
		// anchor.setAttribute("className","external");
		// anchor.setAttribute("class","external");
		} else if(anchor.getAttribute("rel") == "top") {
		 anchor.setAttribute("target","_top");
		} else if(anchor.getAttribute("rel") == "newtab") {
			if(!anchor.title)
			{
				anchor.title=""
			}
			anchor.title += "(opens in a new window)";
		// anchor.innerHTML='<span>'+anchor.innerHTML+'</span><span class="pop">Opens in new tab</span>';
		 anchor.setAttribute("target","_new");
		// anchor.setAttribute("className","external");
		// anchor.setAttribute("class","external");
		}
	}
   }
 } 
}
externalLinks()
/****
	thickbox to grey out screen and place box in middle with content/image from an element on the page.
	Insert following <a href="javascript:void(0);" onclick="thickbox('elementId');">click</a>  <div id="elementId" style="display:none;">Content to go in thickbox</a>
	copyright 2007 Frank Forte
****/
function thickbox(el,t,l,w,h){
	if(!t){
		var t='15%';
	}
	if(!l){
		var l='25%';
	}
	if(!w){
		var w='50%';
	}
	if(!h){
		var h='65%';
	}

	if(typeof(el) == 'object'){
	tbox=el;
	} else {
	tbox=returnObjById(el).innerHTML;
	}
	
	returnObjById('dark').style.display='block';
	
	var lb=returnObjById('light');
	lb.style.top=t;
	lb.style.left=l;
	lb.style.width=w;
	lb.style.height=h;
	lb.innerHTML=tbox+'<a href="javascript:void(0);" onclick="closeThickbox()" class="closeThickbox">close</a>';
	lb.style.display='block';

	return true;

}
/****
	thickbox to grey out screen and place box in middle with content/image that you want
	Insert following <a href="javascript:void(0);" onclick="thickbox(getImage('/image_path/image.jpg','image name for alt tag'));">click</a>
	copyright 2007 Frank Forte
****/
function thickboxstring(content,t,l,w,h){
	if(!t){
		var t='15%';
	}
	if(!l){
		var l='25%';
	}
	if(!w){
		var w='50%';
	}
	if(!h){
		var h='65%';
	}
	if(typeof(content) != 'string'){
	return false;
	} else {
		var lb=returnObjById('light');
		lb.style.top=t;
		lb.style.left=l;
		lb.style.width=w;
		lb.style.height=h;
	lb.style.display='block';
	lb=returnObjById('dark').style.display='block';
	// note g modifier for global.  i modifier for case insensitive
	var closeButton='<a href="javascript:void(0);" onclick="closeThickbox()" class="closeThickbox">close</a>';
	content=content.replace(/<close ?\/>/,closeButton);
	returnObjById('light').innerHTML=content;
	}
	return true;
}
function closeThickbox(){
	returnObjById('light').style.display='none';
	returnObjById('dark').style.display='none';
}
function getImage(imgPath,alt){
	return '<img src="'+imgPath+'" alt="'+alt+'" />';
}

function ajaxFin(){
	returnObjById('ajaxWait').style.display='none';
}
function ajaxWait(u){
	
	var lb=returnObjById('ajaxWait');
	lb.style.display='block';
	lb.innerHTML='<a href="javascript:void(0);" onclick="this.parentNode.style.display=\'none\'" class="bm_close" style="float:right">X</a>processing... <br /><img src="/themes/minimalist/images/wait.gif" alt="" />';
	
/*
	// get cancel url
	if(u){
		var u='http://'+window.location.hostname+u+'?ajaxC='+getT();
	} else {
		var u='http://'+window.location.hostname+'/edit-api?ajaxC='+getT();
	}
*/
}

/****
   popup1 to open link in popup window
   Insert following in <a href> tag:  onClick="popup1(this, 'name1')
   Copyright  2007 Frank Forte.
**/
function popup1(window, windowname)
{
	var newwindow = '';
	if (!newwindow.closed && newwindow.location) {
	  newwindow.location.href = myform.href;
	} else {
	  newwindow=window.open(myform,windowname,'width=650,height=650,resizable=yes');
		if (!newwindow.opener) {newwindow.opener = self;}
	}
	if (window.focus) {newwindow.focus()}
	return false;
}

/****
   popup2 to open popup and send data to popup window
   Insert following in FORM tag:  onSubmit="popup2(this, 'name1')
   Copyright  2007 Frank Forte.
**/
function popup2(myform, windowname)
{
	var newwindow = '';  // initialize variable
	if (! window.focus)return true;  // test for the window.focus method - required to bring popup to front when clicked.
	newwindow=window.open('', windowname, 'height=400,width=600,scrollbars=yes,resizable=yes');  //just opens window, no data sent yet
	myform.target=windowname;  // sends form data to window
	newwindow.focus()  // needed to bring window to front if already open
	return true;  //useless, just in case.
}






// PHP to JavaScript functions
function in_array(needle, haystack, strict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function is_object( mixed_var ){
    if(mixed_var instanceof Array) {
        return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}
function is_int( mixed_var ) {

    if (typeof mixed_var !== 'number') {
        return false;
    }
 
    if (parseFloat(mixed_var) != parseInt(mixed_var)) {
        return false;
    }
    
    return true;
}
function isset () {
     
    var a=arguments, l=a.length, i=0;
     
    if (l===0) {
        throw new Error('Empty isset');
    }
     
    while (i!==l) {
        if (typeof(a[i])=='undefined' || a[i]===null) {
            return false;
        } else {
            i++;
        }
    }
    return true;
}






// Get base url
var url = document.location.href;
var xend = url.lastIndexOf("/") ; // get rid of last slash on base url
var base_url = url.substring(0, xend);
var ajax_get_error = false;

function ajax_get (url) {
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}
	// escape '+' sign (converted back to + on way back)
	url = url.replace(/\+/g,'aaplusaa');
	// get rid of null character
	url = url.replace(/%00/g,"");

	// Do Ajax
	ajax_do (url);

	return true;
}

function ajax_do (url) {
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}

	// Create new JS element
	var jsel = document.createElement('script');
	jsel.type = 'text/javascript';
	jsel.src = url;
	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild (jsel);

	return true;
}
function ajaxEdit(url,silent){
	if(!silent){
	ajaxWait(url);
	}
	url+='&ajaxT='+getT();
	ajax_get (url);
	
	// if timer runs out and alt executes, it should also send a "cancel" request
	// this tries to stop action or user will get
	// failed message, but action might occur anyway!

}

function getT() {
	t = new Date();
	return t.getTime();
}

// onkeyup="checkSubmit(event)"
function checkSubmit(e){
	var charCode;
	if(e && e.which){ //if which property of event object is supported (NN4)
	e = e
	charCode = e.which
	} else {
	e = event
	charCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(charCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
	submit_form();
	return false;
	} else {
	return true;
	}
}
