

function addtocart(item,variant) {
	// get mat option
	var matopt = document.getElementById ('matoption_' + variant).value;
	if (matopt) {
		window.location = baseHref + 'content/page/catalog/id/' + item +'/cartadd/' + item +',' + variant + ','  + matopt;		
	}else{
		alert ('Please select matt colour for this reproduction') ;
		document.getElementById ('matoption_' + variant).focus();;
	}
}


var captcha_sndTimeout ;
function captcha_playIt(soundUrl) {
	clearTimeout(captcha_sndTimeout)
	var soundFrameObj = document.getElementById("soundframe");
	soundFrameObj.src = "about:blank";
	soundFrameObj.src = soundUrl;
	captcha_sndTimeout  = setTimeout("captcha_endPlay()", 20000);
}
function captcha_endPlay() {
	clearTimeout(captcha_sndTimeout)
	document.getElementById("soundframe").src = "about:blank";
}
function captcha_refresh(){
	captcha_endPlay();
	document.getElementById('captchastr').value = '';
	document.getElementById('captcha_img').src =baseHref +  'captcha.php?t=img&rf=' + new Date().getTime()  ;
}



function preloadImages(arr){
	var tmp_img ;
	for (var i = 0 ; i < arr.length ; i ++ ) {
		tmp_img = new Image();
		tmp_img.src = baseURL + arr[i] ;	
	}
}


function showProdImage(img , img_w, img_h , img_title, img_caption){
	hideProdImage();
	if (!img || !img_w || !img_h ) return ;
	var tmp_img = new Image(img_w, img_h);
	tmp_img.src = img ;
	document.getElementById ('image_placeholder').appendChild(tmp_img) ;
	document.getElementById ('image_placeholder_caption').innerHTML = '' + URLDecode(img_caption) + '&nbsp;' ;
	document.getElementById ('image_placeholder_title').innerHTML = '' + URLDecode( img_title) + '&nbsp;' ;
	
	setTimeout( 'setPImgeSrc(\''+img+'\')' , 1 );
	var container = document.getElementById ('image_placeholder_container') ;
	// handle tiny tiny images
	if (img_w < 200) img_w = 200 ;
	img_w = parseInt(img_w) +  40 
	img_h = parseInt(img_h) +  40 
	container.style.width=img_w + 'px' ; 
	// where to place this
	var w = 5; 
	var h = 5 ;
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	w += scrOfX;
	h += scrOfY;
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	if ( myWidth >  img_w) {
		w += parseInt( (myWidth -img_w) / 2) 
	}
	if ( myHeight >  img_h) {
		h += parseInt( (myHeight - img_h   ) / 2) ;
	}
	// show it
	h = h - 30 ;
	if (h < 0 ) h = 0 
	container.style.left = w + 'px' ;
	container.style.top = h + 'px' ;
	// make background so that photo can stand out
//	var bkg_div = document.createElement('DIV') ;
//	bkg_div.id = 'pic_bkg_div' ;
//	bkg_div.style.height = findHeight(document.getElementById('wrapper')) + 'px'
//	bkg_div.style.width = findWidth(document.getElementById('wrapper')) + 'px'
//	document.getElementById('wrapper').appendChild(bkg_div);
	// show picture
	container.style.display='block' ; 
}	
function setPImgeSrc(img){
	// stupid explder cannot fully show image right away
	document.getElementById ('image_placeholder').childNodes[0].src = img ;
}

function hideProdImage(){
	try {
		document.getElementById('image_placeholder_container').style.display='none';
		document.getElementById ('image_placeholder').removeChild(document.getElementById ('image_placeholder').childNodes[0]) ;
		document.getElementById('wrapper').removeChild(document.getElementById('pic_bkg_div'));
		document.getElementById ('image_placeholder').removeChild(document.getElementById ('image_placeholder').childNodes[0]) ;
	}catch (e){
		//console.log(e)
	};
}

function testEnter(event) {
	if (event.keyCode) {
		if (event.keyCode ==13 ) {
			return true ;
		} 
	}
}


function countCheckedBoxes (elem){
	var boxes_checked = 0 ;
	for (var i = 0 ; i < elem.length ; i ++) {
		if (elem[i].checked == true ) {
			boxes_checked ++ ;
		}
	}					
	return boxes_checked ; 
}



function URLDecode(encoded ){
	// Replace + with ' '
	// Replace %xx with equivalent character
	// Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = "0123456789ABCDEFabcdef";
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			}
		} else {
			plaintext += ch;
			i++;
		}
	} // while
	return plaintext;
}




