var $j = jQuery.noConflict();

function ordermultiple(arg){
	
    if (arg.selector===undefined) return false;

    var addstring='';

    $j(arg.selector).each(function(){

        var stock=parseInt($j(this).attr('stock'));
        var items=0;
        if (this.value>0){
            if (this.value>stock){
                items=stock;
            } else {
                items=this.value;
            }

            id=new String(this.id);
            if (id.indexOf('product_')!=-1){
                id=this.id.substr(8);
            } else {
                id=this.id;
            }
			
            if (cc[id] && cc[id]>0 && (arg.watchforcart!==undefined && arg.watchforcart)){
                stock=stock-items-cc[id];
            } else {
                stock=stock-items;
            }
            
            addstring+=id+"_"+this.value+"^";

            if (stock<=0){
                disableproduct($j(this));
            } else {
                $j(this).attr('stock', stock);
            }
            
            if (arg.clearinputs!==undefined && arg.clearinputs)
                this.value="";
        }
    });
        
    if (addstring)
        $j.post('/ajaxer.php', {method: 'add_to_cart', ps: addstring}, function(data){
            eval(data.cart);
            eval(data.message)
        }, 'json');
        
}

function bindinstock(){

    $j(".txt_prod_summary[type!=hidden]").each(function(){
        $j(this).keyup(function(e){
            id=this.id.substr(8);
            
            if(( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))) { return false; } else { } 
            $j('#update_button_'+id).css('visibility', 'visible');
            
            var stock=parseInt($j(this).attr('stock'));
            if (cc[id] && location.pathname!='/actions/checkout.php') stock-=cc[id];
            if (this.value > stock){
                if (stock<=0){
                    var message='Sorry, this item is out of stock.'+"\n";
                    if (cc[id]) message+=' There are '+cc[id]+' items of this product in your cart though.';
                    $j.cursorMessage(message, {
                        offsetX: 10,
                        offsetY: 10,
                        hideTimeout: 5000
                    });
                    disableproduct($j(this));
                } else {
                    this.value=stock;
                    var items = stock==1? "item" : "items";
                    $j.cursorMessage('Sorry, only '+stock+' '+items+' in stock available', {
                        offsetX: 10,
                        offsetY: 10,
                        hideTimeout: 5000
                    });
                }
            } else {
                $j(this).removeClass('insufficient-stock');
                $j(this).unbind('mouseover');
            }
        });
    });

}

function disableproduct(obj){
    obj.attr('disabled', true);
    obj.val('');
    obj.css('background-color', 'rgb(128,128,128)');
}

function addtocart(productid, qty){
    if (qty>0){
        $j.post("/ajaxer.php", {method: 'add_to_cart', ps: productid+"_"+qty+"^" }, function(d){
            eval(d.message);
            eval(d.cart);
            stockamount=$j('#'+productid).attr('stock')-qty;
            if (cc[productid]) stockamount-=cc[productid];
            if (stockamount<=0) {
                stockamount=0; $j('#'+productid).attr('disabled', true);
            }
            $j('#'+productid).attr('stock', stockamount);
        }, 'json');
    }

}


$j(document).ready(function(){
    bindinstock();

    $j(".notifierlink").click(function(e){
        tooltip($j(this).attr("href"), e, $j(this).attr("pid"), $j(this).attr("instockdate"), $j(this).attr("ptitle")); return false;
    });

    $j(".addtocartbutton").click(function(){
        ordermultiple({
            selector: ".txt_prod_summary[type!=hidden]",
            clearinputs: true
        });
    });

    $j('.addtobookmarkhref').text('Add to bookmark');

});

function readmore(category_id){
  $j("#cat_" + category_id).find(".readmore").css("display","block");
  $j("#readmore_link_" + category_id).css("display","none");  
  $j("#readless_link_" + category_id).css("display","inline");  
}
function readless (category_id){
  $j("#cat_" + category_id).find(".readmore").css("display","none");
  $j("#readmore_link_" + category_id).css("display","inline");  
  $j("#readless_link_" + category_id).css("display","none");  
}

