get_item = function(id, get){
	switch(id){
				case 'test_kit_bundle':
					var name = 'Test Kit Bundle';
					var item = '32757';
					var loc = 1;
				break;
				case 'test_kit_bundle_strap':
					var name = 'Test Kit Bundle w/HR Strap';
					var item = '69010';
					var loc = 2;
				break;
				case 'test_kit_bundle_monitor':
					var name = 'Test Kit Bundle w/HR monitor & Strap';
					var item = '78334';
					var loc = 3;
				break;
				case 'lipid':
					var name = 'Lipid Panel Test Strips';
					var item = '59341';
					var loc = 4;
				break;
				case 'gtt':
					var name = 'GTT Test Strips';
					var item = '59339';
					var loc = 5;
				break;
				case 'lipid_capillary':
					var name = 'Lipid Capillary Blood Collectors 40 UL';
					var item = '59344';
					var loc = 6;
				break;
				case 'gtt_capillary':
					var name = 'GTT Capillary Blood Collectors 15 UL';
					var item = '59343';
					var loc = 7;
				break;
				case 'x_standard':
					var name = 'Xiser Standard';
					var item = 'X-1';
					var loc = 8;
				break;
				case 'x_commercial':
					var name = 'Xiser Commercial';
					var item = 'X-2';
					var loc = 9;
				break;
				case 'exercise_credit':
					var name = 'Exercise Test Credit';
					var item = '70010';
					var loc = 10;
				break;
				case 'rmr_credit':
					var name = 'RMR Test Credit';
					var item = '70011';
					var loc = 11;
				break;
				case 'multi_chem':
					var name = 'Multi-Chemistry Control Solution';
					var item = '59342';
					var loc = 12;
				break;
				case 'gloves_large':
					var name = 'Disposable Gloves - Large';
					var item = '75333';
					var loc = 13;
				break;
				case 'gloves_medium':
					var name = 'Disposable Gloves - Medium';
					var item = '75334';
					var loc = 14;
				break;
				case 'gloves_small':
					var name = 'Disposible Gloves - Small';
					var item = '75335';
					var loc = 15;
				break;
				case 'gauze':
					var name = 'Gauze';
					var item = '77900';
					var loc = 16;
				break;
				case 'auto':
					var name = 'Auto Lancets';
					var item = '20004';
					var loc = 17;
				break;
			}
			if(get == 'item'){
				return item;		
			} else if(get == 'name') {
				return name;
			} else if(get =='loc'){
				return loc;
			} 
}
$(function(){
	$('#purchase_order').change(function(){
		if($('#purchase_order').val() == 'paypal'){
		$('#ulDiscount').slideDown('slow');
			if($('#ulDiscount').css('display') == 'none'){
				$('#ulDiscount').slideDown('slow');
			}
		display_totals();	
		} else {
			if($('#ulDiscount').css('display') == 'block'){
				$('#ulDiscount').hide('fast',function(){
				display_totals();
				});}}});
	$('#checkout input').keyup(function(){
		var thisid = this.id;
		var thisval = this.value;

		if(thisval >= 200){
			alert('Order QTY amount too High');
			return false;
			}//
			
	
		var testcredit = false;
		if($('#test_kit_bundle').val() >= 1 || $('#test_kit_bundle_strap').val() >= 1 || $('#test_kit_bundle_monitor').val() >= 1 || $('#exercise_credit').val() >= 1 || $('#rmr_credit').val() >= 1){
			testcredit = true;
		}//end
		
		if(testcredit == true){
			$('#authID').css('display','block');
		} else {
			$('#authID').css('display','none');
		}//end if
		if(thisval >= 1 && !isNaN(thisval)){
			
			if($('#'+thisid+'_price').html()){
			
				var price = $('#'+thisid+'_price').html();
				price = price.replace("$",'');
				var total = parseFloat(eval(thisval*price)).toFixed(2); 
				// renamed float to float1 due to errors in Safari... reserved word, methinks
				// BVH 10152007
				var float1 = total.toString().indexOf(".");
			
				if(float1 == -1){
					total = total+'.00';
				}
			}
			
			var msg = "For each ordered Test Kit, please check whether you want to forego your RMR credits and get 1 1/2 the amount of exercise test credits.<br/><br/>";
			
			switch(thisid){
				case 'test_kit_bundle':
					if($('#'+thisid+'_size').css('display') == 'none'){
					    $('#'+thisid+'_credit').show();	
						$('#'+thisid+'_size').slideDown('slow');	
	
					}
					var name = get_item(thisid,'name');
					var item = get_item(thisid,'item');
					var loc  = get_item(thisid,'loc');
					var html = msg;
					for(a=0;a<=eval(thisval-1); a++){
						html = html+'<input name="test_kit_bundle_credit_send['+a+']" value="18" id="test_kit_bundle_credit_send" type="checkbox" />	Send me 18 Exercise Test Credits Instead and NO RMR Test Credits.<br />';
					}//end for
					
					$('#test_kit_bundle_credit').html(html);
				break;
				case 'test_kit_bundle_strap':
					if($('#'+thisid+'_size').css('display') == 'none'){
				    	$('#'+thisid+'_credit').show();	
						$('#'+thisid+'_size').slideDown('slow');	
					}
					var name = get_item(thisid,'name');
					var item = get_item(thisid,'item');
					var loc  = get_item(thisid,'loc');
					var html = msg;
					for(a=0;a<=eval(thisval-1); a++){
						html = html+'<input name="test_kit_bundle_strap_credit_send['+a+']" value="18" id="test_kit_bundle_strap_credit_send" type="checkbox" />	Send me 9 Exercise Test Credits Instead and NO RMR Test Credits.<br />';
					}//end for
					
					$('#test_kit_bundle_strap_credit').html(html);
				break;
				case 'test_kit_bundle_monitor':
					if($('#'+thisid+'_size').css('display') == 'none'){
					    $('#'+thisid+'_credit').show();	
						$('#'+thisid+'_size').slideDown('slow');	
					}
					var name = get_item(thisid,'name');
					var item = get_item(thisid,'item');
					var loc  = get_item(thisid,'loc');
					var html = msg;
					for(a=0;a<=eval(thisval-1); a++){
						html = html+'<input name="test_kit_bundle_monitor_credit_send['+a+']" value="18" id="test_kit_bundle_monitor_credit_send" type="checkbox" />	Send me 9 Exercise Test Credits Instead and NO RMR Test Credits.<br />';
					}//end for
					
					$('#test_kit_bundle_monitor_credit').html(html);
				break;
				default:
					var name = get_item(thisid,'name');
					var item = get_item(thisid,'item');
					var loc  = get_item(thisid,'loc');
				break;
			}
		} else {
		
			switch(thisid){
				case 'test_kit_bundle':
					if($('#'+thisid+'_size').css('display') == 'block'){
						$('#'+thisid+'_size input').val('0');
						$('#'+thisid+'_credit').hide();	
						$('#'+thisid+'_size').slideUp('slow');	
						$('#test_kit_bundle_credit').html('');
					}
					var loc = 1;
				break;
				case 'test_kit_bundle_strap':
					if($('#'+thisid+'_size').css('display') == 'block'){
						$('#'+thisid+'_size input').val('0');
						$('#'+thisid+'_size').slideUp('slow');	
						$('#'+thisid+'_credit').hide();	
						$('#test_kit_bundle_strap_credit').html('');
					}
					var loc = 2;
				break;
				case 'test_kit_bundle_monitor':
					if($('#'+thisid+'_size').css('display') == 'block'){
						$('#'+thisid+'_size input').val('0');
						$('#'+thisid+'_size').slideUp('slow');	
						$('#'+thisid+'_credit').hide();	
						//$('#'+thisid+'_credit_send').removeAttr('checked');
						$('#test_kit_bundle_monitor_credit').html('');
					}
					var loc = 3;
				break;
				default:
					var loc = get_item(thisid,'loc');
				break;
			}
		}
		
		if(name && loc){
			var input = ' <input id="item_total" name="item_total" type="hidden" value="'+total+'" />';
			var html = '<div id="'+loc+'"><ul class="item"><li class="name">'+name+'</li><li class="number">'+item+'</li><li class="order">'+thisval+'</li><li class="price">$'+total+'</li></ul>'+input+'<br /></div>';
			$('#cart #'+loc).remove();
			$('#cart').html($('#cart').html()+html);

			display_totals();
				delete inputHide;
				delete name;
				delete loc;
				delete item;
				delete html;
				delete thisid;
				delete thisval;
				delete float1;
				delete price;
		}
		if(!name && loc)
		{	$('#cart #'+loc).remove();
			display_totals();
		}
		if($('#cart').html() == '')
		{	$('#shipping').html('<strong>$0.00</strong>');
			$('#total').html('<strong>$0.00</strong>');
			//$('#tax').html('<strong>$0.00</strong>');
		}});});

