/*****************************************************************\
|*  In the name of Allah, The Most Gracious, The Most Merciful   *|
|*****************************************************************|
|*          ####  #     ####  ####  ####  ####  ####             *|
|*          #  #  #     #     #  #  #  #  #  #  #                *|
|*          ####  #     ###   ####  ####  #  #  ####             *|
|*          #  #  #     #     #     #     #  #     #             *|
|*    www.  #  #  ####  ####  #     #     ####  ####  .net       *|
|*****************************************************************|
|*  aleppos version 1.0.0
|*****************************************************************|
|* Copyright ©2005–2006 Aleppos Network Ltd. All Rights Reserved *|
|*This file may not be redistributed in whole or significant part*|
|* http://www.aleppos.net           ALEPPOS IS NOT FREE SOFTWARE *|
\*****************************************************************/

// ==================================================================
// Browser detection and limitation workarounds

// Define the browser we have instead of multiple calls throughout the file
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = ((userAgent.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_ie7    = ((is_ie) && (userAgent.indexOf('msie 7.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);

// Catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

// Is the visiting browser compatible with AJAX?
var AJAX_Compatible = false;

// Help out old versions of IE that don't understand element.style.cursor = 'pointer'
var pointer_cursor = (is_ie ? 'hand' : 'pointer');


// ==================================================================
String.prototype.AL_length = function()
{
	return (is_ie && this.indexOf('\n') != -1) ? this.replace(/\r?\n/g, '_').length : this.length;
};

if ('1234'.substr(-2, 2) == '12') // (which would be incorrect)
{
	String.prototype.substr_orig = String.prototype.substr;

	String.prototype.substr = function(start, length)
	{
		return this.substr_orig( (start < 0 ? this.length + start : start), length);
	};
};


// ==================================================================
function array_pop(a)
{
	if (typeof a != 'object' || !a.length)
	{
		return null;
	}
	else
	{
		var response = a[a.length - 1];
		a.length--;
		return response;
	}
};

if (typeof Array.prototype.shift === 'undefined')
{
	Array.prototype.shift = function()
	{
		for(var i = 0, b = this[0], l = this.length-1; i < l; i++)
		{
			this[i] = this[i + 1];
		}
		this.length--;
		return b;
	};
};

function array_push(a, values)
{
	for (var i = 1; i < arguments.length; i++)
	{
		a[a.length] = arguments[i];
	}
	return a.length;
};


// ==================================================================
function fetch_object(idname)
{
	if (document.getElementById)
	{
		return document.getElementById(idname);
	}
	else if (document.all)
	{
		return document.all[idname];
	}
	else if (document.layers)
	{
		return document.layers[idname];
	}
	else
	{
		return null;
	}
};

function fetch_tags(parentobj, tag)
{
	if (parentobj == null)
	{
		return new Array();
	}
	else if (typeof parentobj.getElementsByTagName != 'undefined')
	{
		return parentobj.getElementsByTagName(tag);
	}
	else if (parentobj.all && parentobj.all.tags)
	{
		return parentobj.all.tags(tag);
	}
	else
	{
		return new Array();
	}
};

function fetch_tag_count(parentobj, tag)
{
	return fetch_tags(parentobj, tag).length;
};


/*****************************************************************\
|* AL_PHP_Emulator
\*****************************************************************/

/**
* PHP Function Emulator Class
*/
function AL_PHP_Emulator()
{
};

// ==================================================================
AL_PHP_Emulator.prototype.stripos = function(haystack, needle, offset)
{
	if (typeof offset == 'undefined')
	{
		offset = 0;
	}

	index = haystack.toLowerCase().indexOf(needle.toLowerCase(), offset);

	return (index == -1 ? false : index);
};

AL_PHP_Emulator.prototype.ltrim = function(str)
{
	return str.replace(/^\s+/g, '');
};

AL_PHP_Emulator.prototype.rtrim = function(str)
{
	return str.replace(/(\s+)$/g, '');
};

AL_PHP_Emulator.prototype.trim = function(str)
{
	return this.ltrim(this.rtrim(str));
};

AL_PHP_Emulator.prototype.preg_quote = function(str)
{
	// replace + { } ( ) [ ] | / ? ^ $ \ . = ! < > : * with backslash+character
	return str.replace(/(\+|\{|\}|\(|\)|\[|\]|\||\/|\?|\^|\$|\\|\.|\=|\!|\<|\>|\:|\*)/g, "\\$1");
};

AL_PHP_Emulator.prototype.match_all = function(string, regex)
{
	var gmatch = string.match(RegExp(regex, "gim"));
	if (gmatch)
	{
		var matches = new Array();

		var iregex = new RegExp(regex, "im");
		for (var i = 0; i < gmatch.length; i++)
		{
			matches[matches.length] = gmatch[i].match(iregex);
		}

		return matches;
	}
	else
	{
		return false;
	}
};

AL_PHP_Emulator.prototype.unhtmlspecialchars = function(str)
{
	f = new Array(/&lt;/g, /&gt;/g, /&quot;/g, /&amp;/g);
	r = new Array('<', '>', '"', '&');

	for (var i in f)
	{
		str = str.replace(f[i], r[i]);
	}

	return str;
};

AL_PHP_Emulator.prototype.htmlspecialchars = function(str)
{
	//var f = new Array(/&(?!#[0-9]+;)/g, /</g, />/g, /"/g);
	var f = new Array(
		(is_mac && is_ie ? new RegExp('&', 'g') : new RegExp('&(?!#[0-9]+;)', 'g')),
		new RegExp('<', 'g'),
		new RegExp('>', 'g'),
		new RegExp('"', 'g')
	);
	var r = new Array(
		'&amp;',
		'&lt;',
		'&gt;',
		'&quot;'
	);

	for (var i = 0; i < f.length; i++)
	{
		str = str.replace(f[i], r[i]);
	}

	return str;
};

AL_PHP_Emulator.prototype.unescape_cdata = function(str)
{
	var r1 = /<\=\!\=\[\=C\=D\=A\=T\=A\=\[/g;
	var r2 = /\]\=\]\=>/g;

	return str.replace(r1, '<![CDATA[').replace(r2, ']]>');
};

AL_PHP_Emulator.prototype.in_array = function(ineedle, haystack, caseinsensitive)
{
	var needle = new String(ineedle);

	if (caseinsensitive)
	{
		needle = needle.toLowerCase();
		for (var i in haystack)
		{
			if (haystack[i].toLowerCase() == needle)
			{
				return i;
			}
		}
	}
	else
	{
		for (var i in haystack)
		{
			if (haystack[i] == needle)
			{
				return i;
			}
		}
	}
	return -1;
};

AL_PHP_Emulator.prototype.str_pad = function(text, length, padstring)
{
	text = new String(text);
	padstring = new String(padstring);

	if (text.length < length)
	{
		padtext = new String(padstring);

		while (padtext.length < (length - text.length))
		{
			padtext += padstring;
		}

		text = padtext.substr(0, (length - text.length)) + text;
	}

	return text;
};

AL_PHP_Emulator.prototype.urlencode = function(text)
{
	text = escape(text.toString()).replace(/\+/g, "%2B");

	// this escapes 128 - 255, as JS uses the unicode code points for them.
	// This causes problems with submitting text via AJAX with the UTF-8 charset.
	var matches = text.match(/(%([0-9A-F]{2}))/gi);
	if (matches)
	{
		for (var matchid = 0; matchid < matches.length; matchid++)
		{
			var code = matches[matchid].substring(1,3);
			if (parseInt(code, 16) >= 128)
			{
				text = text.replace(matches[matchid], '%u00' + code);
			}
		}
	}

	// %25 gets translated to % by PHP, so if you have %25u1234,
	// we see it as %u1234 and it gets translated. So make it %u0025u1234,
	// which will print as %u1234!
	text = text.replace('%25', '%u0025');

	return text;
};

AL_PHP_Emulator.prototype.ucfirst = function(str, cutoff)
{
	if (typeof cutoff != 'undefined')
	{
		var cutpos = str.indexOf(cutoff);
		if (cutpos > 0)
		{
			str = str.substr(0, cutpos);
		}
	}

	str = str.split(' ');
	for (var i = 0; i < str.length; i++)
	{
		str[i] = str[i].substr(0, 1).toUpperCase() + str[i].substr(1);
	}
	return str.join(' ');
};

// initialize the PHP emulator
var PHP = new AL_PHP_Emulator();


// ==================================================================
function set_cookie(name, value, expires)
{
	document.cookie = name + '=' + escape(value) + '; path=/' + (typeof expires != 'undefined' ? '; expires=' + expires.toGMTString() : '');
};

function delete_cookie(name)
{
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT' +  '; path=/';
};

function fetch_cookie(name)
{
	cookie_name = name + '=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (';', value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
};


// ==================================================================
function fetch_offset(obj)
{
	var left_offset = obj.offsetLeft;
	var top_offset = obj.offsetTop;

	while ((obj = obj.offsetParent) != null)
	{
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}

	return { 'left' : left_offset, 'top' : top_offset };
};


// ==================================================================
//change the opacity for different browsers
function change_opacity(obj, opacity)
{
	// In NN / Moz, when opacity is 100, flickering occurs. If the opacity is reduced to 99.99, this does not happen.
	// This code will achieve this, if uncommented
	if (is_moz && opacity == 100)
	{
		opacity = 99.99;
	}

	if (opacity <= 100 && opacity >= 0)
	{
		// Safari and Konqueror:
		obj.style.KhtmlOpacity = opacity / 100;
		// Old Mozilla and Firefox:
		obj.style.MozOpacity = opacity / 100;
		// CSS3 opacity for browsers that support it:
		obj.style.opacity = opacity / 100;
		// IE filter opacity:
		obj.style.filter = "alpha(opacity=" + opacity + ")";
	}
};


/*****************************************************************\
|* AL_AJAX_Handler
\*****************************************************************/
function AL_AJAX_Handler(async)
{
	this.async = async ? true : false;
};

// ==================================================================
AL_AJAX_Handler.prototype.init = function()
{

 	if (window.ActiveXObject)
	{
		var control = new Array(
			'Microsoft.XMLHTTP',
			'MSXML2.XMLHTTP',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.5.0'
		);

		for (var i = 0; i < control.length; i++)
		{
			try
			{
				this.handler = eval("new" + " Active" + "X" + "Object(control[i])");
				return true;
			}
			catch(e)
			{
			}
		}
	}

	if (window.createRequest)
	{
		try
		{
			this.handler = window.createRequest();
			return true;
		}
		catch(e)
		{
		}
	}

	if (typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			this.handler = new XMLHttpRequest();
			return (this.handler.setRequestHeader ? true : false);
		}
		catch(e)
		{
			return false;
		}
	}

	return false;
};

AL_AJAX_Handler.prototype.is_compatible = function()
{
	var tester = AL_AJAX_Handler.prototype.init();
	if (tester)
	{
		delete_cookie('ajax_compatible');
	}
	else
	{
		expires = new Date();
		expires.setTime(expires.getTime() + (1000 * 86400 * 365));
		set_cookie('ajax_compatible', 'false', expires);
	}
	
	return tester;
};

AL_AJAX_Handler.prototype.onreadystatechange = function(event)
{
	if (!this.handler)
	{
		if  (!this.init())
		{
			return false;
		}
	}
	if (typeof event == 'function')
	{
		this.handler.onreadystatechange = event;
	}
	else
	{
		alert('XML Sender OnReadyState event is not a function');
	}
};

AL_AJAX_Handler.prototype.not_ready = function()
{
	return (this.handler.readyState && (this.handler.readyState < 4));
};

AL_AJAX_Handler.prototype.send = function(desturl, datastream)
{
	if (!this.handler)
	{
		if (!this.init())
		{
			return false;
		}
	}
	if (!this.not_ready())
	{
		this.handler.open('POST', desturl, this.async);
		this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.handler.send(datastream);

		if (!this.async && this.handler.readyState == 4 && this.handler.status == 200)
		{
			return true;
		}
	}
	return false;
};

AL_AJAX_Handler.prototype.fetch_data = function(xml_node)
{
	if (xml_node && xml_node.firstChild && xml_node.firstChild.nodeValue)
	{
		return PHP.unescape_cdata(xml_node.firstChild.nodeValue);
	}
	else
	{
		return '';
	}
};

// we can check this variable to see if browser is AJAX compatible
var AJAX_Compatible = AL_AJAX_Handler.prototype.is_compatible();


/*****************************************************************\
|*  CVS: $RCSfile: aleppos_global.js,v $ - $Revision: 1.0 $
\*****************************************************************/
