/**
 * ajax
 */


/**
 * ajaxrequest construct
 *
 */
var AjaxRequest = function()
{
	/*var response=null;
	var status=null;
	var statusText=null;
	*/
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
			this.request = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		var tmp = null;
		// code for IE6, IE5
		try{
			this.request = new ActiveXObject("MSXML2.XMLHTTP.4.0");
		}catch(ex){
			try{
				this.request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
			}catch(ex){
				try{
					this.request = new ActiveXObject("MSXML2.XMLHTTP");
				}catch(ex){
					try{
						this.request = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(ex){
						alert("Your browser does not have the correct ActiveXObject!");
						this.request = null;
					}
				}
			}
		}
	}else{
		alert("Your browser does not support XMLHTTP!");
	}
	if(this.request != null){
		//this.request.responseHandler = this;
		//alert(request.onresponsedata);
		//this.request.onresponsedata =  function(response, status, statusText) {};
		//this.request.onresponsedata =  function(response, status, statusText) {};
	}
}

//////////////////////////////////////
////	properties
//////////////////////////////////////


/**
 * property of default url
 * @var string
 */
AjaxRequest.prototype.defaultURL = "ajaxHandler2.php?";



//////////////////////////////////////
////	request-functions
//////////////////////////////////////


/**
 * executes request to default url
 * @var string data or undefined
 * @var string method or undefined = GET
 * @var string syncMode or undefined = false=async
 */
AjaxRequest.prototype.execute = function (data, method, syncMode)
{
	return this._sendRequest(this.defaultURL,data,method,syncMode);
}

/**
 * executes request to url
 * @var string url 
 * @var string data or undefined
 * @var string method or undefined = GET
 * @var string syncMode or undefined = false=async
 */
AjaxRequest.prototype.executeUrl = function (url, data, method, syncMode)
{
	return this._sendRequest(url,data,method,syncMode);
}

/**
 * executes the request
 *
 * @protected
 * @var string url 
 * @var string data 
 * @var string method
 * @var string syncMode
 */
AjaxRequest.prototype._sendRequest = function (url, data, method, syncMode)
{
	//set method
	if(typeof(method)=="undefined"){
		method="POST";
	}
	// set params
	if(typeof(data)=="undefined"){
		data="";
	}
	// sync-mode
	if(typeof(syncMode)=="undefined"){
		syncMode=true;
	}
	// sync-mode
	var paramsGET = "";
	var paramsPOST = (data=="" ? null : data);
	if(method=="GET"){
		paramsGET = data;
		paramsPOST= null;
	}
	if(syncMode==false){
		this.request.onreadystatechange = function(){}
	}else{
	// callback from request to this function
	this.request.onreadystatechange = function(){
		//this.onReadyStateChange;
		// status 0 UNINITIALIZED open() has not been called yet.
	    // status 1 LOADING send() has not been called yet.
	    // status 2 LOADED send() has been called, headers and status are available.
	    // status 3 INTERACTIVE Downloading, responseText holds the partial data.
	    // status 4 COMPLETED Finished with all operations.

	    switch( this.readyState )
	    {
	    	// uninitialized
	        case 0:
	        // loading
	        case 1:
	        // loaded
	        case 2:
	        // interactive
	        case 3:
	            break;
	        // complete
	        case 4:
	        	onResponseData(this.responseText, this.status,this.statusText);
	    	}
	   	 
	};
	}
	// set callback to this from request
	//this.request.onresponsedata=this.onResponseData;
	
	//opens the object
	this.request.open(method, url+paramsGET, syncMode );
	//set header if method is post
	if(method=="POST"){
		this.request.setRequestHeader("Method"			, "POST " + url+paramsGET + " HTTP/1.1");
        this.request.setRequestHeader('Content-Type'	, 'application/x-www-form-urlencoded' );
        this.request.setRequestHeader('Content-Length'	, (paramsPOST==null ? "0" : paramsPOST.length) );
	}
	//we send the request
	this.request.send(paramsPOST);    // param = POST data
	//if synchronized we have already the response
	if(syncMode==false){
		//call response-method directly
		return onResponseData(this.request.responseText, this.request.status, this.request.statusText);
	}else{
		return true;
	}
}



//////////////////////////////////////
////	response-functions
//////////////////////////////////////


function onResponseData(response, status, statusText)
{
	//alert("AJAX ONRESPONSE"+response+","+ status+","+ statusText);
	//i try to read it
	var result=null;
	try{
		eval("result="+response);
	}catch(ex){
		alert("ERROR "+ex+"\nRESPONSE: "+response);
	}
	try{
		//alert("I TRY "+result.remarkMethod+"(result.data);");
		eval(result.remarkMethod+"(result.data);");
	}catch(ex){
		alert("ERROR "+ex+"\nRESPONSE: remarkMethod failed");
	}

	//debug("RESULT data:"+result.data);
	return result;
}






function setVariantPullDown(params)
{
	alert("PARAMS are "+params);
	var cbData = [
		params.containerID,
		params.variantData,
		params.priceData,
		params.editorData,
		params.scaleData,
		params.pricePerPageData,
		params.groupImageData,
		params.groupEventData,
		params.languageData,
		params.offlineVisible
		];
	ax_setVariantPullDown(cbData);
}




function debug(message)
{
	var date = new Date();
	message = date.toLocaleString()+" - "+message+"<br/>\n";
	var container = document.getElementById('tmp_console');
	if(typeof(container)!="undefined" && container!=null ){
		container.innerHTML+=message;
	}
	container = null;		
}


