/*******************************************************************************
Step1Ajax.js
This script is used in both the retail step 1 cart 
as well as the vendors (professional).
*******************************************************************************/

/*
This function is called every time a quantity is changed.
*/
function step1UpdateQty(iCatId, cartType) {
    //var i2CatId = iCatId
   // iCatId is categories.ID
   // cartType is PROFESSIONAL or RETAIL
   //alert('iCatId : '+iCatId+' | cartType : '+cartType);
   //alert('i2CatId : '+i2CatId);

	//Validate the form before updating
	step1ValidateForm();	//Found in Step1Validate.js

	var sRequest = "update-quantity";

	//Get all input tags
	var aInputs = $$('input')
	var aTagInfo;

	for (iIndex in aInputs) {
		oTag = aInputs[iIndex]
		aTagInfo = parseInputName(oTag.name);
		
		if (aTagInfo["valid"] && iCatId == aTagInfo["catId"]) {
			sRequest += ";catId:" + aTagInfo["catId"] +
				",prodId:" + aTagInfo["prodId"]
			if (oTag.value == "") {
				sRequest += ",qty:0";
			}
			else if (oTag.value.toInt() == oTag.value) {
				sRequest += ",qty:" + oTag.value;
			}
			else {
				sRequest += ",qty:-1";
			}
			sRequest += ";";
		}
	}




	sRequest = "catId=" + iCatId + "&prodInfo=" + sRequest;
	sRequest += "&cart=" + cartType;

//alert(sRequest);
//alert('iCatId : '+iCatId);
	var oRequest = new Request({method:'post', url:'/cart/step1AjaxRequest.php?requestMode=update-quantity&cart='+cartType});

	//--- Success ---
	oRequest.addEvent('success', function (responseText, responseXML) {
		step1ParseResponse(responseText,cartType,iCatId);
		//alert(responseText);
		//alert('iCatId --: '+iCatId);
		
	});

	//--- Failure ---
	oRequest.addEvent('failure', function (xhr) {
		//alert(xhr.statusText);
	});

	//--- Exception ---
	oRequest.addEvent('exception', function (headerName, value) {
		//alert(headerName + " - " + value);
	});

	oRequest.send(sRequest);
}





function step1ParseResponse(responseText,cartType,iCatId) {
   // responseText looks like this
   //discounted:true;discounted-price:20;count:7;RawTotal:244.65000915527;order-subtotal:244.65;total-savings:0.00;order-total:244.65;
   
   //alert('iCatId ----: '+iCatId);

	var aTokens = responseText.split(";");
	var aPair;
	var discounted;
	var discountedPrice = 99999.99;
	for (var iLoop = 0; iLoop < aTokens.length; iLoop++) {
		sToken = aTokens[iLoop];
		aPair = sToken.split(":");
   

    //discounted:true
		if (aPair[0] == "discounted") {
		    discounted = aPair[1];
        //alert(aPair[0]+" "+aPair[1]);
		}

    //discounted-price:20
		if (aPair[0] == "discounted-price") {
		    discountedPrice = aPair[1];
        //alert(aPair[0]+" "+aPair[1]);
		}
    
		if (aPair[0] == "RawTotal") {
      $("RawTotal").value     = aPair[1];  // input
		}
		
		if (aPair[0] == "order-subtotal") {
			$("subtotal").innerHTML = aPair[1];  // span
		}
		else if (aPair[0] == "total-savings") {
			$("savings").innerHTML = aPair[1];
		}
		else if (aPair[0] == "order-total") {
			$("total").innerHTML = aPair[1];
			//alert(aPair[1]);
			
		}
	}

  //if(discounted == 'true'){
    //alert(discounted);
  //}
  
  
	// call routine to update price
	if(cartType == 'RETAIL' ){
     if(discounted == 'true'){
       updatePricing( discountedPrice,iCatId );
     }
  }
	
}

