// JavaScript Document
var currentURL;
function getURLParam(name,url)
{
name = name.replace(/[[]/,"\[").replace(/[]]/,"\]");
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec(url);
if( results == null )
return "";
else
return results[1];
}

function setSubnavSelected(url){
	var currentEl = document.getElementById('subnav_' + getURLParam('CatalogueID',url));
	if(currentEl) currentEl.className = "selected";
	currentURL = url;
}

// checks if attribute label exists 
function checkAttributes(name){
	var att = document.getElementById('product-attributes');
	var searchindex = att.innerHTML.toString().toLowerCase().indexOf('>' + name.toLowerCase() + '<');
	if(searchindex > -1){
		return true;
	}else{
		return false;
		
	}
}

function showSizeChartLink(){
	if(!checkAttributes('size')){
		var obj = document.getElementById('size-chart');
		if(obj) obj.style.display = "none";
	}
}

function showGroupingTitle(){
	var grouping = document.getElementById('product-grouping');
	var searchindex = grouping.innerHTML.toString().toLowerCase().indexOf('<select');
	if(searchindex > -1){
		var obj = document.getElementById('grouping-title');
		if(obj) obj.innerHTML = "Material";
	}
}


var popwin;
function openWindow(theURL,width,height,winfeatures,winname) {   
	var x = 10; 
	var y = 10;
	if(winfeatures==undefined){
		winfeatures = '';
	}
	if(winname==undefined){
		winname = '';
	}
	if(width!=undefined){
		x=(screen)?(screen.availWidth - width)/2:(640 - width)/2;
		winfeatures += ',width='+width;
	}
	if(height!=undefined){
		y=(screen)?(screen.availHeight - height)/2:(480 - height)/2;
		winfeatures += ',height='+height;
	}

	popwin = window.open(theURL,winname,'screenX='+x+',screenY='+y+',top='+y+',left='+x+',' + winfeatures);
	if(popwin.name==winname) popwin.focus();
}

// override draw product function so that we can trigger a custom event when new product is selected
function DrawProduct(b,j,g,d,a){var h=document.getElementById("Grouping_"+j);var c;var f=true;var i=h;while(f){i=i.parentNode;if(i.id){if(i.id.indexOf("catProdTd_")!=-1){c=i.id.substring(10);f=false}}}var e=CMS.CatalogueRetrieve.ServerSideDrawProduct(b,g,a,d);i.innerHTML=e.value;ProcessJS(i);if(e.value.indexOf("lightbox")!=-1){initLightbox()}
 customProductHandler(b,j,g,e);
}

function customProductHandler(catalogid,productid,value,html){
	if(html.value.indexOf("Material: 9ct Gold")>-1){
		alert("Please be aware due to the bespoke nature of a 9ct Gold piece the delivery timeframe for this product is two-three weeks.");
	}
}