function showOverlay(){
	$('body').append(
		overlay	= $('<div id="overlay"></div>')
	);
	overlay.css({
		'background-color' : '#000',
		'opacity' : '0.6',
		'cursor' : 'auto',
		'height' : $(document).height()
	});
	overlay.show();
}
function hideOverlay(){
	overlay.hide();
}

// Simple Pop out DIV
var viewport = {
    o: function() {
        if (self.innerHeight) {
			this.pageYOffset = self.pageYOffset;
			this.pageXOffset = self.pageXOffset;
			this.innerHeight = self.innerHeight;
			this.innerWidth = self.innerWidth;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			this.pageYOffset = document.documentElement.scrollTop;
			this.pageXOffset = document.documentElement.scrollLeft;
			this.innerHeight = document.documentElement.clientHeight;
			this.innerWidth = document.documentElement.clientWidth;
		} else if (document.body) {
			this.pageYOffset = document.body.scrollTop;
			this.pageXOffset = document.body.scrollLeft;
			this.innerHeight = document.body.clientHeight;
			this.innerWidth = document.body.clientWidth;
		}
		return this;
    },
    init: function(el) {
    	jQuery(el).css("display","");
        jQuery(el).css("left",Math.round(viewport.o().innerWidth/2) + viewport.o().pageXOffset - Math.round(jQuery(el).width()/2));
        jQuery(el).css("top",Math.round(viewport.o().innerHeight/2) + viewport.o().pageYOffset - Math.round(jQuery(el).height()/2));
    }
};// End Simple Pop out DIV
//Get X and Y position of an element
var elementPos = {
	init: function(el)
	{
		xPos = document.getElementById(el).offsetLeft;
		tempEl = document.getElementById(el).offsetParent;
		while (tempEl != null) {
			xPos += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		}

		yPos = document.getElementById(el).offsetTop;
		tempEl = document.getElementById(el).offsetParent;
		while (tempEl != null) {
			yPos += tempEl.offsetTop;
			tempEl = tempEl.offsetParent;
		}
		yPos = yPos - 250;
		window.scroll(xPos,yPos);
  }
};//End Get X and Y position of an element

//Edit State from Country
function dropdownCountry(country_name,divid){
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=showState&country_name='+country_name+'&divid='+divid,
    	success: function(responseText, responseStatus) {
			jQuery('#'+divid).html(responseText);
        }
     });
}//End edit State from Country
//Apply Coupon Code to Cart Total
function applyCoupon(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	var coupon_code = jQuery('#coupon_code').val();
	if(coupon_code==''){
    	jQuery('.couponError').css("display","");
		return false;
	}
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=applyCoupon&coupon_code='+coupon_code+'&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText=='couponused'){
		    	jQuery('#coupon_code').val('');
    			jQuery('.couponError').css("display","");
	    		jQuery('.couponError').html("Coupon Code had been used before.");
			}else if(responseText=='couponexpiry'){
		    	jQuery('#coupon_code').val('');
    			jQuery('.couponError').css("display","");
	    		jQuery('.couponError').html("Coupon Code Expired");
			}else if(responseText=='couponfailed'){
		    	jQuery('#coupon_code').val('');
    			jQuery('.couponError').css("display","");
	    		jQuery('.couponError').html("Invalid Coupon Code");
			}else if(responseText=='minpurchasefailed'){
		    	jQuery('#coupon_code').val('');
    			jQuery('.couponError').css("display","");
	    		jQuery('.couponError').html("Order total less than minimum purchase");
			}else{
		    	jQuery('#coupon_code').val('');
	    		jQuery('.couponError').css("display","none");
				jQuery('.CartDetail').html(responseText);
				UpdateCheckout();
			}
        }
     });
}//End Apply Coupon Code to Cart Total

