
function popupCards(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=800,height=800,left = 470,top = 200');");
}

/*
function resizeLayout(){
	// this extends the left and right hand gutter backgrounds to 100% document height

	var leftcontentBG = document.getElementById("leftcontentBG");
	var rightcontentBG = document.getElementById("rightcontentBG");
	var htmlheight = document.body.parentNode.scrollHeight;
	var windowheight = window.innerHeight;
	if ( htmlheight < windowheight )
	{
		document.body.style.height = windowheight + "px";
		document.body.style.height = windowheight + "px";
		leftcontentBG.style.height = windowheight + "px";
		rightcontentBG.style.height = windowheight + "px";
	}
	else
	{
		document.body.style.height = htmlheight + "px";
		leftcontentBG.style.height = htmlheight + "px";
		rightcontentBG.style.height = htmlheight + "px";
	}
}
*/

function updateGrandTotal(obj,amount) {

	if(obj.options[obj.selectedIndex].value=='standard') {

		document.getElementById('grandTotal').innerHTML = (parseFloat(document.getElementById('grandTotal').innerHTML) - parseFloat(amount)).toFixed(2);

	} else {

		document.getElementById('grandTotal').innerHTML = (parseFloat(document.getElementById('grandTotal').innerHTML) + parseFloat(amount)).toFixed(2);

	}

}

