//Ussage: <A href='bob.html' onClick='return popUp('url', 600, 420);'>
function popUp(URL, w, h) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h + "');");
	return false;
}

clicks = 0;
function isFirstClick() {
	clicks++;
	if(clicks > 1) return false;
	return true;
}

function printOrderForm(text) {
	text=document
	print(text)
}

//**********
//** Script by John Resig
//** Modified by the good folks at QuirksMode.org
//** http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
//** End Script by John Resig
//**********



//**********
//** Script by Simon Willison
//** http://www.webreference.com/programming/javascript/onloads/
//**
//** Replace "window.onload=" with a call to this function
//**
//** Example 1:
//**   addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);  //instead of window.onload = nameOfSomeFunctionToRunOnPageLoad;
//**
//** Example 2:
//**	addLoadEvent(function() {
//**		//more code to run on page load 
//**	});
//**
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
//** End Script by Simon Willison
//**********






//Takes a query string starting with a '?' and returns a keyValue array with
//the variables as key and the values as values.
//The most common use would be you might feed this parseQueryStr(location.search)
//Example str: '?page=1&search=asd&view=main_view'
//
//To check if a variable exists, store use this code:
//		var keyValuePairs = parseQueryStr(location.search);
//		if (keyValuePairs['z'] != null) {
//			//Var 'z' exists and is a valid variable in the query string
//		} else {
//			//Var 'z' does not exist because it is not a valid variable in the query string
//		}
function parseQueryStr(query_str) {
	var str = null;
	var keyValuePairs = new Array();

	//If query_str is a string longer than length 1 and the first char is a '?', remove the '?' and put the result in str
	if ((query_str.length > 1) && (query_str.charAt(0) == '?')) str = query_str.substring(1, query_str.length);

	//If str is not null
	if (str) {
		//Split str apart by '&' characters and store them in split_amp
		var split_amp = str.split("&");

		//Go through each split_amp
		for(var i=0; i < split_amp.length; i++) {

			//Now split apart by '=' characters
			var split_eq = split_amp[i].split("=");

			//If we have the right number of values, store them in our keyValuePairs array
			if (split_eq.length == 2) {
				keyValuePairs[split_eq[0]] = split_eq[1];
			}
		}
	}

	return keyValuePairs;
}

//Examples:
//	requireLogin(window.opener) (This will change the url in the parent window of a popup)
//	requireLogin(window) (This will change the url in the current window)
function requireLogin(win) {
	var str = '';
	var keyValuePairs = parseQueryStr(win.location.search);

	keyValuePairs['cmd'] = 'require_login';

	for ( var i in keyValuePairs )
	{
		str += ((str == '') ? '?' : '&') + i + '=' + keyValuePairs[i];
	}

	win.location.search = str;
}



function htmlEncodeAttr(str) {
	if (typeof(str) == "string") {
		str = str.replace(/&/g, "&amp;"); // must do &amp; first 
		str = str.replace(/"/g, "&quot;");
		str = str.replace(/</g, "&lt;");
		str = str.replace(/>/g, "&gt;");
		return '"' + str + '"';
	}

	return '';
}

function htmlEncodeOutput(str) {
	if (typeof(str) == "string") {
		str = str.replace(/&/g, "&amp;"); // must do &amp; first 
//		str = str.replace(/"/g, "&quot;");
		str = str.replace(/</g, "&lt;");
		str = str.replace(/>/g, "&gt;");
		return str;
	}

	return '';
}


