/**
	RichCart operation
*/
richCart={
	id:null, // richCart id
	url:null,//request url
	isAddOpenCart:false, //check if add to cart open the cart,if  it is true mouseleave will don't use.
	isPageFresh:true,	//check if page is fresh, if true will ajax post request to server and then get response data.
	isOpened:false,   //check richCart oppened status,if true richCart is oppened.
	maskDivId:null,   //mask div,div can't cover select in IE , but iframe can cover it.div can cover iframe, so set maskDiv()iframe under the richCart.
	itemCountClass:null,  //display  0 Item = $0.00 
	formId:null,//store post form
	mouseX:null,//store mouse event point
	mouseY:null,//store mouse event point
	requestFormDate:null,
	postForm:function(formId,e){
		if(!formId){
			return;
		}
		
			this.mouseX=this.checkOutMouseX(e);
			this.mouseY=this.checkOutMouseY(e);
		
		this.formId=formId,
		this.isPageFresh=false;
		this.isAddOpenCart=false;
		this.ajaxPostForm(formId);
	},
	postWishlistForm:function(formId,e){
		if(!formId){
			return;
		}
		if(!this.mouseX){
			this.mouseX=this.checkOutMouseX(e);
		}
		if(!this.mouseY){
			this.mouseY=this.checkOutMouseY(e);
		}
		this.formId=formId,
		this.isPageFresh=false;
		this.isAddOpenCart=false;
		this.wishlistAjaxPostForm(formId);
	},
	postBYOBForm:function(formId,e){
		if(!formId){
			return;
		}
		if(!this.mouseX){
			this.mouseX=this.checkOutMouseX(e);
		}
		if(!this.mouseY){
			this.mouseY=this.checkOutMouseY(e);
		}
		this.formId=formId,
		this.isPageFresh=false;
		this.isAddOpenCart=false;
		this.byobAjaxPostForm(formId);
	},
	postFormSync:function(formId){
		if(!formId){
			return;
		}
		this.isPageFresh=false;
		this.isAddOpenCart=true;
		this.ajaxPostFormSync(formId);
	},
	checkOutMouseX:function(evt) {
	if (evt.pageX)
		return evt.pageX;
	else if (evt.clientX)
		return evt.clientX + (document.documentElement.scrollLeft ?
				document.documentElement.scrollLeft :
				document.body.scrollLeft);
	else return null;
	},
	checkOutMouseY:function(evt) {
	if (evt.pageY)
		return evt.pageY;
	else if (evt.clientY)
		return evt.clientY + (document.documentElement.scrollTop ?
				document.documentElement.scrollTop :
				document.body.scrollTop);
	else return null;
	},
	/*
		ajax send request
	*/
	ajaxPostForm:function(formId){
		this.requestFormDate = this.getDatabyForm(formId);
		var bindParam={
			type: "POST",
			url:this.url,
			data:this.requestFormDate,
			success: function(data){
				togglePopup(richCart,data);

				/*--this is handled in the popupCheckoutContainer.jsp file--
				clearOmnitureVars();
				s.events="scAdd";
				s.pageName="Item Added to Cart";
				displayAlert();
				void(s.t());*/				
				 					
				
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert("error:"+textStatus);
			}
		};
		if(!formId){
			bindParam.data=null;
			bindParam.success=function(data){richCart.handleResponse(data,true);};
		}
		var quantity = $("#prodcutDetailSkuQuantity").val();
		if(this.isNumber(quantity))
			$.ajax(bindParam);
		else
			alert("You entered an invalid value for the field 'quantity' which expects a number.");
	},
	isNumber:function(obj){
		if(/^\d+$/.test(obj))
	 		return true;
		 else
			 return false;
	},
	ajaxPostFormSync:function(formId){
		var bindParam={
			async: false,
			type: "POST",
			url:this.url,
			data:this.getDatabyForm(formId),
			success: function(data){
				richCart.handleResponseSync(data);
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert("error:"+textStatus);
			}
		};
		if(!formId){
			bindParam.data=null;
			bindParam.success=function(data){richCart.handleResponse(data,true);};
		}
		$.ajax(bindParam);
	},
	/*
		populate data via formId.
		data structure:{name:value}
	*/
	getDatabyForm:function(formId){
		//alert('formId: '+formId);
		var data={};
		try{
			$('#'+formId).find("input,select,textarea").each(function(){
		      if(this.name){
		      //alert(this.name + " = "+$(this).val());
				data[this.name] = $(this).val();
			  }
			});
		}catch(e){
			alert("err"+e);
		}
		return data;
	},
	getDatabyForm2:function(formId){
		var data={};
		try{
			$('#'+formId).find("input,select,textarea").each(function(){
			var tempName = this.id;
			if (null==tempName || tempName =="") tempName = this.name;
			tempName = tempName.replace(/\//g,'_');
			tempName = tempName.replace(/\./g,'_');
		      if(tempName){
		      //alert(tempName + "="+ $(this).val());
				data[tempName] = $(this).val();
			  }
			});
		}catch(e){
			alert("err"+e);
		}
		
		return data;
	},
	getProductDatabyForm:function(formId){
		var data={};
		try{
			$('#'+formId).find("input,select,textarea").each(function(){
		      if(this.name == '/franklincovey/order/purchase/FCCartModifierFormHandler.catalogRefIds' || this.name == '/franklincovey/order/purchase/FCCartModifierFormHandler.ProductId' || this.name == '/franklincovey/order/purchase/FCCartModifierFormHandler.quantity'){
				data[this.name] = $(this).val();
			  }
			});
		}catch(e){
			alert("err"+e);
		}
		return data;
	},
	/*
		handle response data.
		fill data into richCart,show richCart.
	*/
	handleResponse:function(data){
	    $("#"+this.id).html(data);
	    $("#checkOut").show();
		/*this.showCart();
		if(!firstHover){
			if(this.itemCountClass){
				 if($("#itemCount")){
					$("."+this.itemCountClass).html("<span>"+$("#itemCount").val()+" Item = " +$("#subTotal").val()+"</span>");
				 }
			}
		}*/
	},
	handleResponseSync:function(data,firstHover){
	    $("#"+this.id).html(data);
		if(!firstHover){
			if(this.itemCountClass){
				 if($("#itemCount")){
				 	if($("#itemCount").val() == undefined){
				 		$("."+this.itemCountClass).html("<span>0 Item = 0.00</span>");
				 	} else {
						$("."+this.itemCountClass).html("<span>"+$("#itemCount").val()+" Item = " +$("#subTotal").val()+"</span>");
					}
				 }
			}
		}
	},
	/*
		show richCart:if cart is'not opened and page don't fresh,direct to show.
				     if cart is not opened and page fresh,read data from server,and then show.
	*/
	showCart:function(){

	    if(this.id){
			if(this.isOpened!=true){
				if(this.isPageFresh==false){
					this.fixIeCover.setMaskDiv($("#"+this.id).width(),$("#"+this.id).height());
					this.fixIeCover.showMaskDiv(); 
					$("#"+this.id).show(300,function(){
						richCart.isOpened=true;
					});	
				}else if(this.isPageFresh==true){
					this.ajaxPostForm();
					this.isPageFresh=false;
				}
			}
		}
	},
	/*
		close richCart:if cart is opened and don't add to cart hide page.
				   
	*/
	closeCart:function(){
		if(this.id){
			if(this.isOpened==true){
				if(this.isAddOpenCart==false){
					$("#"+this.id).hide(500,function(){
						richCart.isOpened=false;
					});
					this.fixIeCover.hideMaskDiv();
				}
			}
		}
	},
	/*
		force close cart:don't need to check if  cart is opened or add to cart.
	*/
	forceCloseCart:function(){
		if(this.id){ 
			$("#"+this.id).hide(500,function(){
				richCart.isOpened=false;
			});
			this.isOpened=false;
			this.isAddOpenCart=false;
			this.fixIeCover.hideMaskDiv();
		}
	},
	fixIeCover:{
		isIe:function(){
			if(jQuery.browser.msie){
				return true;
			}
			return false;
		},
		setMaskDiv:function(width,height){
			if(!this.isIe() && !richCart.maskDivId) return;
			$("#"+richCart.maskDivId).width(width);
			$("#"+richCart.maskDivId).height(height);
		},
		showMaskDiv:function(){
			if(!this.isIe() && !richCart.maskDivId) return;
			$("#"+richCart.maskDivId).show(300);
		},
		hideMaskDiv:function(){ 
			if(!this.isIe() && !richCart.maskDivId) return;
			$("#"+richCart.maskDivId).hide(500);
		}
		
	},
	byoAjaxPostForm:function(formId){
		var bindParam={
			async: false,
			type: "POST",
			url:this.url,
			data:this.getDatabyForm(formId),
			success: function(data){
				handleBYOResponse(richCart,data);
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert("error:"+textStatus);
			}
		};
		if(!formId){
			bindParam.data=null;
			bindParam.success=function(data){richCart.handleResponse(data,true);};
		}
		$.ajax(bindParam);
	},
	wishlistAjaxPostForm:function(formId){
		var bindParam={
			async: false,
			type: "POST",
			url:contextPath+"/cart/includes/franklinCartContents.jsp",
			data:this.getDatabyForm(formId),
			success: function(data){
				handleWishlistResponse(richCart,data,formId);
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert("error:"+textStatus);
			}
		};
		if(!formId){
			bindParam.data=null;
			bindParam.success=function(data){richCart.handleResponse(data,true);};
		}
		$.ajax(bindParam);
	},
	byobAjaxPostForm:function(formId){
		this.requestFormDate = this.getDatabyForm(formId);
		var bindParam={
			type: "POST",
			url:"/store/cart/includes/bundleError.jsp",
			data:this.requestFormDate,
			success: function(data){
				//check for errors!!
				toggleBYOBPopup(richCart,data);
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert("error:"+textStatus);
			}
		};
		if(!formId){
			bindParam.data=null;
			bindParam.success=function(data){richCart.handleResponse(data,true);};
		}
		var quantity = $("#prodcutDetailSkuQuantity").val();
		if(this.isNumber(quantity))
			$.ajax(bindParam);
		else
			alert("You entered an invalid value for the field 'quantity' which expects a number.");
	},
	isNumber:function(obj){
		if(/^\d+$/.test(obj))
	 		return true;
		 else
			 return false;
	}	
}

