/*
	JS Extending Library
	Must be loaded before any other JS scripts
	v 2.1.0.8
	build 200810091500
*/

var jsExtLibData = {
	browser: null,
	version: null,
	debug: false,
	channelFree: true,//for ajax2.js
	funcDone: false,//for ajax2.js
	funcCache: {}
};

function checkPassStrength(password) {
	var hasDigits = false;
	var hasLowerLetters = false;
	var hasUpperLetters = false;
	var hasOtherSigns = false;

	var digits = "0123456789";
	var lowerLetters = "abcdefghijklmopqrstuvwxyz";
	var upperLetters = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
	var otherSigns = "~`!@#$%^&*+-=_|\\/()[]{}<>,.;:?\"\'";

	var SO = password.length;
	for (var i = 0; i < SO; i++) {
		hasDigits |= digits.indexOf(password.charAt(i)) >= 0;
		hasLowerLetters |= lowerLetters.indexOf(password.charAt(i)) >= 0;
		hasUpperLetters |= upperLetters.indexOf(password.charAt(i)) >= 0;
		hasOtherSigns |= otherSigns.indexOf(password.charAt(i)) >= 0;
	}
	
	return Math.round(Math.log(Math.pow((hasDigits ? 10 : 0) + (hasLowerLetters ? 26 : 0) + (hasUpperLetters ? 26 : 0) + (hasOtherSigns ? 32 : 0), SO)));
}

function uniqid(l) {
	if (!l) l = 16;

	var digits = "0123456789", letters = "ABCDEFGHIJKLMOPQRSTUVWXYZ", res = "";
	for (var i = 0; i < l; ++i) res += (random(1,2) == 1 ? digits.charAt(random(0, digits.length - 1)) : letters.charAt(random(0, letters.length - 1)));

	return res;
}

function time() {
	return new Date().getTime();
}

function IDgen() {
	return time().toString() + Number.rand(10000, 99999).toString();
}

function isset(v) {
	return (typeof(v) == 'undefined' ? false : true);
}

function empty(v) {
	return (isset(v) ? (v == '0'   ||   v == 0   ||   v == null   ||   v == '' ? true : false) : true);
	//return (isset(v) ? (in_array(v, ['0', 0, null, '']) ? true : false) : true);
}

function $(id) {
	//Do not chache this function
	return document.getElementById(id);
}

function addEvent(ev, o, func, useCapture) {
	// useCapture required only for compability with older versions
	if (! o) return false;
	
	if (isIE()) o.attachEvent("on"+ ev, (typeof(func) == 'string' ? function(){eval(func)} : func));
	else o.addEventListener(ev, (typeof(func) == 'string' ? function(){eval(func)} : func), false);
}

function removeEvent(ev, o, func) {
	if (! isIE()) o.removeEventListener(ev, function(){}, false);
	else o.detachEvent("on" + ev, function(){});
}
/*
function browser(full) {
	if (!isset(full)) var full = false;

	if (jsExtLibData['browser'] == '') {
		if (navigator.appName == 'Microsoft Internet Explorer') jsExtLibData['browser'] = 'IE';
		else if (navigator.platform == 'MacPPC'   &&   navigator.appName == 'Netscape') jsExtLibData['browser'] = 'SAFARI';
		else if (/Opera/i.test(navigator.userAgent)) jsExtLibData['browser'] = 'OPERA';
		else if (/Firefox/i.test(navigator.userAgent)) jsExtLibData['browser'] = 'FIREFOX';
	}
	if (!full) return jsExtLibData['browser'];
	
	switch (jsExtLibData['browser']) {
		case 'IE': jsExtLibData['version'] = ''; break;
	}
	return {browser: jsExtLibData['browser'], version: jsExtLibData['version']};
}
*/
function isIE() {
	if (jsExtLibData.funcCache.isIE) return jsExtLibData.funcCache.isIE;
	return (jsExtLibData.funcCache.isIE = (navigator.appName == 'Microsoft Internet Explorer'));
}

function isSAFARI() {
	if (jsExtLibData.funcCache.isSAFARI) return jsExtLibData.funcCache.isSAFARI;
	return (jsExtLibData.funcCache.isSAFARI = (navigator.platform == 'MacPPC'   &&   navigator.appName == 'Netscape'));
}

function isIE7() {
	if (jsExtLibData.funcCache.isIE7) return jsExtLibData.funcCache.isIE7;
	return (jsExtLibData.funcCache.isIE7 = (navigator.userAgent.indexOf('MSIE 7') != -1 ? true : false));
}

function isOpera() {
	if (jsExtLibData.funcCache.isOpera) return jsExtLibData.funcCache.isOpera;
	return (jsExtLibData.funcCache.isOpera = (/Opera/i.test(navigator.userAgent)));
}

function isFF() {
	if (jsExtLibData.funcCache.isFF) return jsExtLibData.funcCache.isFF;
	return (jsExtLibData.funcCache.isFF = (/Firefox/i.test(navigator.userAgent)));
}