function bulk_copy_form_fields(from, to){
            $j("[name*='"+from+"']").each(function(){
                $j(this).val($j("[name='"+this.name.replace(from, to)+"']").val());
            });

}

function copy_billing_address(id){
    
	if($j('#'+id).is(':checked')  == true){

            bulk_copy_form_fields('billing_', 'delivery_');
            
		} else {

            $j("[name*='billing_']").val('');

		}
	
}

function copy_checkout_detail(id)
{
	if($j('#'+id).is(':checked')  == true){
			bulk_copy_form_fields('billing', 'delivery');
		} else {
			$j("[name*='billing']").val('');
		}
	
}

function registerAllVariable( div, file, param )
{ 

var fullurl="/actions/contactUs.php?n=" + $j('#name').val() + "&e=" + $j( '#email' ).val() + "&p=" + $j( '#phone' ).val() + "&m=" + $j('#message').val();
$j.get(fullurl, null, function (data){
		if(data!=1){
			$j("#contactUs").html('<img src="/images/naturalcandles/messagesent.gif"/>');
			$j("#message_det_form").css("display","none");
			$j("#info_message").css("display","");
		} else {
			$j("#contactUsHint").html(data);
		}
	});

} 
function registerAllVariable_contact( div, name, email, phone, message )
{

$j.post("/actions/contactUs.php", {
		n: name,
		e: email,
		p: phone,
		m: message
	}, function (data){
		if(data!=1){
			$j("#contactUs").html('<img src="/images/naturalcandles/messagesent.gif"/>');
			$j("#message_det_form").css("display","none");
			$j("#info_message").css("display","");
		} else {
			$j("#contactUsHint").html(data);
		}
});

} 

function add_to_credit(credit_left,credit_id)
{
	if(document.getElementById("credit_amt_"+credit_id).value>credit_left)
	{
		alert("You Cannot Use Credit amount more than available credit");
  
		 return false;
	}
	
	
}

var intervalID, dissapearId;
var height = 0;
var elem;
var open1 = false;


function add_to_order_open( order_id ){
	var url = new String( "/actions/add_to_existing_order.php?order_id=" );
	url += order_id;
	document.location.href=url;
}

function hide_product_block( order_id ) {
	var elem = new String( "product_table_" );
	elem += order_id;
	document.getElementById( elem ).style.display = 'none';
}


form_submited=false;
function submit_once()
{
	var temp=form_submited;
	
	if($j('#termsno').is(':checked')){
		$j("#terms_alert").css("display","block");
		return false;
	}
	form_submited=true;
	return (!temp);
}

function submit_once_disable_button()
{
	var temp=form_submited;
	
	if(document.getElementById("termsNo").checked)
	{
		document.getElementById("terms_alert").style.display="block";
		return false;
	}
	
	document.getElementById("process_order").disabled = true;
	
	form_submited=true;
	return (!temp);
}

function add_bookmark(n, elem){
    
	$j.post("/ajaxer.php", {method : "add_bookmark", pid : n }, function (d){
           switch(d.message){
               case 1:
                   $j.cursorMessage('Product added to your bookmark successfully' , { hideTimeout: 4000, offsetX: 10, offsetY: 10 } );
                   if (elem) $j(elem).hide();
               break;

           }
           
    }, 'json');
    
}

function delete_bookmark(n){
    $j.post("/ajaxer.php", {method : "remove_bookmark", product_id : n }, function(d){
        $j('#b_product_'+n).hide();
    });
} 

function  user_msg(){
var url="/actions/user_msg.php";
$j.get(url, null, function(data){
	$j("#user_msg").html(data);
});
} 

function  user_msg_all(){
var url="/actions/user_msg_all.php";
$j.get(url, null, function(data){
	if (data!=1){
		$j("#msg_notification_all").css("display","");
		$j("#user_msg_all").html(data);
	} else {
		$j("#msg_notification_all").css("display","none");
	}
});
}
 
