// JavaScript Document

// EDIT THESE VALUES IF REQUIRED
var alertText = 'Are you sure you wish to perform this action' + "\n"; //DEFAULT TEXT DISPLAYED ON CONFIRM BUTTONS/LINKS WHEN NO ALT/TITLE
var newWindowLink = true; //OPEN EXTERNAL LINKS BY DEFAULT IN A NEW WINDOW (TRUE/FALSE)?

var stampDesignComplete = false;


var woms = new Array(); //CREATE ARRAY OF FUNCTIONS TO LAUNCH ONLOAD

//ONLOAD MANAGER
function womOn(){
  window.onload = womGo;
}

function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}

function womAdd(func){
  woms[woms.length] = func;
}

function CreateBookmarkLink() {

	title = document.title;
	url = window.location.href;
	
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	} else if(window.external) {
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) {
		return true;
	}
	
}

function objToggle(obj) { 

	var obj = document.getElementById(obj);

	if(obj.style.display == "block") { 
		obj.style.display = "none";
	} else { 
		obj.style.display = "block";
	}

}

//BROWSER HEIGHT (available height allowing for browser toolbars etc.)

var viewportwidth;
var viewportheight;

function getBrowserSizes(){
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		  viewportwidth = window.innerWidth,
		  viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   viewportwidth = document.documentElement.clientWidth,
		   viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	
	//-->
	
}


// COMPLETES DELIVERY ADDRESS THE SAME AS STANDARD ADDRESS
function same_delivery() {
	
	if (document.getElementById('use_billing'))
	{
		// SPAN TO SHOW BUTTON WHICH AUTO-COMPLETES DELIVERY ADDRESS
		document.getElementById('use_billing').innerHTML	=	'<label for="same_address">Deliver to my billing address</label>';
		document.getElementById('use_billing').innerHTML	+=	'<input type="checkbox" class="checkbox" name="same_address" id="same_address" value="" />';
		
		
		var from_fields=new Array();
		var to_fields=new Array();
		from_fields[0]='first_name';
		from_fields[1]='surname';
		from_fields[2]='telephone_number';
		from_fields[3]='address_1';
		from_fields[4]='address_2';
		from_fields[5]='town';
		from_fields[6]='county';
		from_fields[7]='postcode';
		from_fields[8]='country';
		from_fields[9]='company';
		from_fields[10]='address_3';
		
		to_fields[0]='delivery_first_name';
		to_fields[1]='delivery_surname';
		to_fields[2]='delivery_telephone_number';
		to_fields[3]='delivery_address_1';
		to_fields[4]='delivery_address_2';
		to_fields[5]='delivery_town';
		to_fields[6]='delivery_county';
		to_fields[7]='delivery_postcode';
		to_fields[8]='delivery_country';
		to_fields[9]='delivery_company';
		to_fields[10]='delivery_address_3';
		
		var default_values=new Array();
		for (i=0;i<to_fields.length;i++){
			var field_id=to_fields[i];
			if(document.getElementById(to_fields[i])) default_values[i]=document.getElementById(to_fields[i]).value;
		}
		
		// ACTION WHEN USER CLICKS BUTTON
		document.getElementById('same_address').onclick=function() {
			if (document.getElementById('same_address').checked==true) {
				for (i=0;i<from_fields.length;i++) {
					if(document.getElementById(to_fields[i]) && document.getElementById(from_fields[i]))
					{
						document.getElementById(to_fields[i]).value=document.getElementById(from_fields[i]).value;
					}
				}
			}
			else {
				for (i=0;i<default_values.length;i++) {
					if(document.getElementById(to_fields[i]))	document.getElementById(to_fields[i]).value=default_values[i];
				}
			}
		}
	}
}



/*
*
*
*
*
*
*/

function updateBasket(){
	
	document.getElementById("basket").submit();
}

/*
*
*	function changeDeliveryOption
*
*/


function changeDeliveryOption(site_root, val)
{
	window.location	=	site_root + "?page=pages&url=basket&delivery_option_id="+val;	
}


/*
*
*	function removeNotice()
*	remove a notice (defined by divID) after 5 seconds by fading out
*
*/

function removeNotice(divID){
	
	if (document.getElementById(divID))
	{
		setTimeout(function(){$('#'+divID).fadeOut('slow'); }, 3000);
	}
	
	
}



$(document).ready(function(){
						   
	if($('.categories').length>0)	initMenu();	//INITIALISE SLIDE MENU IF PRESENT
	
	//IF FEATURED/PRODUCT DIV PRESENT
	//if($('.featured').length==0 || $('.product').length==0) return false;
	
	$(".checkoutConfirmButton").click(function(){								   
		this.attr('disabled',true);
		//this.attr.addClass('transparent');
	});

	$(".select_size").change(function()
	{		
		stamp_colours($(this));			 
	});
	
	$(".add_product").click(function(){									 
		if(!choose_stamp_validation($(this))) return false;
	});
	
});




/*
*
*	function confirmPageUnload
*
*/

function confirmPageUnload(){
	
	window.onbeforeunload = confirmBrowseAway;
}


/*


*/

function confirmBrowseAway()
{	
	if(!stampDesignComplete) return "If you continue, you will lose any changes you have made to your design.";
}


/*

*/

function designComplete(state)
{
	stampDesignComplete = state;
}


