/**
 * RGX - Webdevelopment ( www.rgx.nl )
 *
 * @author Mark van der Velden
 * @verson 1.4
 */



/**
 * xmlHttp initiator
 */
function getAJAXObj() {
	var xmlHttp;

	try {	// Firefox, Opera 8.0+, Safari

		xmlHttp=new XMLHttpRequest();

	} catch (e) {

		try {	// Internet Explorer
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {
			try {	// Oldskool Internet Explorer
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	return xmlHttp;
}




/**
 * Collection of wrappers for storeObject
 */
function storePage(objectID, updateTarget) {
	storeObject(objectID, 'page', updateTarget);
}

function storeProduct(objectID, updateTarget) {
	storeObject(objectID, 'product', updateTarget);
}



/**
 * The `magic`
 * Here we call ajax.php with all gathered information and feed it back to
 * our page
 */
function storeObject(objectID, objectType, updateTarget) {

	var xmlHttp = getAJAXObj();
	var	action	= '';

	if (xmlHttp) {
		if (updateTarget) {
			xmlHttp.onreadystatechange=function() {
				if(xmlHttp.readyState==4) {
					updateTarget.innerHTML = xmlHttp.responseText;
				}
			}
		}

		switch(objectType) {
			case 'product':
				action	= 'storeproduct';
				break;
			case 'page':
				action	= 'storepage';
				break;
			default:
				action	= 'none';
		}

		xmlHttp.open("GET", "ajax.php?action="+ action +"&objectid="+ objectID, true);
		xmlHttp.send(null);
	}
}




/**
 * Show a content object
 * returns true on failure, false on success
 * This is to trigger the url's default href as fallback
 */
function showObject(actionName, updateTarget, objectID) {
	updateTarget.innerHTML = '<iframe allowtransparency="allowtransparency" frameborder="0" scrolling="no" src="ajax.php?action='+ actionName +'&amp;objectid='+ objectID +'" width="240" height="500" name="iframeHolder"></iframe>';
	return false;
}