function moveObj2Event(e, id, offsetX, offsetY) {
	var x, y, o = $(id);
	if (o) {
		if (! e) e = window.event;
		
		if (! isset(offsetX)) offsetX = 0;
		if (! isset(offsetY)) offsetY = 0;
		
		x = parseInt(e.clientX) + parseInt(document.body.scrollLeft) + offsetX;
		y = parseInt(e.clientY) + parseInt(document.body.scrollTop) + offsetY;
		
		if (document.body.clientHeight < o.offsetHeight + y) y = y - o.offsetHeight;
		if (document.body.clientWidth < o.offsetWidth + x) x = x - o.offsetWidth;
		
		o.style.left = x +'px';
		o.style.top = y +'px';
	}
}

function copyright(o, from) {
	var y = new Date();
	y = parseInt(y.getFullYear());
	if (empty(from)) {
		o.innerHTML = y;
		return;
	}
	from = parseInt(from);
	
	o.innerHTML = (y - from > 0 ? from +'-'+ y : y);
}

function checkData(s, type) {
	// Data validation
	switch (type) {
		case 'date':
			if (/^(0|1|2|3)?[0-9]{1}\.(0|1|2)?[0-9]{1}\.(1|2)[0-9]{3}$/gmi.test(s)) return true;
		break;
		case 'mail': case 'email':
			if (/^[a-z0-9_\-\.\^]+@[a-z0-9_\-\.]+\.[a-z]+$/i.test(s)) return true;
		break;
		case 'int': case 'integer': case 'number':
			if (s == parseInt(s)) return true;
		break;
		case 'float': case 'double':
			if (/^[0-9]+((.|,)[0-9]{0,2})?$/gmi.test(s)) return true;
		break;
		case 'httpLink':
			if (/^http(s)?:\/\/[a-z0-9_\.-]+\.[a-z]{2,5}/gmi.test(s)) return true;
		break;
		case 'ftpLink':
			if (/^ftp:\/\/[a-z0-9_\.-]+\.[a-z]{2,5}/gmi.test(s)) return true;
		break;
		case 'digitsletters': case 'digits-letters': case 'dl': case 'ld':
			if (/^[0-9a-z]+$/gmi.test(s)) return true;
		break;
	}
	return false;
}

function validateForm(vData, buttonID, debug) {
	// Automatic form validation
	
	var ok, v, id, el, checkSum = 0, realSum = 0, tmp, alerts = [];
	var fEls = eval(vData);
	if (! isset(debug)) debug = false;
	
	/*
		EXAMPLE of vData to pass
		
		var fEls = {
			'login' : {
				'max' : 16,
				'min' : 1,
				'func' : 'checkData("%value%", "dl")',
				'alert' : 'Invalid data in field Login.'
			},
			'pass' : {
				'max' : 50,
				'min' : 6
			},
			'sex' : {
				'min' : 1
			}
		};

	*/
	
	if (debug) {
		var console = $('console');
		console.innerHTML = '<table>';
	}
	
	for (id in fEls) {
		v = trim($(id).value.toLowerCase());
		for (el in fEls[id]) {
			if (el == 'alert') continue;
			checkSum++;
			ok = false;
			switch (el) {
				case 'max':
					if (v.length <= fEls[id][el]) {
						ok = true;
						realSum++;
						if (debug) console.innerHTML += '<tr onMouseOver="this.style.backgroundColor=\'#efe\'" onMouseOut="this.style.backgroundColor=\'\'"><td>'+ id +'</td><td> : </td><td>MAX</td><td> : </td><td><b>true</b></td></tr>';
					} else {
						if (debug) console.innerHTML += '<tr onMouseOver="this.style.backgroundColor=\'#efe\'" onMouseOut="this.style.backgroundColor=\'\'"><td>'+ id +'</td><td> : MAX : false<br />';
					}
				break;

				case 'min':
					if (v.length >= fEls[id][el]) {
						ok = true;
						realSum++;
						if (debug) console.innerHTML += '<tr onMouseOver="this.style.backgroundColor=\'#efe\'" onMouseOut="this.style.backgroundColor=\'\'"><td>'+ id +'</td><td> : </td><td>MIN</td><td> : </td><td><b>true</b></td></tr>';
					} else {
						if (debug) console.innerHTML += '<tr onMouseOver="this.style.backgroundColor=\'#efe\'" onMouseOut="this.style.backgroundColor=\'\'"><td>'+ id +'</td><td> : </td><td>MIN</td><td> : </td><td>false</td></tr>';
					}
				break;

				case 'func':
					tmp = fEls[id][el].replace(/%value%/gm, v);
					if (eval(tmp)) {
						ok = true;
						realSum++;
						if (debug) console.innerHTML += '<tr onMouseOver="this.style.backgroundColor=\'#efe\'" onMouseOut="this.style.backgroundColor=\'\'"><td>'+ id +'</td><td> : </td><td>FUNC ('+ tmp +')</td><td> : </td><td><b>true</b></td></tr>';
					} else {
						if (debug) console.innerHTML += '<tr onMouseOver="this.style.backgroundColor=\'#efe\'" onMouseOut="this.style.backgroundColor=\'\'"><td>'+ id +'</td><td> : </td><td>FUNC ('+ tmp +')</td><td> : </td><td>false</td></tr>';
					}
				break;
			}
			if (! ok) {
				if (isset(fEls[id]['alert'])) {
					if (fEls[id]['alert'] != '') alerts.push(fEls[id]['alert']);
				}
			}
			if (alerts.length > 0) alert(alerts.join('\n'));
		}
	}
	
	if (debug) {
		console.innerHTML += '</table>';
		console.innerHTML += realSum +' == '+ checkSum;
	}
	
	if (isset(buttonID)) {
		if (buttonID != '') {
			if (realSum == checkSum) $(buttonID).disabled = false;
			else $(buttonID).disabled = true;
		}
	}
	return (realSum == checkSum ? true : false);
}