//Cancel Update Checkout Info
function CancelUpdate(divid){
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CancelUpdate&divid='+divid,
    	success: function(responseText, responseStatus) {
			jQuery('.'+divid).html(responseText);
        }
     });
}//End Cancel Update Checkout Info
//Edit Billing Info
function editBilling(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=editBilling&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
			jQuery('.BillTable').html(responseText);
        }
     });
}//End Edit Billing Info
//Save Billing Info
function SaveBill(){
	if (jQuery('#bill_firstname').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter First Name!");
      	jQuery('#bill_firstname').focus();
	    return false;
	}else{
		var bill_firstname = escape(jQuery('#bill_firstname').val());
	}
	if (jQuery('#bill_lastname').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Last Name!");
      	jQuery('#bill_lastname').focus();
	    return false;
	}else{
		var bill_lastname = escape(jQuery('#bill_lastname').val());
	}
	if (jQuery('#email').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Email!");
      	jQuery('#email').focus();
	    return false;
	}
	if(validateEmail(jQuery('#email').val())){
 		jQuery('.errorCode').html("Please Enter Your Valid Email.");
  		jQuery('#email').focus();
  		return false;
	}else{
		var email = jQuery('#email').val();
	}
	if (jQuery('#bill_address').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing Address.");
      	jQuery('#bill_address').focus();
	    return false;
	}
	if (jQuery('#billing_country').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing Country.");
      	jQuery('#billing_country').focus();
	    return false;
	}
	if (jQuery('#bill_state').val()==""&&jQuery('#bill_state2').val()==''){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing State.");
      	jQuery('#bill_state').focus();
	    return false;
	}
	if (jQuery('#bill_city').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing City.");
      	jQuery('#bill_city').focus();
	    return false;
	}
	if (jQuery('#bill_zipcode').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Zip Code.");
      	jQuery('#bill_zipcode').focus();
	    return false;
	}
	if (jQuery('#bill_phone').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Phone.");
      	jQuery('#bill_phone').focus();
	    return false;
	}
	var sameshipping = jQuery('#sameshipping:checked').val();
	var icno = escape(jQuery('#icno').val());
	var company = escape(jQuery('#company').val());
	var bill_address = escape(jQuery('#bill_address').val());
	var billing_country = jQuery('#billing_country').val();
	var bill_state = jQuery('#bill_state').val();
	var bill_state2 = escape(jQuery('#bill_state2').val());
	var bill_city = escape(jQuery('#bill_city').val());
	var bill_zipcode = escape(jQuery('#bill_zipcode').val());
	var bill_phone = escape(jQuery('#bill_phone').val());
	var bill_fax = escape(jQuery('#bill_fax').val());
	
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=SaveBill&bill_firstname='+bill_firstname+'&bill_lastname='+bill_lastname+'&email='+email+'&company='+company+'&icno='+icno+'&bill_address='+bill_address+'&billing_country='+billing_country+'&bill_state='+bill_state+'&bill_state2='+bill_state2+'&bill_city='+bill_city+'&bill_zipcode='+bill_zipcode+'&bill_phone='+bill_phone+'&bill_fax='+bill_fax+'&sameshipping='+sameshipping,
    	success: function(responseText, responseStatus) {
	    	UpdateCheckout();
        }
     });
}//End Save Billing Info
//Edit Shipping Info
function editShipping(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=editShipping&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
			jQuery('.ShipTable').html(responseText);
        }
     });
}//End Edit Shipping Info
//Save Shipping Info
function SaveShip(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
		if (jQuery('#ship_firstname').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter First Name!");
      	jQuery('#ship_firstname').focus();
	    return false;
	}else{
		var ship_firstname = escape(jQuery('#ship_firstname').val());
	}
	if (jQuery('#ship_lastname').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Last Name!");
      	jQuery('#ship_lastname').focus();
	    return false;
	}else{
		var ship_lastname = escape(jQuery('#ship_lastname').val());
	}
	if (jQuery('#ship_address').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Shipping Address.");
      	jQuery('#ship_address').focus();
	    return false;
	}
	if (jQuery('#shipping_country').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Shipping Country.");
      	jQuery('#shipping_country').focus();
	    return false;
	}
	if (jQuery('#ship_state').val()==""&&jQuery('#ship_state2').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing State.");
      	jQuery('#ship_state').focus();
	    return false;
	}
	if (jQuery('#ship_city').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Shipping City.");
      	jQuery('#ship_city').focus();
	    return false;
	}
	if (jQuery('#ship_zipcode').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Shipping Zip Code.");
      	jQuery('#ship_zipcode').focus();
	    return false;
	}
	if (jQuery('#ship_phone').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Phone.");
      	jQuery('#ship_phone').focus();
	    return false;
	}

	var ship_address = escape(jQuery('#ship_address').val());
	var shipping_country = jQuery('#shipping_country').val();
	var ship_state = jQuery('#ship_state').val();
	var ship_state2 = escape(jQuery('#ship_state2').val());
	var ship_city = escape(jQuery('#ship_city').val());
	var ship_zipcode = escape(jQuery('#ship_zipcode').val());
	var ship_phone = escape(jQuery('#ship_phone').val());
	var ship_fax = escape(jQuery('#ship_fax').val());

	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=SaveShip&ship_firstname='+ship_firstname+'&ship_lastname='+ship_lastname+'&ship_address='+ship_address+'&shipping_country='+shipping_country+'&ship_state='+ship_state+'&ship_state2='+ship_state2+'&ship_city='+ship_city+'&ship_zipcode='+ship_zipcode+'&ship_phone='+ship_phone+'&ship_fax='+ship_fax,
    	success: function(responseText, responseStatus) {
	    	UpdateCheckout();
        }
     });
}//End Save Shipping Info