add_hidden = function(){
	var gather = '';
	var id;
	var price;
	var val;
	var isNzero;
	var item;
	var name;
	var num = 1;
	var extradata;
	var total;
	$('#checkout input').each(function(){
		 id = this.id;
	     val = this.value;
		 price = 0;
		 isNzero = (val === '0');
	
		if( val != '0' ){
			 price = parseFloat($('#'+id+'_price').text().replace('$','')).toFixed(2);
			 
		}

		if(price != '0' ){
			if(price != 'NaN'){
				item = get_item(id,'item');
				name = get_item(id,'name');
				total = parseFloat(price).toFixed(2);
				 var	inputHide = ' <input id="item_name_'+num+'" name="item_name_'+num+'" type="hidden" value="'+name+'" />';
					inputHide = inputHide+' <input id="item_number_'+num+'" name="item_number_'+num+'" type="hidden" value="'+item+'" />';
					inputHide = inputHide+' <input id="amount_'+num+'" name="amount_'+num+'" type="hidden" value="'+total+'" />';
					inputHide = inputHide+' <input id="quantity_'+num+'" name="quantity_'+num+'" type="hidden" value="'+val+'" />';
					
					if($('#'+id+'_size').css('display') == 'block'){
						var extranum = 0;
						var twocnt   = 1;
						$('#'+id+'_size input').each(function(){
							
							if(twocnt == 2){
								inputHide = inputHide+'<input name="on'+extranum+'_'+num+'" value="'+extradata+this.id+'" type="hidden" />';
								inputHide = inputHide+'<input name="os'+extranum+'_'+num+'" value="'+this.value+'" type="hidden" />';
								extranum++;
								twocnt = 1;
							} else {
								extradata = this.id+': '+this.value+', ';
								twocnt++;
							}
						});
					}
				
				inputHide = inputHide+'<input name="num_of_prod" value="'+num+'" type="hidden" />';
				
				gather = gather+inputHide+'\n';
				num++;
			}
		}
	
		delete id;
		delete val;
		delete price;
	});
	
	var shipping =  parseFloat($('#shipping').text().replace('$','')).toFixed(2);
	
	if( $('#purchase_order').val() == 'paypal'){
		 var dis = $('#discount').text().replace('$','');
		 dis = dis.replace('(','');dis = dis.replace(')','');
		 dis = parseFloat(dis).toFixed(2);
		shipping = eval(parseFloat(shipping)-parseFloat(dis));
		
		
	}
	inputship = '<input name="shipping_1" value="'+shipping+'" type="hidden">';
	return gather+inputship;
}
display_totals = function(){

			var main_total  = 0;
			var new_price   = 0;
			var setXiser    = false;
			var setShipping = false;
			var setPaypal   = false;
			if( $('#ulDiscount').css('display') == 'block'){
				setPaypal = true;
			}			
			$("#cart input").each(function(i){ 
			if(this.id == 'item_total') {
			  new_price  = parseFloat(this.value);
			  main_total = parseFloat(main_total);
			  main_total = eval(main_total+new_price).toFixed(2); 
			 delete new_price;
			 }
			});
			var shipping = 0.00;
			if(main_total >=1.00 && main_total <= 300){
				shipping = 15.00;
			} else if (main_total >=301 && main_total <= 1000.00){
				shipping = 25.00;
			} else if (main_total >=1001 && main_total <= 1500.00){
				shipping = 50.00;
			} else if(main_total >= 1501.00){
				shipping = 90.00;
			}
			shipping = parseFloat(shipping).toFixed(2);
			if ( ($('#x_standard').val() >= 1) || ($('#x_commercial').val() >= 1) ){
				shipping = eval(parseFloat(shipping)+parseFloat(50)).toFixed(2);	
			}

			var discount = 0.00;
			
			if(main_total == 0){
				discount = 0.00;
			} else if( (main_total >= 1) && (main_total <= 400)){
				discount = 5.00;
			} else if( (main_total >= 401) && (main_total <= 1000)){
				discount = 10.00;
			} else if( main_total >= 1001){
				discount = 20.00;
			}
					
				$('#discount').html('<strong>($'+discount.toFixed(2)+')</strong>');
				if(setPaypal == true){
					main_total = eval(parseFloat(main_total)-parseFloat(discount)).toFixed(2);
				}
			$('#shipping').html('<strong>$'+shipping+'</strong>');
			$('#total').html('<strong>$'+eval(parseFloat(main_total)+parseFloat(shipping)).toFixed(2)+'</strong>');
			return true;
}
checkOut = function(){
	if(document.getElementById('purchase_order').value == 'null'){
		$('#purchase_order').css('border-color','red');
		alert('Please select a payment type.');
		return false;
	}else{
		$('#purchase_order').removeAttr('style');
	}//end if
	var total = $('#total').text();
	total = parseFloat(total.replace('$',''));
	if(total < 100.00){
		alert('Minimum order $100.00');
		return false;
	}//end if
	
	if($('#test_kit_bundle').val() >= 1 || 
		   $('#test_kit_bundle_strap').val() >= 1 || 
		   $('#test_kit_bundle_monitor').val() >= 1 
		    ){ 
		var authid_input = $('#authid').val();
		
		if(authid_input == ''){
			alert('Please enter your Authorization Code below.');
			return false;
		}//end if
		if( parseInt( authid_input.length ) <= 7){
			alert('Please enter a valid Authorization Code below.');
			return false;
		}
	}//end if
	
	
	//add validation.
	var message = '';
	if($('#test_kit_bundle').val() >=1 ){
		var number = $('#test_kit_bundle').val();
		var totaled =0;
			
		$('#test_kit_bundle_size input').each(function(){
			totaled = eval(parseInt(this.value)+totaled);
		
			if( this.value == ''){
				$('#test_kit_bundle_size #'+this.id).css('border-color','red');
				message = message+this.id.toUpperCase()+' Masks not selected for Test Kit Bundle \n'; 
			}else{
				$('#test_kit_bundle_size #'+this.id).removeAttr('style');
			}
		});
		
		if(totaled != eval(number*12)){
		 	$('#test_kit_bundle_size input').css('border-color','red');
		 	message = message+'Total number Test Kit Bundle Masks must equal '+eval(number*12)+'. \n\n';
		}else{
			$('#test_kit_bundle_size input').removeAttr('style');
		}//end if

	}//end if
	
	if($('#test_kit_bundle_strap').val() >=1 ){
		var number = $('#test_kit_bundle_strap').val();
		totaled = 0;
		$('#test_kit_bundle_strap_size input').each(function(){
			totaled = eval(parseInt(this.value)+totaled);
			if(this.value == ''){
				$('#test_kit_bundle_strap_size #'+this.id).css('border-color','red');
				message = message+this.id.toUpperCase()+' Masks not selected for Test Kit Bundle W/HR Strap. \n'; 
			}else{
				$('#test_kit_bundle_strap_size #'+this.id).removeAttr('style');
			}
		});
		
		if(totaled != eval(number*6)){
		   $('#test_kit_bundle_strap_size input').css('border-color','red');
			message = message+'Total numer of Test Kit Bundle W/HR Strap Masks must equal '+eval(number*6)+'. \n\n';
		}else{
			$('#test_kit_bundle_strap_size input').removeAttr('style');
		}
	}//end if
	
	if($('#test_kit_bundle_monitor').val() >=1 ){
		var number = $('#test_kit_bundle_monitor').val();
		totaled =0;
		$('#test_kit_bundle_monitor_size input').each(function(){
			totaled = eval(parseInt(this.value)+totaled);
			if( this.value == ''){
				$('#test_kit_bundle_monitor_size #'+this.id).css('border-color','red');
				message = message+this.id.toUpperCase()+' Masks not seleted for Test Kit Bundle w/HR Monitor and Strap.\n'; 
			}else{
				$('#test_kit_bundle_monitor_size #'+this.id).removeAttr('style');
			}
		});
		
		if(totaled != eval(number*6)){
		    $('#test_kit_bundle_monitor_size input').css('border-color','red');
		   message = message+'Total number of Test Kit Bundle w/HR Monitor & Strap Masks must equal '+eval(number*6)+'. \n\n';
		}else{
			$('#test_kit_bundle_monitor_size input').removeAttr('style');
		}
	}//end if
	
	
	if(message != ''){
		alert(message);
		return false;
	}
	
	
	
	$('#cart').html($('#cart').html()+add_hidden());
	
	// to stop submitting the order if they cancel paypal
	var doSubmit = true;
	
	if(document.getElementById('purchase_order').value == 'paypal'){
		// added 10/13/2007 BVH
		// allow users to cancel Paypal submission
		if(confirm("You are about to be redirected to Paypal to complete your purchase totalling $"+total+". Before continuing, please review your order carefully for errors. Continue?")){
 			var post_data = 'receive=this';
			$('input').each(function(){
				post_data = post_data+'&'+this.name+'='+this.value;
			});
			
			$.ajax({ 
				  type: "POST", 
				  url: "ajax_processor.php", 
				  data: post_data, 
				  async: false, 
				  success: function(msg){
					//edit 10/16/07 MEB
					// get order id and send it as invoice number.
					$('#invoice').val(msg);
					document.order_form.action = 'https://www.paypal.com/cgi-bin/webscr';
					return false;
				  }});
		}else{
			// customer cancelled paypal submission. do nothing
			//doSubmit = false;
			return false;
		}
	}else{
		// if not paypal, do a generic confirmation
		if(!confirm("Please review your order carefully for errors before continuing. Continue?")){
			return false;
		}
	}
	//alert(doSubmit);
	//if(doSubmit == true){
		document.order_form.submit();
	//}
};


