function printDiv(elem){
	$.get ('content/page/print/',function(data){
		var frame = document.getElementById('printframe') ;
		if (!frame)  {
			var frame = document.createElement('IFRAME');
			frame.name = 'printframe';
			frame.id = 'printframe';
			frame.style.width = '1px';
			frame.style.height = '1px';
			//frame.style.visibility = 'none' ;
			frame.style.top = '-999em';
			frame.style.position = 'absolute';
			//frame.style.display = 'none' ;
			frame = document.body.appendChild(frame)
		}
		if (frame) {
			var doc = frame.contentDocument;
			if (doc == undefined || frame == null)  doc = frame.contentWindow.document;
			doc.open ();
			doc.write(data.replace(/\%CONTENT\%/ , document.getElementById(elem).innerHTML)) ;	
			doc.close();
			if ($.browser.msie) {	// ie
				// we have to waint till frame is registered with IE
				setTimeout ("printDivComplete()",200);
			}else{//iothers
				printDivComplete()
			}
		}
	})
}
function printDivComplete(){
	window.frames['printframe'].focus();
	window.frames['printframe'].print();
}



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 ;
	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' ;
	// 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'
	bkg_div.style.width = '100%' ;
	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;
}