//Edit Delivery Method
function SetShipping(shippingid){
	jQuery("#loading").css("display","");
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=SetShipping&shipping_id='+shippingid+'&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
			jQuery('.DeliveryTable').html(responseText);
	    	UpdateCheckout(shippingid);
        }
     });
}//End Edit Delivery Method

function SaveCustNote(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	var cust_note = jQuery('#cust_note').val();

	cust_note = escape(cust_note);
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=SaveNote&custnote='+cust_note+'&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
        }
     });
}
function CheckCheckoutCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.frm2Checkout.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	  	    	
	    	}
        }
     });
}
function CheckWorldCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.frmWorldPay.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	  	    	
	    	}
        }
     });
}
function CheckAuthorizeCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.frmAuthorize.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	  	    	
	    	}
        }
     });
}
function CheckLinkCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.frmLinkPoint.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	  	    	
	    	}
        }
     });
}
function CheckCyberCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.frmCyber.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	  	    	
	    	}
        }
     });
}
function CheckIpayCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.frmiPay.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	  	    	
	    	}
        }
     });
}
function CheckPaypalCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.frmCheckout.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	    	
	    	}
        }
     });
}
function CheckOfflineCheckout(){
	var cookie_cartid = jQuery('#cookie_cartid').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=CheckInfoCheckout&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='failed'){
		    	document.offlineForm.submit();
	    	}else{
		    	alert('Please fill in billing and shipping info before checkout.');	    	
	    	}
        }
     });
}
//Update Cart Total
function UpdateCheckout(shipping_id){
	viewport.init("#loading");
	var cookie_cartid = jQuery('#cookie_cartid').val();

	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=UpdateCheckout&shipping_id='+shipping_id+'&cart_id='+cookie_cartid,
    	success: function(responseText, responseStatus) {
			jQuery("#loading").css("display","none");
			jQuery('.CheckoutTotal').html(responseText);

    	}
	});
}//End Update Cart Total

//Update Simulate/Calculate Shipping Total
function SimulateShipping(){
	viewport.init("#loading");
	var cookie_cartid = jQuery('#cookie_cartid').val();
	var cal_country = jQuery('#cal_country').val();
	var cal_state = jQuery('#cal_state').val();

	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=SimulateShipping&cart_id='+cookie_cartid+'&cal_country='+cal_country+'&cal_state='+cal_state,
    	success: function(responseText, responseStatus) {
			jQuery("#loading").css("display","none");
			jQuery('.CheckoutTotal').html(responseText);
			jQuery('.estimateship').hide();
			jQuery('.estimateShipping').show();
    	}
	});
}//End Update Simulate/Calculate Shipping Total

//Update Cart Total
function SimulateState(stateid){
	jQuery('#cal_state').val() = stateid;
}//End Update Cart Total

