// JavaScript Document by Denford Berriman 

//---------------------------
//   AJAX
//---------------------------
function openAjax() {
	var ajax;
	try{
		ajax = new XMLHttpRequest();
	}catch(ee){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				ajax = false;
			}
		}
	}
	return ajax;
}

function CpForm(FormName){
	comp = "document." + FormName;
	var frm = eval(comp);
	Cps = "";
	for (i=0; i<frm.length; i++){
		Cps = Cps + frm.elements[i].name + "=" + frm.elements[i].value + "&";
	}
	Cps = Cps.substring(0,Cps.length -1);
	return Cps;
}

function make_array_from_inputs(FormName){
	comp = "document." + FormName;
	var frm = eval(comp);
	Cps = "";
	for (i=0; i<frm.length; i++){
		val = encodeURIComponent(frm.elements[i].value)
		Cps = Cps + frm.elements[i].name + "=" + encodeURIComponent(val) + "&";
	}
	Cps = Cps.substring(0,Cps.length -1);
	return Cps;
}

// ----  myAjax
function myAjax(page_to_load,destination_div_id,url_vars_or_form_name,loading_content,loading_message_div,method_used,show_loading,loadergif) { 

	if(document.getElementById) {
		
		var ajax = openAjax();
		
		//---start denford --- add below to stop multiple requests at same time, let's keep it to just the most recent request.
		//maybe neew to refer to 'ajax'
		ajax.abort();
		//---end denford ---
		
		if(show_loading=='1'){
			var loading_div_content = document.getElementById(loading_message_div);
		}
		var output_div_content = document.getElementById(destination_div_id);
		if(method_used=='1'){
			ajax.open("POST", page_to_load, true);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			ajax.setRequestHeader("Pragma", "no-cache");
			url_string = make_array_from_inputs(url_vars_or_form_name)
		}else{
			url_string = null
			ajax.open("GET", page_to_load + url_vars_or_form_name, true);
		}
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				if(show_loading=='1'){
					loading_div_content.style.display = 'inline';
					loading_div_content.innerHTML = "<div align='center'><div class='space10'></div><img src='"+loadergif+"' alt=' Loading..' /><div class='space10'></div></div>"
				}else{
					output_div_content.innerHTML = ""
				}
			}
			if(ajax.readyState == 4) {
				if(show_loading=='1'){
					loading_div_content.innerHTML = ""
					//loading_div_content.style.display = 'none';
				}else{
					output_div_content.innerHTML = ""
				}
				if(ajax.status == 200) {
					var output_div = null;
					output_div = ajax.responseText;
					output_div = output_div.replace(/\+/g," ");
					output_div = unescape(output_div);
					output_div_content.innerHTML = output_div;
					//roundAddBox();
					//rollUp('addProd');
					//roundList();
				} else {
				output_div_content.innerHTML = "<div class'achtung' align='center'>an error has occured<br />please try again</div>";
					return false
				}
			}
		}
		ajax.send(url_string);
	}		 
}

//motherlist - nifty work around
function motherAjax2(page_to_load,destination_div_id,url_vars_or_form_name,loading_content,loading_message_div,method_used,show_loading,loadergif) { 
	
	if(document.getElementById) {
		var ajax = openAjax();
		if(show_loading=='1'){
			var loading_div_content = document.getElementById(loading_message_div);
		}
		var output_div_content = document.getElementById(destination_div_id);
		if(method_used=='1'){
			ajax.open("POST", page_to_load, true);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			ajax.setRequestHeader("Pragma", "no-cache");
			url_string = make_array_from_inputs(url_vars_or_form_name)
		}else{
			url_string = null
			ajax.open("GET", page_to_load + url_vars_or_form_name, true);
		}
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				if(show_loading=='1'){
					loading_div_content.style.display = 'inline';
					loading_div_content.innerHTML = "<div align='center'><div class='space10'></div><img src='images/"+loadergif+"' alt=' Loading..' /><div class='space10'></div></div>"
				}else{
					output_div_content.innerHTML = ""
				}
			}
			if(ajax.readyState == 4) {
				if(show_loading=='1'){
					loading_div_content.innerHTML = ""
					//loading_div_content.style.display = 'none';
				}else{
					output_div_content.innerHTML = ""
				}
				if(ajax.status == 200) {
					var output_div = null;
					output_div = ajax.responseText;
					output_div = output_div.replace(/\+/g," ");
					output_div = unescape(output_div);
					output_div_content.innerHTML = output_div;
					Nifty('div.motherList','medium')
					//roundAddBox();
					//rollUp('addProd');
					//roundList();
				} else {
				output_div_content.innerHTML = "<div class'achtung' align='center'>an error has occured<br />please try again</div>";
					return false
				}
			}
		}
		ajax.send(url_string);
	}		 
}
//motherform
function motherAjax(page_to_load,destination_div_id,url_vars_or_form_name,loading_content,loading_message_div,method_used,show_loading,loadergif) { 
	
	if(document.getElementById) {
		var ajax = openAjax();
		if(show_loading=='1'){
			var loading_div_content = document.getElementById(loading_message_div);
		}
		var output_div_content = document.getElementById(destination_div_id);
		if(method_used=='1'){
			ajax.open("POST", page_to_load, true);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			ajax.setRequestHeader("Pragma", "no-cache");
			url_string = make_array_from_inputs(url_vars_or_form_name)
		}else{
			url_string = null
			ajax.open("GET", page_to_load + url_vars_or_form_name, true);
		}
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				if(show_loading=='1'){
					loading_div_content.style.display = 'inline';
					loading_div_content.innerHTML = "<div align='center'><div class='space10'></div><img src='images/"+loadergif+"' alt=' Loading..' /><div class='space10'></div></div>"
				}else{
					output_div_content.innerHTML = ""
				}
			}
			if(ajax.readyState == 4) {
				if(show_loading=='1'){
					loading_div_content.innerHTML = ""
					//loading_div_content.style.display = 'none';
				}else{
					output_div_content.innerHTML = ""
				}
				if(ajax.status == 200) {
					var output_div = null;
					output_div = ajax.responseText;
					output_div = output_div.replace(/\+/g," ");
					output_div = unescape(output_div);
					output_div_content.innerHTML = output_div;
					motherAjax2('plates/motherListPlate.php','motherListBox','?r=1','loading..','motherListBox','2','1','loaderWonO.gif')
					Nifty('div.motherList','medium')
					//roundAddBox();
					//rollUp('addProd');
					//roundList();
				} else {
				output_div_content.innerHTML = "<div class'achtung' align='center'>an error has occured<br />please try again</div>";
					return false
				}
			}
		}
		ajax.send(url_string);
	}		 
}


