﻿//Ajah GET Method
//Developed by Ehsan Pakgohar
//epak83[at]yahoo.com
//5/6/2009
//===============================
var loaddiv="";
if(typeof HTMLElement!="undefined" && ! HTMLElement.prototype.insertAdjacentElement)
{ 
	HTMLElement.prototype.insertAdjacentElement = function 
	(where,parsedNode) 
	{ 
		switch (where){ 
		case 'beforeBegin': 
		this.parentNode.insertBefore(parsedNode,this) 
		break; 
		case 'afterBegin': 
		this.insertBefore(parsedNode,this.firstChild); 
		break; 
		case 'beforeEnd': 
		this.appendChild(parsedNode); 
		break; 
		case 'afterEnd': 
		if (this.nextSibling) 
		this.parentNode.insertBefore(parsedNode,this.nextSibling); 
		else this.parentNode.appendChild(parsedNode); 
		break; 
	} 
	} 

	HTMLElement.prototype.insertAdjacentHTML = function 
	(where,htmlStr) 
	{ 
	var r = this.ownerDocument.createRange(); 
	r.setStartBefore(this); 
		var parsedHTML = r.createContextualFragment(htmlStr); 
		this.insertAdjacentElement(where,parsedHTML) 
	} 

	HTMLElement.prototype.insertAdjacentText = function 
	(where,txtStr) 
	{ 
		var parsedText = document.createTextNode(txtStr) 
		this.insertAdjacentElement(where,parsedText) 
	} 
}
//==============================
//Call Function GetData('URL+Request','Control')
var DIVHTML="<div id='ajaxloading' dir='rtl' style='position: absolute;border-style:outset; border-width:1px; width: 119px; z-index: 1;left:expression(document.body.clientWidth/2-ajaxloading.offsetWidth/2);top:0 '><table border='0' width='100%' id='table1' border='0' style='background-color:#FFFFDF;cellspacing='0';cellpadding='0'><tr><td style='border-width:0'><img border='0' src='images/loading.gif' width='16' height='16'></td><td style='border-width:0'><font face='Tahoma' size='1'>لطفا صبر کن&#1740;د...</font></td></tr></table></div>";
var DIVHTML1="<div id='ajaxloading' dir='rtl' style='width: 119px; z-index: 1;'><table  cellspacing='0' cellpadding='0' border='0' style='border:0px;width:100%'><tr><td style='border-width:0'><img border='0' height='16' src='images/loading.gif' width='16' /></td><td style='border-width:0'><font face='Tahoma' size='1'>لطفا صبر کنید...</font></td></tr></table></div>";
function SetPauseh(){
	if (document.getElementById("ajaxloading")==undefined){
		if(loaddiv!="" && loaddiv!=undefined)
		{
			document.getElementById(loaddiv).innerHTML=DIVHTML1;
		}
		else
			document.body.insertAdjacentHTML("beforeEnd",DIVHTML);
		document.getElementById("ajaxloading").style.visibility="";		
		}
	else{
		document.getElementById("ajaxloading").style.visibility="";
		}
	setTimeout("",200);	
}

function GetDatah(murl,mdata,div){
	if(div!=undefined)
		loaddiv=div;
	SetPauseh();
	var url =murl+"&"+Math.random();
	if (window.XMLHttpRequest) {
		XMLR = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject) {
			try {
				XMLR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}
	if (XMLR) {
		XMLR.onreadystatechange = GetSearchResultsh;
		XMLR.open("GET", url, true);
		if (mdata)
		{
			//XMLR.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
			//XMLR.setRequestHeader("Accept-Charset","windows-1252, utf-8");
			XMLR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			XMLR.send(mdata);
		}
		else
		{
			XMLR.send(null);
		}
	}
	else {
		alert("Sorry, but I couldn't create an XMLHttpRequest");
		document.getElementById("ajaxloading").style.visibility="hidden";
	}	
}

function PostData(murl,mdata,div){
	if(div!=undefined)
		loaddiv=div;
	SetPauseh();
	var url =murl+"&"+Math.random();
	if (window.XMLHttpRequest) 
	{
		XMLR = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject) {
			try {
				XMLR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}
	if (XMLR) 
	{
		XMLR.onreadystatechange = GetSearchResultsh;
		XMLR.open("POST", url, true);
		//XMLR.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
		//XMLR.setRequestHeader("Accept-Charset","windows-1252, utf-8");
		XMLR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		XMLR.send(mdata);
	}
	else 
	{
		alert("Sorry, but I couldn't create an XMLHttpRequest");
		document.getElementById("ajaxloading").style.visibility="hidden";
	}	
}
function GetSearchResultsh(){
	if (XMLR.readyState == 4)
	{
		if (XMLR.status == 200)
		{
			var outMsg 
			outMsg=XMLR.responseText;
			RDatah(LTrim(getTag(outMsg)));
			//if (navigator.appName=="Netscape")
			//{
			//	document.getElementById("ajaxloading").style.display="none";
			//}
			//else
			//{
				if(loaddiv=="" || loaddiv==undefined)
					document.getElementById("ajaxloading").style.visibility="hidden";
			//}
		}
		else 
		{
			alert("Error message : "+GetErrorTexth(XMLR.status)+"\nThere was a problem with the request");
			document.getElementById("ajaxloading").style.visibility="hidden";
			RDatah("LOADERROR");
		}

		// Do something here with outMsg
	}			
}
function getTag(data)
{
//<mytag>data</mytag>
	var i=InStr(data,"<mytag>")+7;
	if (i>0)
	{
		var j=InStr(data,"</mytag>");
		if (j>0)
		{
			return Mid(data,i,j-i);
		}
		else
		{
			if(data)
			{
				return data;
			}
			else
				return "";
		}
	}
	else
	{
		return data;
	}
}
function InStr(String1, String2)
{
	var a = 0;
	if (String1 == null || String2 == null || String1==undefined)
	{
		return 0;
	}
	else
	{
		String1 = String1.toLowerCase();
		String2 = String2.toLowerCase();

		a = String1.indexOf(String2);
		if (a == -1)
			return 0;
		else
			return a + 1;	
	}
}
function Mid(String, Start, Length)
{
	if (String == null)
		return (false);

	if (Start > String.length)
		return '';

	if (Length == null || Length.length == 0)
		return (false);

	return String.substr((Start - 1), Length);
}
function LTrim(strValue)
{
	while (strValue.charCodeAt(0)==65279)
	{
		strValue=Replace(strValue,strValue.charAt(0),"");
	}
	return strValue;
}
function Replace(Expression, Find, Replace,Repeat)
{
	var temp = Expression;
	var a = 0;
	var rtime=0;
	var Rep=0;
	if (Repeat!=undefined){Rep=Repeat;}
	for (var i = 0; i < Expression.length; i++) 
	{
		a = temp.indexOf(Find);
		if (a == -1)
			break
		else
		{
			temp = temp.substring(0, a) + Replace + temp.substring((a + Find.length));
			rtime++;
			if (Rep>0 && Rep>=rtime)break;
		}
	}
	return temp;
}
function RDatah(data)
{
	//Return XML Data
}
function GetErrorTexth(a)
{
	switch (a)
	{
	case 301:
		return a+" Moved permanently";
		break;
	case 302:
		return a+" Found";
		break;
	case 303:
		return a+" See Other";
		break;
	case 403:
		return a+" Forbidden";
		break;
	case 404:
		return a+" Not Found";
		break;
	case 500:
		return a+" Internal Server Error";
		break;
	default:
		return a;
		break;
	}
}