//Edit State from Simulate Shipping
function dropdownSimulate(country_name,divid){
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=showSimulate&country_name='+country_name+'&divid='+divid,
    	success: function(responseText, responseStatus) {
			jQuery('#'+divid).html(responseText);
        }
     });
}//End edit State from Simulate Shipping
//Pop Up Tell A Friend
function friend(product_id){
	
	showOverlay();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=TellFriend&product_id='+product_id,
    	success: function(responseText, responseStatus) {
			windowDiv = "<div class='popup_inner'><div class='popup_header'><div style='float:right;'><a href='#' class='popup_close'></a></div><b>"+friendtitle+"</b></div><div class='popup_content'><form id='friendform' name='friendform' action='"+webpath+"ecompro/ajax/ecommerce.php' method='post'>"+responseText+"</div><div class='popup_footer'></div></div>";
		    jQuery("#popwizard").html(windowDiv);
		    viewport.init("#popwizard");
		    $(window).bind("scroll", function() {
				centerDialog('popwizard')
			});
		    jQuery(".popup_close").click(function(){
		        jQuery(".popup_inner").remove();
				jQuery("#popwizard").css("display","none");				
				hideOverlay();
		        return false;
		    });
        	//var randomnumber=Math.floor(Math.random()*1001);
       		//jQuery('#code').attr("src",webpath+"common/lib.randimage.php?randomnumber="+randomnumber);

			jQuery(".emailBtn").click(function(e){
				e.preventDefault();
				var valid = true;
				var product_id = jQuery('#product_id').val();
				var yourname = jQuery('#yourname').val();
				if (yourname == "") {
			      	jQuery('.errorCode').html("Please Fill in Your Name!");
			      	jQuery("#yourname").focus();
			      	return false;
			      	valid = false;
			    }
				var youremail = jQuery('#youremail').val();
				if(validateEmail(youremail)){
			 		jQuery('.errorCode').html("Please Enter Your Valid Email.");
			  		jQuery("#youremail").focus();
			  		return false;
			      	valid = false;
				}
				var friendname = jQuery('#friendname').val();
				if (friendname == "") {
			      	jQuery('.errorCode').html("Please Fill in Your Friend Name!");
			      	jQuery("#friendname").focus();
			      	return false;
			      	valid = false;
			    }
				var friendemail = jQuery('#friendemail').val();
				if(validateEmail(friendemail)){
			 		jQuery('.errorCode').html("Please Enter Your Friend Valid Email.");
			  		jQuery("#friendemail").focus();
			  		return false;
			      	valid = false;
				}
				var yourmessage = jQuery('#yourmessage').val();
				if(yourmessage == ""){
			 		jQuery('.errorCode').html("Please Fill in Email Message.");
			  		jQuery("#yourmessage").focus();
			  		return false;
			      	valid = false;
				}
				var emailcode = jQuery('#emailcode').val();
				if (emailcode == "") {
			 		jQuery('.errorCode').html("Please Enter Valid Code.");
			  		jQuery("#emailcode").focus();
			  		return false;
			      	valid = false;
				}
			    if(valid == true){
					viewport.init("#loading");
					jQuery("#loading").css("display","");
					jQuery.ajax({
				    	type: "POST",
				    	url: webpath+"ecompro/ajax/ecommerce.php",
				    	data: 'pageaction=EmailFriend&product_id='+product_id+'&yourname='+yourname+'&youremail='+youremail+'&friendname='+friendname+'&friendemail='+friendemail+'&yourmessage='+yourmessage+'&emailcode='+emailcode,
					    success: function(responseText, responseStatus) {
							jQuery("#loading").css("display","none");
						    if(responseText=='success'){
					       		jQuery(".popup_content").html('<br/>Thank you for recommending our product, an email has been sent to your friend.<br/><br/>');
						    }else{
				            	var randomnumber=Math.floor(Math.random()*1001);
				           		jQuery('#code').attr("src",webpath+"common/lib.randimage.php?randomnumber="+randomnumber);
			 					jQuery('.errorCode').html(responseText);
				       		}
				    	}
				    });
			    }
			});
        }
     });
}//End Pop Up Friend