//motherlist - nifty work around
function eventAjax2(page_to_load,destination_div_id,url_vars_or_form_name,loading_content,loading_message_div,method_used,show_loading,loadergif) { 
	
	if(document.getElementById) {
		var ajax = openAjax();
		if(show_loading=='1'){
			var loading_div_content = document.getElementById(loading_message_div);
		}
		var output_div_content = document.getElementById(destination_div_id);
		if(method_used=='1'){
			ajax.open("POST", page_to_load, true);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			ajax.setRequestHeader("Pragma", "no-cache");
			url_string = make_array_from_inputs(url_vars_or_form_name)
		}else{
			url_string = null
			ajax.open("GET", page_to_load + url_vars_or_form_name, true);
		}
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				if(show_loading=='1'){
					loading_div_content.style.display = 'inline';
					loading_div_content.innerHTML = "<div align='center'><div class='space10'></div><img src='images/"+loadergif+"' alt=' Loading..' /><div class='space10'></div></div>"
				}else{
					output_div_content.innerHTML = ""
				}
			}
			if(ajax.readyState == 4) {
				if(show_loading=='1'){
					loading_div_content.innerHTML = ""
					//loading_div_content.style.display = 'none';
				}else{
					output_div_content.innerHTML = ""
				}
				if(ajax.status == 200) {
					var output_div = null;
					output_div = ajax.responseText;
					output_div = output_div.replace(/\+/g," ");
					output_div = unescape(output_div);
					output_div_content.innerHTML = output_div;
					Nifty('div.eventList','medium')
					//roundAddBox();
					//rollUp('addProd');
					//roundList();
				} else {
				output_div_content.innerHTML = "<div class'achtung' align='center'>an error has occured<br />please try again</div>";
					return false
				}
			}
		}
		ajax.send(url_string);
	}		 
}
//eventform
function eventAjax(page_to_load,destination_div_id,url_vars_or_form_name,loading_content,loading_message_div,method_used,show_loading,loadergif) { 
	
	if(document.getElementById) {
		var ajax = openAjax();
		if(show_loading=='1'){
			var loading_div_content = document.getElementById(loading_message_div);
		}
		var output_div_content = document.getElementById(destination_div_id);
		if(method_used=='1'){
			ajax.open("POST", page_to_load, true);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			ajax.setRequestHeader("Pragma", "no-cache");
			url_string = make_array_from_inputs(url_vars_or_form_name)
		}else{
			url_string = null
			ajax.open("GET", page_to_load + url_vars_or_form_name, true);
		}
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				if(show_loading=='1'){
					loading_div_content.style.display = 'inline';
					loading_div_content.innerHTML = "<div align='center'><div class='space10'></div><img src='images/"+loadergif+"' alt=' Loading..' /><div class='space10'></div></div>"
				}else{
					output_div_content.innerHTML = ""
				}
			}
			if(ajax.readyState == 4) {
				if(show_loading=='1'){
					loading_div_content.innerHTML = ""
					//loading_div_content.style.display = 'none';
				}else{
					output_div_content.innerHTML = ""
				}
				if(ajax.status == 200) {
					var output_div = null;
					output_div = ajax.responseText;
					output_div = output_div.replace(/\+/g," ");
					output_div = unescape(output_div);
					output_div_content.innerHTML = output_div;
					eventAjax2('plates/eventsListPlate.php','eventsListBox','?r=1','loading..','eventsListBox','2','1','loaderWonO.gif')
					Nifty('div.eventsList','medium')
					//roundAddBox();
					//rollUp('addProd');
					//roundList();
				} else {
				output_div_content.innerHTML = "<div class'achtung' align='center'>an error has occured<br />please try again</div>";
					return false
				}
			}
		}
		ajax.send(url_string);
	}		 
}

//---------------------------
