//<![CDATA[

// AJAX object.
var ajaxRequest = createXmlHttpRequestObject();

// Return XMLHttpRequest object.
function createXmlHttpRequestObject() {
	// Object to handle XMLHttpRequest.
  	var ajaxRequest;

  	try { // Mozilla, Safari, Opera ...
		ajaxRequest = new XMLHttpRequest();
    }
    catch (e) {
    	try {
    		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    	}
    	catch (e) {
    		try { // IE
    			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		catch (e) {
				ajaxRequest = false;
    		}
    	}
	}

  	// Return result.
  	if (!ajaxRequest) {
		alert("Nie masz AJAXa");
    	return  false;
    }
  	else
    	return ajaxRequest;
}

// Method to send AJAX request.
// @param String action Name of action.
// @param String actionMethod Name of function to execute.
// @param Array parameters Array with arguments to send.
// @param Function functionResponse Name of function to execute.
// @param String formButton Name of button.
// @param String messageBox Name of message box div.
function ajaxRequestMethod(action, actionMethod, parameters, functionResponse, formButton, messageBox) {
	// just call extended function with customText = 'zapisywanie'
	ajaxRequestMethodExtended(action, actionMethod, parameters, functionResponse, formButton, messageBox, 'zapisywanie...', 1, 'GET');
}

// Method to send AJAX request.
// @param String action Name of action.
// @param String actionMethod Name of function to execute.
// @param Array parameters Array with arguments to send.
// @param Function functionResponse Name of function to execute.
// @param String formButton Name of button.
// @param String messageBox Name of message box div.
function ajaxRequestMethodPost(action, actionMethod, parameters, functionResponse, formButton, messageBox) {
	// just call extended function with customText = 'zapisywanie'
	ajaxRequestMethodExtended(action, actionMethod, parameters, functionResponse, formButton, messageBox, 'zapisywanie...', 1, 'POST');
}


// Method to send AJAX request.
// @param String action Name of action.
// @param String actionMethod Name of function to execute.
// @param Array parameters Array with arguments to send.
// @param Function functionResponse Name of function to execute.
// @param String formButton Name of button.
// @param String messageBox Name of message box div.
// @param String customText to show when doing something
function ajaxRequestMethodExtended(action, actionMethod, parameters, functionResponse, formButton, messageBox, customText, resend, method) {
	// Make request
  	if (ajaxRequest.readyState == 4 || ajaxRequest.readyState == 0) {
		// Set message on messageBox.
  		if (messageBox != null) {
			// box object
			var box = new GetObject(messageBox);
			box.obj.innerHTML = customText;
		}
		
		// show ajax loader
		ajaxShowLoader();

		// Lock send button.
		if (formButton != null) {
			// Value of formButton object.
			var formButton = new GetObject(formButton);
			formButton.obj.disabled = true;
		}

		// Make arguments list to send.
		var toSend = '';
		for (var i = 0; i < parameters.length; i++) {
			var parameter = new GetObject(parameters[i]);
			if(validValue(parameter, "isEmpty", null, "test")) {
				toSend = toSend + parameters[i] + '=' + encodeURIComponent(parameter.obj.value) + '&';
			}
		}
  		// Execute requerst.
  		if(method == 'GET')
        	ajaxRequest.open('GET', 'index.php?do=' + action + '&ajax=true&action=' + actionMethod + '&' + toSend + new Date().getTime(), true);
        else{
        	var post = 'do=' + action + '&ajax=true&action=' + actionMethod + '&' + toSend + new Date().getTime();
			ajaxRequest.open('POST', 'index.php', true);
        	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			//ajaxRequest.setRequestHeader("Content-length", post.length);
			//ajaxRequest.setRequestHeader("Connection", "close");
        }

        // Set method to response.
        ajaxRequest.onreadystatechange = functionResponse;

        // Send data to server.
        if(method == 'GET')
        ajaxRequest.send(null);
        else
        ajaxRequest.send(post);
  	}
  	else if(resend == 1){
    	// Repeate request.
    	setTimeout('ajaxRequest(' + parameters + ', ' + functionResponse + ', ' + formButton + ', ' + messageBox + ')', 1000);
    }
}

function ajaxRequestMethodSimple(action, actionMethod, parameters, functionResponse, formButton, messageBox, customText, resend) {
	ajaxRequestMethodSimpleChoose(action, actionMethod, parameters, functionResponse, formButton, messageBox, 'GET', customText, resend);
}

// Method to send AJAX request.
// @param String action Name of action.
// @param String actionMethod Name of function to execute.
// @param Array parameters Array with arguments to send.
// @param Function functionResponse Name of function to execute.
// @param String formButton Name of button.
// @param String messageBox Name of message box div.
function ajaxRequestMethodSimpleChoose(action, actionMethod, parameters, functionResponse, formButton, messageBox, method, customText, resend, method) {
	// check custom text value
	if(typeof(customText) == "undefined")
		customText = 'zapisywanie...';
	// check resend
	if(typeof(resend) == "undefined")
		resend = 0;
		
	// Make request
  	if (ajaxRequest.readyState == 4 || ajaxRequest.readyState == 0) {
		// Set message on messageBox.
  		if (messageBox != null) {
			// box object
			var box = new GetObject(messageBox);
			box.obj.innerHTML = customText;
		}
			
		// show ajax loader
		ajaxShowLoader()

		// Lock send button.
		if (formButton != null) {
			// Value of formButton object.
			var formButton = new GetObject(formButton);
			formButton.obj.disabled = true;
		}

		// Make arguments list to send.
		var toSend = parameters + '&';

  		// Execute requerst.
  		// Execute requerst.
  		if(method == 'GET')
	    	ajaxRequest.open('GET', 'index.php?do=' + action + '&ajax=true&action=' + actionMethod + '&' + toSend + new Date().getTime(), true);
        else{
        	var post = 'do=' + action + '&ajax=true&action=' + actionMethod + '&' + toSend + new Date().getTime();
			ajaxRequest.open('POST', 'index.php', true);
        	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			//ajaxRequest.setRequestHeader("Content-length", post.length);
			//ajaxRequest.setRequestHeader("Connection", "close");
        }

        // Set method to response.
        ajaxRequest.onreadystatechange = functionResponse;

        // Send data to server.
        if(method == 'GET')
        	ajaxRequest.send(null);
        else
        	ajaxRequest.send(post);
    	
  	}else if(resend == 1){
    	// Repeate request.
    	setTimeout('ajaxRequest("' + parameters + '",' + functionResponse + ', ' + formButton + ',' + messageBox + ')', 1000);
    }
}

// Method to get AJAX response.
// @param String formButton Name of button.
// @return Boolean.
function ajaxResponseMethod(formButton) {
  	// If transaction is completed.
  	if (ajaxRequest.readyState == 4) {
    	// If transaction is completed successful.
    	if (ajaxRequest.status == 200) {
			// Unlock send button.
			if (formButton != null) {
				// Value of formButton object.
				var formButton = new GetObject(formButton);
				formButton.obj.disabled = false;
			}
			
			// hide ajax loader
			ajaxHideLoader();
			
			// Return response.
      		return ajaxRequest.responseText;
    	}
    	// If transaction is completed failed.
    	else {
		  	alert("Tranzakcja zakończona niepowodzeniem: " + ajaxRequest.statusText);
    	}
  	}
}

function ajaxHideLoader(){
	var ajaxLoaderTop = new GetObject('ajax_loader_top');
	ajaxLoaderTop.styl.backgroundImage = '';
}
function ajaxShowLoader(){
	var ajaxLoaderTop = new GetObject('ajax_loader_top');
	ajaxLoaderTop.styl.backgroundImage = 'url("gfx/ajax_loader_top.gif")';
}

//]]>