/*
Outputs a key->value pair array with the following keys:
	catId
	prodId
	valid		-	true if valid, false otherwise
*/

function parseInputName(sInputName) {
	var bValid = true;	//True until proven false;
	var iCatId = 0;
	var iProdId = 0;

	if (sInputName) {
		if (sInputName.indexOf("quantity") == 0) {
			var aPieces = sInputName.split("_")
			if (aPieces.length != 3) {
				bValid = false;
			}
			else {
				iCatId = parseInt(aPieces[1]);
				if (isNaN(iCatId)) {
					bValid = false;
				}
				else {
					iProdId = parseInt(aPieces[2]);
					if (isNaN(iProdId)) {
						bValid = false;
					}
				}
			}
		}
		else {
			bValid = false;
		}
	}
	else {
		bValid = false;
	}

	if (bValid == false) {
		iCatId = -1;
		iProdId = -1;
	}

	return {catId: iCatId, prodId: iProdId, valid: bValid};
}








function updatePricing(discountedPrice,iCatId ) {
    discountedPrice = Number(discountedPrice).toFixed(2);

//-----------------------------------------------

	//Get all input tags
	var aInputs = $$('input')
	var aTagInfo;
  var sSpanName; 

	for (iIndex in aInputs) {
		oTag = aInputs[iIndex]
		aTagInfo = parseInputName(oTag.name);
		
		if (aTagInfo["valid"] && iCatId == aTagInfo["catId"]) {
		    sSpanName = 'price_'+aTagInfo["catId"]+'_'+aTagInfo["prodId"];
		    //alert( sSpanName );
      	//alert( aTagInfo["catId"]+'_'+aTagInfo["prodId"]);  //this has the product id in it
      	// 2010.06.14 KLS added dollar sign to unit price.
       //$(sSpanName).innerHTML = discountedPrice;  // span
       $(sSpanName).innerHTML = '$'+discountedPrice;  // span

		}
	}
//-------------------------------------------















//------------------------------------------------------------------------------
     //var el        = obj.elements;
     //var total = parseFloat(document.getElementById("subtotal").innerHTML)



   //alert('updatePricing');
  /* <span class="step1-tableItem" name="price_2_1" id="price_2_1">$34.95</span> */
  // price_2_1 - category id is frist number, priducts.ID is second number
  //--------
  // pull all spans
		
 // $('price_2_1').innerHTML = discountedPrice;  // span




  /*
  var i = 0;
  var aSpans = $$('input')
  //alert(aSpans.length);
	for (iIndex in aSpans) {
		oTag = aSpans[iIndex]
    //alert(oTag.name);
    i++;
    //if(i == 15){
    //   break;
    //}
		if(oTag.name == "price_2_1"){
    //   shippingmethods_ID = oTag.value;
       alert('price_2_1');
    }    
  }
	*/	
		
		





		
		/*
		aSpanInfo = parseSpanName(oSpan.name);

    need to create 
		
		
		if (aTagInfo["valid"] && iCatId == aTagInfo["catId"]) {
			sRequest += ";catId:" + aTagInfo["catId"] +
				",prodId:" + aTagInfo["prodId"]
			if (oTag.value == "") {
				sRequest += ",qty:0";
			}
			else if (oTag.value.toInt() == oTag.value) {
				sRequest += ",qty:" + oTag.value;
			}
			else {
				sRequest += ",qty:-1";
			}
			sRequest += ";";
			*/
		//}

    


   
    //var sSpanName;
    //sSpanName = 'cat-'+iCatId+'-price'
    //alert(discountedPrice +' updatePricing '+ iCatId);
    //alert(sSpanName);
    
    //this works for the first in line.
    //$(sSpanName).innerHTML = discountedPrice;  // span
		
		
		
    //need to get the products.ID

    //id="item-1-price


    //cat-2-price
    
    /* <span class="step1-tableItem" name="cat-2-price" id="item-1-price">$34.95</span> */ 
}