function formCounter(id, max, name) {
	var o, c; // Object and Counter
	if (!name) name = 'counter';
	
	if (typeof(id) == 'string') o = $(id);
	else o = id;
	if (typeof(name) == 'string') c = $(name);
	else c = name;
	
	if (o) {
		var allow = max - o.value.length;
		if (allow < 0) {
	       	allow = 0;
			o.value = o.value.substr(0, max);
		}
	   	if (c) c.innerHTML = allow;
	}
}

function overlay(id, content) {
	var o;
	if (typeof(id) == 'string') o = $(id);
	else o = id;
	
	if (o) {
		o.innerHTML = content;
		toggleOn(o);
	}
}

function elrkjt(x, y, z) {
	// mailto: protection from bots
	document.write('<'+'a hr'+'ef="mai'+'lto:'+ x +'@'+ y +'.'+ z +'">'+ x +'@'+ y +'.'+ z +'</a>');
}

function imgSwap(id, state1, state2) {
	var o;
	if (typeof(id) == 'string') o = $(id);
	else o = id;
	
	if (o) {
		if (o.src.indexOf(state1) > -1) o.src = state2;
		else o.src = state1;
	}
	return true;
}

function classSwap(id, state1, state2) {
	var o;
	if (typeof(id) == 'string') o = $(id);
	else o = id;
	
	if (o) {
		if (o.className == state1) o.className = state2;
		else o.className = state1;
	}
	return true;
}

function changeOpacity(id, v) {
	var o;
	if (typeof(id) == 'string') o = $(id);
	else o = id;
	
	if (isIE()) o.filters.alpha.opacity = (v * 100).toString();
	else o.style.opacity = v;
}
/*
var imgPreloader = {
	stack: [],
	loading: [],
	loaded: [],
	onLoad: null,
	
	add: function(url) {
		this.stack.push(url);
	},
	
	load: function() {
		var img = '';
		var id;
		while (img = this.stack.shift()) {
			id = this.loading.length;
			
			this.loading[id] = {
				o: new Image(),
				path: img
			};
			this.loading[id].o.src = img;
			this.loading[id].o.onload = function() {
				var id = imgPreloader.getIDbyPath(parse_url(this.src).path);
				if (id === false) return;
				imgPreloader.loaded.push(imgPreloader.loading[id].path);
				delete imgPreloader.loading[id];
				if (count(imgPreloader.loading) == 0) imgPreloader.onLoadFire();
			}
		}
	},
	
	getIDbyPath: function(path) {
		var el;
		for (el in this.loading) {
			if (this.loading[el].path == path) return el;
		}
		return false;
	},
	
	onLoadFire: function() {
		if (typeof(this.onLoad) == 'function') this.onLoad();
	}
};
*/
var imgPreloader = {
	images: [],
	loading: [],
	loaded: [],
	onLoad: null,
		
	add: function(url) {
		imgPreloader.images.push(url);
	},
	
	load: function() {	
		var i = 0;
		var SO = imgPreloader.images.length;
		for(i = 0; i < SO; ++i) {
				imgPreloader.loading[i] = new Image();
				imgPreloader.loading[i].src = imgPreloader.images[i];
				imgPreloader.loading[i].onload = function() {
					imgPreloader.loaded.push(this.src);					
					if(imgPreloader.loaded.length == count(imgPreloader.loading)) imgPreloader.onLoadFire();
				}
		}		
	},
		
	onLoadFire: function() {		
		if (typeof(imgPreloader.onLoad) == 'function') {
			for(i = 0; i < count(imgPreloader.loading); ++i) imgPreloader.loading[i].onload = '';
			imgPreloader.onLoad();			
		}
	},
	
	skip: function() {
		if (typeof(imgPreloader.onLoad) == 'function') {
			imgPreloader.onLoad();
			imgPreloader.onLoad= null;
		}
	}
};

if (typeof(setCursor) == 'undefined') {
	function setCursor(type) {
		document.body.style.cursor = type;
	}
}