var test = "/^([a-zA-Z0-9 _ ])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
var no = new RegExp("[^0-9\.]", "g");
var email = new RegExp('^[-+\\.0-9=a-z_]+@([-0-9a-z]+\\.)+([0-9a-z]){2,4}$','i');
var ret = true;
var _d = document;

var agt=navigator.userAgent.toLowerCase();
var ie = (agt.indexOf("msie") != -1);
var ns = (navigator.appName.indexOf("Netscape") != -1);
var opera = (navigator.appName.indexOf("Opera") != -1);
var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
var mac = (agt.indexOf("mac")!=-1);

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function dog(id)
{
	if (_d.getElementById) {
		element = _d.getElementById(id);
	}else {
		element = _d.all[id];
	}
	return element;
}

function targetElement(ev)
{
	if (ie)
		return ev.srcElement;
	else
		return ev.target;
}

function checkForInt(elem)
{
	if (elem.value.match(no)){elem.style.background='#FFCCCC'; return false;}elem.style.background='#CCFFCC';return true;
}

function mailCheck(id, val)
{
	try{dog(id+"OK").style.display = 'none';}catch(e){}
	dog(id+"Error").style.display = 'none';
	if (!(val.match(email))){
		dog(id+"Error").style.display = 'block';
		return false;
	}
	else 
		try{dog(id+"OK").style.display = 'block';}catch(e){}
	
	return true;
}

if (ie)
{
	_d.getElementsByClassName = function(classname){
		var i =0;
		var a = document.getElementsByTagName('*');
		var elements  = new Array();
		while (element = a[i++]) {
			if (element.className == classname)
			{
				elements.push(element);
			}
		}
		return elements;
	}
}

function evalEvent(el, ev)
{
	if (ie){
		el.fireEvent('on'+ev);
	}else{
		var EventObj = window.document.createEvent("Event");
		EventObj.initEvent(ev, false, true);
		el.dispatchEvent(EventObj);
	}
}

