var global_divObj;
var global_selectObj;
var global_selectedValue;
var global_prod_id;

function in_array(stringToSearch, arrayToSearch) {
            for (s = 0; s < arrayToSearch.length; s++) {
                        thisEntry = arrayToSearch[s].toString();
                        if (thisEntry == stringToSearch) {
                                   return true;
                        }
            }
            return false;
}

function getAjaxObject() {
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
return xmlhttp;
}

function updateProperties(id, prod_id, div, final_parent) {
  var xmlhttp = getAjaxObject();
  var parentChildren = document.getElementsByTagName('div');
  var formProperties = document.getElementById('final_properties');
  var obj = document.getElementById('id_'+final_parent);
  if (obj)  {
    formProperties.removeChild(obj);
  }
  
  var selectedText = null;
  for(x in parentChildren)  {
    if(parentChildren[x] && parentChildren[x].parentNode)  {
      if(parentChildren[x].parentNode.id==div)  {
				var selects = parentChildren[x].getElementsByTagName('select');
				var select = selects[0];
				if(select)
					selectedText = select.options[select.selectedIndex].text;
				
        document.getElementById(div).removeChild(parentChildren[x]);
        showPrice(prod_id);
      }
    }
  }

  global_divObj = document.getElementById(div);
  global_selectObj = document.getElementById(id);
  global_selectedValue = global_selectObj.options[global_selectObj.selectedIndex].value;
  var global_prod_id = prod_id;
  xmlhttp.onreadystatechange=function() {
    if(xmlhttp.readyState==4) {
      var oldDiv = document.getElementById('DIV_'+global_selectedValue);
      if(oldDiv){
        oldDiv.parentNode.removeChild(oldDiv);
      }
      
      var newDiv = document.createElement('div');
      newDiv.setAttribute('id','DIV_'+global_selectedValue);
      global_divObj.appendChild(newDiv);
      newDiv.innerHTML= xmlhttp.responseText;
			
			// Set selectedText to previously selected text
			if(selectedText != null) {
				var selects = newDiv.getElementsByTagName('select');
				if(selects[0]) {
					var select = selects[0];
					for(i=0; i<select.options.length;i++) {
						var option = select.options[i];
						if(option) {
							if(option.text==selectedText) {
								option.selected=true;
								select.value = option.value;
								select.onchange();
							}else{
								option.selected = false;
							}
						}
					}
				}
			}
			
			showPrice(global_prod_id);	
    }
  }
  xmlhttp.open("GET","/lib/propertyhandler.php?id="+id+"&value="+global_selectedValue+"&productid="+prod_id+"&parent="+final_parent,true);
  xmlhttp.send(null);
}

function showPrice(prod_id)  {	
  var parentCounter = 0;
  var propertyCounter = 0;
  var forcedParents = new Array();
  var finalPropertyIds = new Array();
  var divs = document.getElementsByTagName('div');
  for(x in divs) {
    if(divs[x]) {
      if(divs[x].className) {
        var split = divs[x].className.split('_');
        if (split[0] == 'PARENT' && split[2]) {
          forcedParents.push(split[2]);
          parentCounter++;
        } else if(divs[x].className=='PARENT_DIV') {
          parentCounter++;
        } else if (split[0] == 'FINAL' && split[1] == 'PROPERTY' && split[2]) {
          propertyCounter++;
          finalPropertyIds.push(split[2]);
        }
      }
    }
  }
  var forcedFilled = true;
  for (i in forcedParents)  {
    if(!in_array(forcedParents[i], finalPropertyIds))  {
      forcedFilled = false;
    }
  }
  if (forcedFilled) {
    getPrice(prod_id);
    document.getElementById('prices').style.display = 'block';
  } else  {
    document.getElementById('prices').style.display = 'none';
  }
}

function getPrice(prod_id) {
  var divs = document.getElementsByTagName('div');
  var property = "";
  var formProperties = document.getElementById('final_properties');
  for(x in divs) {
    if(divs[x]) {
      if(divs[x].className != null) {
	if(divs[x].className.substr(0,14)=='FINAL_PROPERTY')  {
		var values = divs[x].innerHTML.split("-");
		
		var oldProperty = document.getElementById("id_"+values[0]);
		if(oldProperty)
			oldProperty.parentNode.removeChild(oldProperty);
		
	  newElement = document.createElement("input");
	  newElement.setAttribute("type", "hidden");
	  newElement.setAttribute("name", "property_"+values[0]);
	  newElement.setAttribute("value",values[1]);
	  newElement.setAttribute("id","id_"+values[0]);
	  formProperties.appendChild(newElement);
	  property += divs[x].innerHTML+"-";
	}
      }
    }
  }
  
  var xmlhttp = getAjaxObject();
  if (quantityType) {
    xmlhttp.onreadystatechange=function() {
      if(xmlhttp.readyState==4) {
        document.getElementById('quantity').innerHTML = xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET","/lib/propertyhandler.php?usergroup="+usergroup+"&productid="+prod_id+"&properties="+property,true);  
		xmlhttp.send(null);
  } else  {
    xmlhttp.onreadystatechange=function() {
      if(xmlhttp.readyState==4) {
        document.getElementById('price_exc').innerHTML = xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET","/lib/propertyhandler.php?properties="+property+"&productid="+prod_id,true);
		xmlhttp.send(null);
  }
}
