// 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 ../

function vp_width() {// returns viewport width
	var vp_width = document.body.clientWidth;// for IE and iPhone
	if(document.documentElement.clientWidth) {var vp_width = document.documentElement.clientWidth;}
	else if (window.innerWidth) {var vp_width = window.innerWidth;}
	//alert(vp_width);
	return vp_width;
	}
// /global vars ======================================================

// img preloads =====================================================
//hh = new Image(); hh.src = "/pix/hh.gif"; hh_roll = new Image(); hh_roll.src = "/pix/hh_roll.gif";
bg_trans = new Image();
bg_trans.src = "/pix/bg_trans.png";
// /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];}

	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 n = 1;
function crossfade(speed) {
	dissolve(divs_to_fade[n],100,'up',speed);
	n++;
	if (n == divs_to_fade.length) {n = 0;}
	dissolve(divs_to_fade[n],1,'down',speed);
	}
function dissolve(obj_id,o_v,updown,speed) {
	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 += 5;
			window.setTimeout('dissolve("'+obj_id+'",'+(o_v)+',"'+updown+'","'+speed+'")',speed);
			}
		}
	if (updown == 'up') {
		if (o_v >= 0) {
			o_v -= 5;
			window.setTimeout('dissolve("'+obj_id+'",'+(o_v)+',"'+updown+'","'+speed+'")',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:
	var test_width = vp_width();
	//if (test_width > 480) {detect_height();}// if in smart phone, do NOT resize columns!!!!
	if (test_width <= 480) {update_orientation();}// if in smart phone, resize big graphics
	
	// 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 update_orientation() {
	window.scrollTo(0, 1);// turns off address bar in iPhone
	//alert(vp_width());
	var width = (vp_width()-6);// 3px pad on left and right
	document.getElementById('shell').style.width = width+'px';//alert(document.getElementById('shell').style.width);
	//move_nav();// in /_includes.php/nav.php
	if (typeof in_wordpress != 'undefined' && in_wordpress == true) {
		img_resize_ajax('img_resize',(width-8));
		}
	else {
		img_resize((width-8));// 1px stroke on all images adn 3px pad on all boxes
		}
	img_resize2((width-8));
	}

var resized = new Array();
function img_resize(width) {
	var n = -1;
	for(var k in document.images) {
		var v = document.images[k];
		if (typeof v.width == 'undefined') {continue;}
		n++;
		//alert(v.width);
		resize = false;
		for(var i = 0; i < resized.length;i++) {
			if (resized[i] == v.src) {
				resize = true;
				break;
				}
			}
		if (parseInt(v.width) > width) {resize = true;}
		if (resize) {
			// calc new height according to width =====
			var orig_w = v.width;//alert(orig_w);
			var orig_h = v.height;//alert(orig_h);
			
			var x_ratio = (width/orig_w);
			var new_w = width;//alert(new_w);
			var new_h = Math.ceil(x_ratio * orig_h);//alert(new_h);
			
			v.width = new_w;
			v.height = new_h;
			resized[n] = v.src;
			}// /if (resize) {
		}// /for(var k in document.images) {
	//alert(resized);
	}
var resized2 = new Array();
function img_resize2(width) {
	//alert(width+' -- '+parseInt((width*0.50)));return;
	var n = -1;
	for(var k in document.images) {
		var v = document.images[k];
		if (typeof v.width == 'undefined') {continue;}
		n++;
		var resize = false;
		for(var i = 0; i < resized2.length;i++) {
			if (resized2[i] == v.src) {
				resize = true;
				break;
				}
			}
		if (parseInt(v.width) < width && parseInt(v.width) > parseInt((width*0.50))) {resize = true;}
		if (resize) {
			// calc new height according to width =====
			var orig_w = v.width;
			var orig_h = v.height;
			
			var x_ratio = (parseInt((width*0.50))/orig_w);
			var new_w = parseInt((width*0.50));//alert(new_w);
			var new_h = Math.ceil(x_ratio * orig_h);//alert(new_h);
			
			v.width = new_w;
			v.height = new_h;
			resized2[n] = v.src;
			}// /if (resize) {
		}// /for(var k in document.images) {
	//alert(resized2);
	}
function img_resize_ajax(process,width) {
	for(var k in document.images) {
		var v = document.images[k];
		if (typeof v.width == 'undefined') {continue;}
		img_resize_ajax_async_request(process,document.images[k],width,v.src);
		}
	}
function img_resize_ajax_async_request(process,obj,width,src) {
	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
					img_resize_ajax_async_response(obj,xhr.responseText);// CANNOT return value!!!!!
					//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();
					//my_ajax_function(xhr.responseXML);// CANNOT return value!!!!!
					}
				}
			else {
				//alert('XMLHttpRequest error: '+xhr.responseText);
				}
			}
		}
	// send with GET =====
	var url = '/_includes.php/process_img_resize.php?process='+process+'&width='+width+'&src='+src;
	xhr.open('GET',url,true);
	xhr.send(null);
	}
