function manifestAdd(cProductId,cActionId){
  var elements=document.forms["frmProductAdd"].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 />";		
		}
		
		document.getElementById('productErrorMessage').innerHTML=validationMessage;

      }else if(validation_response.data=='token-error'){

		document.getElementById('tokenErrorDialog').style.display='block';      
      	
      }else{
        document.getElementById('productErrorDialog').style.display='none';
        productSubmit();
      }
	}
  }

}

//acTracked and acUnavailable[]=array

function inventoryCheck(oForm){

	var acCheck=new Array();
	var checkIndex=0;
	var nNumTracked=acTracked.length;
	
	//assemble our list of options for the combination
	for(var i=0;i<nNumTracked;i++){
		var oElement=oForm['pc_product_attribute' + acTracked[i]];
		if(oElement&&(oElement.type=='select'||oElement.type=='select-one')){
			var optionId=oElement.value;
			if(optionId===""){ //if no option has been chosen
				inventoryErrorDisplay(0);
				return;
			}
			acCheck[checkIndex]=optionId;
			checkIndex++;
		} 
	}
		
	if(!acUnavailable||!acUnavailable.length){
		inventoryErrorDisplay(0);
		return;
	}
		
	var nNumUnavailable=acUnavailable.length;
		
	for(var i=0;i<nNumUnavailable;i++){
	
		bMatch=true;
		
		var nNumOptions=acUnavailable[i].length;
		
		for(var j=0;j<nNumOptions;j++){

			if(!in_array(acUnavailable[i][j],acCheck)){
				bMatch=false;
				break;
			}
		}
		
		if(bMatch){
			inventoryErrorDisplay(1);
			return;
		}
	}
	
	inventoryErrorDisplay(0);
}		

function inventoryErrorDisplay(bDisplay){
	if(bDisplay){

		try{
			document.getElementById('productErrorMessage').innerHTML=cOutOfStockMessage;
		}catch(e){
			document.getElementById('productErrorMessage').innerHTML='Sorry, this configuration is out of stock';
		}
		
        document.getElementById('productErrorDialog').style.display='block';
    	document.getElementById('addToCartContainer').style.display='none';
    
	}else{	//remove the warning
	  document.getElementById('productErrorMessage').innerHTML='';
	  document.getElementById('productErrorDialog').style.display='none';
      document.getElementById('addToCartContainer').style.display='block';
	}
}

function myAppear(cUrl){
	//tb_show('',cUrl + '&KeepThis=true&TB_iframe=true&height=515&width=800');
	tb_show('',cUrl + '&KeepThis=true&TB_iframe=true&height=436&width=618');
	return false;
}

function swatchClick(swatchId){
	document.getElementById('enlarged').src=acSwatches[swatchId];
	currentSwatchId=swatchId;
}

function colorSelect(optionId){
for(iOption in acColorOption){
	if(optionId==iOption){
		swatchClick(acColorOption[iOption]);
		break;
	}
}

}