/**
 *
 */
function getVariationPrice(formIteration)
{
    /**
     * @var    string    the form to be updated
     */
    var formName = 'order_' + formIteration;

    /**
     * @var    string    the name of the input field containing the variationValueIDs
     */
    var fieldNameVariation = 'product[variation][]';

    /**
     * @var    string    our productID
     */
    var fieldNameProductID = 'product[product_id]';

    var fieldNameProductPrice = '';

    /**
     * @var    string    the querystring
     */
    var queryString = '?ajax_request=variation_price';

    /**
     * @var    string    the content of the variation fields
     */
      var formContent = document.forms[formName].elements[fieldNameVariation];

    /**
     * @var    array    storage for the selected variationValueIDs
     */
      var selectedItems = new Array(formContent.length-1);

    //  read the variationValueIDs and append them to the querystring
      for(var i=0; i< formContent.length; i++)
      {
          queryString += '&variation_' + i + '=' + formContent[i].value;
          if(i>0)
              selectedItems[i-1] = formContent[i].value;
      }

    // append productID
      queryString += '&product_id=' + document.forms[formName].elements[fieldNameProductID].value;


    var url = queryString;
    //alert(url);
      var http_request = false;

        // check browser for request object
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            //alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }

    // define function which is called after server's response
    http_request.onreadystatechange = function()
        {
            setVariationPrice(http_request, formIteration, selectedItems);
        };
        http_request.open('GET', url, true);
        http_request.send(null);
        return true;
}

function setVariationPrice(http_request, formIteration, selectedItems)
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			//alert(http_request.responseText);
			var xmldoc = http_request.responseXML;

			// set the available variations
			var selects = xmldoc.getElementsByTagName('select');
			var optionNode = null;
			var optionNodeDoc = null;

			for(var i=0; i<selects.length; i++)
			{
				var selectDoc = getElement('id', 'order_' + formIteration + '_vv_' + (i+1));
				optionNode = selects[i].firstChild;

				var optionNodeTmp = selects[i].firstChild;
				var sizeNew=0;
				var sizeOld=selectDoc.length;
				while(optionNodeTmp != null)
				{
					if(optionNodeTmp.nodeName == 'option')
						++sizeNew;
					optionNodeTmp = optionNodeTmp.nextSibling;
				}

				// empty old variation values
				for(var x = sizeOld; x > 0; x--)
				{
					if(selectDoc.options[x] != null)
					{
						selectDoc.options[x] = null;
					}
				}

				// fill variation values with given data
				for(var x=0; x<sizeNew; x++)
				{
					// nodes are not always options -> skip others
					while(optionNode != null && optionNode.nodeName != 'option')
						optionNode = optionNode.nextSibling;

					// set data if available
					if(optionNode!=null)
					{
						selectDoc.options[x] = new Option(optionNode.getAttributeNode('label').nodeValue,optionNode.getAttributeNode('value').nodeValue, x==0?true:false,optionNode.getAttributeNode('value').nodeValue==selectedItems[i]?true:false);
						// next
						optionNode = optionNode.nextSibling;
					}
				}
			}

			// new price
			var price = xmldoc.getElementsByTagName('price').item(0).firstChild.data;
			var priceNonSpecial = xmldoc.getElementsByTagName('priceNonSpecial').item(0).firstChild.data;
			var priceMsrp = xmldoc.getElementsByTagName('priceMsrp').item(0).firstChild.data;
			if (xmldoc.getElementsByTagName('notice').item(0).firstChild)
			{
				var notice = xmldoc.getElementsByTagName('notice').item(0).firstChild.data;
			}
			else
			{
				var notice = '';
			}

			// we have to update 3 fields (price + priceNonSpecial + notice)
			if(price != priceNonSpecial)
			{
				if(getElement('id', 'priceNonSpecial_' + formIteration, 0))
				{
					setContent('id', 'priceNonSpecial_' + formIteration, 0, priceNonSpecial);
				}
			}

			setContent('id', 'price_' + formIteration, 0, price);

			if(getElement('id', 'priceMsrp_' + formIteration, 0))
			{
				setContent('id', 'priceMsrp_' + formIteration, 0, priceMsrp);
			}

			if(getElement('id', 'notice_' + formIteration, 0))
			{
				setContent('id', 'notice_' + formIteration, 0, notice);
			}
			
			// set Issues
			var issues = xmldoc.getElementsByTagName('issues').item(0).firstChild.data;
			if(issues > 0)
			{
				setContent('id', 'issues', 0, issues);
			}

			// set new productNo
			var productNo = xmldoc.getElementsByTagName('productNo').item(0).firstChild.data;
			setContent('id', 'productNo', 0, productNo);
		}
		else
		{
			//alert('There was a problem with the request.');
		}
	}
}