function getData(url , query , method , callback)
{
        var XMLHttpRequestObject = false;
        try{
            XMLHttpRequestObject = new XMLHttpRequest();
		}catch(e){
			try{
				XMLHttpRequestObject = new XDomainRequest();
			}catch(e){
				try{
					XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					try{
						XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(e){
						alert("no XMLHttpRequest");
						return false;
					}
				}
			}
		}
		if (method == "get" || method == "GET")
			url = url + "?" + query;

		XMLHttpRequestObject.open(method,url,true);

		XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		XMLHttpRequestObject.onreadystatechange = function()
		{
			if(   XMLHttpRequestObject.readyState == 4) 
				if (XMLHttpRequestObject.status == 200)
				{
					if (callback != noAction)
						callback(XMLHttpRequestObject.responseText);
				}
				else
				{
					if (XMLHttpRequestObject.status!=0)
						alert("خطا:" + XMLHttpRequestObject.status + "  خطا در برقراری ارتباط با سرور\n"+"URL '"+ url+ "' Not Found!");
					return false;
				}
		}
		
		XMLHttpRequestObject.send(query);

}

function openBox(url , query , e)
{
	query += "&" + (new Date()).getTime();
	var OH = parseInt(dog("hiddenDiv").style.height);
	var NH = _d.body.offsetHeight;
	dog("hiddenDiv").style.height = NH + "px";
	dog("hiddenDiv").style.display = "block";
	dog('dataTd').innerHTML = '';
//	_d.location.replace();
	_d.body.style.cursor = 'wait';
	getData(url , query , "GET" , onLoadBox);
}

function onLoadBox(txt)
{
	with(dog('dataTd'))
	{
		innerHTML = txt;
		try{
			style.opacity = 0.0;
			style.filter.opacity = 0;
		}
		catch(e){}
		style.display = "block";
		var w = offsetWidth;
		var h = offsetHeight;
		style.top =  (_d.documentElement.clientHeight - h - 200)/2 + getScrolled()[0] + "px";
		style.left = (_d.documentElement.clientWidth - w)/2 + getScrolled()[1] + "px";
		try{
			style.opacity = 1.0;
			style.filter.opacity = 100;
		}
		catch(e){}
	}
	_d.body.style.cursor = 'Default';
}

function closeBox()
{
	dog('dataTd').style.display='none';
	dog('dataTd').innerHTML = '';
	dog('hiddenDiv').style.display='none';
}

function Login(WaitText)
{
	var ret = true;
	var user = dog("txtUsername").value;
	var pass = dog("txtPassword").value;
	if (typeof(WaitText)=='undefined')
		WaitText = "<span style='direction:rtl; color:#FF0000;'> لطفا چند لحظه صبر کنید </span>";
	dog("txtUsernameError").style.display = "none";
	dog("txtPasswordError").style.display = "none";	
	if (user == "")
	{
		dog("txtUsernameError").style.display = "block";
		ret = false;
	}
	if (pass == "" && !dog("chkForget").checked)
	{
		dog("txtPasswordError").style.display = "block";
		ret = false;
	}	
	if (!ret)
		return false;
	dog("userStatus").style.color = "#000000";
	dog("userStatus").innerHTML = WaitText;
	url = "index.php";
	query = "hdnAction=userlogin&txtUsername=" + user + "&txtPassword=" + pass;
	if (dog("chkForget").checked)
		query += "&chkForget=1";
//	_d.location = url + "?" + query;
	getData(url, query, "POST" , onLogin);
	return false;
}

function onLogin(txt)
{
	var txtArray = txt.split("||");
	if (txtArray[0].toLowerCase() == 'ok'){
		if(txtArray[1].toLowerCase() == "admin")
			_d.location = "admin/index.php";
		else if (txtArray[2].toLowerCase() == "active")
			_d.location = _d.location.pathname;
		else
			displayLoginError(txtArray[3]);
	}
	else
	{
		displayLoginError(txt);
	}
}

function displayLoginError(str)
{
	dog("userStatus").style.color = "#FF0000";
	dog("userStatus").innerHTML = str;
	dog("txtUsername").value = "";
	dog("txtPassword").value = "";
}

function onLoadData(txt)
{
	_d.body.style.cursor = '';
	dog("dataTd").innerHTML = txt;
	return false;
}

function LoadPartN(page , table)
{
	url = "";
	query = "hdnAction=loadAllRows&page=" + page;
	if (typeof(table) != 'undefiend')
		query = query + "&table=" + table;
	query += initQuery;
//	_d.location.href(url + "?" + query);
	dog("dataTd").innerHTML = "<span style='direction:rtl; color:#FF0000;'> لطفا چند لحظه صبر کنید </span>";
	getData(url , query , "GET" , onLoadData);
}

function onLogout(txt)
{
	txt = txt.toLowerCase();
	if(txt == "ok")
		window.location = "index.php";
	else 
		alert(txt);
}

function onLoadJS(txt)
{
	var s = document.createElement('Script');
	s.setAttribute('type', 'text\/javascript');
	s.text = txt;
	document.getElementsByTagName('head')[0].appendChild(s);
}

function showResult(txt, callback)
{
	if (typeof(callback)=='function')
		return callback(txt);
	alert(txt);			
}

function noAction(txt)
{
}

function getPosition(el)
{
	t = 0;
	l = 0;
	for (; el; el = el.offsetParent)
	{
	 t += el.offsetTop;
	 l += el.offsetLeft;
	}
	return [t, l];
}

function getScreenSize()
{
 var WW;
 var WH;
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 if (typeof window.innerWidth != 'undefined')
 {
      WW = window.innerWidth,
      WH = window.innerHeight
 }

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 if (typeof _d.documentElement != 'undefined' && typeof _d.documentElement.clientWidth != 'undefined' && _d.documentElement.clientWidth != 0)
 {
       WW = _d.documentElement.clientWidth,
       WH = _d.documentElement.clientHeight
 }
 // older versions of IE
 else
 {
       WW = _d.getElementsByTagName('body')[0].clientWidth,
       WH = _d.getElementsByTagName('body')[0].clientHeight
 }
 return [WW, WH];
}

function getScrolled()
{
	if (_d.body && _d.body.scrollTop)
	{
		st = _d.body.scrollTop;
		sl = _d.body.scrollLeft;
	}
	else if (_d.documentElement && !_d.documentElement.scrollTop)
	{
		st = 0;
		sl = 0;
	}
	else if (_d.documentElement && _d.documentElement.scrollTop)
	{
		st = _d.documentElement.scrollTop;
		sl = _d.documentElement.scrollLeft;
	}
	return [st, sl];
}

