var init = new (function(){
    this.init = function (){
        if(document.getElementById('SpecUpdate'))
            document.getElementById('SpecUpdate').style.display = "none";
    }
});
Interface.addListener(init);

httpdata = new HttpRequestData();

httpreq = new HttpRequest();
httpreq.setResponseFormat('xml');
httpreq.setMimeType('text/xml');
/*httpreq.setResponseFormat('text');
httpreq.setMimeType('text');*/
httpreq.setHandlerResponse(handler);
httpreq.setHandlerError(handlerError);

function ajaxupdate(changed){
    httpdata.getFormData('ProductPoolSelect');
    httpdata.setData('changed', changed);
    httpreq.post('./ignition/lib/ajax/product_pool_select.php',
            httpdata.getHttpRequestData());
}

function handler(xml){
    var poolSpecs = xml.getElementsByTagName('poolSpec');
    for(i=0; i<poolSpecs.length; i++){
        var intag = "";
        tagid = poolSpecs[i].attributes.getNamedItem('type').value;
        vals = poolSpecs[i].childNodes;    // goes in option.
        var select = document.getElementById(tagid);
        select.options.length = 0;
        for(j=0; j<vals.length; j++){
            if(vals[j].attributes && vals[j].attributes.getNamedItem('selected'))
                select.options[select.options.length] = new Option(vals[j].firstChild.nodeValue, vals[j].firstChild.nodeValue, null, 'selected');
            else
                select.options[select.options.length] = new Option(vals[j].firstChild.nodeValue, vals[j].firstChild.nodeValue)
        }
    }

	//alert(getFirstChild(xml, 'isStocked'));
	document.getElementById("QfCode").innerHTML = "Quickfind Code: #"+getFirstChild(xml, 'productToBuy');
	document.getElementById("ProductToBuyID").value = getFirstChild(xml, 'productToBuy');
	if(getFirstChild(xml, 'isStocked') == 'Y'){
        if(getFirstChild(xml, 'pricePrev'))
		    document.getElementById("ProductToBuyPricePrev").innerHTML = "&pound; "+ getFirstChild(xml, 'pricePrev');
        else 
		    document.getElementById("ProductToBuyPricePrev").innerHTML = "";
		document.getElementById("ProductToBuyPrice").innerHTML = "&pound; "+ getFirstChild(xml, 'priceIncTax');
		document.getElementById("ProductToBuyQty").value = getFirstChild(xml, 'quantity');
		document.getElementById("BuyButton").style.display = '';
	} else {
		document.getElementById("ProductToBuyPrice").innerHTML = "&pound; "+ getFirstChild(xml, 'priceIncTax') + " <strong style=\"color: black\">OUT OF STOCK</strong>";
		document.getElementById("BuyButton").style.display = 'none';
	}
    document.getElementById("ProductToBuyName").innerHTML = getFirstChild(xml, 'name');
    document.getElementById("ProductToBuyDesc").innerHTML = getFirstChild(xml, 'description');

    // Populate Specs.
    specs = xml.getElementsByTagName('spec');
    if(specs.length > 0){
        if(document.getElementById('ProductSpecsTable')){
            data = "";
            for(i=0; i<specs.length; i++){
                td = "<th>" + specs[i].firstChild.firstChild.nodeValue + ":</th>";
                td += "<td>" + specs[i].lastChild.firstChild.nodeValue + "</td>";
                data += "<tr>" + td + "</tr>";
            }
            var table = document.getElementById('ProductSpecsTable');
            table.innerHTML = "<table>%s</table>".sprintf(data);
        } else {
            data = "";
            for(i=0; i<specs.length; i++){
                td = "<th>" + specs[i].firstChild.firstChild.nodeValue + ":</th>";
                td += "<td>" + specs[i].lastChild.firstChild.nodeValue + "</td>";
                data += "<tr>" + td + "</tr>";
            }
            document.getElementById('SpecHolder').innerHTML = '<h3>Specifications</h3><table class="productSpecs" cellspacing="0">' + data + '</table>';
        }
    } else {
        if(document.getElementById('ProductSpecsTable'))
            document.getElementById('ProductSpecsTable').innerHTML = '';
        if(document.getElementById('SpecHolder'))
            document.getElementById('SpecHolder').innerHTML = '';
    }

    // Populate Images
    var images = xml.getElementsByTagName("image");
	//var imgsHtml = "<tr>";
	var imgsHtml = "";
    for(i=0; i<images.length; i++){
        //if(eval(images[i].attributes) != undefined && images[i].attributes.length > 0)
			//if(images[i].attributes[0].nodeName == "default" && images[i].attributes[0].value == "yes"){
			//if(images[i].attributes[0].nodeName == "default") continue;
            //document.getElementById('MainImage').style.backgroundImage = "url(images/products/%s)".sprintf(images[i].childNodes[2].firstChild.nodeValue);
        //}
		imgsHtml += '<div id="ProductImages_' + images[i].childNodes[0].firstChild.nodeValue  + '" class="imageThumb" onmouseover="swapImg(\'' + images[i].childNodes[2].firstChild.nodeValue + '\')" style="background-image: url(images/products/' + images[i].childNodes[3].firstChild.nodeValue + ')"></div>';
    }
	imgsHtml += '<div class="clear"></div>';
	var prodImg = document.getElementById('ProductImages');
	prodImg.innerHTML = imgsHtml;
}

function getFirstChild(xml, tagName){
    try {
        var elems = xml.getElementsByTagName(tagName);
        if(elems.length > 0)
            return elems[0].firstChild.nodeValue;
        else return false;
    } catch (e){
        return false;
    }
}

function handlerError(){
    //alert("Oh no!");
}

