/*
$Id: js_functions.inc.js 3135 2007-11-16 03:49:22Z fesh $
*/

/* preload images */
if (document.images) {
	var spacerOn = new Image();
	spacerOn.src = "/images/spacer.gif";
}

var win=null;

function popup_image(image_url, title) {
	var winl = (screen.width - 200)/2;
	var wint = (screen.height - 300)/2;
	var settings ='top='+wint+',';
	settings +='left='+winl+',';
	settings +='width=200, ';
	settings +='height=300, ';
	settings +='scrollbars=no,';
	settings +='resizable=yes';

	title = title.replace(/\s/g, '%20');

	win=window.open('image_popup.php?image_url='+image_url+'&title='+title, 'image',settings);

	if(parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function prod_popup(image_type) {
	var winl = (screen.width - 700)/2;
	var wint = (screen.height - 550)/2;
	var settings ='top='+wint+',';
	settings +='left='+winl+',';
	settings +='width=700, ';
	settings +='height=550, ';
	settings +='scrollbars=no,';
	settings +='resizable=yes';
{/literal}
	win=window.open('/product_popup.php?style={$comp_product.style}&type='+image_type+'&colour='+current_colour, 'colours',settings);
{literal}
	if(parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function popup_gallery(g, i, w, h) {
	var winl = (screen.width - 700)/2;
	var wint = (screen.height - 500)/2;
	var settings ='top='+wint+',';
		settings +='left='+winl+',';
		settings +='width=700, ';
		settings +='height=500, ';
		settings +='scrollbars=no,';
		settings +='resizable=yes';

	win=window.open('/gallery_popup.php?g='+g+'&i='+i+'&w='+w+'&h='+h, 'image',settings);

	if(parseInt(navigator.appVersion) >= 4) {
	   win.window.focus();
	}
}

function PopupContentPage(w, h, page, resize) {
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings  ='height='+h+',';
		settings +='width='+w+',';
		settings +='top='+wint+',';
		settings +='left='+winl+',';
		settings +='scrollbars=yes,';
		settings +='resizable='+resize+'';

	win=window.open(page,'content',settings);
	if(parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}


// Usage = 'var blah = new createRequestObject();' and then to connect 'blah.connect(arg1, arg2, arg3, arg4);'
var createRequestObject = function() {
	var xmlhttp;
	var completeRequest = false;

	if (window.XMLHttpRequest) {	// Non-IE Browser
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch(error) {
			alert(error.message);
		}
	}
	else if (window.ActiveXObject) {	// Internet Explorer
		try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(error) {
			try {
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(error) {
				alert(error.message);
			}
		}
	}

	if (!xmlhttp) {
		return null;
	}

	/**
	  *  Function to create AJAX connection and process
	  *
	  * Args:
	  * @ sURL - URL for AJAX to connection
	  * @ sMethod - The request method to be used for the connection - POST or GET
	  * @ sVars - Request options (i.e. a=1&b=2)
	  * @ fnDone - Function to execute on successful AJAX return
	  *
	  * example:
	  * var xmlRequest = new xmlHttpRequestObject();
	  *       xmlRequest.connect('a.php', 'post', 'name=test&age=22', test);
	  *
	  **/
	this.connect = function(sURL, sMethod, sVars, fnDone) {
		if (!xmlhttp) {
			return false;
		}

		completeRequest = false;
		sMethod = sMethod.toUpperCase();

		try {
			if (sMethod == 'GET') {
				xmlhttp.open(sMethod, sURL + '?' + sVars, true);
				sVars = '';
			}
			else {
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader('Method', 'POST ' + sURL + ' HTTP/1.1');
				xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			}

			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && !completeRequest) {
					bComplete = true;

					if (fnDone) {
						fnDone(xmlhttp);
					}
				}
			};

			xmlhttp.send(sVars);
		}
		catch(z) {
			return false;
		}

		return true;
	};

	return this;
};