//Pop Up Write Review
function writeReview(product_id){
	showOverlay();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=WriteReview&product_id='+product_id,
    	success: function(responseText, responseStatus) {
			windowDiv = "<div class='popup_inner'><div class='popup_header'><div style='float:right;'><a href='#' class='popup_close'></a></div><b>"+reviewtitle+"</b></div><div class='popup_content'><form id='reviewform' name='reviewform' action='"+webpath+"ecompro/ajax/ecommerce.php' method='post'>"+responseText+"</div><div class='popup_footer'></div></div>";
		    jQuery("#popwizard").html(windowDiv);
		    viewport.init("#popwizard");
		    $(window).bind("scroll", function() {
				centerDialog('popwizard')
			});
		    jQuery(".popup_close").click(function(){
		        jQuery(".popup_inner").remove();
				jQuery("#popwizard").css("display","none");
				hideOverlay();
		        return false;
		    });
            //var randomnumber=Math.floor(Math.random()*1001);
           	//jQuery('#validationcode').attr("src",webpath+"common/lib.randimage.php?randomnumber="+randomnumber);

			jQuery(".reviewSubmit").click(function(e){
				var author = jQuery('#author').val();
				if (author == "") {
					jQuery('.errorCode').css("display","");
			      	jQuery('.errorCode').html("Please Fill in Your Name.");
			      	jQuery("#author").focus();
			      	return false;
			    }
				var reviews = jQuery('#reviews').val();
				reviews = escape(reviews);
				if (reviews == "") {
					jQuery('.errorCode').css("display","");
			      	jQuery('.errorCode').html("Please Enter a Review!");
			      	jQuery("#reviews").focus();
			      	return false;
			    }
			    var validationcode = '';

			    if ($("#validationcode").length>0){
					var validationcode = jQuery('#validationcode').val();
					if (validationcode == "") {
						jQuery('.errorCode').css("display","");
				      	jQuery('.errorCode').html("Please Fill in Verify Code!");
				      	jQuery("#validationcode").focus();
				      	return false;
				    }
			    }
				var product_id = jQuery('#product_id').val();

				viewport.init("#loading");
				jQuery("#loading").css("display","");
				jQuery.ajax({
			    	type: "POST",
			    	url: webpath+"ecompro/ajax/ecommerce.php",
			    	data: 'pageaction=submitComment&product_id='+product_id+'&author='+author+'&reviews='+reviews+'&validationcode='+validationcode,
			    	success: function(responseText, responseStatus) {
						jQuery("#loading").css("display","none");
				    	if(responseText!='failed'){
 							jQuery('.CommentTable').html(responseText);
 							jQuery('.popup_content').html("<br/>Thank you for sending us review.<br/><br/>");
						}else{
				            var randomnumber=Math.floor(Math.random()*1001);
				           	jQuery('#validationcode').attr("src",webpath+"common/lib.randimage.php?randomnumber="+randomnumber);
							jQuery('.errorCode').css("display","");
							jQuery('.errorCode').html('Invalid Verify Code');
						}
			        }
			     });
			});
        }
     });
}//End Pop Up Write Review
//Click Add to Cart Button
function addCart(product_str){
	showOverlay();
	viewport.init("#loading");
	jQuery("#loading").css("display","");

	var submitCart = '';
	var product_qty = jQuery('#product_qty').val();
	var cookie_cartid = jQuery('#cookie_cartid').val();
	var mySplit = product_str.split("_");
	var option_str = '';
	var i = 0;
	$('.relatedoption :selected').each(function(i, selected){
		i = i + 1;
		if(i==1){
			if($(selected).val()==''){
				alert("Please Select Option!");
				jQuery("#loading").css("display","none");
			  	submitCart = 'failed';
			}
			option_str = $(selected).val();
		}else{
			option_str = option_str+', '+$(selected).val();
		}
	});
	if(submitCart!='failed'){

		jQuery.ajax({
	    	type: "POST",
	    	url: webpath+"ecompro/ajax/ecommerce.php",
	    	data: 'pageaction=addCart&product_id='+mySplit[1]+'&cart_id='+cookie_cartid+'&product_qty='+product_qty+'&option_str='+option_str,
	    	success: function(responseText, responseStatus) {
				jQuery("#loading").css("display","none");
				windowDiv = "<div class='popup_inner'><div class='popup_header'><div style='float:right;'><a href='#' class='popup_close'></a></div><b>"+poptitle+"</b></div><div class='popup_content'>"+responseText+"</div><div class='popup_footer'></div></div>";
			    jQuery("#popwizard").html(windowDiv);
			    viewport.init("#popwizard");
			    $(window).bind("scroll", function() {
					centerDialog('popwizard')
				});
				setTimeout(function(){
					jQuery(".popup_inner").fadeOut();
					jQuery("#popwizard").css("display","none");
					hideOverlay();
				}, 5000);
			    jQuery(".popup_close").click(function(){
			        jQuery(".popup_inner").remove();
					jQuery("#popwizard").css("display","none");
					hideOverlay();
			        return false;
			    });
				jQuery('#cartBox').html(responseText);
				if(jQuery('#CartLink').length>0){
					jQuery.ajax({
				    	type: "POST",
				    	url: webpath+"ecompro/ajax/ecommerce.php",
				    	data: 'pageaction=UpdateCartLink',
				    	success: function(responseText, responseStatus) {
							jQuery('#CartLink').html(responseText);
					    	
				    	}
			    	});
		    	}
	        }
	     });
	 }
 }
 
