function getXmlHttpRequestObject() 
{
	if (window.XMLHttpRequest) 
	{
		return new XMLHttpRequest(); //Not IE
	} 
	else if(window.ActiveXObject) 
	{
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	}
}


var http1 = getXmlHttpRequestObject();


function isinproject(prodno) {
	var d = new Date();
	http1.open('get', '/projectFuncs.php?option=inproj&prodno=' + prodno+ "&nocache=" + d.getTime());
    http1.onreadystatechange = inProjReturn;
    http1.send(null);
    /*var myProjectString = getCookie('myproject');
    
    if(!myProjectString) return false;

    var myProjectArray = myProjectString.split(",");

    for(var i=0; i<myProjectArray.length;i++) {
	if(myProjectArray[i] == prodno) return true;
    }

    return false;*/
}
var inproj;
if ( inproj == null || inproj == 'undefined')
{
	isinproject(productNumber);
}
function inProjReturn()
{
	if (http1.readyState == 4)
	{
		if (http1.responseText=="1")
		{
			inproj = true;
		}
		else 
		{
			inproj = false;
		}
		//alert(http1.responseText);
	}
}

function switchImg(imageUrl, imgTarget) {
    var theImg = document.getElementById(imgTarget);
    theImg.setAttribute('src', imageUrl);
}
function sendRequest(option,prodno)
{
	if (http1.readyState == 4 || http1.readyState == 0)
	{
		var d = new Date();
		http1.open('get', '/projectFuncs.php?option=' + option + '&prodno=' + prodno+ "&nocache=" + d.getTime());
		http1.onreadystatechange = doNothing;
    	http1.send(null);
		//alert('sent'+'/projectFuncs.php?option=' + option + '&prodno=' + prodno);
	}
}

function doNothing()
{
	if (http1.readyState == 4) 
	{
    	// only if "OK"
    	if (http1.status == 200) 
		{
			//alert(http1.responseText);
		}
	}
}

function setUpProject() {
    
    if(!document.getElementById('prodProject')) return;

    var theLink = document.getElementById('prodProject').childNodes[0];
    var prodno = theLink.title;
    if(!inproj) 
	{
		theLink.id = "prodNoProj";
		theLink.innerHTML = "Add to Project";
		theLink.onclick = function addIt() 
		{
			sendRequest('add', prodno);
			inproj = true;
			setUpProject();
			return false;
		}
    } 
	else 
	{
		theLink.id = "prodProj";
		theLink.innerHTML = "Remove from Project";
		theLink.onclick = function removeIt() 
		{
			sendRequest('delete', prodno);
			inproj=false;
			setUpProject();
			return false;
		}
    }
}


function setUpProd() {

    if(!document.getElementById('productImages')) return;

    var links = document.getElementById('productImages').getElementsByTagName("a");
   
    for(i=0;i<links.length;i++) {
	if(links[i].id == "prodSpTh") {
	    links[i].onclick = function() {
		openWindow(this.href, 320,500,10,10);
		return false;
	    }
	} else {
	    links[i].onclick = function() {
		switchImg((this.href), "prodImage");
		return false;
	    }
	}
    }

    links = document.getElementById('prodFins').getElementsByTagName("a");
    
    for(i=0;i<links.length;i++) {
        links[i].onclick = function() {
	   openWindow(this.href, 420, 420);
	    return false;
	}
    }

    var print = document.getElementById('prodPrint');

    print.onclick = function() {
	pLink = this.href.split('/');
	pLink = pLink[4];

	openPrintWindow('/print.php?prodno='+pLink, 720, 700);
	return false;
    }
}

function prodFoot() {
    var theDiv = document.getElementById("prodData");
    newHeight = theDiv.offsetHeight + 20;
    fixFooter(newHeight);
}

addLoadEvent(setUpProd);
addLoadEvent(setUpProject);
//addLoadEvent(prodFoot);