function sendEmailNotification( divID, pID, nID ) {
    $j.post("/actions/outOfStock.php?pid=" + pID + "&email=" + $j( '#outOfStockEmail_outofstocknotifier' ).val() + "&did=" + divID + "&n=" + nID,
    null, null, 'script');
}

function tooltip(id, e, pid, instockdate, ptitle){
    var obj=$j("#outofstocknotifier");

obj.css("top", e.pageY-200);
obj.css("left", e.pageX-150);
obj.show();

obj.html('<table width="300" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td width="7" valign="top"><img src="/images/naturalcandles/cor1.jpg" width="7" height="7" /></td><td valign="top" background="/images/naturalcandles/line1.jpg"><img src="/images/naturalcandles/line1.jpg" width="2" height="7" /></td><td width="6"><img src="/images/naturalcandles/cor2.jpg" width="6" height="7" /></td></tr><tr><td background="/images/naturalcandles/line4.jpg"><img src="/images/naturalcandles/line4.jpg" width="7" height="1" /></td><td bgcolor="#ebebeb">	<b><div id="error_outofstocknotifier"></div></b>	<div align="center"><table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr><td width="71%" align="left" class="textprodcolor"><b>Out of stock notification</b></td><td width="29%" align="left" class="textprodcolor"><div align="right"><a class="closeButton" onClick="$j(\'#outofstocknotifier\').hide();"><u>close</u></a></div></td></tr><tr><td colspan="2" align="left" style="font-family: Verdana; font-size: 10px; font-weight: normal">Estimated Back in Stock Date : '+instockdate+'</td></tr><tr><td colspan="2" align="left" style="font-family: Verdana; font-size: 10px; color: #000000; font-weight: normal">Please enter your email address and we will<br> send you an email notification when new stock<br>for '+ptitle+' is available.</td></tr><tr><td colspan="2" align="left"><input name="text" type="text" class="form_login_field_long" id="outOfStockEmail_outofstocknotifier" style="width:250px" size="50"/></td></tr><tr><td colspan="2" align="right"><a href="javascript:sendEmailNotification( \'outofstocknotifier\', \''+pid+'\', \''+pid+'\' );"><img src="/images/naturalcandles/sendred.gif" border="0" /></a></td></tr></table></div>	</td><td background="/images/naturalcandles/line2.jpg"><img src="/images/naturalcandles/line2.jpg" width="6" height="1" /></td></tr><tr><td><img src="/images/naturalcandles/cor4.jpg" width="7" height="8" /></td><td background="/images/naturalcandles/line3.jpg"><img src="/images/naturalcandles/line3.jpg" width="1" height="8" /></td><td><img src="/images/naturalcandles/cor3.jpg" width="6" height="8" /></td></tr></table>');

}

// Old new_added.js below

	function chk_terms(val)
	{
		if(val == 'Y')
		{
			document.getElementById("terms_alert").style.display="none";
		}
	}
	function check_terms()
	{
		if(document.getElementById("termsNo").checked)
		{
			document.getElementById("terms_alert").style.display="block";
			return false;
		}
		else
		{
			return true;
		}
	}
	function terms_hide()
	{
		document.getElementById("terms_alert").style.display="none";	
	}

	function userssss(){
	$j.post("/ajaxer.php", { method: "get_outofstockandusermessage" }, function(data){
		$j("#cart_content").html(data.cart_content);

		if (data.usermsg==""){
			$j("#msg_notification").css("display", "none");
		} else {
			$j("#msg_notification").css("display", "");
			$j("#user_message").html(data.usermsg);
		}
	}, 'json');
	} 


	function closeNotification(){
	$j("#user_notificatio").css('display',"none");
	}

	function makeValidation(){ 
	var user=document.form1.uname.value;
	var pass=document.form1.pass.value;
	var cpass=document.form1.cpass.value;

	if(user=="" || pass=="" || cpass=="" ){
	alert("Please fillup all the fields");
	return false;
	 }
	}
		
	function showDiv(){
	document.getElementById("showDiv").style.display="block";
	}