function centerDialog(el){
	 
    if (self.innerHeight) {
		cpageYOffset = self.pageYOffset;
		cpageXOffset = self.pageXOffset;
		cinnerHeight = self.innerHeight;
		cinnerWidth = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		cpageYOffset = document.documentElement.scrollTop;
		cpageXOffset = document.documentElement.scrollLeft;
		cinnerHeight = document.documentElement.clientHeight;
		cinnerWidth = document.documentElement.clientWidth;
	} else if (document.body) {
		cpageYOffset = document.body.scrollTop;
		cpageXOffset = document.body.scrollLeft;
		cinnerHeight = document.body.clientHeight;
		cinnerWidth = document.body.clientWidth;
	}


    var leftvalue = Math.round(cinnerWidth/2) + cpageXOffset - Math.round(jQuery('#'+el).width()/2);
    var topvalue = Math.round(cinnerHeight/2) + cpageYOffset - Math.round(jQuery('#'+el).height()/2);
    $('#'+el).animate({
		left: leftvalue,
		top: topvalue
	}, {
		duration: 10
	});
}
//End Click Add to Cart Button
$(function() {
	//Product detail Star Rating
	if($('.auto-submit-star').length>0){
	 	$('.auto-submit-star').rating({
			callback: function(value, link){
				var product_id = $('#rateproduct').val();
				jQuery.ajax({
					type: "POST",
					url: webpath+"ecompro/ajax/ecommerce.php",
					data: 'pageaction=StarRating&rating='+value+'&product_id='+product_id,
					success: function(responseText, responseStatus) {
						jQuery('.startext').html(responseText);
				    }
				 });
			}
		});
	}
	//Youtube Video
	jQuery(".showvideo").click(function(e){
		showOverlay();
		var youtubelink = this.href.replace(new RegExp("watch\?v=", "i"), 'v/');
		windowDiv = "<div class='popup_inner' style='width:500px'><div class='popup_header'><div style='float:right;'><a href='#' class='popup_close'></a></div><b>You Tube</b></div><div class='popup_content'><embed width='480' height='385' src='"+youtubelink+"' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' wmode='transparent'></embed></div><div class='popup_footer'></div></div>";
	    jQuery("#popwizard").html(windowDiv);
	    viewport.init("#popwizard");
	    $(window).bind("scroll", function() {
			centerDialog('popwizard')
		});
	    jQuery(".popup_close").click(function(){
	        jQuery(".popup_inner").remove();
			jQuery("#popwizard").css("display","none");
			hideOverlay();
	        return false;
	    });
	    return false;
    });
	jQuery(".enquiryBtn").click(function(e){
		showOverlay();
		var product_str = jQuery(this).attr('id');
		var mySplit = product_str.split("_");
		var product_id = mySplit[1];
		jQuery.ajax({
	    	type: "POST",
	    	url: webpath+"ecompro/ajax/ecommerce.php",
	    	data: 'pageaction=enquiryForm&product_id='+product_id,
	    	success: function(responseText, responseStatus) {
				windowDiv = "<form id='frmEnquiry' name='frmEnquiry' action='"+webpath+"ecompro/ajax/ecommerce.php' method='post'><div class='popup_inner'><div class='popup_header'><div style='float:right;'><a href='#' class='popup_close'></a></div><b>"+enquirytitle+"</b></div><div class='popup_content'>"+responseText+"</div><div class='popup_footer'></div></div>";
			    jQuery("#popwizard").html(windowDiv);
			    viewport.init("#popwizard");
			    $(window).bind("scroll", function() {
					centerDialog('popwizard')
				});
			    jQuery(".popup_close").click(function(){
			        jQuery(".popup_inner").remove();
					jQuery("#popwizard").css("display","none");
					hideOverlay();
			        return false;
			    });
            	//var randomnumber=Math.floor(Math.random()*1001);
           		//jQuery('#enquirycode').attr("src",webpath+"common/lib.randimage.php?randomnumber="+randomnumber);
			    jQuery(".sendEnquiryBtn").click(function(e){
				var product_id = jQuery('#product_id').val();
				var enquiryname = jQuery('#enquiryname').val();
				if (enquiryname == "") {
			      	jQuery('#errorenquiry').html("Please Fill in Your Name.");
			      	jQuery("#enquiryname").focus();
			      	return false;
			    }
				var enquiryemail = jQuery('#enquiryemail').val();
				if(validateEmail(enquiryemail)){
			 		jQuery('.errorCode').html("Please Enter Your Email Address.");
			  		jQuery("#enquiryemail").focus();
			  		return false;
				}
				var enquirymsg = jQuery('#enquirymsg').val();
				if (enquirymsg == "") {
			      	jQuery('#errorenquiry').html("Please Enter Your Question or Inquiry.");
			      	jQuery("#enquirymsg").focus();
			      	return false;
			    }
				var enquirycode = jQuery('#enquirycode').val();
				if (enquirycode == "") {
			      	jQuery('#errorenquiry').html("Please Fill in Verify Code.");
			      	jQuery("#enquirycode").focus();
			      	return false;
			    }
				var option_str = '';
				var i = 0;
				$('.relatedoption :selected').each(function(i, selected){
					i = i + 1;
					if(i==1){
						option_str = $(selected).val();
					}else{
						option_str = option_str+', '+$(selected).val();
					}
				});
				document.frmEnquiry.option_str.value = option_str;

				viewport.init("#loading");
				jQuery("#loading").css("display","");
				jQuery.ajax({
			    	type: "POST",
			    	url: webpath+"ecompro/ajax/ecommerce.php",
			    	data: 'pageaction=sendEnquiry&product_id='+product_id+'&enquiryname='+enquiryname+'&enquiryemail='+enquiryemail+'&option_str='+option_str+'&enquirymsg='+enquirymsg+'&enquirycode='+enquirycode,
			    	success: function(responseText, responseStatus) {
						jQuery("#loading").css("display","none");
				    	if(responseText!='failed'){
							jQuery('.popup_content').html("<br/>Thank you for contacting us. We will review your inquiry and get back to you soon.<br/><br/>");
						}else{
				            var randomnumber=Math.floor(Math.random()*1001);
				           	jQuery('#enquirycode').attr("src",webpath+"common/lib.randimage.php?randomnumber="+randomnumber);
							jQuery('#errorenquiry').css("display",'');
							jQuery('#errorenquiry').html("Invalid Verify Code");
						}
			        }
			     });
			});
			}
		});
	});
});
//Offline Payment Info
function OfflinePaymentInfo(payment_method){
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=OfflinePaymentInfo&payment_method='+payment_method,
    	success: function(responseText, responseStatus) {
			jQuery('.OfflineInfo').html(responseText);
        }
     });
}//End Offline Payment Info
//function Validate Email contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
function validateEmail(email){
	if(! /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email)){
	      	return true;
		}
}//End Validate Email
//Product Options
function selectOptions(item_id, option_id, num_opt){
	var product_id = jQuery('#product_id').val();
	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=ProductOptions&product_id='+product_id+'&item_id='+item_id+'&option_id='+option_id+'&num_opt='+num_opt,
    	success: function(responseText, responseStatus) {
			jQuery('#ProductOptions').html(responseText);
        }
     });
}//End Offline Payment Info
//Customer Sign Up Password Matching
function matchPassword(){
	var password = jQuery('#password').val();
	var password1 = jQuery('#password1').val();
	if(password1!=password){
		jQuery('#errorCode').html('Password Does Not Match, Please Re-enter');
		jQuery('#password1').focus();
	}
}
//End Customer Sign Up Password Matching
//Register New Customer at Checkout Page
function validateForm2(){
	if (jQuery('#bill_firstname').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your First Name.");
      	jQuery('#bill_firstname').focus();
	    return false;
	}else{
		var bill_firstname = escape(jQuery('#bill_firstname').val());
	}
	if (jQuery('#bill_lastname').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Last Name.");
      	jQuery('#bill_lastname').focus();
	    return false;
	}else{
		var bill_lastname = escape(jQuery('#bill_lastname').val());
	}
	if (jQuery('#email').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Email.");
      	jQuery('#email').focus();
	    return false;
	}
	if(validateEmail(jQuery('#email').val())){
 		jQuery('.errorCode').html("Invalid Email Address. Please Re-enter.");
  		jQuery('#email').focus();
  		return false;
	}else{
		var email = jQuery('#email').val();
	}	
	if (jQuery('#bill_address').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing Address.");
      	jQuery('#bill_address').focus();
	    return false;
	}
	if (jQuery('#billing_country').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing Country.");
      	jQuery('#billing_country').focus();
	    return false;
	}
	if (jQuery('#bill_state').val()==""&&jQuery('#bill_state2').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing State.");
      	jQuery('#bill_state').focus();
	    return false;
	}
	if (jQuery('#bill_city').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Billing City.");
      	jQuery('#bill_city').focus();
	    return false;
	}
	if (jQuery('#bill_zipcode').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Zip Code.");
      	jQuery('#bill_zipcode').focus();
	    return false;
	}
	if (jQuery('#bill_phone').val()==""){
		jQuery('.errorCode').css("display","");
      	jQuery('.errorCode').html("Please Enter Your Phone.");
      	jQuery('#bill_phone').focus();
	    return false;
	}
	var icno = escape(jQuery('#icno').val());
	var company = escape(jQuery('#company').val());
	var bill_address = escape(jQuery('#bill_address').val());
	var billing_country = jQuery('#billing_country').val();
	var bill_state = jQuery('#bill_state').val();
	var bill_state2 = escape(jQuery('#bill_state2').val());
	var bill_city = escape(jQuery('#bill_city').val());
	var bill_zipcode = escape(jQuery('#bill_zipcode').val());
	var bill_phone = escape(jQuery('#bill_phone').val());
	var bill_fax = escape(jQuery('#bill_fax').val());
	var sameshipping = $('#sameshipping:checked').val();

	jQuery.ajax({
    	type: "POST",
    	url: webpath+"ecompro/ajax/ecommerce.php",
    	data: 'pageaction=newregister&bill_firstname='+bill_firstname+'&bill_lastname='+bill_lastname+'&icno='+icno+'&email='+email+'&company='+company+'&bill_address='+bill_address+'&billing_country='+billing_country+'&bill_state='+bill_state+'&bill_state2='+bill_state2+'&bill_city='+bill_city+'&bill_zipcode='+bill_zipcode+'&bill_phone='+bill_phone+'&bill_fax='+bill_fax+'&sameshipping='+sameshipping,
    	success: function(responseText, responseStatus) {
	    	if(responseText!='duplicated'&&responseText!='failed'){
      			jQuery('.errorCode').html(responseText);
    		}else if(responseText=='duplicated'){
	    		var pathname = window.location.pathname;
      			jQuery('.errorCode').html("Your email address is already exist. If you have registered, please <a href='"+pathname+"?homeinclude=login'>sign in to your account</a> before checkout.");
      			jQuery('#email').focus();
    		}
        }
     });
}
//End Register New Customer at Checkout Page
