//	Tekdroid Javascript Common Functions (these need to be in all pages)

window.onload = function() {
	trace('trace start');
}

function cookieCreate(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function cookieRead(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function cookieDelete(name) {
	cookieCreate(name,"",-1);
}

function form_submit(form_id) {
	//	generic function to allow submission of form via ajax from 'onchange'
    var whichform = document.forms[form_id];
    if (whichform) { HTML_AJAX.formSubmit(whichform, 'target'); }
}

function getElementsByStyleClass (className) {
	var all = document.all ? document.all : document.getElementsByTagName('*');
	var elements = new Array();
	for (var e = 0; e < all.length; e++)
		if (all[e].className == className)
			elements[elements.length] = all[e];
	return elements;
}

//	Credit: http://www.quirksmode.org/js/dhtmloptions.html

function getObjById(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers) {
		this.obj = getObjNN4(document,name);
		this.style = this.obj;
	}
}

function getObjNN4(obj,name) {
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++) {
		if (x[i].id == name)
			foundLayer = x[i];
	else if (x[i].layers.length)
		var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function is_in(haystack, needle) {
	if (haystack.indexOf(needle) >= 0) { return 1; }
	else { return 0; }
}

function is_ini(haystack, needle) {
	if (haystack.toLowerCase().indexOf(needle) >= 0) { return 1; }
	else { return 0; }
}

function link_confirm(url,message) {
	if (! message) { message = 'Please Confirm'; }
	if (confirm(message)) { location.href=url; }
}

function link_input(url, text, force) {
	var myval = prompt(text, '');
	if (myval || force) { location.href = url + myval; }
}

function link_popup(url, name) {
	var scr_x = screen.width*0.50;
	var scr_y = screen.height*0.50;
	var win = window.open(url, name, "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=yes,width="+scr_x+",height="+scr_y+"");
	if (win) { win.focus() }
}

function link_popup_big(url, name) {
	var scr_x = screen.width*0.75;
	var scr_y = screen.height*0.75;
	var win = window.open(url, name, "scrollbars=yes,width=" + scr_x + ",height=" + scr_y + ",resizable");
	if (win) { win.focus() }
}

function link_popup_confirm(url,message) {
	if (! message) { message = 'Please Confirm'; }
	if (confirm(message)) { link_popup(url); }
}

function link_popup_confirm_big(url,message) {
	if (! message) { message = 'Please Confirm'; }
	if (confirm(message)) { link_popup_big(url); }
}

function link_popup_form(myform,name) {
	if (! window.focus) return true;
	var scr_x = screen.width*0.50;
	var scr_y = screen.height*0.50;
	var win = window.open('', name, "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=yes,width="+scr_x+",height="+scr_y+"");
	myform.target=name;
	return true;
}

function link_popup_input(url, text, name, force) {
	var myval = prompt(text, '');
	if (myval || force) { link_popup(url + myval, name); }
}

function link_popup_input_big(url, text, name, force) {
	var myval = prompt(text, '');
	if (myval || force) { link_popup_big(url + myval, name); }
}

function markup_add(num, pc) { return round_price(num * (1 + pc / 100)); }

function num_add(num1, num2) { return num1 * 1 + num2 * 1; }

function parseUri(sourceUri){
	//	Credit: http://badassery.blogspot.com/2007/02/parseuri-split-urls-in-javascript.html
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
	var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
	var uri = {};

	for(var i = 0; i < 10; i++) { uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : ""); }

	if(uri.directoryPath.length > 0) { uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/"); }

	return uri;
}

function post_data(myurl, myarray) {
	var myform = document.createElement('form');
	myform.method = 'POST';
	myform.action = myurl;

	for (var mykey in myarray) {
		var myinput = document.createElement('input') ;
		myinput.setAttribute('name', mykey) ;
		myinput.setAttribute('value', myarray[mykey]);
		myform.appendChild(myinput);
	}

	document.body.appendChild(myform);
	myform.submit();
	document.body.removeChild(myform);
}

function round_price(num) { return Math.round(num *100)/100; }

function send_form(form_id) {
	var which_form = document.getElementById(form_id);
	which_form.submit();
}

function trace( msg ) { if( typeof( jsTrace ) != 'undefined' ) { jsTrace.send( msg ); } }

function toggle_expansion(div_id,img_id,img1,img2) {
	var which_div = document.getElementById(div_id);
	if (! which_div) { return; }

	if (which_div.className=='blockexpand') { which_div.className='blockcollapse'; }
	else { which_div.className='blockexpand'; }

	if (img_id) {
		var which_img = document.getElementById(img_id);
		if (which_img && img1 && img2) {
			var uri = parseUri(which_img.src);
			var prefix = uri['protocol'] + '://' + uri['domain'] + uri['directoryPath'];
			if(uri['fileName'] == img1) { which_img.src = prefix + img2; }
			else { which_img.src = prefix + img1; }
		}
	}
}

function toggle_visibility(div_id) {
	var which_div = document.getElementById(div_id);
	if (which_div) {
		if (which_div.style.visibility == 'hidden') { which_div.style.visibility='visible'; }
		else { which_div.style.visibility='hidden'; }
	}
}



