function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function changeHeight()
{

	var arrayPageSize = getPageSize();
	
	// get header, footer and content height
	//var headerHeight = 144;
	//var footerHeight = 155;
	var headerHeight = parseInt($('header').getHeight());
	var footerHeight = parseInt($('footer').getHeight());

	var menuHeight = parseInt($('menu').getHeight());
	var contentHeight = parseInt($('content').getHeight());
	//alert(contentHeight);
	// calculate page height - ( header + footer)
	var ph = arrayPageSize[1] - (headerHeight + footerHeight + menuHeight + 45);
	
	// if contentHeight is less than page height then resize content to page height ( footer at the bottom of the page);
	if(contentHeight<ph)
	{
		$('content').style.height = ph + "px";
		/*
		if (self.innerHeight) {	// all except Explorer
			$('content').style.height = ph + "px";
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			$('content').style.height = ph  + "px";
		} else if (document.body) { // other Explorers
			$('content').style.height = ph + "px";
		}	
		*/
	}	
}

function changeHeightI()
{
	

	var arrayPageSize = getPageSize();
	
	// get header, footer and content height
	var headerHeight = parseInt($('header').getHeight());
	var footerHeight = parseInt($('footer').getHeight());

	var contentHeight = parseInt($('content-index').getHeight());
	//alert(contentHeight);
	// calculate page height - ( header + footer)
	var ph = arrayPageSize[1] - (headerHeight + footerHeight + 30);
	
	// if contentHeight is less than page height then resize content to page height ( footer at the bottom of the page);
	if(contentHeight<ph)
	{
		$('content').style.height = ph + "px";
	}	
}

function showhidepics(hidenum)
{

	if(parseInt(hidenum)==3 || parseInt(hidenum)==4)
	{	
		$('pic-spacer').style.display="block";
	}
	else
	{
		$('pic-spacer').style.display="none";
	}

	for(var i=1;i<=4;i++)
	{
		if(i==parseInt(hidenum))
		{
			$('pic'+i).style.display = "none";
		}
		else
		{
			$('pic'+i).style.display = "block";
			
		}
	}
	
	
}

function showhidemoreinfo(linkelm)
{
	var state = $('moreinfo-box').style.display;
	
	if(state=="block")
	{

		$('moreinfo-box').style.display = "none";
		linkelm.style.backgroundImage = "url('/images/arrow_down.gif')";
	}
	else
	{

		$('moreinfo-box').style.display = "block";
		linkelm.style.backgroundImage = "url('/images/arrow_up.gif')";
	}

}


function opennewwindow(url,name)
{ 
var height=500;
var display_setting="menubar=0,toolbar=0,screenX=0,screenY=0,top=0,left=0,scrollbars=1,status=0,width=700, height="+height; 
window.open(url,name,display_setting); 
}

function opennewwindow1(url,name)
{ 
var height=700;
var display_setting="menubar=0,toolbar=0,screenX=0,screenY=0,top=0,left=0,scrollbars=1,status=0,width=700, height="+height; 
window.open(url,name,display_setting); 
}

function clearSearch(button)
{
	if(button.value=='Search Site or Enter Style Number')
	{
		button.value = '';
	}
}

function resetSearch(button)
{
	if(button.value == '')
	{
		button.value='Search Site or Enter Style Number';
	}
}

function clearSubscription(button)
{
	if(button.value=='Enter your E-mail Address Here')
	{
		button.value = '';
	}
}

function resetSubscription(button)
{
	if(button.value == '')
	{
		button.value='Enter your E-mail Address Here';
	}
}

function showcurrencydiv()
{

	var cdd = $('currency-dd');	
	var state = cdd.style.display;

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

	
}

function submitform()
{
	document.forms[0].submit();
}

function fetchcolours()
{
	var baseurl  = $('baseurl').value;
	var sizebox = $('size');
	
	var productid = $('productid').value;
	
	var sizeid = sizebox.options[sizebox.selectedIndex].value;
	
	if(parseInt(sizeid)>0)
	{
		
		var myAjax = new Ajax.Request(baseurl+'/products/fetchcoloursajax/',
		{ method: 'post',encoding: 'UTF-8', parameters:{sizeid:sizeid,productid:productid},
			onComplete: putcolours
		}
		);
		
		
	}
	
}

function putcolours(transport)
{
	var colourbox = $('colour');
	clearSelectBox(colourbox);
	//alert(transport.responseText);
	var results = eval("(" + transport.responseText + ")");
	
	for(var i=0;i<results[0].length;i++)
	{
		var opt = new Option(results[0][i]['name'], results[0][i]['colourid']);
		colourbox.options[colourbox.options.length] = opt;
	}
}

function fetchcoloursb(num)
{
	var baseurl  = $('baseurl').value;
	var sizebox = $('size_'+num);
	
	var productid = $('productid_'+num).value;
	
	var sizeid = sizebox.options[sizebox.selectedIndex].value;
	
	if(parseInt(sizeid)>0)
	{
		
		var myAjax = new Ajax.Request(baseurl+'/products/fetchcoloursajax/',
		{ method: 'post',encoding: 'UTF-8', parameters:{sizeid:sizeid,productid:productid,num:num},
			onComplete: putcoloursb
		}
		);
		
		
	}
	
}

