// shared.js script, version 2.1, 08-13-03

// global vars ======================================================
var loc = location.pathname.split('/');
loc.length = loc.length - 1; // removes filename 

var d = location.pathname.replace(/\/[^\/]*/g,'../'); // return one depth too deep, see next line for fix
d = d.substring(0,(d.length-3)); // turns this: ../../ into this ../
// /global vars ======================================================

// img preloads =====================================================
/*
hh = new Image(); hh.src = "/pix/hh.gif"; hh_roll = new Image(); hh_roll.src = "/pix/hh_roll.gif";
*/	
// /img preloads =====================================================

// popup window functions ==========================================
function popflex(URL,winName,W,H,scroll) {
	if (scroll == "no")
		{scroll = "scrollbars=no";}
	else if (scroll == "yes")
		{scroll = "scrollbars";}
	var popwin = window.open(URL, winName,"top=0,left=30,width="+W+",height="+H+",resizable=no,"+scroll+"");
	popwin.focus();
	}	
// /popup window functions ==========================================

function showhide_v3() {
	divid = arguments[0];
	classes = arguments[1];
	onstate = arguments[2];
	force = ''; //stops toggling between on and off and forces either on or off whenever clicked 
	if (typeof arguments[3] != 'undefined') {force = arguments[3];}
	//alert(divid+','+classes+','+onstate+','+force);

	var state = 'none';
	if (document.getElementById(divid).className == classes+' none' || 
	document.getElementById(divid).className == 'none') {
		var state = onstate;
		}
	if (force != '') {state = force;}
	document.getElementById(divid).className = classes+' '+state;
	return divid;
	}

var timeoutID;
function showhide_timeout() {
	interval = arguments[0];
	divid = arguments[1];
	classes = arguments[2];
	onstate = arguments[3];
	force = ''; //stops toggling between on and off and forces either on or off whenever clicked 
	if (typeof arguments[4] != 'undefined') {force = arguments[4];}
	
	window.clearTimeout(timeoutID);
 	delete timeoutID;
	timeoutID = setTimeout('showhide_v3("'+divid+'","'+classes+'","'+onstate+'","'+force+'")',interval);
	}

function slideshow(div_target,div_array,classtype) {
	//alert(div_target);
	//alert(div_array);

	var classes = '';
	if (typeof arguments[3] != 'undefined') {classes = arguments[3];}
	
	for(var n = 0; n < div_array.length; n++) {
		document.getElementById(div_array[n]).className = classes+' none';
		//alert(div_array[n]+' == '+div_target);
		if (div_array[n] == div_target) {
			document.getElementById(div_array[n]).className = classes+' '+classtype;
			}
		}
	}

function rotator(prefix,classtype,n,total,time) {
	/*
	call from html like:
	
	<p class="blurb" id="blurb_1">This is some text</p>
	<p class="none" id="blurb_2">and here is another line</p>
	<p class="none" id="blurb_3">and another</p>
	<p class="none" id="blurb_4">and finish.</p>
	<script type="text/javascript">rotator('blurb','block',1,4,50000);</script>
	*/
	for (var i = 1; i <= total; i++) {
		var divid = prefix+'_'+i;
		var display = 'none';
		if (i == n) {display = classtype;}
		document.getElementById(divid).className = display;
		}
	n++;
	if (n > total) {n = 1;}
	setTimeout('rotator("'+prefix+'","'+classtype+'",'+n+','+total+','+time+')',time);
	}

function setFades() {
	for(var n = 0;n < divs_to_fade.length; n++) {
		var divid = divs_to_fade[n][0];
		var start_time_up = divs_to_fade[n][1];
		var fade_speed_up = divs_to_fade[n][2];
		var start_time_down = divs_to_fade[n][3];
		var fade_speed_down = divs_to_fade[n][4];
		var amount = 5;
		if (typeof divs_to_fade[n][5] != 'undefined') {var amount = parseInt(divs_to_fade[n][5]);}
		
		fadeup_id = window.setTimeout("dissolve('"+divid+"',1,'down',"+fade_speed_up+","+amount+")",start_time_up);
		fadedown_id = window.setTimeout("dissolve('"+divid+"',100,'up',"+fade_speed_down+","+amount+")",start_time_down);
		if (n > 0) {
			//alert(document.getElementById(divs_to_fade[n][0]).innerHTML);
			var content = document.getElementById(divs_to_fade[n][0]).innerHTML;
			// add new div through the DOM with content, after fadeup and remove on fadedown
			
			}
		}
	}

