function createRequestObject() {
	var req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
    
    return req ;
}

var http = createRequestObject(); // We create the HTTP Object 

function handleTitleHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			document.getElementById("content_header_admin").innerHTML = '<a href="javascript:swapHeader();">'+http.responseText+'</a>' ;
		}
	}
}

function handleFeaturedHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			document.getElementById("featured").innerHTML = '<em>This is now the featured product</em>' ;
		}
	}
}

function handleDropImgHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			window.location = 'products_admin.php?product='+http.responseText ;
		}
	}
}














//functions
function rollover(imgId,replaceImg) {
	//alert(replaceImg) ;
	document.images[imgId].src = replaceImg.src ;
}

function hide(elementID){
	document.getElementById( elementID ).style.display = 'none' ;
}

function show(elementID){
	document.getElementById( elementID ).style.display = 'block' ;
}

function removeProduct(ID){
	choice = confirm("Are you sure you want to delete this product? This cannot be undone.") ;
	
	if(choice){
		window.location = "action/remove_product.php?product=" + ID ;
	}
}

function swapHeader(){
	var header = document.getElementById("content_header_admin") ;
	var title  = header.firstChild.firstChild.nodeValue ;
	var nodeID = document.getElementById("nodeID").firstChild.nodeValue ;
	
	header.innerHTML = '<form name="edit_header" method="" action=""><input type="hidden" id="formID" value="'+nodeID+'" /><input type="text" id="new_title" value="'+title+'" style="width: 220px;" /><input type="button" value="Save" onclick="editHeader();"</form>' ;
}

function editHeader(){
	var nodeID = document.getElementById("formID").value ;
	var title  = document.getElementById("new_title").value ;
	
	
	var url = "action/edit_title.php?product="+nodeID+"&title="; // The server-side script
	http.open("GET", url + escape(title), true) ;
	http.onreadystatechange = handleTitleHttpResponse ;
	http.send(null) ;
}

function editDesc(a_ID){
	window.location = "products_admin_editing.php?product="+a_ID ;
}

function makeFeatured(a_ID){
	var url = "action/make_featured.php?product="; // The server-side script
	http.open("GET", url + escape(a_ID), true) ;
	http.onreadystatechange = handleFeaturedHttpResponse ;
	http.send(null) ;
}

function uploadFile(obj) {
	var uploadDir = obj.value;
	uploaderId = 'uploader'+obj.name;
	uploader = obj.name;
	
	uploadDirectory = document.getElementById('formName'+obj.name).uploadDirectory.value ;
	document.getElementById('formName'+obj.name).submit();
	document.getElementById(uploaderId).innerHTML = '<br /><img src="img/indicator.gif" border="0" alt="uploading..." title="File is Uploading, Please Wait" /><br /><br />';
}

function removeImage(a_which,a_ID){
	choice = confirm("Are you sure you want to delete this image? This cannot be undone.") ;
	
	if(choice){
		var url = "action/remove_image.php?product="+a_ID+"&whichCol=" ; // The server-side script
		http.open("GET", url + escape(a_which), true) ;
		http.onreadystatechange = handleDropImgHttpResponse ;
		http.send(null) ;
	}
}