/* Copyright Muchachamaca SCP, Spain - All rights reserved */

function GetParams (query)
{
	var result = new Object();
	var parms = query.split('&');
	for (var i = 0; i < parms.length; i++)
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) 
		{
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			result[key] = val;
		}
	}
	return result;
}
docParams = GetParams (window.location.search.substring(1));

/* ------------------------------- */


function mailAntiSpam (user, domain, tld)
{
	document.write("<a href=\"mailto:" + user + "@" + domain + "." + tld + "\">" + user + "@" + domain + "." + tld + "</a>")
}


function scramble (tag, domain, name, subject, params, content)
{
	if (subject==null) subject = "";
	if (params==null) params = "";
	if (content==null) content = "";
		
	var a = "@";
	var e = name;
	var g = domain;
	e += a + g;

	if (content == "=")
		content = e;

	if (subject != "")
		e += "?subject=" + subject;
		
	document.write ("<" + tag + " ");
	document.write ("href=\"mai");
	document.write ("lto:");
	document.write (e + "\" ");
	document.write (params + ">");
	
	document.write (content);

	if (tag == "a" || tag == "img")
		document.write ("</" + tag + ">");
}


/* ------------------------------- */


function FormatAmount (inAmount)
{
	var sample = (1000.1).toLocaleString(); // 1,000.1
	var theThousandsSep = sample[1];
	var theDecimalSep = sample[5];
	if (theThousandsSep != ',' && theThousandsSep > '.')
		theThousandsSep = '';
	if (theDecimalSep != ',' && theDecimalSep > '.')
		theDecimalSep = '.';
	
	return FormatAmount2 (inAmount, theDecimalSep, theThousandsSep);
}


function FormatAmount2 (inAmount, theDecimalSep, theThousandsSep)
{
	var theString = "";

	// round extra decimals
	theAmount = Math.round(inAmount * 100);
	
	// get the part after the decimal
	theFrac = theAmount % 100;

	// get the part before the decimal
	theInt = Math.round ((theAmount - theFrac) / 100);
	
	if (theInt == 0)
		theString = "0";
	else
	{
		while (theInt > 0)
		{
			theVal = theInt % 1000;
			if (theInt >= 1000)
			{
				if (theVal < 10)
					theVal = "00" + theVal;
				else if (theVal < 100)
					theVal = "0" + theVal;
			}
			
			if (theString == "")
				theString = theVal;
			else
				theString = theVal + theThousandsSep + theString;
			theInt = (theInt - theVal) / 1000;
		}
	}
	
	theString += theDecimalSep;
	
	if (theFrac < 10) theString += "0";
	theString += theFrac;
	
	return theString;
}


/* ------------------------------- */


var thePreloadImageUrls = new Array();
var thePreloadImages = new Array();

function addPreloadImage (inImageUrl)
{
	thePreloadImageUrls.push (inImageUrl);
}

function preloadImages()
{
	for (i = 0; i < thePreloadImageUrls.length; i++)
	{
		thePreloadImages[i] = new Image;
		thePreloadImages[i].src = thePreloadImageUrls[i];
	}
}

function clearPreloadImages()
{
	thePreloadImageUrls = new Array();
	thePreloadImages = new Array();
}


/* ------------------------------- */


function toggleVisibility (inId)
{
	obj = document.getElementById (inId);
	if (obj)
		obj.style.display = (obj.style.display == "none") ? "" : "none";
}

function hideElement (inId)
{
	obj = document.getElementById (inId);
	if (obj)
		obj.style.display = "none";
}

function showElement (inId)
{
	obj = document.getElementById (inId);
	if (obj)
		obj.style.display = "";
}


/* ------------------------------- */


function selectOption (id, inValue)
{
	obj = document.getElementById (id);
	if (obj)
	{
		for (i = 0; i < obj.options.length; i++)
		{
			if (obj.options[i].value == inValue)
			{
				obj.options[i].selected = true;
				break;
			}
		}
	}
}


function getPopupValue (id, _default)
{
	obj = document.getElementById(id);
	if (!obj || obj.selectedIndex < 0)
		value = _default;
	else
		value = obj.options[obj.selectedIndex].value;
	return value;
}


/* ------------------------------- */


function setRadioValue (name, value)
{
	radios = document.getElementsByName(name);
	for (var i = 0; i < radios.length; i++)
		if (radios[i].value == value)
			return radios[i].checked = 1;
}


