// Used for pws checkout process.

/*
@var	id - of container want to show/hide on event.
@Desc	use to show/hide container on any event.
*/
function showHide(id){
	if($("#"+id).css("display") == 'none')
	$("#"+id).show('fast');
	else
	$("#"+id).hide('fast');
}

/*
* @var		show_id: Div to be show, hide_id: Div to be hide.
* @Desc	to show/hide TIN/SSN number option
*/
function showHideTaxId(show_id, hide_id){
	showHide("div_"+hide_id);
	showHide("div_"+show_id);
	$('input[name="tax_id"]').attr('value', '');
	$('input[name="business_'+hide_id+'_id1"]').attr('value', '');
	$('input[name="business_'+hide_id+'_id2"]').attr('value', '');
	$('input[name="business_'+hide_id+'_id3"]').attr('value', '');
}

/*
* @var	checkbox: checkbox id.
* @Desc	to show/hide TIN/SSN number option
*/
function showLightboxOnCheck(checkbox, link){
	if($("#"+checkbox).is(":checked"))
	{
		$("a#"+link).trigger("click");
	}
}

function showLightbox(link){
	$("a#"+link).trigger("click");
}

function alsoInAutoship(link, sku){
	showLightbox(link);
	$('#removed_sku').val(sku);
}

/*
* @Desc	Use to close light box through cancel link.
*/
function closeLightbox(action){
	if(action == 'keepAutoship'){
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  //fade them both out
		});
		$('#autoship_flag').attr('checked', true);
	}
	else if(action == 'removeAutoship'){
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  //fade them both out
		});
		$('#autoship_flag').attr('checked', false);
		$.ajax({
			async: true,
			url: "autoship_select.php?sku=all&act=del",
			success: function(response){
				window.location='show_cart.php';
			}
		});
	}
	else if(action == 'removeAutoshipCart'){
		var sku = $('#removed_sku').val();
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  //fade them both out
		});
		$.ajax({
			async: true,
			url: "autoship_select.php?sku="+sku+"&act=del",
			success: function(response){
				window.location='remove_item.php?sku='+sku;
			}
		});
	}
	else{
		$('#fade , .popup_block').fadeOut();
		$('#autoship_flag').attr('checked', false);
	}
	return false;
}

/*
* @Desc refresh cart page and display "Set Up Your AutoShip With Items From Your Cart" light box.
*/
function setMyAutoship(){
	closeLightbox();
	
		$("#autoship_list").show('fast');
		$("#autoship_flag").attr('checked', 'checked');
		window.location='show_cart.php?pThru=conv_auto';
}

/*
* @var	div_id: div having hyperlink use to add to cart.
* @var	sku: product idendity.
* @var	msg: to be display after success.
* @Desc	Use to add item in cart using ajax.
*/
function addToCart(div_id, sku, msg, toAutoship){
	$("#"+div_id).html("<font color='#619437'>adding... please wait!</font>");
	if(toAutoship){
		var pageUrl = "add_item.php?qty=1&sku="+sku+"&pThru=ajax&toAutoship="+toAutoship;
	}else{
		var pageUrl = "add_item.php?qty=1&sku="+sku+"&pThru=ajax"
	}
	$.ajax({
		async: true,
		url: pageUrl,
		success: function(response){
			$("#"+div_id).html(msg);
			var div_patt=/^add_it\d{1,2}$/
			if(div_id.search(div_patt) != -1)
			{
				$("input[name='btn_purchase_member']").attr('value', 'Check Out Now');
			}
			$("#add_order").val(1);
			if(toAutoship){
				$('#autoship_flag').attr('checked', true);
			}
		}
	});
}

function addToCartWithQuantity(div_id, sku, msg, quantity_text){
	$("#"+div_id).html("<font color='#619437'>adding... please wait!</font>");
	var qty=$('#'+quantity_text).val();
	$.ajax({
		async: true,
		url: "add_item.php?qty="+qty+"&sku="+sku+"&page_through=ajax",
		success: function(response){
			$("#"+div_id).html(msg);
		}
	});
}

function addToSession(myval){
	$.ajax({
		async: true,
		url: "productlist.php?free_item_sku="+myval+"&page_through=ajax",
		success: function(response){
			return true;
		}
	});
}
/*
* @Desc 	When item added in order, should reflet on cart when close light box.
*/
function reloadCart(valChk, parm){
	var chkValue = $('#'+valChk).val();
	closeLightbox();
	if( $('#'+valChk).is(':checked') )
	{	
		window.location='show_cart.php?pThru='+parm;
	}
	else
	{
		window.location='show_cart.php';
	}
	
}

// Function use to check at least one checkbox checked.
function checkAtleastOne(button){
	$('#parent_btn').val(button);
	var atleastone = false;
	$('input[name="auto_sku[]"]').each(function(){
		if($(this).is(':checked')){
			atleastone = true;
		}
	});

	if(atleastone == false){
		$("a#at_least_one_id").trigger("click");
		$('#lightbox').val('');
	}
}

// Function use to update autoship quantity.
function updateAutoshipQty(sku, inputbox, msg_id, msg){
	var qty = $('#'+inputbox+'_'+sku).val();
	$('#'+msg_id).html(msg);
	$.ajax({
		async: true,
		url: "autoship_select.php?sku="+sku+"&qty="+qty+"&act=edit",
		success: function(response){
			$('#'+msg_id).html('');
			window.location='show_cart.php';
		}
	});
}

function searchAutoshipProduct(div_id, search_box){
	$('#pws_loader').show('fast');	
	var key = $('#'+search_box).val();
	$.ajax({
		async: true,
		url: "autoship_search.php?search_key="+key,
		success: function(response){
			$('#'+div_id).html(response);
			$('#pws_loader').hide('fast');
		}
	});
}

function setupAutoship(div_id){
	$('#autoship_flag').attr('checked', true);
	$('#'+div_id).show('slow');
}

function showHideDiscountedList(checkbox, dis_list){
	if($("#"+checkbox).is(":checked")){
		//$('#'+dis_list).show('fast');
	}else{		
		//$('#'+dis_list).hide('fast');
	}
}

function addToAutoship(sku, qty, act, autoship_flag){
	if($("#"+autoship_flag).is(":checked")){
		window.location='autoship_select.php?sku='+sku+'&qty='+qty+'&act='+act+'&pThru=setup_auto';		
	}else{		
		window.location='autoship_select.php?sku='+sku+'&qty='+qty+'&act='+act;
	}
}

// Function use to add items in autoship.
function addAutoshipOrder(sku){	
	$('#pws_loader').show('fast');
	$.ajax({
		async: true,
		url: "autoship_select.php?sku="+sku+"&qty=1&act=add&pThru=ajax",
		success: function(response){				
			$('#autoship_'+sku).html('Already in Autoship');
			$('#autoship_summary').html(response);
			$('#pws_loader').hide('fast');
		}
	});
}

function ajaxDivReplace(data)
{
	var divs = $(data).filter(function(){ return $(this).is('div') });

	divs.each(function(index) 
	{
		var id = $(this).attr('id');
		var b = 'div[id=' +id +']';
		var newHtml = $(this).html();
		$(b).html(newHtml);
	});
}

// register the click event for ajax load and page load