function dissolve(obj_id,o_v,updown,speed,amount) {//alert(obj_id+','+o_v+','+updown+','+speed+','+amount);return false;
	if (typeof amount == 'undefined') {var amount = 5;}
	var obj = document.getElementById(obj_id);
	if (obj == null) {return;}
	if (obj.filters) {obj.style.filter = 'alpha(opacity='+o_v+')';}
	obj.style.KHTMLOpacity = o_v/100;
	obj.style.MozOpacity = o_v/100;
	obj.style.opacity = o_v/100;
	if (updown == 'down') {
		if (o_v <= 100) {
			o_v += parseInt(amount);
			dissolve_up_id = window.setTimeout('dissolve("'+obj_id+'",'+(o_v)+',"'+updown+'","'+speed+'","'+amount+'")',speed);
			}
		}
	if (updown == 'up') {
		if (o_v >= 0) {
			o_v -= parseInt(amount);
			dissolve_up_id = window.setTimeout('dissolve("'+obj_id+'",'+(o_v)+',"'+updown+'","'+speed+'","'+amount+'")',speed);
			}
		}
	}

function load_functions() {
	/*
	NOTE: this function MUST be called from body onload, ex: <body onload="load_functions();">
	this avoids render timing issues in Safari
	*/

	// functions to be run on every page:
	detect_height();
	
	// functions run on a per/page basis. loadfunctions is an array declared in /folder/pagescript.js, each array object represents a function declared either in /shared.js or /folder/pagescript.js
	if (typeof loadfunctions !== 'undefined') {
		for (var n=0; n < loadfunctions.length; n++) {
			eval(loadfunctions[n]+'();');
			}
		}
	}

function detect_height() {
	/*
	NOTE: this function MUST be called from body onload, ex: <body onload="detect_height();">
	this avoids render timing issues in Safari
	*/
	
	var is_ie6 = ('ActiveXObject' in window && !('XMLHttpRequest' in window));// returns true or false
	
	function numberorder(a,b) {return a-b;}

	var minheight = 400;

	// page boxes =======================================================
	var pagecontainer = document.getElementById('pagecontainer');
	
	var box0 = document.getElementById('pagebox0');
	var box1 = document.getElementById('pagebox1');
	var box2 = document.getElementById('pagebox2');
	var box3 = document.getElementById('pagebox3');
	var boxfull = document.getElementById('pageboxfull');
	
	var boxes = new Array();

	boxes[0] = 0;
	if (box0 != null) {
		box0_H = box0.offsetHeight;//alert(box0_H);
		boxes[0] = box0_H;
		}
	boxes[1] = 0;
	if (box1 != null) {
		box1_H = box1.offsetHeight;//alert(box1_H);
		boxes[1] = box1_H;
		}
	boxes[2] = 0;
	if (box2 != null) {
		box2_H = box2.offsetHeight;//alert(box2_H);
		boxes[2] = box2_H;
		}
	boxes[3] = 0;
	if (box3 != null) {
		box3_H = box3.offsetHeight;//alert(box3_H);
		boxes[3] = box3_H;
		}
	boxes[4] = 0;
	if (boxfull != null) {
		boxfull_H = boxfull.offsetHeight;//alert(boxfull_H);
		boxes[4] = boxfull_H;
		}
	
	// find out who's longest =====
	boxes.sort(numberorder);//alert(boxes);
	var longest = boxes[(boxes.length-1)];//alert(longest);
	if (longest < minheight) {longest = minheight;}
	// /find out who's longest =====
	
	// reset all containers to the longest value =====
	if (is_ie6) {
	 	if (box0 != null) {box0.style.setExpression("height",'"'+longest+'px"');}
		if (box1 != null) {box1.style.setExpression("height",'"'+longest+'px"');}
		if (box2 != null) {box2.style.setExpression("height",'"'+longest+'px"');}
		if (box3 != null) {box3.style.setExpression("height",'"'+longest+'px"');}
		if (boxfull != null) {boxfull.style.setExpression("height",'"'+longest+'px"');}
		if (pagecontainer != null) {pagecontainer.style.setExpression("height",'"'+longest+'px"');}
  	document.recalc(true);
		}
	else {
		if (box0 != null) {box0.style.minHeight = longest+'px';}
		if (box1 != null) {box1.style.minHeight = longest+'px';}
		if (box2 != null) {box2.style.minHeight = longest+'px';}
		if (box3 != null) {box3.style.minHeight = longest+'px';}
		if (boxfull != null) {boxfull.style.minHeight = longest+'px';}
		if (pagecontainer != null) {pagecontainer.style.minHeight = longest+'px';}
		}
	// reset all containers to the longest value =====
	// /page boxes =======================================================

	// shell boxes =======================================================
	var shellcontainer = document.getElementById('shellcontainer');
	
	var shellbox0 = document.getElementById('shellbox0');
	var shellbox1 = document.getElementById('shellbox1');
	var shellbox2 = document.getElementById('shellbox2');
	
	var shellboxes = new Array();

	shellboxes[0] = 0;
	if (box0 != null) {
		shellbox0_H = shellbox0.offsetHeight;//alert(shellbox0_H);
		shellboxes[0] = shellbox0_H;
		}
	shellboxes[1] = 0;
	if (shellbox1 != null) {
		shellbox1_H = shellbox1.offsetHeight;//alert(shellbox1_H);
		shellboxes[1] = shellbox1_H;
		}
	shellboxes[2] = 0;
	if (shellbox2 != null) {
		shellbox2_H = shellbox2.offsetHeight;//alert(shellbox2_H);
		shellboxes[2] = shellbox2_H;
		}
	
	// find out who's longest =====
	shellboxes.sort(numberorder);//alert(boxes);
	var longest = shellboxes[(shellboxes.length-1)];//alert(longest);
	if (longest < minheight) {longest = minheight;}
	// /find out who's longest =====
	
	// reset all shellboxes to the longest value =====
	if (is_ie6) {
 		if (shellbox0 != null) {shellbox0.style.setExpression("height",'"'+longest+'px"');}
		if (shellbox1 != null) {shellbox1.style.setExpression("height",'"'+longest+'px"');}
		if (shellbox2 != null) {shellbox2.style.setExpression("height",'"'+longest+'px"');}
		if (shellcontainer != null) {shellcontainer.style.setExpression("height",'"'+longest+'px"');}
		document.recalc(true);
		}
	else {
		if (shellbox0 != null) {shellbox0.style.minHeight = longest+'px';}
		if (shellbox1 != null) {shellbox1.style.minHeight = longest+'px';}
		if (shellbox2 != null) {shellbox2.style.minHeight = longest+'px';}
		if (shellcontainer != null) {shellcontainer.style.minHeight = longest+'px';}
		}
	// reset all shellboxes to the longest value =====
	// /shell boxes =======================================================

	return true;
	}

