//javascript functions
//get Info for Extras - Laser, Scrimshaw, Checkering...

function requestExtrasInfo() {
            var sProd = document.getElementById("txtProdNameId").value;
            //alert('Prod : ' + sProd);
            var oXmlHttp = zXmlHttp.createRequest();
            oXmlHttp.open("get", "getextras.php?prod=" + sProd, true);
            //alert('Prod : ' + sProd);
            oXmlHttp.onreadystatechange = function () {
                if (oXmlHttp.readyState == 4) {
                    if (oXmlHttp.status == 200) {
                        displayExtrasInfo(oXmlHttp.responseText);
                    } else {
                        displayExtrasInfo("An error occurred : " + oXmlHttp.statusText + " " + oXmlHttp.status); //statusText is not always accurate
                    }
                }            
            };
            oXmlHttp.send(null);
}

function displayExtrasInfo(sText) {
            var divExtrasInfo = document.getElementById("divExtrasInfo");
            divExtrasInfo.innerHTML = sText;
}

// checking finishing pick(s)
function checkFinish()
{
	var check      = document.getElementsByName("check");
	var laser      = document.getElementsByName("laser");
	var scrim      = document.getElementsByName("scrim");
	var oval       = document.getElementsByName("oval");
	var medal      = document.getElementsByName("medal");
	var count      = 0;
	
	if (check[0].value != 'N/A' && check[0].value != 'Check')
		{
			count++; 
		}
	if (laser[0].value != 'N/A' && laser[0].value != 'Laser')
		{
			count++; 
		}
	if (scrim[0].value != 'N/A' && scrim[0].value != 'Scrim')
		{
			count++; 
		}
	if (medal[0].value != 'N/A' && medal[0].value != 'Medals')
		{
			count++; 
		}
	if (count > 1 )
		{
		alert ('Please choose only one option');
		return false;
		}
	if (count == 0)
		{
		var answer = confirm('No Finishing selection was made.\nClick OK to go back and choose your finishing options.\nCANCEL to get the grips Smooth')
		if (answer) 
			{
			return false;
			}
			else
			{
			return true;
			}
		}	
	return true;
		
}





//test for flash or no flash and reload current page
function testFlash(pageURL) {
	//test for flash version

	if (pageURL) {
		var player = new MM_FlashInfo();
		var requireFlashVersion = 6;

		if (player.version >= requireFlashVersion) {
			window.location = pageURL + "?flash=true";
		} else {
			window.location = pageURL + "?flash=false";
		}
	}
}

//swap image with a new image URL
function swapImg(imgName, imgURL) {
	imgNum = getImgNum(imgName);

	if (imgNum > -1) {document.images[imgNum].src = imgURL;}
}

//find image number using older method for compatibility
function getImgNum(imgName) {
	for (i=0; i<document.images.length; i++) {
		if (document.images[i].name == imgName) {return i;}
	}

	return -1;
}

//go to the pulldown url
function gotoURL(thisPulldown) {
	if (thisPulldown) {
		window.location = thisPulldown.options[thisPulldown.selectedIndex].value;
		return true;
	} else {
		return false;
	}
}

//Popup handling code

