var wsCall = null;
var responseGood = false;

function OpenAttributeWindow(merchantProdID, PricingPackageID)
{
    wsCall = new wsObj();
    wsCall.functionName = "GetMerchantProductAttributes";
    wsCall.callbackFunction = GetAttributeResponse;
    wsCall.addParam("MerchantProductID", merchantProdID);
    wsCall.addParam("PricingPackageID", PricingPackageID);
    wsCall.sendRequest();
}

function OpenWindow(mode, querystring, height, width){
    var url = mode+".aspx?prodID="+querystring;
    if (height == null)
    {
        height = 300;
    }
    if (width == null)
    {
        width = 400;
    }
    window.open(url, 'Product_Pricing_Options', 'location=no,height=' + height + ',width=' + width + ',menubar=0,resizable=no,scrollbars=0,statusbar=false,dependent,alwaysraised,status=false,titlebar=no,toolbar=0,screenY=300,screenX=300');
}

function GetAttributeResponse()
{
    if(wsCall.goodResponse()) {  
    alert(wsCall.http.responseText);          
        var attributes = wsCall.http.responseXML.getElementsByTagName("Attribute");
        //var stateSelect = document.getElementById("stateSelect");
        if(attributes) {
            for(i=0; i<=attributes.length; i++) {
                if (i>=1 && attributes[i].getAttribute("Value") != attributes[i-1].getAttribute("Value"))
                {
                    alert(attributes[i].getAttribute("Value"));
                }
                //alert(attributes[i].getAttribute("Value"));
                //alert(attributes[i].getAttribute("PriceOffset"));
            }
        }        
    }
}

 function Recalculate(groupID, attPrice, attID)
    {    
        //loop through the att_attID radio group, grab the attID of each radio value in group and take class away for Price_groupID_attID       
        var groupName = 'Att_' + groupID;
        var oldPrice = "0";
        
        for(var i=0; i<document.form1.elements.length; i++)
        {    
            if (document.form1.elements[i].type == 'radio') {
                if (document.form1.elements[i].id == groupName) {
                    if (document.getElementById("Price_" + groupID + "_" + document.form1.elements[i].value.substr(document.form1.elements[i].value.lastIndexOf('_')+1)).className == "selOption")
                    {
                        oldPrice = document.getElementById("Price_" + groupID + "_" + document.form1.elements[i].value.substr(document.form1.elements[i].value.lastIndexOf('_')+1)).innerHTML.replace("+", "");;
                        document.getElementById("Price_" + groupID + "_" + document.form1.elements[i].value.substr(document.form1.elements[i].value.lastIndexOf('_')+1)).className = "";
                    }
                }
            }
        }

        oldPrice = oldPrice.replace("$", "");        
       
        //update the total cost        
        var newPrice = document.getElementById("totalSum").innerHTML.replace("$", "");
        
        newPrice = parseFloat(newPrice) - parseFloat(oldPrice);
        
        if ((String(newPrice).substr(String(newPrice).indexOf(".")+1)).length > 2)
        {    
             newPrice = roundVal(newPrice);            
        }
        newPrice = parseFloat(newPrice) + parseFloat(attPrice);   
        
        if ((String(newPrice).substr(String(newPrice).indexOf(".")+1)).length > 2)
        {
            newPrice = roundVal(newPrice);
        }  
         
        //take the total, subtract the old radio price if any and then add the new price
        document.getElementById("totalSum").innerHTML = "$" + newPrice; 
        
        //update the class and price for the specific attribute group 
        document.getElementById("Price_" + groupID + "_" + attID).className = "selOption";         
        
        var attPriceString = String(attPrice); 
    }
    function roundVal(val)
    {
	    var dec = 2;
	    var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	    return result;
    }