function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}

function open_window(src) {
    if (src.indexOf("#") < 0) {
        popup = window.open(src, "Image", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,,copyhistory=no,width=500,height=500,screenX=150,screenY=150,top=150,left=150");
    }
}

function PopupPic(sPicURL) { 
	window.open("popup.htm?"+sPicURL, "", "resizable=1,HEIGHT=500,WIDTH=500");
}

function round(number, x) {  
 var result = String(Math.round(number*Math.pow(10,x))/Math.pow(10,x))
 point = result.indexOf(".");
 if ((x>0) && (point == -1)) {
  result = result + '.';
  point = result.indexOf(".");
 }
 if (point == 0) {
  result = "0"+result;
 } else if (point > 0) {
  while (result.length <= point + x) result = result+"0";
 }
 if (point > 0) { result = result.substring(0,result.indexOf('.')) +'.' + result.substring(result.indexOf('.')+1); }
 return result;
}

function fillLevel(dd_changed, dd_level)
{

    // disable add to cart button

    cart_submit = document.getElementById('add-to-cart');
    cart_submit.src = '/images/submit_off.gif';
    cart_submit.disabled = true;

    if (dd_changed && dd_changed.value == "") {
        count = dd_level;
    } else {
        count = dd_level + 1;
    }

    while (select_obj = document.getElementById("po_" + count)) {
        select_obj.length = 1;
        select_obj.options[0] = new Option("");
        select_obj.disabled = true;
        count++;
    }

    // clear price (optional)
    price_obj = document.getElementById("price");

    // clear product id
    product_id = document.getElementById("product-id");
    product_id.value = "";

    // set the pointer to the root of the object
    po_pointer = po;

    // loop though the numbers of levels
    for (var i = 1; i < dd_level; i++) {

        // load the select obect
        select_obj = document.getElementById("po_" + i);

        // only move the pointer if it has children
        if (po_pointer['c']) {
            po_pointer = po_pointer['c'][select_obj.options[select_obj.selectedIndex].value];
        }
    }

    if (!po_pointer) {

        // if for some reason the pointer is undefined do nothing

    } else if (po_pointer['end']) {

        // if 'end' is set we're on a node with price and product id
        // these are both optional
	var outPrice = po_pointer['price'];
	if (ctag == "NZD") {
	price_obj.innerHTML = csign + round(outPrice, 2);
	} else {
	price_obj.innerHTML = csign + round(((outPrice-(outPrice/9)) * crate),2);
	}
	
        //pc_obj.innerHTML = po_pointer['product_id'];
        product_id.value = po_pointer['product_id'];

        if (product_image = document.getElementById('product-image')) {
            if (po_pointer['product_image']) {
                changeImage(po_pointer['product_image']);
            }
        }
     
        // Unhide the add to cart button here
        cart_submit = document.getElementById('add-to-cart');
        cart_submit.disabled = false;
        cart_submit.src = '/images/submit.gif';

    } else if (po_pointer['c']) {

        // if we've got children fill the dropdown

        // get the drop down and reset it
        fill_obj = document.getElementById("po_" + dd_level);
        fill_obj.length = 1;

        if (dd_level == 1) {
            fill_obj.options[0] = new Option("First Select " + po_pointer['cn'],"");
        } else {
            fill_obj.options[0] = new Option("Then Select " + po_pointer['cn'],"");
        }

        fill_obj.disabled = false;

        // fill the dropdown
        count = 1;
        for (var i in po_pointer['c']) {

            // if a stock is set add it to the dropdown text
            if (po_pointer['c'][i]['stock'] == "0") {
                option_output = i + " (no stock)";
            } else {
                option_output = i;
            }

            // add the new option
            fill_obj.options[count] = new Option(option_output,i);
            count++;
        }

    }
}

function changeImage(imageName)
{
    document.getElementById('product-image').src = '/images/245/' + imageName;
    document.getElementById('product-image-link').href = '/images/large/' + imageName;
}
