//alert('jquary.js included');

//ajax response validator
var ajResV = { 

	m_mainSplitor 	: '[*]',
	
	m_garbage 		: '',
	
	m_stus 			: '',// it could be err/ok

	m_isCoded 		: '',// no / b64 / r13
	
	m_res 			: ' ',
	
	m_value 		: '',
	
	m_errNum 		: 0,
	
	m_errMsg 		: '',
	
	dump : function (){
		var $str = "m_garbage: " +this.m_garbage +"\n\n" +"m_stus: " +this.m_stus +"\n\n" +"m_isCoded: " +this.m_isCoded +"\n\n" +"m_res: " +this.m_res.substring(0,200) +" ...";
		alert($str);
	},
	
	errCleaner : function (){
		this.m_errNum = 0;
		this.m_errMsg = '';
	},
	
	validate : function ($value){//analize ajax response and validate it
		
		this.errCleaner();
		this.m_value = $value;
		
		if(this.isEmpty($value)){
			trObj.trAlert('Ajax response is empty! Q');
			return false;
		}
				
		var $det = this.m_value.split('[*]');
		this.m_garbage 	= $det[0];
		this.m_stus 	= $det[1];
		this.m_isCoded 	= $det[2];
		
		if		(this.m_isCoded == 'no'){
			this.m_res 		= $det[3];
		
		}else if(this.m_isCoded == 'b64'){
			this.m_res 		= Base64.decode($det[3]);
			
		}else{
			trObj.trAlert('it seems you are using old version of ajax validate');
			this.dump();
			
		}//end if
		
		if(this.m_stus == 'ok'){
			return true;
		}else{
			return false;
		}	
	},
	
	isNull : function(){
		if(typeof(this.m_value) == 'object'){return(false);}
		return (this.m_value==null || this.m_value=='null' || this.m_value=="");
	},
	
	isUnd : function(){
		if (typeof(this.m_value)=='undefined'){return(true);}
		return (false);
	},
	
	isEmpty : function(val){

		this.m_value = val;
		if ( this.isUnd() || this.isNull() ){
			this.m_stus = 'err';
			this.m_errNum = 1;
			this.m_errMsg = 'value is empty or null';
			return true; 
		}
		this.m_stus = 'ok';
		this.m_errMsgNum = 0;
		this.m_errMsg = 'value is not empty or null';
		return false;
	}
	
};

function make_id($pre_fix){
	if(!$pre_fix){$pre_fix="none";}
	var $str = String(Math.random());
	var	$id = $pre_fix +"_"+ $str.replace(".","");
	if(fObj($id)){
		make_id($pre_fix);
	}else{
		return($id);
	}
};

function del_arr_el($arr,$key,$value){
	//alert("in del arr el ...")
	var $new_arr = [];
	for(var x in $arr){
		if(!nl($key)){
			if(x == String($key)){continue;}
		}else{
			//alert("[del_arr_el: base on value] "+ $arr[x] +" == "+ $value)
			if($arr[x] == $value){continue;}
		}//end if
		$new_arr[x] = $arr[x];
	}//end for
	//alert("[del_arr_el] new_arr : "+ $new_arr)
	return($new_arr);
};

function nl($val){
	if(typeof($val) == 'object'){return(false);}
	return ($val==null || $val=='null' || $val=="");
};

function getObjScrpt($response, cBVariable, cBFunc){
	//alert('$response='+$response);
	if($response){
		//alert($response);
		if(!ajResV.validate($response)){return false;}
		var dtl = ajResV.m_res.split('[*[');
		
		//assigning callback value
		if(dtl[0]){
			eval(dtl[0]+"="+Base64.decode(dtl[1]));
		}else{
			//alert(Base64.decode(dtl[1]));
			eval(Base64.decode(dtl[1]));
		}
		
		//runing callback func
		var cBFuncArg=(dtl[3]) ? '"'+dtl[3]+'"':'';
		if(dtl[2]){eval(dtl[2]+"("+cBFuncArg+")");}

	}else{
		var $value = 'comp=main&cmd=getObjScrpt&cBV='+cBVariable+'&cBF='+cBFunc;
		//alert($value);
		AjTD.sIt($value, "getObjScrpt($response)" );
	}
};