// jQuery
$(document).ready(function(){

	// Gift Cards
	$("div.giftcard a.card").click(function () {
		$("input[name=card]").val($(this).attr("href").replace("#card", ""));
		$("div.giftcard a.card span.selected").removeClass("selected");
		$(this).children("span").addClass("selected");
	});
	$("div.giftcard div.input ul li").hover(function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	});
	$("div.giftcard div.input ul li").click(function () {
		$("div.giftcard div.input ul li input.radio").removeAttr("checked");
		$(this).children("input.radio").attr("checked", "checked");
		$("div.giftcard div.input ul li").removeClass("selected");
		$(this).addClass("selected");
		if ($(this).children("input.radio").val() == "recip") {
			$("div.giftcard div.recipient").show();
			$("div.giftcard div.donor").hide();
		} else {
			$("div.giftcard div.recipient").hide();
			$("div.giftcard div.donor").show();
		}
	});
	$("div.giftcard div.input input, div.giftcard div.input textarea").focus(function () {
		if ($(this).parent().children("div.note").size() > 0) {
			$(this).parent().children("div.note").addClass("red");
		}
	});
	$("div.giftcard div.input input, div.giftcard div.input textarea").blur(function () {
		if ($(this).parent().children("div.note").size() > 0) {
			$(this).parent().children("div.note").removeClass("red");
		}
	});
	$("div.giftcard div.input input.tofixed").blur(function () {
		if ($(this).val() != '') {
			$(this).val(parseFloat($(this).val()).toFixed(2));
		}
	});
	$("div.giftcard div.input button").click(function () {
		$("div.giftcard div.input input[name=amount]").val($(this).val());
		return false;
	});
	$("div.giftcard div.input textarea").each(function(){
		var length = $(this).val().length;
		$(".charactercount").html(length);
		$(this).keyup(function(){
			if ($(this).val().length > $("input[name=maxchars]").val()) $(this).val($(this).val().substring(0, $("input[name=maxchars]").val()));
			var new_length = $(this).val().length;
			$(".charactercount").html(new_length);
		});
	});
	$('div.giftcard div.redeem input[name=code1]').keyup(function() {
		if ($(this).val().length == 4) $('div.giftcard div.redeem input[name=code2]').focus();
	});
	$('div.giftcard div.redeem input[name=code2]').keyup(function() {
		if ($(this).val().length == 4) $('div.giftcard div.redeem input[name=code3]').focus();
	});
	$('div.giftcard div.redeem input[name=code3]').keyup(function() {
		if ($(this).val().length == 4) $('div.giftcard div.redeem input[name=code4]').focus();
	});
	$('div.giftcard div.redeem input[name=code4]').keyup(function() {
		if ($(this).val().length == 4) $('div.giftcard div.redeem input[name=submit]').focus();
	});

	// Blur
	$("input.blur").focus(function(){
		$(this).removeClass("blur");
		if($(this).val() == $(this).attr("defaultValue")) {
		  $(this).val('');
		}
	});
	$("input.blur").blur(function(){
		if($(this).val() == '') {
			$(this).val($(this).attr("defaultValue"));
			$(this).addClass("blur");
		}
	});

	//Search Field
	$('.searchtext').focus(function(){
		if($(this).attr("type") == 'text'){
			$(this).val('');
			$(this).css({ background: '#fff', color: '#000' });
		}
	});

	//Catalog Request
	$('#catalogrequest>input[type]').focus(function(){
		if($(this).attr("type") == 'text'){
			$(this).val('');
			$(this).css({ background: '#fff', color: '#000' });
		}
	});

	$('#catalogrequest>input[name=submit]').click(function(event){
		event.preventDefault();

		var name = $("#catalogrequest>input#name");
		var address = $("#catalogrequest>input#address");
		var city = $("#catalogrequest>input#city");
		var state = $("#catalogrequest>select#state");
		var zip = $("#catalogrequest>input#zip");
		var email = $("#catalogrequest>input#email");

		var error = '';

        if ((name.val() == "Enter your name") || (name.val() == "") || (name.val() == "Missing")) {
			name.css({ color: '#f00' });
			name.val( 'Missing' );
      		error = error + "Name";
      	}

        if ((address.val() == "Address") || (address.val() == "") || (address.val() == "Missing")) {
			address.css({ color: '#f00' });
			address.val( 'Missing' );
      		error = error + "Address";
      	}

        if ((city.val() == "City") || (city.val() == "") || (city.val() == "Missing")) {
			city.css({ color: '#f00' });
			city.val( 'Missing' );
      		error = error + "City";
      	}
		if ((state.val() == "State") || (state.val() == "")) {
      		error = error + "State";
		}
        if ((zip.val() == "Zip/Postal Code") || (zip.val() == "") || (zip.val() == "Missing")) {
			zip.css({ color: '#f00' });
			zip.val( 'Missing' );
      		error = error + "Zip/Postal Code";
      	}

        if ((email.val() == "Email") || (email.val() == "") || (email.val() == "Missing")) {
			email.css({ color: '#f00' });
			email.val( 'Missing' );
      		error = error + "Email";
      	}
		if ( error != '' ) {
			$('.result').html('<img src="/layout/catalog-oops.png" height="262" width="387">');
			$('.result').lightbox_me({
				centered: true,
				overlayCSS: {background: 'transparent'}
			});
		}
		else {

			var emailcontact = 'No';
			var printcontact = 'No';
			if ($('input[name=emailcontact]').is(':checked')) {
				emailcontact="Yes";
			}
			if ($('input[name=printcontact]').is(':checked')) {
				printcontact="Yes";
			}
			if ($('input[name=alternativegiftmarkets]').is(':checked')) {
				alternativegiftmarkets="Yes";
			}

			$.ajax({
			  url: '/layout/formmail.php',
			  type: 'POST',
			  data: "name="+name.val()+"&address="+address.val()+"&city="+city.val()+"&state="+state.val()+"&zip="+zip.val()+"&email="+email.val()+"&emailcontact="+emailcontact+"&printcontact="+printcontact+"&alternativegiftmarkets="+alternativegiftmarkets,
			  success: function(data) {
				name.css({ color: '#9e9e9e' });
				name.val( 'Enter your name' );
				address.css({ color: '#9e9e9e' });
				address.val( 'Address' );
				city.css({ color: '#9e9e9e' });
				city.val( 'City' );
				zip.css({ color: '#9e9e9e' });
				zip.val( 'Zip/Postal Code' );
				email.css({ color: '#9e9e9e' });
				email.val( 'Email' );
				$('input[name=emailcontact]').removeAttr('checked');
				$('input[name=printcontact]').removeAttr('checked');
				$('input[name=alternativegiftmarkets]').removeAttr('checked');
				$('.result').html('<img src="/layout/catalog-thank-you.png" height="262" width="387">');
				$('.result').css({
					display: 'block'
				});
				$('.result').lightbox_me({
					centered: true,
					overlayCSS: {background: 'transparent'}
				});
			  }
			});
		}
	});

	// Slideshow
	if ($("#slideshow").length > 0) {
		$("#slideshow > img").remove();
		$("#slideshow").dynamicSlideshow({duration: 6000});
	}

});