jQuery.fn.center = function () {
	
	this.css("position","absolute");

	this.css("top", ( $(window).height() - $(this).height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() + "px");
	
	return this;
}

function closePopup()
{
	$("#popup").fadeOut('fast');
	$('body').css({'overflow' : 'scroll'});

	$("#screen").remove();
	$("#popup").remove();
}
function openWindow(url, name, width, height) {
	if (!url) {return false;}

	if (!name) {name = "_blank";}

	properties = 'toolbar=0';
	properties += ', scrollbars=1';
	properties += ', location=0';
	properties += ', statusbar=0';
	properties += ', menubar=0';
	properties += ', resizable=1';
	properties += ', width=' + width;
	properties += ', height=' + height;
	properties += ', left=50';
	properties += ', top=50';

	//window.open(url, name, properties);

	if(url.indexOf("jpg") != -1 || url.indexOf("JPG") != -1){

		$('body').css({'overflow' : 'hidden'});

		//popup = "<div id='popup'><img onclick='javascript:closePopup();' src=" + url + " width=" + width + " height=" + height + " id='popup_image'></img>";
		popup = "<div id='popup' class='loading' onclick='javascript:closePopup();'>"; 

		popup += "<a href='javascript: closePopup();'<span style='color: #470A01'>Close</span></a><br /><br />";
		popup += "</div>";

		$("body").append(popup);

		$("#popup").fadeIn('fast').center();
		//Load image
		var img = new Image();
		$(img).load(function() {
			$("#loader").removeClass('loading').append(this);
			$(this).fadeIn();
		}).error(function(){
		}).attr('src', url).attr('width', width).attr('height', height).attr('id', 'popup_image').hide();

		$("#popup").append($(img));
		setInterval(function() { $("#popup").center() }, 1);
		temp_screen = "<div id='screen'><br /></div>";
		$("body").append(temp_screen);
		$("#screen").css({'opacity' : 0.7, 'width':$(document).width, 'height': $(document).height});
		$("#screen").center();


	}else{

		if (!width) {width = 800;}
		if (!height) {height = 600;}
		window.open(url, name, properties);
	}
	//Blank out the entire window with JS
	

}

// Shopping Cart code

var cart = "/shared/shopping_cart/cart.php";

$(document).ready(function()
{
	
	displayCart();	
	$("#cart_arrow").attr("src", "https://checkout.google.com/seller/gsc/v2_2/public/closed.png");
	$("#shopping_cart_content").hide();

});




function addToCart(prod_id)
{
	prod_id = $("#" + prod_id + "-select").val();

	if(prod_id != ''){
		$.get(cart + "?serialno=" + prod_id); 
		if($("#shopping_cart_content").is(":hidden"))
		{
			$("#shopping_cart_content").slideDown("normal");
		}
		displayCart(prod_id);
	}else{
		alert("Please select a material");
	}

}

function addToScrimshawCart(prod_id)
{
	prod_id = $("#" + prod_id + "-select").val();
	if(prod_id != '')
	{
		window.location = "/products/pistol/scrimshaw/index.php?name=" + prod_id;	
	}else{
		alert("Please select a material");
	}
}

function addToMedallionCart(prod_id)
{
	//Need to add the handling of the Colt Medallions here

	var colt_medallion = $("#colt_medallion_select");

	if(prod_id != ''){

		var address_medallion = cart + "?add_medallion=" + prod_id;

		if(colt_medallion){
			address_medallion += "&colt_medallion=" + colt_medallion.val();
		}

		$.get(address_medallion, function(data)
		{
			displayCart(prod_id);
		});
	}else{
	}

}

function updateShoppingCartQty(serial)
{

	var qty = $("#" + serial+"Qty");

	if((qty.val() == parseInt(qty.val())) && qty.val() >= 0)
	{
		// Update the item with the quantity and call redisplay
		$.get(cart + "?serialno=" + serial + "&qty=" + qty.val());

		displayCart();
	}else if(qty.val() == 0)
	{
		//Remove the item
		removeItemFromShoppingCart(serial);
	}else{
		displayCart();
	}

}

function getShoppingCartItems()
{
	$.get(cart + "?total=1", function(data){
		$("#shopping_cart_num_items").html(data);

	});
}

function removeItemFromShoppingCart(prod_id)
{
	$.get(cart + "?serialno=" + prod_id + "&remove=1");
	displayCart();
}


//Scrimshaw cart item:

function removeScrimshawItemFromShoppingCart(prod_id)
{
	$.get(cart + "?grip_no=" + prod_id + "&remove_scrimshaw=1");
	displayCart();
}



// End of scrimshaw cart items 

function displayCart(prod_id)
{
	$.get(cart + "?display=1", function(data){
		$("#shopping_cart_content").html(data);
	});

	getShoppingCartItems();
}


function minimizeCart()
{
	if(!$("#shopping_cart_content").is(":hidden")){

		$("#cart_arrow").attr("src", "https://checkout.google.com/seller/gsc/v2_2/public/closed.png");
		$("#shopping_cart_content").slideUp("normal");
	}else{

		$("#cart_arrow").attr("src", "https://checkout.google.com/seller/gsc/v2_2/public/opened.png");
		$("#shopping_cart_content").slideDown("normal");
	}

}

