


function minicart_show(){
	try {
		if (trim(document.getElementById('minicart_rows').innerHTML.toString())) {
			document.getElementById('minicart').style.display='block' ;
		}
	}catch (e){
	}
}
function minicart_hide (){
	document.getElementById('minicart').style.display='none' ;
}




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 prefilCartForm(){
	//do we have something to use 
	var addy = document.getElementById('savedshinfo').options[document.getElementById('savedshinfo').selectedIndex].value ;
	
	if (addy) {
		var ap = addy.split('____') ;
		var f = document.getElementById('chform') ;
		f['name'].value = ap[0] ;
		f['city'].value = ap[2] ;
		f['phone'].value = ap[6] ;
		f['fax'].value = ap[7] ;
		f['zip'].value = ap[4] ;
		var aparts = ap[1].split ('|')
		f['address1'].value = aparts[0] ;
		if (aparts[1] != undefined) f['address2'].value = aparts[1] ;
		f['country'].value = ap[5] ;
		f['state'].value = ap[3] ;
	}
}



function populateRegFormStates(elem){
	var states = elem[elem.selectedIndex].getAttribute('states').split('|');
	var states_elem  = document.getElementById ( ((elem.name.indexOf ('ship_') > -1) ? 'ship_' : ''   ) +  'state' ) ; 
	
	var ship_states_elem = '' ;
	if (document.getElementById('same_as_billto') ){
		if (elem.name == 'country' && document.getElementById('same_as_billto').checked == true ) {
			var ship_states_elem  = document.getElementById ( 'ship_state' ) ; 
		}
	}	
	if (states.length > 1) {
		if (states_elem.type != 'select-one' ) {
			var select_field = document.createElement('SELECT') ;
			select_field.className = 'finput'
			select_field.name =  ((elem.name.indexOf ('ship_') > -1) ? 'ship_' : ''   ) +  'state'  ; 
			select_field.id =  ((elem.name.indexOf ('ship_') > -1) ? 'ship_' : ''   ) +  'state'  ; 
			select_field.options[0] = new Option (' < Select One > ','') ;
			states_elem.parentNode.replaceChild (select_field , states_elem) ;			
			states_elem = select_field ;
		}
		if (ship_states_elem && ship_states_elem.type != 'select-one' ) {
			var select_field = document.createElement('SELECT') ;
			select_field.className = 'finput'
			select_field.name =  'ship_state'  ; 
			select_field.id =  'ship_state'  ; 
			select_field.options[0] = new Option (' < Select One > ','') ;
			ship_states_elem.parentNode.replaceChild (select_field , ship_states_elem) ;			
			ship_states_elem = select_field ;
		}
		var selected_elem = '' ;
		if (states_elem.selectedIndex) selected_elem = states_elem.selectedIndex ; 
		while (states_elem.length > 1 ) {
			if (ship_states_elem) {
				ship_states_elem.options[1] = null ;
			}
			states_elem.options[1] = null ;
		}
		for (var i = 0 ; i < states.length; i +=2 ){
			if (ship_states_elem) {
				ship_states_elem.options[i/2+1] = new Option (states[i+1] ,states[i] ) ;
			}
			states_elem.options[i/2+1] = new Option (states[i+1] ,states[i] ) ;
		}
		if (states_elem.options[selected_elem]) {
			states_elem.options[selected_elem].selected = true ;
		}
		if (document.getElementById('same_as_billto') ){
			da_connect(document.getElementById('same_as_billto'),'dealerappform', new Array ('state','ship_state')) ;
		}
	}else{
		if (states_elem.type == 'select-one' ) {
			var text_field = document.createElement('INPUT') ;
			text_field.type = 'text' ;
			text_field.className = 'finput'
			text_field.name =  ((elem.name.indexOf ('ship_') > -1) ? 'ship_' : ''   ) +  'state'  ; 
			text_field.id =  ((elem.name.indexOf ('ship_') > -1) ? 'ship_' : ''   ) +  'state'  ; 
			states_elem.parentNode.replaceChild (text_field , states_elem) ;			
		}
		if (ship_states_elem && ship_states_elem.type == 'select-one' ) {
			var text_field = document.createElement('INPUT') ;
			text_field.type = 'text' ;
			text_field.className = 'finput'
			text_field.name =  'ship_state'  ; 
			text_field.id =  'ship_state'  ; 
			ship_states_elem.parentNode.replaceChild (text_field , ship_states_elem) ;			
			da_connect(document.getElementById('same_as_billto'),'dealerappform', new Array ('state','ship_state')) ;
		}
	}
}

function da_connect(button,form,fieldList){
	var f = document.forms[form] ;
	if (button.checked ) {
		if (  f['state'].type !=  f['ship_state'].type ) {
			populateRegFormStates(document.getElementById('country'));
			var fieldList_str =  ( "'" + fieldList.toString().split(',').join ("','") + "'" ) ;
			setTimeout ("da_connect(document.getElementById('"+button.id +"'),'"+form+"', new Array("+ fieldList_str +"))" , 200) ;
			return  false ;
		}
		if (f['state'].type == 'select-one' ) {
			if (  f['state'].options.length !=  f['ship_state'].options.length ) {
				populateRegFormStates(document.getElementById('country'));
				var fieldList_str =  ( "'" + fieldList.toString().split(',').join ("','") + "'" ) ;
				setTimeout ("da_connect(document.getElementById('"+button.id +"'),'"+form+"', new Array("+ fieldList_str +"))" , 200) ;
				return  false ;
			}
		}
	}
	for (var i = 0 ; i < fieldList.length ; i +=2 ) {
		if (button.checked ) {
			if (f[fieldList[i]].type == 'select-one' ) {
				f[fieldList[i+1]].options[f[fieldList[i]].selectedIndex].selected = true ;
				f[fieldList[i+1]].onchange = function (){
					for (var j = 0 ; j < fieldList.length ; j +=2) {
						if (fieldList[j+1] == this.name) {
							this.selectedIndex  = f[fieldList[j]].selectedIndex;
							break;
						}
					}
				}
				f[fieldList[i]].onchange = function (){
					for (var j = 0 ; j < fieldList.length ; j +=2) {
						if (fieldList[j] == this.name) {
							f[fieldList[j+1]].selectedIndex = this.selectedIndex ;
							break;
						}
					}
					if (this.name == 'country' )  populateRegFormStates(this) ;
				}
			}else{ 
				f[fieldList[i+1]].value = f[fieldList[i]].value ;
				f[fieldList[i+1]].readOnly = true ;
				f[fieldList[i]].onkeyup = function (){
					for (var j = 0 ; j < fieldList.length ; j +=2) {
						if (fieldList[j] == this.name) {
							f[fieldList[j+1]].value = this.value ;
							break;
						}
					}
				}
			}
		}else{
			if (f[fieldList[i]].type == 'select-one' ) {
				f[fieldList[i]].onchange = function (){
					if (this.name == 'country' )  populateRegFormStates(this) ;
				};
				f[fieldList[i+1]].onchange= function (){
					if (this.name == 'ship_country' )  populateRegFormStates(this) ;
				};
			}else{
				f[fieldList[i+1]].readOnly = false ;
				f[fieldList[i]].onkeyup = function (){};
			}
		}	
	}
}

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




