//+=+[ (C) Chris Merry ]=+=+=+=+=+=+=+=+[ Insipration Fx - www.infxnet.com ]=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// JAVASCRIPT INCLUDE -- pagesupport.js													   Curtain Makers Online
//+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
//
//  Script Author: Chris Merry  (  k n o c k @ t h e d o o r . c o . u k  )                     
//     
//	Description:    General Javascript functions for Curtain Makers Online		
//
//	Thanks to:	   Ben Clayton, Marc Woodhead :)													   
//
// (C) Chris Merry -- Do not copy this script in anyway without permission.  
//
//+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

	var cmRegxp = /^([0-9]+)$/;

	function XXcheckFormat(obj){
		var valObj = document.getElementById('validated_inputs');
		if(obj){
			var value = obj.value;
			value = value.replace(/[^0-9]*/g,'');
			if (numericRegxp.test(value) == true){
				if(value <= 999 && value >= 100){ 
					if(valObj) valObj.value = valObj.value + 'T';
					ClrError(obj.name);
				}else{
					if(valObj) valObj.value = valObj.value + 'F';
					DispError(obj.name,((value < 100)?"Please enter a value greater than 100cm":"Are you sure you want this much fabric."));
				}
			}else{
				if(valObj) valObj.value = valObj.value + 'F';
				DispError(obj.name,"This should be a measurement value in cm   eg. 100cm");
			}
			obj.value = value;
		}
	}
	
	function zeroIfBlank(inputObj){
		if(inputObj){
			//checkFormat(inputObj);
			var value = inputObj.value;
			if(value == ''){
				inputObj.value = 0;
			}
		}
	}

	function check_curtain_heading(id,fail){
		var headingObj = document.getElementById(id);
		var values = document.getElementsByName(id);
		if(headingObj && values){
			var ok = 0;
			for(var x=0;x<values.length;x++){
				if(values[x].checked == true){
					ok = 1;
				}
			}
			if(!ok) alert("Please select a curtain heading.");
			return (!ok)?1:fail;
		}else{
			return fail;
		}
	}

	function clearIfZero(inputObj){
		if(inputObj){
			var value = inputObj.value;
			if(value == 0){
				inputObj.value = '';
			}
		}
	}

	function update_measurements(action,process){
		var frmObj = document.forms['basketform'];
		if(frmObj && verifylist(frmObj,validate_list)){
			if(process && process != '') frmObj.process.value = process;
			frmObj.ac.value = action;
			frmObj.submit();
		}
	}

	//BC  finished off writing this
	function switching_action(switch_action){
		//alert(switch_action)
		var frmObj = document.forms['basketform'];
		if(frmObj && switch_action != '' && verifylist(frmObj,validate_list)){
			frmObj.ac.value = switch_action;
			frmObj.submit();
		}
	}

	function switch_process(process,action,fabricid){
		var frmObj = document.forms['basketform'];
		if(frmObj && action != '' && verifylist(frmObj,validate_list)){
			frmObj.process.value = process;
			if(fabricid != '') frmObj.fabricid.value = fabricid;
			frmObj.ac.value = action;
			frmObj.submit();
		}
	}


	function submit_action_no_val(action){
		var frmObj = document.forms['basketform'];
		
		if(frmObj && action != ''){
			frmObj.ac.value = action;
			frmObj.submit();
		}
	}
	
	function set_process(process){
		var frmObj = document.forms['basketform'];
		frmObj.process.value = process;
	}
	
	function submit_action(action){
		var frmObj = document.forms['basketform'];

		//BC
		var d= new Date();
		var uniq = d.getMinutes()+":"+d.getSeconds();
		//frmObj.action += "?stop_the_cache_uniq="+ uniq;
		
		//alert(frmObj.action);
		if(frmObj && action != '' && verifylist(frmObj,validate_list)){
			frmObj.ac.value = action;
			frmObj.submit();
		}
	}

	function submit_invoice_action(action,invaction,item,switch_process,switch_action){
		//alert("item="+item);
		var frmObj = document.forms['basketform'];
		var new_name = document.getElementById('new_item_name_input');
		var new_drop = document.getElementById('new_drop');
		var new_totalwidth = document.getElementById('new_totalwidth');
		//alert(action);
		var new_item_name = (new_name)?new_name.value:'';
		var new_item_drop = (new_drop)?new_drop.value:'';
		var new_item_totalwidth = (new_totalwidth)?new_totalwidth.value:'';
		//alert(new_item_name);
		if(frmObj && invaction != '' && action != ''){
			if(invaction == 'delete_item' || verifylist(frmObj,validate_list)){
				if(invaction == 'delete_item'){
					var result = window.confirm("Are you sure you wish to delete this item?");
					if(!result){
						return;
					}
				}
				frmObj.ac.value = (action == -1)?frmObj.ac.value:action;
				//alert("Process: " + process);
				frmObj.switch_process.value = (switch_process == -1 || switch_process == '')?frmObj.switch_process.value:switch_process;
				//alert(invaction);
				frmObj.invoice_action.value = (invaction == -1)?frmObj.invoice_action.value:invaction;
				frmObj.selected_item_number.value = (item == -1)?frmObj.selected_item_number.value:item;
				//alert("frmObj: sel_item_num "+frmObj.selected_item_number.value+" Parsed: "+item);
				if(invaction == 'duplicate_item' || invaction == 'add_matching_item' || invaction == 'new_item'){	// || invaction == 'new_item' no need as we kick them back to first step which force's room/fabric name
					//alert(switch_process);
					if(new_item_name == ''){    // && (switch_process != 'fabric' || (switch_process == 'fabric' && invaction == 'new_item'))){
						//alert("SHOW OVERLAY");
						show_overlay(invaction,switch_process);
					}else{
						//alert("SUMBIT FORM WITH ITEM VALUE: '"+new_item_name+"'");
						frmObj.new_window_name.value = new_item_name;	
						frmObj.new_drop.value = new_item_drop;	
						if(invaction == 'new_item') frmObj.start_new.value = 1;		//START A NEW PROCESS
						frmObj.new_totalwidth.value = new_item_totalwidth;
						if(switch_action && switch_action != null && switch_action != '') frmObj.switch_action.value = switch_action;
						//alert("SUBMIT 1 process: "+frmObj.process.value);
						frmObj.submit();
						//alert("SUBMIT");

					}
				}else{
					//alert("SUBMIT 2 process: "+frmObj.process.value);
					if(switch_action && switch_action != null && switch_action != '') frmObj.switch_action.value = switch_action;

					var s = "action = "+frmObj.ac.value+
							"\nswitch process = "+frmObj.switch_process.value+
							"\ninvoice action = "+frmObj.invoice_action.value+
							"\nselected item no = "+frmObj.selected_item_number.value+
							"\nswitch action = "+frmObj.switch_action.value;
					//alert(s);

					frmObj.submit();
					//alert("SUBMIT");
				}
			}
			//alert(frmObj.invoice_action.value);
		}
	}

	function clearLastResults(){
		var Obj = document.getElementById('validated_inputs');
		if(Obj){
			Obj.value = '';
		}
	}

	//BC where/when is this called ????
	function toggle_printable_quote_layer(){
		var toggle_these = new Array("print_btn_text","hide_btn_text","print_quote_block","invoice_blocks");
		//alert(toggle_these.length);
		for(var x=0;x<=toggle_these.length-1;x++){
			var obj = document.getElementById(toggle_these[x]);
			if(obj){
				obj.style.display = (obj.style.display == 'none')?'block':'none';
			}
		}
		window.scrollTo(0,222);
	}
	
	function switch_forms(type){
		var show = new Array();
		var hide = new Array();
		if(type == 'contact'){
			hide[hide.length] = "submit_order_button";
			hide[hide.length] = "pay_fields";
			show[show.length] = "call_me_button";
		}else{ // pay_fields
			hide[hide.length] = "call_me_button";
			show[show.length] = "pay_fields";
			show[show.length] = "submit_order_button";
		}
		for(var x=0;x<hide.length;x++){
			var hobj = document.getElementById(hide[x]);
			if(hobj){
				hobj.style.display = 'none';
			}
		}
		for(var x=0;x<show.length;x++){
			var sobj = document.getElementById(show[x]);
			if(sobj){
				sobj.style.display = 'block';
			}
		}
	}

	function submitDialogue(e,invaction){
		if(e){
			if(e.keyCode == 13){
				check_entered_value(invaction);
			}
		}else if(window.event){
			if(window.event.keypress == 13){
				check_entered_value(invaction);
			}
		}
	}

	function CheckSearchInput(e,force){
		var frmObj = document.forms['search_form'];
		//alert(frmObj);
		if(e){
			if((e.keyCode == 13 && frmObj) || (force == 1)){
				check_search_field(frmObj);
			}
		}else if(window.event){
			if((window.event.keypress == 13 && frmObj) || (force == 1)){
				check_search_field(frmObj);
			}
		}
	}

	function check_search_field(frmObj){
		var searchtext = document.getElementById('search_text');
		var fillObj = document.getElementById('search_input');
		var frmObj = document.getElementById('searchform');
		//alert(searchtext+' '+fillObj);
		if(searchtext){
			var value = searchtext.value;
			//alert(value);
			if(value != '' && value != 'Fabric, Style etc'){
				fillObj.value = value;
				frmObj.submit();
			}else{
				DispError('searchinput',"Please enter at least one search phrase.");
			}
		}
	}

	function onEnterCheckAndGo(e){
		var frmObj = document.forms['basketform'];
		if(e){
			if(e.keyCode == 13 && frmObj){
				if(verifylist(frmObj,validate_list)){
					if((frmObj.ac.value != '') && (frmObj.category_string.value != '')){
						frmObj.submit();
					}
				}
			}
		}else if(window.event){
			if(window.event.keypress == 13 && frmObj){
				if(verifylist(frmObj,validate_list)){
					if((frmObj.ac.value != '') && (frmObj.category_string.value != '')){
						frmObj.submit();
					}
				}
			}
		}
	}

	function check_entered_value(invaction){
		var frmObj = document.forms['basketform'];
		if(frmObj){
			if(verifylist(frmObj,validate_list)){
				submit_invoice_action(-1,invaction,-1,-1,null);
			}else{
				DispError('new_item_name_input',"Please enter a name for this new item.")
			}
		}
	}

	function hide_overlay(){
		var obj = document.getElementById('sending_overlay');
		if(obj){
			obj.style.visibility = 'hidden';
		}
	}
	
	function show_overlay(invaction,switch_process){
		var obj = document.getElementById('sending_overlay');
		//var container = document.getElementById('container');
		//var selects = container.getElementsByTagName('select');
		if(obj){
			//var process = document.getElementById('process').value;
			var image = (switch_process == 'fabric')?"new_fabric_name_panel.gif":"new_room_name_panel.gif";
			var width_text = (switch_process == 'fabric')?"Length (cm)":((switch_process == 'blind')?"Width":"Width");
			var htmlblk = "";
			// NOTE Fabric uses the totalwidth as the "Lenght (cm)"
			var fname = "";
			if(switch_process=='fabric'){
				htmlblk = "<div align='center' style=\"position:relative;background-image: url('/images/"+image+"');width:400px;height:150px;top:400px;margin-top:auto;margin-bottom:auto;margin-left:auto;margin-right:auto;\">"+
					"<TABLE width='370' height='150' cellpadding='0' cellspacing='0'>"+
					"<TR><TD>"+
					"<table cellpadding='0' cellspacing='0' width='100%'>"+
					"<tr><td valign='left' class='pbody' style='padding-left:10px;' nowrap>"+
					"Item name:&nbsp;"+
					"</td>"+
					"<td valign='right' class='invText'>"+
					"<input type='text' name='new_item_name_input' id='new_item_name_input' class='inputBorder' style='width:140px;'  value=''>"+
					"</td>"+
					"<td align='center' rowspan='2' style='padding-left:10px;'>"+
					"<img src='/images/submit_btn.gif' style='cursor:pointer;' onmousedown=\"check_entered_value('"+invaction+"');\">"+
					"<br>"+
					"<span style='line-height:8px;'><br></span><img src='/images/cancel_btn.gif' style='cursor:pointer;' onmousedown='hide_overlay();'>"+
					"</td>"+
					"</tr>"+
					"<tr>"+
					"<td valign='left' class='pbody' style='padding-left:10px;' nowrap>"+width_text+":&nbsp;</td>"+
					"<td valign='middle'>"+
					"<input type='text' name='new_totalwidth' id='new_totalwidth' class='inputBorder' style='width:50px;'>"+
					"</td><td>&nbsp;</td></tr></table></TD></TR></TABLE></div>";
			}else{
				htmlblk = "<div align='center' style=\"position:relative;background-image: url('/images/"+image+"');width:400px;height:150px;top:400px;margin-top:auto;margin-bottom:auto;margin-left:auto;margin-right:auto;\">"+
					"<TABLE width='370' height='150' cellpadding='0' cellspacing='0'>"+
					"<TR><TD><table cellpadding='0' cellspacing='0' width='100%'>"+
					"<tr><td valign='left' class='pbody' style='padding-left:10px;' nowrap>Item name:&nbsp;"+
					"</td><td valign='right'>"+
					"<input type='text' name='new_item_name_input' id='new_item_name_input' class='inputBorder' style='width:140px;'>"+
					"</td><td align='center' rowspan='3' style='padding-left:20px;'>"+
					"<img src='/images/submit_btn.gif' style='cursor:pointer;' onmousedown=\"check_entered_value('"+invaction+"');\"><br>"+
					"<span style='line-height:8px;'><br></span><img src='/images/cancel_btn.gif' style='cursor:pointer;' onmousedown='hide_overlay();'>"+
					"</td></tr><tr><td valign='left' class='pbody' style='padding-left:10px;'>"+width_text+"&nbsp;</td>"+
					"<td valign='right'><input type='text' name='new_totalwidth' class='inputBorder' id='new_totalwidth' onkeypress=\"submitDialogue(event,'"+invaction+"');\" style='width:50px;'>"+
					"</td></tr>"+
					"<tr><td valign='middle' class='pbody' style='padding-left:10px;' nowrap>Drop:&nbsp;</td><td valign='middle'>"+
					"<input type='text' name='new_drop' id='new_drop' class='inputBorder' style='width:50px;'></td><td>&nbsp;</td></tr></table>"+
					"</TD></TR>"+
					"</TABLE></div>";
			}
			//alert(selects.length);
			//for(var x=0;x<selects.length;x++){
			//	selects[x].style.visibility = (selects[x].style.visibility == 'hidden')?'visible':'hidden';
			//}
			obj.innerHTML = htmlblk;
			obj.style.visibility = 'visible';
			window.scrollTo(0,320);
		}
	}

	function view_fabric_category(action,cat_string,cat_title){
		var frmObj = document.forms['basketform'];
		if(frmObj && action != ''){
			frmObj.ac.value = action;
			frmObj.category_string.value = cat_string;
			frmObj.cat_title.value = cat_title;
			if(verifylist(frmObj,validate_list)){
				frmObj.submit();
			}
		}
	}

	function view_fabric(action,fabricid){
		//alert(action+" "+fabricid);
		var frmObj = document.forms['basketform'];
		if(frmObj && action != '' && verifylist(frmObj,validate_list)){
			frmObj.ac.value = action;
			frmObj.fabricid.value = fabricid;
			frmObj.submit();
		}
	}

	function view_fabric_sample(fabricid,title){
		//alert("func - view_fabric_sample id: "+fabricid+"  title: "+title);
		var esctitle = escape(title);
		var href = '/dyn/_pages/_navpages/sample_request.shtml?fabricid='+fabricid+'%20-%20'+esctitle;
		parent.location.href = href;
	}
	
	function view_fabric_sample_detail(fabricid,title){
		//alert("func - view_fabric_sample id: "+fabricid+"  title: "+title);
		var esctitle = escape(title);
		var href = '/dyn/_pages/_navpages/fabric_sample_detail.shtml?fabricid='+fabricid;
		parent.location.href = href;
	}
	
	function view_similar(action,cat){
		//alert(cat);
		var frmObj = document.forms['basketform'];
		if(frmObj && action != '' && verifylist(frmObj,validate_list)){
			frmObj.ac.value = action;
			frmObj.category_string.value = cat;
			frmObj.submit();
		}
	}


	function toggle_option_layers(value){
		var obj1 = document.getElementById('pole_selector');
		var obj2 = document.getElementById('drop_selector_div');
		var obj3 = document.getElementById('pole_size');
		if(obj1 && obj2){
			//alert(value);
			var display = (value == 'curtains_off_pole')?'block':'none';
			obj1.style.display = display;
			//alert("Display 1: "+display);
			display = (value == 'curtains_with_pelmet_on_track')?'block':'none';
			obj2.style.display = display;
			//alert("Display 2: "+display);
		}
		if(value == 'curtains_off_pole'){
			auto_set_pole_size('hang_type','not_required');
		}
	}

	function auto_set_pole_size(varA,varB){
		//BC
		var selObj = document.getElementById('pole_size');
		var totalwidth = document.getElementById('totalwidth').value;

		//alert("totalwidth="+totalwidth);

		var ops = document.getElementById('pole_size').options;
		var set_to = -1;
		for (var x=0;x<ops.length;x++ ){
			var pole_size = ops[x].innerHTML;
			pole_size = parseInt(pole_size * 100);
			//alert("stock Pole length="+pole_size);
			if (totalwidth <= pole_size){
				set_to = ops[x].value;
				//alert("Pole cost="+set_to);
				setSelectIndex('pole_size',set_to,'basketform');
				var obj_pl = document.getElementById('pole_length');
				if (obj_pl){
					obj_pl.value = ops[x].innerHTML	;//if you want it in m
					//obj_pl.value = pole_size	;//if you want it in cm
					//alert("Pole length="+obj_pl.value+"m");
				}

				return
			}
		}

		var user_result = window.confirm('We cannot supply a pole large enough to span the width entered, click OK if you would like us to remove the pole or click CANCEL to re-enter the width required.');
		if(user_result){
			setRadioIndex(varA,varB);
		}
	}

	function highlightTD(ID){
		var obj = document.getElementById('highlight_radios');
		if(obj){
			var layers = obj.getElementsByTagName('td');
			for(var x=0;x<layers.length;x++){
				//alert(layers[x].id);
				if(layers[x].id.indexOf('cell_') != -1){
					layers[x].style.backgroundColor = (layers[x].id.indexOf('cell_'+ID) != -1)?'#f7eeb6':'#ffffff';
					layers[x].backgroundColor = (layers[x].id.indexOf('cell_'+ID) != -1)?'#f7eeb6':'#ffffff';
				}
			}
		}
	}

	function switch_page(layerid){
		var obj1 = document.getElementById('page_layers');
		//alert("sub switch_page - "+layerid);
		if(obj1){
			var layers = obj1.getElementsByTagName('div');
			for(var x=0;x<layers.length;x++){
				if(layers[x].id.indexOf('layer_') != -1){
					var id = layers[x].id.replace(/layer_/,'');
					layers[x].style.display = (id == layerid)?'block':'none';
				}
			}
		}
	}

	function toggle_visibility(id){
		var obj = document.getElementById(id);
		if(obj){
			var display = (obj.style.display == 'none')?'block':'none';
			obj.style.display = display;
		}else{
			alert("Could not find element with ID: "+id+". please refresh page");
		}	
	}

	function load_page(url){
		if(url != 0 && url != ''){
			location.href=url;
		}
	}
	var lastPref = "B";
	function resize_image_img_src(direction){
		var hide = lastPref;
		var show = (lastPref == 'B')?'A':'B';
		
		//alert("Direction = "+direction);
		
		var hideObj = document.getElementById('BCanim'+hide);
		var showObj = document.getElementById('BCanim'+show);
		 
		var img_source = hideObj.src;
	
		if(img_source.indexOf('300px/') && direction == '+'){
			img_source = img_source.replace('300px/','300px_hi/');
		}else if(img_source.indexOf('300px_hi/') && direction == '-'){
			img_source = img_source.replace('300px_hi/','300px/');
		}

		//alert("hide: "+hide+" "+hideObj.src);

		fadeOut('BCanim'+hide,100);
		hideObj.style.visibility='hidden';

		showObj.style.visibility='visible';
		showObj.src = img_source;

		//alert("show: "+show+" "+showObj.src);

		fadeIn('BCanim'+show,0);
		//alert(showObj);
	//	var imgobj = document.getElementById('fabric_image_panel');
	//	if(imgobj){
	//		var img_source = imgobj.src;
	//		var fnd = 1;
	//		//alert(img_source);
	//		if(img_source.indexOf('300px/') && direction == '+'){
	//			img_source = img_source.replace('300px/','300px_hi/');
	//		}else if(img_source.indexOf('300px_hi/') && direction == '-'){
	//			img_source = img_source.replace('300px_hi/','300px/');
	//		}else{
	//			var msg = (direction == '+')?'You are already zoomed in':'You are zoomed out';
	//			alert(msg);
	//			fnd = 0;
	//		}
	//		//alert(img_source);
	//		if(fnd) imgobj.src = img_source;
	//	}

		lastPref = show;
	}

	function break_url(){
		var s=location.href;
		//alert(s);
		var s1 = s.split("?");
		if (s1.length==2){
			s = s1[1];
			s1 = s.split("&");
			for (var x=0;x<s1.length ;x++ ){
				var s2 = s1[x].split("=");
				//alert(s2[0] + " = " + s2[1]);
				s2[1] = unescape(s2[1]);
				s2[1] = s2[1].replace(/\+/g,' ');
				if(document.getElementById(s2[0])){
					setTextBoxValue(s2[0],s2[1]);
				}
			}
		}

	}

	function setTextBoxValue(name,value){	
		//alert("NAME: "+name+"  VALUE: "+value);
		var obj = document.getElementById(name);
		if(obj){	
			obj.value = value;
		}else{
			alert("setTextBoxValue - control not found = "+name);
		}
	}

	var lastID = "";

	function setCurtainHeading(name,value,tableID){
		//BC
		// This gets called in curtain maker process when making new curtain 
		//  Often the tableID is blank.  I think the "cheadingidx"  needs storing in the basket ??

		//alert("NAME: "+name+"  VALUE: "+value+" TABLEID="+tableID);
		setRadioIndex(name,value);
		
		if(tableID == "")return;

		var obj = document.getElementById(tableID);
		
		if(lastID != ''){
			var lastobj = document.getElementById(lastID);
		}
		//alert(tableID);
		if(obj){	
			//alert(obj);
			if(lastobj){
				lastobj.style.backgroundColor = '#ffffff';
				lastobj.backgroundColor = '#ffffff';
			}
			var frmobj = document.getElementById('cheadingidx');
			if(frmobj) frmobj.value = tableID;
			lastID = tableID;
			obj.style.backgroundColor = '#f7eeb6';
			obj.backgroundColor = '#f7eeb6';
		}
	}
		
	function setRadioIndex(name,current){
		//alert(" setRadioIndex 1) NAME: "+name+"  VALUE: "+current);
		var rads;
		if(document.getElementsByName(name)){
			rads=document.getElementsByName(name);
			var i=-1;
			for (var x=0;x<rads.length;x++ ){
				//rads[x].checked=false; //BC
				if (rads[x].value == current) i=x;
			}
			if(i != -1){ 
				rads[i].checked=true; 

				if(rads[i].onchange){
					rads[i].onchange();
				}else if(rads[i].onclick){
					rads[i].onclick();
				}
			}
		}else{
			alert("setRadioIndex - control not found = "+name);
		}

	}
	
	function XsetSelectIndex(name,current){	
		//alert("NAME: "+name+"  VALUE: "+current);	
		var ops;
		if(document.forms['basketform'][name]){	
			ops=document.getElementById(name).options;
			obj=document.getElementById(name);
			var i=-1;
			for (var x=0;x<ops.length;x++ ){
				if (ops[x].value == current) i=x;
			}
			if(i != -1){ 
				ops.selectedIndex=i;
				if(obj.onchange)  obj.onchange();
			}
		}else{
			alert("setSelectIndex - control not found = "+name);
		}
	}
	
	function XXsetSelectIndex(name,current){	
		//alert("NAME: "+name+"  VALUE: "+current);	
		var obj = document.getElementById(name);
		if(obj){	
			alert("FOUND OBJECT");
			var ops = obj.options;
			var i=-1;
			for (var x=0;x<ops.length;x++ ){
				//alert("Value: "+ops[x].value+"  Result: "+(ops[x].value == current));
				if (ops[x].value == current){
					alert("SET SELECTED INDEX to "+x);
					obj[x].selectedIndex= x;
					obj[x].value=value;
					if(obj.onchange)  obj.onchange();
				}
			}
		}else{
			alert("setSelectIndex - control not found = "+name);
		}
	}

	function setSelectIndex(name,current,frmname){						
		var ops;
		var frmname = 'basketform';
		//alert('Name: ' + name + "  Current: " + current + "  Formname:" +frmname);
		if(document.forms[frmname][name]){	
			ops=document.getElementById(name).options;
			obj=document.getElementById(name);
			if(ops){
				var i=-1;
				for (var x=0;x<ops.length;x++ ){
					if (ops[x].value == current) i=x;
				}
				if(i != -1){ 
					ops.selectedIndex=i;
					if(obj.onchange) obj.onchange();
				}
			}
		}else{
			alert("setSelectIndex - control not found = "+name);
		}
	}

	function setCheckBox(name,current){
		//alert("NAME: "+name+"  VALUE: "+current);
		if(document.forms['basketform'][name]){
			var checks = document.forms['basketform'][name];
			var i=-1;
			//alert("Obj: "+checks);
			//alert("Length: "+checks.length);
			if(checks.length){
				for (var x=0;x<checks.length;x++ ){
					//alert("Value: "+checks[x].value);
					if (checks[x].value == current) i=x;
				}
				if(i == -1){ 
					checks[i].checked=1;
				}
			}else{
				if (checks.value == current) checks.checked=1;
			}
		}else{
			alert("setCheckBox - control not found = "+name);
		}
	}	

	function change_pole_cost(obj){
		if(obj.selectedIndex != -1){
			var length = obj.options[obj.selectedIndex].text;
			var obj = document.getElementById('pole_length');
			if(obj){
				obj.value = length;
			//	alert(obj.value);
			}
		}
	}

	function dump(obj){
		alert(obj);
		var sd="";
		var n=0;
		for (var p in obj){
			sd += p + " = " + obj[p] + "\n";
			
			n++;
			if (n>10){
				alert(sd);
				sd="";
				n=0;
			}
		}
		alert(sd);
	}
	
	
	var lastElements;
	var lastItem;
	function select_item(item,tdcell){	
		var elements = tdcell.parentNode.getElementsByTagName('TD');
		if(tdcell){
			for(var x=0;x<elements.length;x++){
				if(lastElements == item) return;
				elements[x].className = (elements[x].className == 'hilite')?'nolite':'hilite';
				if(lastElements) lastElements[x].className = (lastElements[x].className == 'hilite')?'nolite':'hilite';
				//alert(elements[x].className);
			}
		}
		lastElements = elements;
		var hidden = document.getElementById('selected_item_number');
		if(hidden){
			hidden.value = item;
			//alert(hidden.value);
		}
		lastItem = item;
	}