// AJAX functions =============================================================
function createXMLHttpRequest() {
	try{return new ActiveXObject("Msxml2.XMLHTTP");}
	catch (e) {}
	try{return new ActiveXObject("Microsoft.XMLHTTP");}
	catch (e) {}
	try{return new XMLHttpRequest();}
	catch (e) {}
	alert('XMLHttpRequest not supported');
	return null;
	}

function ajax_response_example(table,table_col,table_id) {
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {// request is finished
			if(xhr.status == 200) {
				if (xhr.responseText != '') {
					//alert(xhr.responseText);// return text
					//alert(xhr.responseXML);// return xml obj, NOTE: on process.php MUST set the header like: ob_start(); print $xml; header("Content-type: application/xhtml+xml"); ob_end_flush();
					}
				}
			else {alert('XMLHttpRequest error: '+xhr.responseText);}
			}
		}
	xhr.open('GET','/admintools/process.php?process=ajax_validate_row_exists&table='+table+'&table_col='+table_col+'&table_id='+table_id,true);
	xhr.send(null);
	}

function ajax_clear_element(handle) {
	if (typeof eval('document.detailform.'+handle+'_single') != 'undefined') {subselect(handle,'remove');}
	else {
		var element = eval('document.detailform.'+handle);
		element.value = '';
		element.focus();
		}
	}

function ajax_check_unique(inputid,table,col,rowid,value) {
	//alert(inputid+','+table+','+col+','+rowid+','+value);return false;
	trim(value);
	if (value == '') {return;}
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {// request is finished
			if(xhr.status == 200) {
				if (xhr.responseText != '') {
					//alert(xhr.responseText);
					ajax_print_msg(inputid,xhr.responseText);
					}
				}
			else {alert('XMLHttpRequest error: '+xhr.responseText);}
			}
		}
	var url = '/admintools/process.php?process=ajax_check_unique&table='+table+'&col='+col+'&id='+rowid+'&value='+value;
	//alert(url);return false;
	xhr.open('GET',url,true);
	xhr.send(null);
	}
function ajax_print_msg(inputid,msg) {
	alert(msg);
	document.getElementById(inputid).focus();
	return false;
	}
// /AJAX functions =============================================================

// utilities ==================================================================
function trim( str, charlist ) {
    // Strip whitespace (or other characters) from the beginning and end of a string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_trim/
    // +       version: 804.1015
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'

    var whitespace;
    
    if(!charlist){
        whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    } else{
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    }
  
	for (var i = 0; i < str.length; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
		str = str.substring(i);
		break;
		}
	}
	for (i = str.length - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
    	}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}// }}}

// {{{ ltrim
function ltrim ( str, charlist ) {
    // Strip whitespace (or other characters) from the beginning of a string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_ltrim/
    // +       version: 804.1015
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: ltrim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld    '

    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    var re = new RegExp('^[' + charlist + ']+', 'g');
    return str.replace(re, '');
}// }}}

// {{{ base64_decode
function base64_decode( data ) {
    // Decodes data encoded with MIME base64
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_base64_decode/
    // +       version: 804.1015
    // +   original by: Tyler Akins (http://rumkin.com)
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'


    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64)      enc += String.fromCharCode(o1);
        else if (h4 == 64) enc += String.fromCharCode(o1, o2);
        else               enc += String.fromCharCode(o1, o2, o3);
    } while (i < data.length);

    return enc;
}// }}}

// {{{ base64_encode
function base64_encode( data ) {
    // Encodes data with MIME base64
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_base64_encode/
    // +       version: 804.1015
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Bayron Guevara
    // *     example 1: base64_encode('Kevin van Zonneveld');
    // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}// }}}
// /utilities ==================================================================
