var ItemHistoryStatus = 0;
var ItemSpecificsStatus = 0;
var divStatus = new Array();
var divDescr = new Array();
var divButton = new Array();

divStatus["itemhistory"] = 0;
divStatus["itemspecifics"] = 0;
divDescr["itemhistory"] = 'Item History';
divDescr["itemspecifics"] = 'Item Specifics';

var category = new Array();
var category_expansion = new Array();
var leaf_p = new Array();


var xmlhttp = false;

try {
    xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
	xmlhttp=false;
    }
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    try {
	xmlhttp = new XMLHttpRequest();
    } catch (e) {
	xmlhttp=false;
    }
}

if (document.all && !document.getElementById) {
    document.getElementById = function(id) { return document.all[id]; }
}

function popUpWindow(myUrl,myName,myAttributes) {
    myPopUpWindow = window.open(myUrl,myName,myAttributes);
    myPopUpWindow.focus();
}

function updateField(myField,myValue) {
    myField=myValue;
}


function switchDiv(id,button) {
    mydiv = document.getElementById(id).style;
    if (divStatus[id] == 0) {
	divStatus[id] = 1;
	mydiv.display = 'block';
	button.value = "Hide "+divDescr[id];
    } else {
	divStatus[id] = 0;
	mydiv.display = 'none';
	button.value = "Show "+divDescr[id];
    }
}


function getcat(cid) {
    var success = false;
    if (!xmlhttp) {
	alert('xmlhttp not found');
	return false; // also UpdateField to '[invalid category]'?
    }
    url = "vendor_getcat.cgi?cat_id_ebay="+cid;
    xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4) {
	    // alert(xmlhttp.responseText);
	    // should cache result too
	    //document.vendor_item_detail_form.expanded_cat_id_ebay.value = xmlhttp.responseText;
	    if(xmlhttp.responseText.charAt(0)=="<") {
		// handle error properly
		document.vendor_item_detail_form.expanded_cat_id_ebay.value = '[unable to fetch category name]';
		success = false;
	    } else {
		eval(xmlhttp.responseText); // sets up catId, category, category_expansion, leaf_p hash entries
		document.vendor_item_detail_form.expanded_cat_id_ebay.value = category_expansion[catId];
		if (leaf_p[catId]==false) { document.vendor_item_detail_form.expanded_cat_id_ebay.value += ' [not a leaf]' }
	    }

	    success = true;
	}
    }
    xmlhttp.setRequestHeader('Accept','message/x-formresult');
    xmlhttp.send(null);
    // not sure if this is always right because we might get here
    // before the async xmlhttp request gets to readyState 4
    return success;

}

function updateTlen() {
    document.vendor_item_detail_form.tlen.value=document.vendor_item_detail_form.title.value.length;
    return true;
}



function getcatchildren(cid) {
    //alert('getting cat children for ' + cid);
    //alert (typeof(cid));
    //if (typeof(cid)!='number') {
    //return false;
    //}
    
    var success = false;
    if (!xmlhttp) {
	alert('xmlhttp not found');
	return false; // also UpdateField to '[invalid category]'?
    }
    url = "vendor_getcat.cgi?req=children&cat_id_ebay="+cid;
    xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4) {
	    //alert ('response length: ' + xmlhttp.responseText.length);
	    // alert(xmlhttp.responseText);
	    // should cache result too
	    //document.vendor_item_detail_form.expanded_cat_id_ebay.value = xmlhttp.responseText;
	    if(xmlhttp.responseText.charAt(0)=="<") {
		// handle error properly
		//document.vendor_item_detail_form.expanded_cat_id_ebay.value = '[unable to fetch category name]';
		alert('unable to fetch subcategory list for this category]');
		success = false;
	    } else {
		//alert ('response length: ' + xmlhttp.responseText.length);
		eval(xmlhttp.responseText); // sets up catId, category, category_expansion, leaf_p hash entries
		//document.vendor_item_detail_form.expanded_cat_id_ebay.value = category_expansion[catId];
		//if (leaf_p[catId]==false) { document.vendor_item_detail_form.expanded_cat_id_ebay.value += ' [not a leaf]' }
	    }
	    success = true;
	}
    }
    xmlhttp.setRequestHeader('Accept','message/x-formresult');
    xmlhttp.send(null);
    // not sure if this is always right because we might get here
    // before the async xmlhttp request gets to readyState 4
    return success;

}