zeroOut = function(){
	var on = document.getElementById('bankAuthID').checked;
	if( on == true ){
		$('#authid').val('00000000'); //.attr({disabled:"disabled"});
	}else{
		$('#authid').val(''); //.removeAttr('disabled');
	}//end if
}//end func

changePwd = function(val){

	switch(val){
 		
 		case 'submit':
 			//validate both input password fields. 
 			if( $('#password1').val() != $('#password2').val() ){
 				alert('Passwords entered do not match.');
 				return false;	
 			} else if(jQuery.trim($('#password1').val()) == "" || jQuery.trim($('#password2').val()) == ""){
 				alert('Please enter a new password in both fields.');
 				return false;	
 			} else {
 				var url = 'changePwd.php?password2='+jQuery.trim($('#password2').val());
 				//alert(url);
 				$('#displayPwd').hide();
 				$('#password1').val("")
 				$('#password2').val("")
 				$('#passStatus').show();
 				$('#passStatus').load(url);
 				//document.cpassword.submit();
 				return true;
 			}//end if
 		
 		break;
 		default:
	 		//display change password box.
	 		$('#displayPwd').show();
	 	break;
 	}

}//end

hideDiv = function(divid){
	//display change password box.
	 $('#'+divid).hide();
}

zeroHideDiv = function(divid){
	 $('#'+divid).html("");
	 hideDiv(divid);
}

// added 10/11/2007 BVH
function selectAllOnFocus(object){
	object.select();
}// end func

function formReset(){
    var x=document.order_form;
    x.reset();
    $('#test_kit_bundle_credit').hide();	
	$('#test_kit_bundle_size').slideUp('slow');	
	$('#test_kit_bundle_credit').html('');
	$('#test_kit_bundle_strap_credit').hide();	
	$('#test_kit_bundle_strap_size').slideUp('slow');	
	$('#test_kit_bundle_strap_credit').html('');
	$('#test_kit_bundle_monitor_credit').hide();	
	$('#test_kit_bundle_monitor_size').slideUp('slow');	
	$('#test_kit_bundle_monitor_credit').html('');
}