// AJAX FUNCTIONS
var rq=false;
var req=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  rq = new ActiveXObject("Msxml2.XMLHTTP");
  req = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   rq = new ActiveXObject("Microsoft.XMLHTTP");
   req = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   rq = false;
   req = false;
  }
 }
@end @*/
if (!rq && typeof XMLHttpRequest!='undefined') {
	try {
		rq = new XMLHttpRequest();
		req = new XMLHttpRequest();
	} catch (e) {
		rq=false;
		req=false;
	}
}
if (!rq && window.createRequest) {
	try {
		rq = window.createRequest();
		req = window.createRequest();
	} catch (e) {
		rq=false;
		req=false;
	}
}

var readyStates = new Array(
	"Initialising: please wait...",
	"Connection Opened",
	"Data sent",
	"Data Received",
	"Loaded!"
)

function confirmAjaxCall(msg,url,obj,method,fm) {
	if (confirm(msg)==1){
		ajaxPageCall(url,obj,method,fm);
	}
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function ajaxPageCall(url,obj,method,fm) {
	
	param = null;
	sendMethod = "GET";
	if(method=="post") {
		sendMethod = "POST";
		inputs = fm.getElementsByTagName("INPUT");	
		param = '';
		// process input fields:
		for(i=0;i<inputs.length;i++){
			if(inputs[i].type=="text"){
				param+=(i!=0)?'&':'';
				param+=inputs[i].name+'='+inputs[i].value;
			}
		}
	}
	obj = (typeof(obj)!='object') ? document.getElementById(obj) : obj;
	
	// Do AJAX request to update quick cart
	if(!rq) { 
		alert("Sorry, it looks like the quick change functionality is not functioning correctly\n\nPlease contact MJDIGITAL");
	} else { // check if XmlHttpRquest is available
		setImgLoader("show");
		// clear RQ
		rq.onreadystatechange = function () {}
		rq.abort();
		// open Async request
		rq.open(sendMethod,url,true);
		rq.onreadystatechange=function() {
		if (rq.readyState==4) {
			
			//alert(rq.responseText);
			//alert(obj.id);
			setLoaderText(" "); // clear loader Text
			origHeight = obj.offsetHeight;
			setImgLoader("hide");
			obj.innerHTML = rq.responseText;
			if(fObjFocus!="undefined") {
				fObjFocus(obj);
			}
			newHeight = obj.offsetHeight;
			// reset page Height
			if(newHeight<origHeight) { 
				remFromPageHeight(origHeight-newHeight);
			} else {
				addToPageHeight(newHeight-origHeight);
			}
		} else {
			setImgLoader("show");
			setLoaderText("<br />"+readyStates[rq.readyState],true);
		}
		}
			rq.send(param);
			return false;
	}	
}

function setLoaderText(txt,inc) {
	l = document.getElementById("div_images_overlay_text");
	l.innerHTML = (inc) ? l.innerHTML+txt : txt;
}

function setImgLoader(state) {
	ol = document.getElementById("div_images_overlay");
	div = document.getElementById("div_images");
	ol.style.width = (div.offsetWidth-2)+"px";
	ol.style.height = (div.offsetHeight-2)-(((div.offsetHeight/3)-15))+"px";
	ol.style.marginTop = "1px";
	ol.style.marginLeft = "1px";
	pad = ((div.offsetHeight/3)-15);
	pad = (pad<0) ? 0 : pad;
	ol.style.paddingTop = pad+"px";
	ol.style.visibility = (state == "show") ? "visible" : "hidden";
	
}

// STANDARD FUNCTIONS
function showImage(img) {
	notice = document.getElementById('div_notice_text');
	var image = '<a href="Javascript:;" onclick="setNotice(\'hide\')"><img src="'+img+'" border="0" alt="Click to close" /></a>';
	notice.innerHTML = image;
	setNotice('show');
}

function setNotice(state) {
	notice = document.getElementById('notice_holder');
	notice.style.display = (state=='show') ? 'block' : 'none';
	window.scrollTo(0,0); // scroll to the top of the page
}

var chRun = false;
var chAdd = 0;
var pageHeight = 0;

function columnHeights() {
		leftDiv = document.getElementById('div_menu_column');
		if(leftDiv) {
			midDiv = document.getElementById('div_content_column');
			highest = leftDiv.offsetHeight;
			if (midDiv) { highest = (midDiv.offsetHeight>highest) ? midDiv.offsetHeight : highest; }
			// apply style
			leftDiv.style.height = ((highest-15)+chAdd)+"px";
			midDiv.style.height = ((highest-15)+chAdd)+"px"; // minus 15 to allow for padding/margins
		
			pageHeight = ((highest-15)+chAdd);
		}
	chRun = true;
}

function addToPageHeight(px) {
	if(chRun == true) {
		leftDiv = document.getElementById('div_menu_column');
		midDiv = document.getElementById('div_content_column');
		if(leftDiv) leftDiv.style.height = (pageHeight + px)+"px";
		if(midDiv)   midDiv.style.height = (pageHeight + px)+"px";
		pageHeight = (pageHeight + px);
	} else {
		if(!window.addEventListener) {
			chAdd += px;
		}
	}
}

function remFromPageHeight(px) {
	if(chRun == true) {
		leftDiv = document.getElementById('div_menu_column');
		midDiv = document.getElementById('div_content_column');
		if(leftDiv) leftDiv.style.height = (pageHeight - px)+"px";
		if(midDiv)   midDiv.style.height = (pageHeight - px)+"px";
		pageHeight = (pageHeight - px);
	} else {
		if(!window.addEventListener) {
			chAdd -= px;
		}
	}
}

function toggleZoom(tw,th,fw,fh,obj) {
	if(obj.width==tw) {
		obj.width = fw;
		obj.height = fh;
		addToPageHeight(fh-th);
	} else {
		obj.width = tw;
		obj.height = th;
		remFromPageHeight(fh-th);
	}
}

function DelWithCon(deletepage_url,field_name,field_value,messagetext) { //v1.0 - Deletes a record with confirmation
  if (confirm(messagetext)==1){
	location.href = eval('\"'+deletepage_url+'?'+field_name+'='+field_value+'\"');
  }
}

if(window.addEventListener) window.addEventListener("load", columnHeights, false); // for non IE browsers
else if(window.attachEvent) window.attachEvent("onload", columnHeights);		   // for IE

