/****************************************************************************
	Administration Site Base Client Script
	Zac Hester
****************************************************************************/


/**
 * Detect IE for work-around code.
 */
var detect = navigator.userAgent.toLowerCase();
var isIE = detect.indexOf('msie') != -1;




/**
 * Page initialization.  Called when the body is loaded. 
 */

function init() {
	//Parse any URL query components.
	//parse_get();

	if(window.init_popup) {
		init_popup();
	}

	if(window.nav_init) {
		nav_init();
	}
}


/**
 * Generic form checking script.
 */
function check_form() {
	var frm = document.getElementById('admin_form');
	var req = frm.elements['form_check'];

	//Allow the form if it doesn't have the standard ID.
	if(!frm) { return(true); }

	//Check for required fields list for this form.
	if(req && req.value.length) {

		//Build a list of fields for checking.
		var field_list = req.value.split(',');

		//Run through each field.
		for(var i = 0; i < field_list.length; ++i) {

			//Check for valid form element.
			if(frm.elements[field_list[i]]
				&& frm.elements[field_list[i]].value) {

				//Reference the element data and trim.
				var test = frm.elements[field_list[i]].value.trim();
	
				//Make sure the element has some info.
				if(test.length == 0) {
					alert('You must complete all required fields'
						+' before submitting the form.');
					frm.elements[field_list[i]].focus();
					return(false);
				}
			}
			else {
				alert('You must complete all required fields'
					+' before submitting the form.');
				return(false);
			}
		}
	}

	//Check for some password fields in the form.
	if(frm.elements['pass1'] && frm.elements['pass2']) {

		//Reference the password field's conditioned data.
		var test1 = frm.elements['pass1'].value.trim();
		var test2 = frm.elements['pass2'].value.trim();

		//Test for specified password and matching fields.
		if(test1.length > 0 && test1 != test2) {
			alert('When entering a password, both password fields'
				+' must match.');
			return(false);
		}
	}

	//If we make it this far, the form is okay.
	return(true);
}


/**
 * Delete confirmation and requesting.
 */
function confirm_delete(objid,referer,type) {

	//Confirm the deletion of this object.
	if(confirm('Are you sure you want to delete this?')) {

		//Run the processor to delete this object from the current module.
		window.location = 'process.php?action=delete'
			+'&id='+objid+'&referer='+referer+'&type='+type;
	}

	//They didn't confirm the deletion.
	return(false);
}


function request_delete(type,referer,objid) {

	//Confirm the deletion of this object.
	if(confirm('Are you sure you want to delete this?')) {

		//Run the processor to delete this object from the current module.
		window.location = 'process.php?action=delete'
			+'&id='+objid+'&referer='+referer+'&type='+type;
	}

	//They didn't confirm the deletion.
	return(false);
}



/**
 * Quickjump handler.
 */
function quickjump() {
	var sel = document.getElementById('qj');
	for(var i = 0; i < sel.options.length; ++i) {
		if(sel.options[i].selected == true
			&& sel.options[i].value.trim().length != 0) {
			window.location = sel.options[i].value;
			return(true);
		}
	}
	sel.options[0].selected = true;
	return(false);
}



/**
 * Simple whitespace trimmer.
 */
function trim(string) {
	var new_string = string.replace(/^\s+/g, '');
	new_string = new_string.replace(/\s+$/g, '');
	return(new_string);
}


/**
 * Add my own trim method to the String object.
 */
String.prototype.trim = function() {
    var str = this.replace(/^\s+/g, '');
    str = str.replace(/\s+$/g, '');
    return(str);
}


/**
 * Establish a global array to store the query data.
 */
var _GET = new Array();

/**
 * Parses the current page's GET query into a global array.
 *
 * @author Zac Hester <zac@zacharyhester.com>
 * @date 2005-10-28
 */
function parse_get() {

	//A temporary place to keep array items.
	var temp = new Array();

	//Pull the GET query of the current page.
	var get = window.location.search.substring(1);

	//Check for any query.
	if(get.length == 0) {
		return(false);
	}

	//Check for multiple query pairs.
	else if(get.indexOf('&') != -1) {

		//Break the query on the standard pair separator.
		var pairs = get.split('&');

		//Run through each key=value pair.
		for(var i = 0; i < pairs.length; ++i) {

			//Break the pair on the standard assignment separator.
			temp = pairs[i].split('=');

			//Load the data in the global array.
			parse_get_assign_pair(temp[0], temp[1]);
		}
	}

	//This query only has one pair.
	else {

		//Break the pair on the standard assignment separator.
		temp = get.split('=');

		//Load the data in the global array.
		parse_get_assign_pair(temp[0], temp[1]);
	}
}

/**
 * Assigns a parsed key=value pair to the global array.
 *
 * @author Zac Hester <zac@zacharyhester.com>
 * @date 2005-10-28
 *
 * @param key An extracted left-hand value from a query pair.
 * @param val An extracted right-hand value from a query pair.
 */
function parse_get_assign_pair(key, val) {

	//Every string from the query needs to be checked for plus-sign as
	//  spaces (PHP-style), and then unescape()d like usual.
	key = key.replace('+', ' ');
	key = unescape(key);
	val = val.replace('+', ' ');
	val = unescape(val);

	//Put the data in the global array.
	_GET[key] = val;
}

function init_popup() {
	var photo_album = document.getElementById('photo_album');
	if(photo_album) {
		var ans = photo_album.getElementsByTagName('a');
		var current_class = '';
		for(var i=0; i < ans.length; ++i) {
			if(isIE) {
				current_class = ans[i].className;
			}
			else {
				current_class = ans[i].getAttribute('class');
			}
			if(ans[i] && current_class == 'album_photo') {
				ans[i].onclick = handle_anchor;
			}
		}
	}
}

function handle_anchor() {
	
	var img_url = this.getAttribute('href');
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'overlay');
	overlay.innerHTML = '&nbsp;';
	overlay.onclick = hide_popup;
	
	var popup = document.createElement('div');
	popup.setAttribute('id', 'popup');

	popup.onclick = hide_popup;
	var the_img = document.createElement('img');
	
	the_img.setAttribute('src',img_url);
	the_img.onclick = hide_popup;
	popup.appendChild(the_img);
	
	document.body.appendChild(overlay);
	document.body.appendChild(popup);
	
	return false;
}

function hide_popup() {
	document.body.removeChild(document.getElementById('overlay'));
	document.body.removeChild(document.getElementById('popup'));
}