function img_resize_ajax_async_response(obj,response) {
	//alert(response);
	eval('var data = '+response+';');//alert(data);
	//for(var k in data) {alert(data[k]);}
	obj.width = data['width'];
	obj.height = data['height'];
	}

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 pagecontainer = document.getElementById('pagecontainer');
	
	var box0 = document.getElementById('pagebox0');
	var box0bot = document.getElementById('pagebox0bottom');
	var box1 = document.getElementById('pagebox1');
	var box1bot = document.getElementById('pagebox1bottom');
	var box2 = document.getElementById('pagebox2');
	var box2bot = document.getElementById('pagebox2bottom');
	var box3 = document.getElementById('pagebox3');
	var box3bot = document.getElementById('pagebox3bottom');
	var boxfull = document.getElementById('pageboxfull');
	var boxfullbot = document.getElementById('pageboxfullbottom');
	
	var boxes = new Array();

	boxes[0] = 0;
	if (box0bot != null) {
		box0_H = box0bot.offsetTop;//alert(box0_H);
		boxes[0] = box0_H;
		}
	boxes[1] = 0;
	if (box1bot != null) {
		box1_H = box1bot.offsetTop;//alert(box1_H);
		boxes[1] = box1_H;
		}
	boxes[2] = 0;
	if (box2bot != null) {
		box2_H = box2bot.offsetTop;//alert(box2_H);
		boxes[2] = box2_H;
		}
	boxes[3] = 0;
	if (box3bot != null) {
		box3_H = box3bot.offsetTop;//alert(box3_H);
		boxes[3] = box3_H;
		}
	boxes[4] = 0;
	if (boxfullbot != null) {
		boxfull_H = boxfullbot.offsetTop;//alert(boxfull_H);
		boxes[4] = boxfull_H;
		}
	
	// find out who's longest =====
	function numberorder(a,b) {return a-b;}
	boxes.sort(numberorder);//alert(boxes);
	var longest = boxes[(boxes.length-1)];//alert(longest);
	if (longest < 400) {longest = 400;}
	// /find out who's longest =====
	
	// reset all containers to the longest value =====
	if (box0bot != null) {box0.style.minHeight = longest+'px';}
	if (box1bot != null) {box1.style.minHeight = longest+'px';}
	if (box2bot != null) {box2.style.minHeight = longest+'px';}
	if (box3bot != null) {box3.style.minHeight = longest+'px';}
	if (boxfullbot != null) {boxfull.style.minHeight = longest+'px';}
	pagecontainer.style.minHeight = longest+'px';
	if (document.all) {
 		if (box0bot != null) {box0.style.setExpression("height",'"'+longest+'px"');}
 		if (box1bot != null) {box1.style.setExpression("height",'"'+longest+'px"');}
 		if (box2bot != null) {box2.style.setExpression("height",'"'+longest+'px"');}
 		if (box3bot != null) {box3.style.setExpression("height",'"'+longest+'px"');}
 		if (boxfullbot != null) {boxfull.style.setExpression("height",'"'+longest+'px"');}
 		pagecontainer.style.setExpression("height",'"'+longest+'px"');
	  document.recalc(true);
		}
	// reset all containers to the longest value =====
	
	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_sync_response_example(table,table_col,table_id) {
	// use this most of the time, stops the js/browser till the server returns the data, kinda like an alert
	// setting the last argument in xhr.open() below to false makes it synchronous
	var xhr = createXMLHttpRequest();
	if (xhr) {
		var url = '/admintools/process.php?process=ajax_validate_row_exists&table='+table+'&table_col='+table_col+'&table_id='+table_id;
		xhr.open('GET',url,false);
		xhr.send(null);
		//alert(xhr.responseText);// return text
		return xhr.responseText;
		//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();
		//return xhr.responseXML;
		}
	else {
		alert('An AJAX connection could not be established.');
		}
	}
function ajax_async_response_example(table,table_col,table_id) {
	// do not use this UNLESS the js/browser needs to continue while the server does its task
	// setting the last argument in xhr.open() below to true makes it synchronous, MUST test the response and then  
	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
					my_ajax_function(xhr.responseText);// CANNOT return value!!!!!
					//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();
					//my_ajax_function(xhr.responseXML);// CANNOT return value!!!!!
					}
				}
			else {alert('XMLHttpRequest error: '+xhr.responseText);}
			}
		}
	// send with GET =====
	var url = '/admintools/process.php?process=ajax_validate_row_exists&table='+table+'&table_col='+table_col+'&table_id='+table_id;
	xhr.open('GET',url,true);
	xhr.send(null);

	// send with POST =====
/*
	var url = '/admintools/process.php';
	var params = 'process=ajax_validate_row_exists&table='+table+'&table_col='+table_col+'&table_id='+table_id;
	xhr.open('POST',url,true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.send(params);
*/
	}

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);}
			}
		}
	// send with GET =====
	xhr.open('GET','/admintools/process.php?process=ajax_check_unique&table='+table+'&col='+col+'&id='+rowid+'&value='+value,true);
	xhr.send(null);
	// send with POST =====
/*
	var url = '/admintools/process.php';
	var params = 'process=ajax_check_unique&table='+table+'&col='+col+'&id='+rowid+'&value='+value;
	xhr.open('POST',url,true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.send(params);
*/
	}
function ajax_print_msg(inputid,msg) {
	alert(msg);
	document.getElementById(inputid).focus();
	return false;
	}
// /AJAX functions =============================================================
