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,navlinks){
	
	popupWas = document.getElementById('image_placeholder_container').style.display == 'block' ? true : false ;
	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) ;
	}
	// snap w and h so they do not skip around for tiny diff in image size
	//w = Math.round(w / 10) * 10 
	//h = Math.round(h / 10) * 10 
	
	// show it
	//container.style.left = w + 'px' ;
	//container.style.top = h + 'px' ;
	
	if ( parseInt(document.getElementById('image_placeholder_container').style.top) && popupWas){
		// if popup is already shown , no not move it
	}else{
		container.style.left = w + 'px' ;
		container.style.top = h + 'px' ;
	}
	
	
	// attempt to remove prev and next links
	try {
		document.getElementById('image_placeholder_container').removeChild(document.getElementById('thum_disp_prevlink'));
	}catch(e){
	}
	try {
		document.getElementById('image_placeholder_container').removeChild(document.getElementById('thum_disp_nextlink'));
	}catch(e){
	}
	
	if (typeof(navlinks) == 'object') {
		// ie sometimes needs bit of help to initally download image
		var img1 = new Image(42,72)
		var img2 = new Image(42,72)
		img1.src=baseHref + 'images/play_right.jpg'
		img2.src=baseHref + 'images/play_left.jpg'
		if (navlinks[0]) {
			// there is previous
			var prevlink = document.createElement ('A') ;
			prevlink.innerHTML = ' '
			prevlink.title = 'Previous'
			prevlink.id = 'thum_disp_prevlink' ;
			prevlink.style.left = ( parseInt(container.style.width) - 100 ) + 'px';
			//prevlink.style.top = ( parseInt(img_h) - 0 ) + 'px';
			prevlink.onclick = function (){
				document.getElementById ('img_' + navlinks[0]).onclick();
			}
			document.getElementById('image_placeholder_container').appendChild(prevlink)
		}	
		if (navlinks[1]) {
			// there is next
			var nextlink = document.createElement ('A') ;
			nextlink.innerHTML = ' '
			nextlink.title = 'Next'
			nextlink.style.left = ( parseInt(container.style.width) - 54 ) + 'px';
			//nextlink.style.top = ( parseInt(img_h) - 0 ) + 'px';
			nextlink.id = 'thum_disp_nextlink' ;
			nextlink.onclick = function (){
				document.getElementById ('img_' + navlinks[1]).onclick();
			}
			document.getElementById('image_placeholder_container').appendChild(nextlink)
			
		}
	}
	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 (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;
}




