var t ='';

if (document.images) {
  	var preload_images		= new Array();
  	preload_images[0]		= new Image();
  	preload_images[0].src	= siteurl+"/core/images/loading.gif";
}


var xmlHttp		= createXmlHttpRequestObject();
var ajaxQueue	= new Array();

function createXmlHttpRequestObject() {
	var xmlHttp;
	
	try { // try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}catch(e){ // assume IE6 or older
		var XmlHttpVersions	=	new Array(	"MSXML2.XMLHTTP.6.0",
											"MSXML2.XMLHTTP.5.0",
											"MSXML2.XMLHTTP.4.0",
											"MSXML2.XMLHTTP.3.0",
											"MSXML2.XMLHTTP",
											"Microsoft.XMLHTTP");

		// try every version until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try { // try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}catch(e){} // Your browser does not support AJAX, ignore the error here we check below
		}
	}

	// return the created object or display an error message
	if(!xmlHttp) {
 		return false;
	}else{
		return xmlHttp;
	}
}

// the function handles the validation for any form field
function add2Queue(urlLoc, postParamaters, showLoading, parentObj) {
	// only continue if xmlHttp isn't void
	if(!xmlHttp)			{ return false; 			}
	if(!urlLoc) 	    	{ return false;	    		}
	if(!postParamaters) 	{ postParamaters = null;	}
	if(!parentObj) 			{ parentObj = '';			}
	if(showLoading != 0)    { showLoading = 1; 			}
	
	// create a package 
	var ajaxDetails = new Array(urlLoc, postParamaters, showLoading, parentObj);

	ajaxQueue.push(ajaxDetails);
	processQueue();
}

function processQueue() {
	// only continue if xmlHttp isn't void
	if(!xmlHttp) { return false; }
	// try to connect to the server
	try {
		// continue only if the XMLHttpRequest object isn't busy
		// and the cache is not empty
		if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && ajaxQueue.length > 0) {
			// get a new set of parameters from the cache
			var cacheEntry = ajaxQueue.shift();
				// should we show the loading screen
				if(cacheEntry[2] == 1) { pLoading(); }
				
				// make a server request to validate the extracted data
				xmlHttp.open("POST", cacheEntry[0], true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = function() { handleRequestStateChange(cacheEntry[3]); }
				xmlHttp.send(cacheEntry[1]);
		}
	}catch(e){ // display an error when failing to connect to the server
		alert(e.toString());
	}
}

// function that handles the HTTP response
function handleRequestStateChange(objParent) {
	if(!xmlHttp) { return; }

	// when readyState is 4, we read the server response
	if(xmlHttp.readyState == 4) { // continue only if HTTP status is "OK"
		if(xmlHttp.status == 200) {
			try { // read the response from the server
				readResponse(objParent);
			}catch(e){ // display error message
				alert(e.toString());
			}
		}
	}
}

function readResponse(objParent) {
	if(document.getElementById(objParent)) {
		var objContainer = document.getElementById(objParent);
	}else{
		var objContainer = document.createElement('div');
			objContainer.id = objParent;
			document.body.appendChild(objContainer);
			centerDiv(objContainer);
	}
	
	removeObj('loading');
	
	objContainer.innerHTML = xmlHttp.responseText;
}

function removeObj(obj) {
	if(typeof(obj) == 'string') {
		if(document.getElementById(obj)) {
			var cntObj = document.getElementById(obj);
		}else{
			return false;
		}
	}else{
		var cntObj = obj;
	}

	cntObj.parentNode.removeChild(cntObj);
}

function pLoading() {
	if(!document.getElementById('blackout')) {
		var objCreate = document.createElement('div');
		objCreate.id = 'blackout';
		document.body.appendChild(objCreate);
		pBlackout(objCreate);
	}
	
	
	if(document.getElementById('loading')) { return false; }

	// create our container
	var objCreate = document.createElement('div');
		objCreate.id = 'loading';
				
	// create our loding text
	var subCreate = document.createElement('p');
		// create our loading image
		
	var imgCreate = document.createElement('img');
		imgCreate.src = siteurl+'/core/images/loading.gif';
		subCreate.appendChild(imgCreate);

	subCreate.appendChild(document.createTextNode('Loading'));
	objCreate.appendChild(subCreate);
	
	// attach our container
	document.body.appendChild(objCreate);
	centerDiv(objCreate);
}

function pBlackout(obj) {
	var pgdimensions		= getPageSizeWithScroll();
		obj.style.width 	= '100%';
		obj.style.height 	= pgdimensions[1]+'px';
		obj.style.left 		= '0px';
		obj.style.top 		= '0px';
		obj.style.display 	= 'block';

	setOpacity('7', obj);
}

function setOpacity(level, obj) {
	if(typeof(obj.style.opacity)	=='string')	{ obj.style.opacity	= level/10; }
	if(typeof(obj.style.filter)		=='string')	{ obj.style.filter	= 'alpha(opacity=' + level*10 + ')'; }
}

function fadeIn(obj) {
  if(document.getElementById(obj)) {
    clearTimeout(t);
    fadeObj = document.getElementById(obj);
    setOpacity(0, fadeObj);
    fadeObj.style.visibility = 'visible';
    t=setTimeout('notchOpacityUp(0, "'+obj+'");',200);
  }
}

function notchOpacityUp(value, obj) {
  value++;
  if(value>10) { return; }
  clearTimeout(t);
  t=setTimeout('notchOpacityUp('+value+', "'+obj+'");',200);
  setOpacity(value, document.getElementById(obj));
  return;
}

function fadeOut(obj) {
  if(document.getElementById(obj)) {
    setOpacity(10, document.getElementById(obj));
    document.getElementById(obj).style.visibility = 'visible';
    clearTimeout(t);
    t=setTimeout('notchOpacityDown(10,"'+obj+'");',400);
  }
}

function notchOpacityDown(value, obj) {
  value--;
  if(value<0) { return; }
  t=setTimeout('notchOpacityDown('+value+', "'+obj+'");',400);
  setOpacity(value, document.getElementById(obj));
  return;
}

function getPageSizeWithScroll(){
	if(window.innerHeight && window.scrollMaxY) { // Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	}else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll, yWithScroll);

	return arrayPageSizeWithScroll;
}

function centerDiv(divobj) {
	divobj.style.top = '0px';
	divobj.style.left = '0px';
	divobj.style.display = 'block';
	var ie=document.all && !window.opera
	var dom=document.getElementById
	var x = ((document.body.clientWidth-divobj.clientWidth)/2);
	this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
	var scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
	var docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
	var docheightcomplete=(this.standardbody.offsetHeight>this.standardbody.scrollHeight)? this.standardbody.offsetHeight : this.standardbody.scrollHeight //Full scroll height of document
	var objheight=divobj.offsetHeight //height of div element
	var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+15+"px" //Vertical position of div element: Either centered, or if element height larger than viewpoint height, 10px from top of viewpoint
	divobj.style.left=x+'px';
	divobj.style.top=Math.floor(parseInt(topposition))+"px"
}

function loadImage(id) {
	if(!document.getElementById('main_image')) { return false; }else{ var mainObj = document.getElementById('main_image'); }
	
	mainObj.src = prod_images[id].src;
}
