/*
+----------------------------------------------------------
|   (SnS) AJAX for IP.Board 1.3
|   =======================================================
|   (Ñ) 2007 Îëåã «Sannis» Åôèìîâ
|   http://ibresource.ru/forums/index.php?showuser=36662
|   http://ibpower.ru/index.php?showuser=33
|   http://forum.sysman.ru/index.php?showuser=3739
|   =======================================================
|   Some code © Copyright 2007 Invision Power Services, Inc.
|   http://www.invisionpower.com
|   =======================================================
|   Some code © Copyright 2005 Richard Heyes
|   http://www.phpguru.org/
+----------------------------------------------------------
*/

function $(elementid)
{
    return document.getElementById(elementid);
} 

function jump_act_error(txt)
{
	txt = txt.substr(6, txt.length-6);
	
	var pos = txt.indexOf('#');
	var msg = '';
	var extra = '';
	
	if( pos < 0 )
	{
		msg = txt;
	}
	else
	{
		msg   = txt.substr(0, pos);
		extra = txt.substr(pos+1, txt.length-pos-1);
	}
	
	if(extra) extra = '&extra=' + extra;
	
	locationjump('act=Error&msg=' + msg + extra);
}

function ips_getcookie( name )
{
	cname = ipb_var_cookie_id + name + '=';
	cpos  = document.cookie.indexOf( cname );

	if ( cpos != -1 )
	{
		cstart = cpos + cname.length;
		cend   = document.cookie.indexOf(";", cstart);

		if (cend == -1)
		{
			cend = document.cookie.length;
		}

		return unescape( document.cookie.substring(cstart, cend) );
	}

	return null;
};

function ips_setcookie( name, value, sticky )
{
	var expire = '';
	var domain = '';
	var path   = '/';

	if ( sticky )
	{
		expire = '; expires=Wed, 1 Jan 2020 00:00:00 GMT';
	}

	if ( ipb_var_cookie_domain != '' )
	{
		domain = '; domain=' + ipb_var_cookie_domain;
	}

	if ( ipb_var_cookie_path != '' )
	{
		path = ipb_var_cookie_path;
	}

	var str = ipb_var_cookie_id + name + '=' + value + '; path=' + path + expire + domain + ';';

	document.cookie = str;
};


/**
* This file is part of the PHP_Unserialize package (http://www.phpguru.org/)
*
* PHP_Unserialize is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* PHP_Unserialize is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP_Unserialize; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* © Copyright 2005 Richard Heyes
*   http://www.phpguru.org/
*/

/**
    * Unserializes a PHP serialized data type. Currently handles:
    *  o Strings
    *  o Integers
    *  o Doubles
    *  o Arrays
    *  o Booleans
    *  o NULL
    *  o Objects
    * 
    * alert()s will be thrown if the function is passed something it
    * can't handle or incorrect data.
    *
    * @param  string input The serialized PHP data
    * @return mixed        The resulting datatype
    */
function PHP_Unserialize(input)
{
	var result = PHP_Unserialize_(input);
	return result[0];
}

/**
    * Function which performs the actual unserializing
    *
    * @param string input Input to parse
    */
function PHP_Unserialize_(input)
{
	var length = 0;
	
	switch (input.charAt(0)) {
		/**
		* Array
		*/
		case 'a':
			length = PHP_Unserialize_GetLength(input);
			input  = input.substr(String(length).length + 4);

			var arr   = new Array();
			var key   = null;
			var value = null;

			for (var i=0; i<length; ++i) {
				key   = PHP_Unserialize_(input);
				input = key[1];

				value = PHP_Unserialize_(input);
				input = value[1];

				arr[key[0]] = value[0];
			}

			input = input.substr(1);
			return [arr, input];
			break;
		
		/**
		* Objects
		*/
		case 'O':
			length = PHP_Unserialize_GetLength(input);
			var classname = String(input.substr(String(length).length + 4, length));
			
			input  = input.substr(String(length).length + 6 + length);
			var numProperties = Number(input.substring(0, input.indexOf(':')))
			input = input.substr(String(numProperties).length + 2);

			var obj      = new Object();
			var property = null;
			var value    = null;

			for (var i=0; i<numProperties; ++i) {
				key   = PHP_Unserialize_(input);
				input = key[1];
				
				// Handle private/protected
				key[0] = key[0].replace(new RegExp('^\x00' + classname + '\x00'), '');
				key[0] = key[0].replace(new RegExp('^\x00\\*\x00'), '');

				value = PHP_Unserialize_(input);
				input = value[1];

				obj[key[0]] = value[0];
			}

			input = input.substr(1);
			return [obj, input];
			break;

		/**
		* Strings
		*/
		case 's':
			length = PHP_Unserialize_GetLength(input);
			return [String(input.substr(String(length).length + 4, length)), input.substr(String(length).length + 6 + length)];
			break;

		/**
		* Integers and doubles
		*/
		case 'i':
		case 'd':
			var num = Number(input.substring(2, input.indexOf(';')));
			return [num, input.substr(String(num).length + 3)];
			break;
		
		/**
		* Booleans
		*/
		case 'b':
			var bool = (input.substr(2, 1) == 1);
			return [bool, input.substr(4)];
			break;
		
		/**
		* Null
		*/
		case 'N':
			return [null, input.substr(2)];
			break;

		/**
		* Unsupported
		*/
		case 'o':
		case 'r':
		case 'C':
		case 'R':
		case 'U':
			alert('Error: Unsupported PHP data type found!');

		/**
		* Error
		*/
		default:
			return [null, null];
			break;
	}
}

/**
    * Returns length of strings/arrays etc
    *
    * @param string input Input to parse
    */
function PHP_Unserialize_GetLength(input)
{
	input = input.substring(2);
	var length = Number(input.substr(0, input.indexOf(':')));
	return length;
}
