String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

function valid_email(email) {
	var filter = /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i;
	return filter.test(email);
}

function valid_username(name) {
	var filter = /^\w+$/; // alphanumeric characters
	return filter.test(name);
}

function numbersOnly(el) {
	el.value = el.value.replace(/[^0-9]/g, "");
}

function latinOnly(el) {
	el.value = el.value.replace(/[^a-zA-Z\- ]/g, "");
}

/*
 * type == 0 @implies id
 * type == 1 @implies credit card (non isracard)
 * type == 2 @implies isracard
 */
function validNumber(num, type) {
	var length = type == 1 ? 16 : 9;
	num = String(num);
	
	if (num.length == 0)
		return false;
	else {
		while (num.length < length)
			num = "0" + num;

		var sum = 0;
		for (var i = length - 1; i >= 0; i--) {
			var temp;
			if (type==2)
				temp = num.substr(i, 1) * (9 - i);
			else
				temp = num.substr(i, 1) * (type == 0 ? i % 2 + 1 : (i + 1) % 2 + 1);

			if (type!=2 && temp > 9)
				temp = temp % 10 + 1;

			sum += temp;
		}
		if (type==2)
			sum %= 11;
		else
			sum %= 10;

		if (sum > 0)
			return false;
		else
			return true;
	}
}

function passwordStrength(password) {
	password = password.trim();

	var cNbr = cCap = cMin = cPct = cSpe = 1;
	var length = password.length;

	for(var c = 0; c < length; c++){
		var char = password.charCodeAt(c);

		if (char < 128) {
			if (char > 47 && char < 58)
				cNbr += 1;
			else if (char > 64 && char < 91)
				cCap += 1;
			else if (char > 96 && char < 123)
				cMin += 1;
			else
				cPct += 2;
		}
		else
			cSpe += 3;
	}

	var level = cNbr * cCap * cMin * cPct * cSpe;
	level = Math.round(Math.log((level * level)));

	return level;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}

	return vars;
}

function checkAll(x) {
	var y = x.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('input');
	
	for (var i=0; i<y.length; i++)
		if (y[i].value==x.value)
			y[i].checked = x.checked;
		else
			y[i].checked = false;
}

function show(id) {
	var x = document.getElementById(id);
	
	x.style.display = x.style.display == 'inline' ? 'none' : 'inline';
	
	return false;
}

var _isDirty = false;
var _innerLink = false;

function isDirty() {
	return _isDirty || (typeof tinyMCE != 'undefined' && tinyMCE.activeEditor.isDirty());
}

function setDirty(e) {
	_isDirty = e.value.length>0;
}

function notDirty() {
	_isDirty = false;
}

function warning() {
	_innerLink = true;

	if (isDirty())
		_innerLink = confirm("ביצעתם שינויים שלא נשמרו. האם אתם בטוחים שברצונכם לנטוש אותם?");

	return _innerLink;
}

window.onbeforeunload = function() {
	if (!_innerLink && isDirty())
		return "ביצעתם שינויים שלא נשמרו. האם אתם בטוחים שברצונכם לנטוש אותם?";
};

function changeList(e) {
	if (warning())
		window.location.href = 'do_campaign_switch.php?id=' + e.value + '&ref=' + encodeURI(window.location.href);
	else
		e.options[0].selected = true;
}

function noenter(e) {
    e = e || window.event;
    var key = e.keyCode || e.charCode;
    return key !== 13; 
}

function removeChilds(e) {
	if (e.hasChildNodes())
		while (e.childNodes.length >= 1)
			e.removeChild(e.firstChild);
}

function find_pos(e) {
	var _left = _top = 0;

	if (e.offsetParent) {
		do {
			_left += e.offsetLeft;
			_top += e.offsetTop;
		} while (e = e.offsetParent);
	}

	return [_left, _top];
}

function create_tooltip(e) {
	var pos = find_pos(e);
	var span = document.createElement('span');
	var title = document.createTextNode(e.title);
	var timestamp = new Date().getTime();
	var id = timestamp + '_tooltip';

	span.id = id;
	span.className = 'tooltip';
	span.title = e.title;
	e.title = '';

	span.appendChild(title);
	e.parentNode.appendChild(span);

	span.style.left = pos[0] - span.offsetWidth - 10 + 'px';
	span.style.top = pos[1] + 10 + 'px';

	e.onmouseout = function() {
		var tooltip = document.getElementById(id);
		e.title = tooltip.title;
		tooltip.parentNode.removeChild(tooltip);
	}
}

function init() {
	if (document.forms[0]) {
		if (document.forms[0].name)
			document.forms[0].name.focus();
		else if (document.forms[0].username)
			document.forms[0].username.focus();
		else if (document.forms[0].msg_content)
			document.forms[0].msg_content.focus();
	}
}

if (window.addEventListener)
	window.addEventListener("load", init, false);
else if (window.attachEvent)
	window.attachEvent("onload", init);
