attr = new Array;
allAttr = new Array;
allSelected = new Array;

if (window.XMLHttpRequest)
  xmlhttp=new XMLHttpRequest();

// code for IE
else if (window.ActiveXObject)
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

function loadIt (loc, pid){
    var id="";
	for (i = 0; i < attr.length && i <= loc; i++) {
		id = id + attr[i].options[attr[i].selectedIndex].value;
		if (i != loc) id = id + "=";
	}
    //allSelected.push(id);
	
    if (loc != attr.length-1){
		if (xmlhttp) {
			
			var getUrl = "http://www.asiajam.com/catalog/xmlget.php?id=" + id + "&pid=" + pid;
			
			xmlhttp.open("GET", getUrl,false);
			xmlhttp.send(null) 

			var xResponse = xmlhttp.responseText;
		
			var sResponse = xResponse.split("\n");
		  // alert(xResponse);
			// loop through the sResponse array
			for (var i=0; i<sResponse.length; i++) {
			
				if(sResponse[i]) {
					//split the row into its values
					var sValues = sResponse[i].split("||");

					var temp = sValues[0].split("=");
					var attrId = temp[0];
					var attrName = temp[1];

					var tempValue = sValues[1].split("--");

					var selectBox = eval("document.cart_quantity.option" + attrId);
					selectBox.length=0;
					selectBox.options[0] = new Option("-Then, Select " + attrName + "-", 0);

					for(j=0; j<tempValue.length; j++){
						
						attrValueTemp = tempValue[j].split("=");

						attrValueId = attrValueTemp[0];
						attrValueName = attrValueTemp[1];

						selectBox.options[j+1] = new Option(attrValueName, attrValueId);

						if (isSelected(attrValueId)) {
							selectBox.options[j+1].selected=true;
						}

					}
				}
			}
	/*        allSelected = new Array;
			for (i = 0; i < attr.length; i++) {
				if(attr[i].options[attr[i].selectedIndex].value != 0)  allSelected.push(id);
			}*/

			for (i = loc+2; i < attr.length; i++) {
				
				attr[i].length=0;
			   
				for (j=0; j < allAttr[i].length; j++) {
				 
				 allAttrTemp = allAttr[i][j].split("==");
				 attr[i].options[j] = new Option(allAttrTemp[1],allAttrTemp[0]);
				}
			}
		

		}
		else {
			alert("Your browser is outdated, and no longer supported.");
		}

	if (id ==0) resetAll();

	}

}

function isSelected(aid){

    

	for (k=0; k<allSelected.length; k++)
	{
		if (allSelected[k] == aid) return true;
	}

	return false;
}


function init(){
    var j = 0;
    
    for (i = 0; i < document.cart_quantity.elements.length; i++) {
    	
    if (document.cart_quantity.elements[i].id.indexOf("attribute") != -1)
        attr[j++] = document.cart_quantity.elements[i];
    }

    for (i = 0; i < attr.length; i++) {
	    allAttr[i] = Array();
	    for (j = 0; j < attr[i].options.length; j++) {
			  a = attr[i].options[j].value + "==" + attr[i].options[j].text;
              allAttr[i][j] = a;
	   // alert(allAttr[i][j]);
		}
		
	}
    
}

function resetAll(){

	allSelected = new Array;

    for (i = 0; i < attr.length; i++) {
    	
        attr[i].length = 0;  // empty it and later structure it again with new option
			  
		for (j=0; j < allAttr[i].length; j++) {
             
			 allAttrTemp = allAttr[i][j].split("==");
             attr[i].options[j] = new Option(allAttrTemp[1],allAttrTemp[0]);
		}			  
		
    }

}
