function getAjaxResPayload($response,$actType){
	var $res=$response.split("[**]");
	if ($res[1]=="ok"){
		if ( $actType == 'norm'){
			return $res[2];
		}else if( $actType == 'dim_1'){
			$resPayLoad=$res[2].split("[^(*");
			for(var x in $resPayLoad){
				$resPayLoad[x] = String(Base64.decode($resPayLoad[x]));
			}//end for
			return $resPayLoad;
		}//end if
	}else{
		//alert("Invalid ajax res");
		return false;
	}
}

function ajaxScreenlocker($act){
	//alert($element);
	if($act=='lock'){
		fObj("fullBodyDivId").style.display='block';
		fObj("prodGroupId").disabled="disabled";
	}else if ($act=='unlock'){
		fObj("fullBodyDivId").style.display='none';
		fObj("prodGroupId").disabled="";
	}//end if
}//end func

function fObj(id){
	return document.getElementById(id);
}

function und(val){
	return (typeof(val)=='undefined');
}//end func

var BrowserDetect = {
	init: function(){
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	}//end func
	,
	searchString: function (data) {
		for( var i=0;i<data.length;i++ ){
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if( dataString ){
				if( dataString.indexOf(data[i].subString) != -1 )return(data[i].identity);
			}else if( dataProp ){
				return(data[i].identity);
			}//end if
		}//end for
	}//end func
	,
	searchVersion: function( dataString ){
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	}//end func
	,
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"

		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

}//end class

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);
	}//end if
}//end func

function nl($val){
	if(typeof($val) == 'object')return(false)
	return ($val==null || $val=='null' || $val=="");
}//end func

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);
}//end func






//*****************************************************************************

//var xmlHttp = '';
function get_data($value,$mode,$module,$sub_module,$call_back_func){ 

	transfer_data.send_data($value, $mode, $module, $sub_module, $call_back_func,'text','text');
	
}//end func


function fObj($id,$parent) {
	//alert("fObj :"+id)
	if( typeof($parent) == 'object'){
		var e = $parent.document.getElementById($id);
	}else{
		var e = document.getElementById($id);
	}//end if
	if(e){
		return(e);
	}else{
		return(false);
	}//end if
}//end func

var Base64 = {
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function(input){
		if( nl(input) ){
			//alert("[base64-encode] : input is null.")
			return;
		}//end if
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = Base64._utf8_encode(input);
		while(i < input.length){

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if(isNaN(chr2)){
				enc3 = enc4 = 64;
			}else if(isNaN(chr3)) {
				enc4 = 64;
			}//end if

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}//end while
		return(output);
	}//end func
	,

	// public method for decoding
	decode : function(input){
		if( nl(input) ){
			//alert("[base64-decode] : input is null.")
			return;
		}//end if
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while(i < input.length){
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);
			if(enc3 != 64){
				output = output + String.fromCharCode(chr2);
			}//end if
			if(enc4 != 64){
				output = output + String.fromCharCode(chr3);
			}//end if
		}//end while
		output = Base64._utf8_decode(output);
		return(output);
	}//end func
	,

	// private method for UTF-8 encoding
	_utf8_encode : function(string){
		if( nl(string) ){
			//alert("[base64-_utf8_decode] : input is null. ")
			return;
		}//end if
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for(var n = 0; n < string.length; n++){
			var c = string.charCodeAt(n);
			if(c < 128){
				utftext += String.fromCharCode(c);
			}else if((c > 127) && (c < 2048)){
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}else{
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}//end if
		}//end for
		return(utftext);
	}//end func
	,

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		if( nl(utftext) ){
			//alert("[base64-_utf8_decode] : input is null. ")
			return;
		}//end if
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while( i < utftext.length ){
			c = utftext.charCodeAt(i);
			if(c < 128){
				string += String.fromCharCode(c);
				i++;
			}else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}else{
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}//end if
		}//end while
		return(string);
	}//end func
}//end class


function numberSplitor($num){
	$num = String(parseInt($num));
	var $strLen=$num.length-1;
	var $outval = '';
	var $currentDigit = '';
	var $dotNotifier = 1;
	while ($strLen >= 0){
		$currentDigit = $num.charAt($strLen);
		$outval = $currentDigit + $outval;
		if ( parseInt($dotNotifier/3) == ($dotNotifier/3) ) $outval = '.' + $outval;
		$strLen--;
		$dotNotifier++;
	}
	if ($outval.charAt(0) == '.') $outval = $outval.substring(1,$outval.length);
	
	return $outval;

}//end func

//******************* AJAX PART ***************************//

//alert("ajax load .js");

var myTransfer_data = {
	xml_http_obj_list : []
	,
	send_data : function ($value, $call_back_func){

		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]) ){
			send_info("your Browser Does not Support HTTP Object.");
			return;
		}//end if
		
		var $params = $value + "&sid=" + make_id('Qheap');
		
		BrowserDetect.init();
		if(BrowserDetect.browser =='Explorer' || BrowserDetect.browser =='Opera'){
			this.xml_http_obj_list[$id].onreadystatechange = Function ("myTransfer_data.get_data(\""+ $call_back_func +"\",'"+ $id +"')");
		}else{
			this.xml_http_obj_list[$id].onload = Function ("myTransfer_data.get_data(\""+ $call_back_func +"\",'"+ $id +"')");
		}//end if
		
		//alert('params : '+ $params)
		$url = "ajax_handler.php";
		
		//alert($url)
		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 !");
		}//end catch
		
	}//end func
	,
	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;
		}//end switch
		
		return($value);
	}//end func
	,
	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;
		}//end switch
		
		return($label);
	}//end func
	,
	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);
		}//end if
	}//end 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;
			}//end switch	
	}//end func
	,
	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 = ['Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.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");
		}//end if
		return($xml_http_obj);
	}//end func
}//end class

var ajResV = { //ajax response validator

	m_mainSplitor 	: '[*]'
	,
	m_garbage 		: ''
	,
	m_stat 			: ''// 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_stat: " +this.m_stat +"\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)){
			return false;
		}
				
		var $det = this.m_value.split('[*]');
		this.m_garbage 	= $det[0];
		this.m_stat 	= $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{
			alert('it seems you are using old version of ajax validate');
			this.dump();
			
		}//end if
		
		if(this.m_stat == '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_stat = 'err';
			this.m_errNum = 1;
			this.m_errMsg = 'value is empty or null';
			return true; 
		}
		this.m_stat = 'ok';
		this.m_errMsgNum = 0;
		this.m_errMsg = 'value is not empty or null';
		return false;
	}
	
}//end obj
