

function showColorChart(chart , variant){
	var variant_str = ( variant == undefined) ? '' :   '/variant/' + variant  ;
	var url = baseHref + 'content/page/resources_colorcharts/chart/' + chart + variant_str    ;
	window.open ( url  , 'colorchart_' + chart , 'width=770,height=550') ;

}

function setChartColor (color , variant){
	try {
		var p = opener.document.getElementById('option_colorchart_' + variant);
		for (var i = 0 ; i < p.options.length ; i++ ) {
			if (p.options[i].value == color ) {
				//alert (color + ' ' + p.options[i].value ) ;
				p.options[i].selected = true ;
				this.close();
				return;
			}
		}
	}catch (e){
		this.close();
	}
}



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 = '&nbsp;' + URLDecode(img_caption) ;
	document.getElementById ('image_placeholder_title').innerHTML = '&nbsp;' + URLDecode( img_title) ;
	
	setTimeout( 'setPImgeSrc(\''+img+'\')' , 1 );
	var container = document.getElementById ('image_placeholder_container') ;
	// handle tiny tiny images
	if (img_w < 200) img_w = 200 ;
	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
	container.style.left = w + 'px' ;
	container.style.top = h + 'px' ;
	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 ('image_placeholder').removeChild(document.getElementById ('image_placeholder').childNodes[0]) ;
	}catch (e){};
}
function addToCart (optid , product) {
	// see if there are any test for this one
	var f = document.getElementById ('addtocartform' + product) ;
	var tests_str = '' ; 
	if ( f['optiontests_'  + optid] ) {
		tests_str = f['optiontests_'  + optid].value ;
	}
	var errors  = new Array () ;
	if (tests_str ){
	 	var tests = tests_str.split (',') ; 
	 	for (var i = 0 ; i < tests.length ; i++) {
	 		var test_type, test_error, test_value, test_value2 ;
			test_type = f['optiontest_' + tests[i] + '_type'].value ;
			test_error = f['optiontest_' + tests[i] + '_error'].value ;
			test_value1 = f['optiontest_' + tests[i] + '_value1'].value ;
			if (f['optiontest_' + tests[i] + '_value2']) {
				test_value2 = f['optiontest_' + tests[i] + '_value2'].value ;
			}
			switch (test_type) {
				case "exactly":
					if (test_value1 > 1 ) { // checkboxes
						if ( countCheckedBoxes(f['option_' + tests[i] + '[]']) != test_value1 ){
							errors[errors.length] = test_error  ;
						}					
					}else{ // select
						if (useCoolSelects) {
							if (f['option_' + tests[i]].value == '') {
								errors[errors.length] = test_error  ;
							}
						}else{
							if (f['option_' + tests[i]].options[0].selected == true) {
								errors[errors.length] = test_error  ;
							}
						}
					}
					break;	 		
				case "range":
						if ( countCheckedBoxes(f['option_' + tests[i] + '[]']) < test_value1 || countCheckedBoxes(f['option_' + tests[i] + '[]']) > test_value2  ){
							errors[errors.length] = test_error  ;
						}
					break;	 		
				case "atmost":
						if ( countCheckedBoxes(f['option_' + tests[i] + '[]']) > test_value1  ){
							errors[errors.length] = test_error  ;
						}
					break;	 		
				case "atleast":
						if ( countCheckedBoxes(f['option_' + tests[i] + '[]']) < test_value1  ){
							errors[errors.length] = test_error  ;
						}
					break;	 		
				case "comment":
					if (test_error.length > 0 ) { // do test
						if (!trim(f['option_' + tests[i]].value)){
							errors[errors.length] = test_error  ;
						}
					}
			} // end of switch type
	 	}// end of for
	}// end of IF tests_str
	if ( f['option_colorchart_'  + optid] ) {
		// there is color chart to be tested
		if (f['option_colorchart_'  + optid].options[0].selected == true ) { // no color selected
			errors[errors.length] = 'Please select a color'  ;
		}
	}

	if (errors.length > 0 ) {
		//   show some errors
		alert ( errors.join ("\n") ) ;
	}else{ // all good
		f.cartadd.value = optid ;
		f.submit ();
	} 
}


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;
}