var AjTD = {
	xml_http_obj_list : [],
	
	sIt : function ($value, $call_back_func){
		
		$value += '&reqType=ajax';
		var $h_label_type = 'text';
		var $h_value_type = 'text';
		//alert($call_back_func)
		var $id = make_id("xml");
		this.xml_http_obj_list[$id] = this.get_xml_http_obj();
		if( nl(this.xml_http_obj_list[$id]) ){
			alert("your Browser Does not Support HTTP Object.");
			return;
		}//end if
		
		var $params = $value + "&sid=" + make_id('Qheap');
		
		//BrowserDetect.init();
		if(isIe()){
			this.xml_http_obj_list[$id].onreadystatechange = Function ("AjTD.get_data(\""+ $call_back_func +"\",'"+ $id +"')");
		}else{
			this.xml_http_obj_list[$id].onload = Function ("AjTD.get_data(\""+ $call_back_func +"\",'"+ $id +"')");
		}//end if
		
		$url = isLocal() ? "di-ajax-req-handler.php" : ((document.location+'').match(/http:\/\/www\./gi)) ? 'http://www.didiran.com/di-ajax-req-handler.php' : 'http://didiran.com/di-ajax-req-handler.php';
			
		this.xml_http_obj_list[$id].open("POST" ,$url ,true);
		this.xml_http_obj_list[$id].setRequestHeader(this.get_header_label($h_label_type),this.get_header_value($h_value_type));
		try{
			this.xml_http_obj_list[$id].send($params);
		}//end try
		
		catch($err){
			if($err){alert("internet connection not available !");}
		}
		
	},
	
	get_header_value : function($type){
		var $value = '';
		switch($type){
			case('text'):
					$value = "application/x-www-form-urlencoded";
				break;
				
			case('file'):
					$value = "";
				break;
				
			default:
					$value = $type;
				break;
		}
		
		return($value);
	},
	
	get_header_label : function($type){
		var $label = '';
		switch($type){
			case('text'):
					$label = "Content-Type";
				break;
				
			case('file'):
					$label = "Content-Type";
				break;
				
			default:
					$label = $type;
				break;
		}
		
		return($label);
	},
	
	get_data : function ($call_back_func,$id){
		//alert(this.xml_http_obj_list[$id].readyState)
		if (this.xml_http_obj_list[$id].readyState == 4 || this.xml_http_obj_list[$id].readyState == "complete"){ 
			$status = this.check_status($id);
			//alert($status)
			if(!$status){return;}
			var $response = this.xml_http_obj_list[$id].responseText;
			this.xml_http_obj_list = del_arr_el(this.xml_http_obj_list,$id,'');
			if(nl($response)){$response='null';}
			eval($call_back_func);
		}
	},
	
	check_status : function($id){
		 switch(this.xml_http_obj_list[$id].status){
				  case(200): //good
						return(true);
						break;
						
				  case(408): 
				  case(504): //timeout
						alert('A problem occured talking to the server.  Please try again.');
						return(false);
				  default:
						alert('A problem occured talking to the server.  Please try again later.');
						return(false);
						break;
			}	
	},
	
	get_xml_http_obj : function(){
		var $xml_http_obj = null;
		if(window.XMLHttpRequest){
			$xml_http_obj = new XMLHttpRequest();
		}else if (window.ActiveXObject){

			var $active_x_objects = ['Microsoft.XMLHTTP','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP'];
			for(var $i = 0; $i < $active_x_objects.length; $i++){
				try{
					$xml_http_obj = new ActiveXObject($active_x_objects[$i]);
				}//end try
				catch(err){}
			}//end for
		//$xml_http_obj = new ActiveXObject("Msxml2.XMLHTTP");
		}
		return($xml_http_obj);
	}
};
