function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sendRequest(address, responseHandler) {
	http.open('get', address);
    http.onreadystatechange = responseHandler;
    http.send(null);
}

function addGroupHandler () {
	if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
			document.getElementById('message').innerHTML = update;
        }
    }
}

function addPromoHandler () {
	if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
			document.getElementById('promo').innerHTML = update;
			document.getElementById("strTitle").value = '';
			document.getElementById("strBodyPromo").value = '';
			document.getElementById("promoSave").style.display = 'none';
        }
    }
}

function checkValuesAndSendRequest () {
	blnFailed = false;
	for( var i = 3; i < arguments.length; i++ ) {
		if (
			( arguments[i]  == 'undefined' ) ||
			( arguments[i]  == '' )
		) {
			blnFailed = true;
		}
		if ( !blnFailed ){
			sendRequest ( arguments[0], arguments[1] );
		} else {
			alert ( arguments[2] );
		}
	}
}

function confirmDelete (strAction,strSubAction,strKeys,customMessage) {
	if ( !customMessage ){
		customMessage = 'Are you sure you want to delete this item?';
	}
	if ( confirm( customMessage )){
		ajaxContent(strAction,strSubAction,strKeys)
	}
}

function confirmAjaxDelete ( strURL, responseHandler, customMessage ) {
	if ( !customMessage ){
		customMessage = 'Are you sure you want to delete this item?';
	}
	if ( confirm( customMessage )){
		sendRequest( strURL, responseHandler );
//		return true;
	} else {
//		return false;
	}
}

function loading(){
	var loadingDisplay = document.getElementById('overlay');
	loadingDisplay.style.display = "block";
}

function hideloading(){
	var loadingDisplay = document.getElementById('overlay');
	loadingDisplay.style.display = "none";
}

//Browser Support Code
function ajaxContent(strAction,strSubAction,strKeys,tiny){
	loading();
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajax');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			hideloading();
		}
	}
	if (strSubAction) {
		action = "?strAction=" + strAction + "&strSubAction=" + strSubAction;
	} else {
		action = "?strAction=" + strAction;
	}
	var queryString = action + "&" + strKeys + "&ajax=1";
	ajaxRequest.open("GET", "index.php" + queryString, true);
	ajaxRequest.send(null); 
}


//AJAX BUILT FOR THE AJAX LIST

//Browser Support Code
function ajaxList(id,table){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajax_'+table);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "pageObjects/ajaxList.php?ajaxId=" + id + "&ajaxTable=" + table, true);
	ajaxRequest.send(null); 
}

function updateAjax(){
	var x;
	var tables = new Array();
	
	if(parent.document.getElementById('ajaxValues')){
		var longstring = parent.document.getElementById('ajaxValues').value;
		var tables=longstring.split(";");
		for (x in tables){
			ajaxList(parent.document.getElementById('idPage').value,tables[x]);
		}
	}
}