function getRadioValue (name)
{
	radios = document.getElementsByName(name);
	for (var i = 0; i < radios.length; i++)
		if (radios[i].checked)
			return radios[i].value;
}


/* ------------------------------- */

function changeInputType (oldObject, newType)	// editbox_focus helper
{
	// IE does not allow simply changing the input type of an element
	// so we have to replace the whole element with a new one
	
	if (!oldObject.parentNode)
		return oldObject;
	
	var newObject = document.createElement('input');
	newObject.type = newType;
	for (i = 0; i < oldObject.attributes.length; i++)
		if (oldObject.attributes[i].specified && oldObject.attributes[i].nodeName != 'type') // user defined and not 'type'
			newObject.setAttribute(oldObject.attributes[i].nodeName, oldObject.attributes[i].nodeValue);
	oldObject.parentNode.replaceChild(newObject,oldObject);
	return newObject;
}

function editbox_focus (obj,inFocus,inText)
{
	if (inFocus)
	{
		obj.style.color = "black";
		if (obj.value == inText)
			obj.value = "";
		if (obj.getAttribute('rel') == 'password' && obj.type != 'password')
		{
			obj = changeInputType (obj, 'password');
			obj.focus();
		}
	}
	else if (obj.value == "")
	{
		if (obj.getAttribute('rel') == 'password' && obj.type != 'text')
			obj = changeInputType (obj, 'text');
		obj.value = inText;
		obj.style.color = "#aaa";
	}
}

/* ------------------------------- */

String.prototype.trim = function ()
{
	return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

Array.prototype.removeByValue = function (value, count)
{
	if (count == null)
		count = 1;
	else if (count == 0)
		count = 0x7FFFFFFF;
	
	for (var i = 0; i < this.length && count > 0; i++)
	{
		if (this[i] == value)
		{
			this.splice(i,1);
			count--;
		}
	}
}

Array.prototype.contains = function(value)
{
    var i = this.length;
    while (i--) {
        if (this[i] == value) {
            return true;
        }
    }
    return false;
}

/* ------------------------------- */


function addLoadEvent (func)
{	
	var oldFunc = window.onload;
	if (typeof window.onload != 'function')
		window.onload = func;
	else
		window.onload = function() { oldFunc(); func(); }
}

function addUnloadEvent (func)
{	
	var oldFunc = window.onunload;
	if (typeof window.onunload != 'function')
		window.onunload = func;
	else
		window.onunload = function() { oldFunc(); func(); }
}


/* ------------------------------- */

function setCookie (name, value, expiredays, path, domain, secure)
{
	var expires = new Date();
	expires.setDate(expires.getDate()+expiredays);
	
	document.cookie = name + "=" + escape(value) +
		(expires ? "; expires=" + expires.toGMTString() : "") +
		(path ? "; path=" + path : "") +
		(domain ? "; domain=" + domain : "") +
		(secure ? "; secure" : "");
}

function getCookie (c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf (c_name + "=");
		if (c_start!=-1)
		{
			c_start = c_start + c_name.length+1;
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return null;
}

/* ------------------------------- */

// event stuff

//*** This code is copyright 2003 by Gavin Kistner, !@phrogz.net
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)

//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);

function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

// blur anchors after focus to avoid ugly highlighting

function MyBlur (event)
{
	var evt = window.event || event; //evt evaluates to window.event or inexplicit e object, depending on which one is defined
	if (!evt.target) //if event obj doesn't support e.target, presume it does e.srcElement
		evt.target=evt.srcElement //extend obj with custom e.target prop
	evt.target.blur();
	return true;
}

function addMenuBlur()
{
	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++)
		AttachEvent (anchors[i], 'focus', MyBlur, false);
}

function disableInput()
{
	var buttons = document.getElementsByTagName('input');
	for (i = 0; i < buttons.length; i++)
		buttons[i].disabled="disabled";
}


/* ------------------------------- */


function dump(arr)
{
	var dumped_text = "";
	
	if (typeof(arr) == 'object')
	{
		dumped_text += "Object {\n";
		for (var item in arr) {
			var value = arr[item];
	
			if (typeof(value) == 'object')
				dumped_text += "    '" + item + "' => ...\n";
			else
				dumped_text += "    '" + item + "' => \"" + value + "\"\n";
		}
		dumped_text += "}\n";
	}
	else
		dumped_text = typeof(arr) + ": " + arr;
		
	return dumped_text;
}


/* ------------------------------- */

function testmenow (p1)
{
	alert (p1);
}


