﻿//-------------- Ajax Object ---------------------//
var ajax = {
	ajaxHistory : [{page: "index.php", method : "GET", divId: "main", params:"show=main"}],	//store request function parameters

	//perform previous requests in order of request time that pushed in ajaxHistory array 
	back : function(){
		if(this.ajaxHistory.length > 1){
			this.ajaxHistory.pop();
			var obj = this.ajaxHistory.pop();
			this.request(obj.page, obj.method, obj.divId, obj.params);
		}
		else if(arguments[0]===true){
			history.back();
		}
	},

	//Get suitable Ajax object
GetXMLHttpRequest: function() {
		var object = null;
		if (window.ActiveXObject) {
			var axo = [
			'MSXML2.XmlHttp.5.0',
			'MSXML2.XmlHttp.4.0',
			'MSXML2.XmlHttp.3.0',
			'MSXML2.XmlHttp',
			'Microsoft.XmlHttp'
			];
			for (var i = 0; i < axo.length ;  i++)
			{
				try
				{
					httpObj = new ActiveXObject(axo[i]);
					return httpObj;
				}
				catch(e) {
				}
			}
		} else if (window.XMLHttpRequest) {
			object = new XMLHttpRequest();
		}
		if (object === null) {
			throw new Error("Your browser does not support AJAX");
		}
		return object;
	},
	
	request :function(serverPage, getOrPost, objID, str) {
		var xmlhttp = this.GetXMLHttpRequest();					//get XMLHTTP object
		var stateChange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				if(typeof jQuery=== "function"){
					$('#'+objID).hide();	//jQuery hide
					$('#'+objID).html(xmlhttp.responseText).slideDown();	//jQuery show
					}else{
						document.getElementById(objID).innerHTML = xmlhttp.responseText;
					}
				}
			}
		var loading = "<img src='./images/loading.gif' /> در حال بارگذاری...";
		if(document.URL.indexOf("management") >= 0){
			loading = "<img src='../images/loading.gif' /> در حال بارگذاری...";
		}
		document.getElementById(objID).innerHTML = loading;	//loading...
		var page='';
		if(typeof str != "undefined" && getOrPost.toUpperCase() == "GET"){
			page = serverPage + "?"+ str;				//if GET arguments has been sent, create extra variable(just use in GET method)
		}
		else{
			page = serverPage;
		}
		if(getOrPost.toUpperCase() == "POST"){
			xmlhttp.open("POST", serverPage, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.onreadystatechange = stateChange;
			xmlhttp.send(str);
		}
		else{
			xmlhttp.open("GET", page, true);
			xmlhttp.onreadystatechange = stateChange;
			xmlhttp.send(null);
			this.ajaxHistory.push({page: arguments[0], method : arguments[1], divId: arguments[2], params:arguments[3]});
		}
	},
	
	submitForm : function(theform, serverPage, objID){
		var  getFormValues = function(formID){
			var str = "";
			var element = null;
			//Run through a list of all objects contained within the form.
			for(var i = 0; i < document.forms[formID].elements.length; i++){
				element = document.forms[formID].elements[i];
				if ( element.type == "radio" || element.type == "checkbox") {
					if(element.checked == true){
						str += element.name + "=" + encodeURI(element.value	) + "&";
					}
				}
				else if(element.tagName.toUpperCase() != "FIELDSET"){
					str += element.name + "=" + encodeURI(element.value) + "&";
				}
			}
			//Then return the string values.
			return str;
		}
		var string = getFormValues(theform);	//string of form values that should be passed
		this.request(serverPage,"POST", objID,  string);		//send request to proper page with POST method
	}
}
//------------------------- End of Ajax object -----------------------------------//
function makerequest(){
	ajax.request(arguments[0], "GET", arguments[1], arguments[2]);
}
function submitform(){
	ajax.submitForm(arguments[0], arguments[1], arguments[2]);
}

function delCurrent(){
	if(document.getElementById('current')){
		var current = document.getElementById('current');
		if(typeof current != "null"){
			current.removeAttribute("id");	
		}
	}
}

function checkCurrent(element,serverPage, objID){
	element.blur();
	delCurrent();
	element.parentNode.id = "current";
	makerequest(serverPage, objID);
}

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}