function manifestAdd(oFrm,cProductId,cActionId){
	
  var elements=oFrm.elements;
  var numElements=elements.length;
  var query_string='';
  query_string+='pc_product_id=' + encodeURIComponent(cProductId);

  for(i=0;i<numElements;i++){
    attributeCheck = /^pc_product_attribute\d+$/i
    if(attributeCheck.test(elements[i].name)||elements[i].name=='CSRF_Token'){  //if this element is a product attribute
      query_string+='&' + encodeURIComponent(elements[i].name) + '=' + encodeURIComponent(elements[i].value);
    }
  }
  
  var req=newXMLHttpRequest();
  var myhandler=new getReadyStateHandler(req,manifestHandler,manifestErrorHandler);
  req.onreadystatechange=myhandler;
  req.open("POST","ajax-action.php",true);
  req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  req.send('a=' + cActionId + '&' + query_string + '&random=' + Math.random());

  return false;

}

function productSubmit(){

    productForm = document.getElementById('frmProductAdd')

	if(typeof(submitParent)!="undefined"){
	  submitParent(productForm);
	}else{
	  //productForm.submit();
	  showAddedProduct();
	}
    
}

function showAddedProduct(){
	tb_show('Added to your Shopping Cart','product-added.php?KeepThis=true&TB_iframe=true&height=225&width=430&modal=true');
	document.location = '#';
}


function manifestErrorHandler(requestStatus){
	productSubmit();
}

function manifestHandler(someXML){

	if(someXML.getElementsByTagName("validationStatus").length){
  
    var validation_response=someXML.getElementsByTagName("validationStatus")[0].childNodes.item(0);

    if(validation_response!=undefined){

      if(validation_response.data=='error'){
    	  
        document.getElementById('productErrorDialog').style.display='block';
    
		var numProductErrors=someXML.getElementsByTagName("field").length;
		var validationMessage='';
				
		for(i=0;i<numProductErrors;i++){
			var oField=someXML.getElementsByTagName("field")[i];
			validationMessage+=oField.getElementsByTagName('reason').item(0).firstChild.data + "<br />";		
		}
		
		//tb_show('<b>Friendly, No-Hassle Returns</b>', '#TB_inline?height=160&width=400&inlineId=blkReturns', null); return false;
		document.getElementById('productAddNotification').innerHTML=validationMessage;
		tb_show('Problem Adding Product','#TB_inline?height=160&width=300&inlineId=productAddNotification',null);

      }else if(validation_response.data=='token-error'){

//		document.getElementById('tokenErrorDialog').style.display='block';
    	tb_show('Problem Adding Product','#TB_inline?height=160&width=300&inlineId=tokenErrorDialog',null);
    	  
      }else{
        //document.getElementById('productErrorDialog').style.display='none';
        productSubmit();
      }
	}
    
  }

}