function putcoloursb(transport)
{
	
	//alert(transport.responseText);
	var results = eval("(" + transport.responseText + ")");
	
	var num = results[1];
	
	var colourbox = $('colour_'+num);
	clearSelectBox(colourbox);
	
	
	for(var i=0;i<results[0].length;i++)
	{
		var opt = new Option(results[0][i]['name'], results[0][i]['colourid']);
		colourbox.options[colourbox.options.length] = opt;
	}
}

function clearSelectBox(box)
{
	var option_number = box.options.length;
	for(var i = 0;i<option_number;i++)
	{
		box.remove(0);
	}
}

function changeyear(element)
{
	var baseurl  = $('baseurl').value;
	var year = element.options[element.selectedIndex].value;
	document.location.href= baseurl+'/press/index/year/'+year
}

function checkAll(checkbox,form)
{
	var checkstate = checkbox.checked;
	var elems = form.elements;  
	var elemCount = elems.length;  
	
	for(var i=0;i<elemCount;i++)
	{    
		if(elems[i].type=="checkbox")
		{      
			elems[i].checked=checkstate;    
		}  
	}
}

function checkVoucher()
{
	var baseurl  = $('baseurl').value;
	var voucher_code = $('voucher_code').value;
	
	
	
	var myAjax = new Ajax.Request(baseurl+'/checkout/checkvouchercodeajax/',
	{ method: 'post',encoding: 'UTF-8', parameters:{voucher_code:voucher_code},
		onComplete: putVoucher
	}
	);
	
	
}

function putVoucher(transport)
{
	//alert(transport.responseText);
	var results = eval("(" + transport.responseText + ")");
	
	if(results['result']==0)
	{
		$('voucher_result').innerHTML = "Invalid Code!";
		$('voucher_sum_t').innerHTML  = "0.00";
		$('voucher_sum').value  = "0.00";
		$('voucher_id').value = 0;
		$('voucher_type').value = 0;
		$('voucher_value').value = 0;
	}
	else if(results['result']==1)
	{
		$('voucher_result').innerHTML = "Expired Voucher Code!";
		$('voucher_sum_t').innerHTML  = "0.00";
		$('voucher_sum').value  = "0.00";
		$('voucher_id').value = 0;
		$('voucher_type').value = 0;
		$('voucher_value').value = 0;
	}
	else
	{
		$('voucher_result').innerHTML = "Valid Code!";
		$('voucher_id').value = results['voucher_id'];
		$('voucher_type').value = results['type'];
		$('voucher_value').value = results['value'];
	}
	calculateVoucher();	
}

function calculateVoucher()
{
	
	var voucher_type = $('voucher_type').value;
	
	var voucher_value = $('voucher_value').value;
	
	var subtotal = $('sub_total').value;
	
	var subtotalnosale = $('sub_total_nosale').value;
	
	var er = $('exchange_rate').value;
	
	var vv = 0;
	
	if(voucher_type==2)
	{
		vv = voucher_value*parseFloat(subtotalnosale)/100;
		
	}
	else
	{
		vv = voucher_value;
	}
	
	vv = parseFloat(vv).toFixed(2);

	$('voucher_sum').value  = vv;
	
	var vv_currency = er*vv
	
	$('voucher_sum_t').innerHTML  = vv_currency.toFixed(2);
	
	calculateTotal();
}

function calculateTotal()
{
	var voucher = $('voucher_sum').value;
	var subtotal = $('sub_total').value;
	var shipping = $('shipping').value;
		
	var productstotal = parseFloat(subtotal) - parseFloat(voucher);
	
	if(productstotal<0)
	{
		productstotal = 0;
	}
	
	var total = productstotal + parseFloat(shipping);
	
	var er = $('exchange_rate').value;
	
	$('total').value = total.toFixed(2);
	
	var total_currency = total*er;
	
	$('total_t').innerHTML  = total_currency.toFixed(2);
}

function changeShipping(el)
{
	var value = el.options[el.selectedIndex].value;
	
	var er = $('exchange_rate').value;
	
	var sp = '0.00';
	
	if(value!='0')
	{
		var sv = value.split(";");
		sp = sv[1];
	}
	sp = parseFloat(sp);
	$('shipping').value = sp.toFixed(2);
	var shipping_currency = sp*er;
	$('shipping_t').innerHTML = shipping_currency.toFixed(2);
	calculateTotal();
	
	
}

function copyDetails()
{
	
	$('shipping_address').value = $('customer_address').value;
	$('shipping_town').value = $('customer_town').value;
	
	$('shipping_county').value = $('customer_county').value;
	
	$('shipping_post_code').value = $('customer_post_code').value;
	
	
	var countrysb = $('shipping_country_id');
	for(var i=0;i<countrysb.options.length;i++)
	{
		if(countrysb.options[i].value == $('customer_country_id').value)
		{
			countrysb.selectedIndex = i;
		}
	}

}
