
function unset_select_value( id ){
	if( document.getElementById( id ) ){
		combo=document.getElementById( id );
		combo.options.length=0;
		combo.options[0]= new Option('הכל',0 );
		document.getElementById( "tr_"+id ).style.display='none';
	}
	if( document.getElementById("tr_fromPrice") && document.getElementById("tr_SalesSubCat3ID").style.display=='none' && document.getElementById("tr_ManufacturID").style.display=='none'  )
		document.getElementById( "tr_fromPrice" ).style.display='block';
}


function set_select_data_value( id, where_value, select_val, path){
	try{
		var key = window.event.charCode ? window.event.charCode : window.event.keyCode;
	}catch(err){	
		key='mozila';
	}
		//alert(document.getElementById( id ));
		//alert(where_value);
	if( document.getElementById( id ) || key=='mozila'){

		var combo = document.getElementById( id );
		var retArr = new Array();
		var myAjax = new Ajax.Request(
				path+'ajax/autocomplete_Prof_Cat.php?q='+where_value,
				{
					method: 'get', 
					onComplete: function(response) { 

									//alert(response.responseText);

						retArr=response.responseText.split(';');
						combo.options.length=0;
						//combo.options[0]= new Option('הכל',0 );
						selectedIndex=0;
						for(i=0; i< (retArr.length/2)-1; i++){
							if(retArr[2*i]==select_val)
								selectedIndex=(i+1);
							combo.options[i] = new Option(retArr[2*i+1], retArr[2*i]);
							//combo.options[i+1] = new Option(retArr[2*i+1], retArr[2*i]);
						}
						combo.selectedIndex=selectedIndex;	
								//alert(retArr.length)
					}
					
				}  
				
		);
	}
}

/***********************************************************************************************************************************************************/

//this function add addtion to second list box
function btnAddSubAddition_onclick()
{
		//check if there is selected items
		// -1 means there is noe selected items
	 if (document.form1.slcSubAddition.selectedIndex!= -1 && document.form1.slcSubAdditionSelected.length<5) 
	 {
			 //alert(document.form1.slcSubAddition.options[document.form1.slcSubAddition.selectedIndex].text);
			 document.form1.slcSubAdditionSelected.length = document.form1.slcSubAdditionSelected.length + 1;

			 //alert(document.form1.slcSubAdditionSelected.length)
			 document.form1.slcSubAdditionSelected.options[document.form1.slcSubAdditionSelected.length-1].text = document.form1.slcSubAddition.options[document.form1.slcSubAddition.selectedIndex].text;

			 document.form1.slcSubAdditionSelected.options[document.form1.slcSubAdditionSelected.length-1].value = document.form1.slcSubAddition.options[document.form1.slcSubAddition.selectedIndex].value;
			 
			//call to AddToSubselectedAddition() function which add data to the array
			/*AddToSubSelectedAddition();*/ 
			delfromSubAddition(document.form1.slcSubAddition.options[document.form1.slcSubAddition.selectedIndex].value)
			/*disabledbtnAddSubAddition() assaf*/
	}else if( document.form1.slcSubAdditionSelected.length>=5 ){
		alert("ניתן לבחר 5 סיווגים בלבד");
	}
}


function btnDelSubAddition_onclick()
{
		//check if there is selected items
		// -1 means there is noe selected items
	 if (document.form1.slcSubAdditionSelected.selectedIndex!= -1) 
	 {
			 //alert(document.form1.slcSubAddition.options[document.form1.slcSubAddition.selectedIndex].text);
			 document.form1.slcSubAddition.length = document.form1.slcSubAddition.length + 1;
			// alert(document.form1.slcSubAddition.length)
			 document.form1.slcSubAddition.options[document.form1.slcSubAddition.length-1].text = document.form1.slcSubAdditionSelected.options[document.form1.slcSubAdditionSelected.selectedIndex].text;

			 document.form1.slcSubAddition.options[document.form1.slcSubAddition.length-1].value = document.form1.slcSubAdditionSelected.options[document.form1.slcSubAdditionSelected.selectedIndex].value;
			 
			//call to AddToSubselectedAddition() function which add data to the array
			/*AddToSubSelectedAddition();*/
			delfromSubAdditionSelected(document.form1.slcSubAdditionSelected.options[document.form1.slcSubAdditionSelected.selectedIndex].value)
			/*disabledbtnAddSubAddition() assaf */
	}
}

//check if the sub additionlist box is empty
function disabledbtnAddSubAddition()
{	
	if (document.form1.slcSubAddition.length == 0 ) 
	  document.form1.btnAddSubAddition.disabled = true;
	  else
	  document.form1.btnAddSubAddition.disabled = false;
}

function AddToSubSelectedAddition()
{  // alert(document.form1.slcAddition.options[document.form1.slcAddition.selectedIndex].value);
	SubSelectedAddition0[SubSelectedAdditionCounter] = document.form1.slcAddition.options[document.form1.slcAddition.selectedIndex].value;
		//alert("SubSelectedAddition0[SubSelectedAdditionCounter]="+SubSelectedAddition0[SubSelectedAdditionCounter])
	SubSelectedAddition1[SubSelectedAdditionCounter] = document.form1.slcSubAddition.options[document.form1.slcSubAddition.selectedIndex].value;
		//alert("SubSelectedAddition1[SubSelectedAdditionCounter]="+SubSelectedAddition1[SubSelectedAdditionCounter])
	SubSelectedAddition2[SubSelectedAdditionCounter] = document.form1.slcSubAddition.options[document.form1.slcSubAddition.selectedIndex].text;
		//alert("SubSelectedAddition2[SubSelectedAdditionCounter]="+SubSelectedAddition2[SubSelectedAdditionCounter])
	SubSelectedAdditionCounter++;
	//print_onclick();
}

/*function btnDelSubAddition_onclick()
{

	if (document.form1.slcSubAdditionSelected.selectedIndex!= -1 ) 
		{  
			delfromSubSelectedAddition(document.form1.slcSubAdditionSelected.options[document.form1.slcSubAdditionSelected.selectedIndex].value)
		}
	slcAddition_change()
}*/

function slcAddition_change()
{
	 var SelectedAddition = document.form1.slcAddition.options[document.form1.slcAddition.selectedIndex].value;
	 var i=0;
	 var c=0;
	 var indexForSlcSubAdditionSelected=0;//index for Selected items
	 
	 //cleanning the list of the Selected SubAddition
	 document.form1.slcSubAdditionSelected.length = 0;
	 

	 //deciding the length of the list of the SubAddition
	 for(i=0;i<SubAddition0.length;i++)
	 {
	   if (SelectedAddition == SubAddition0[i]) 
			if (isSelectedSubAddition(SubAddition1[i]) == false)
				{
					c++;
				}
			  else
			   {
			   indexForSlcSubAdditionSelected++;
			   }
				
	 }
	 document.form1.slcSubAddition.length = c;
	 document.form1.slcSubAdditionSelected.length = indexForSlcSubAdditionSelected;
	 c=0;
	 indexForSlcSubAdditionSelected=0;
	 for(i=0;i<SubAddition0.length;i++)
	 { 
	   if (SelectedAddition == SubAddition0[i]) 
		  {
			if (isSelectedSubAddition(SubAddition1[i]) == false)
			{   //inser the data to the slcsubaddition list box
				// if it not exist on selected items
				document.form1.slcSubAddition[c].text = SubAddition2[i];
				document.form1.slcSubAddition[c].value = SubAddition1[i];
				c++;
			}
			else
			{  
				document.form1.slcSubAdditionSelected[indexForSlcSubAdditionSelected].text = SubAddition2[i];
				document.form1.slcSubAdditionSelected[indexForSlcSubAdditionSelected].value = SubAddition1[i];
				indexForSlcSubAdditionSelected++;
			}
		  }
	  }
	  
	  
	 
	   //calling to diable the addbtn
	 disabledbtnAddSubAddition()
}

function delfromSubAddition(parameter)
{ 
  var i;
  var whatTodel;
  //alert(parameter);
  for(i=0;i<document.form1.slcSubAddition.length;i++)
	{
	 if (document.form1.slcSubAddition.options[i].value == parameter )
		{
		 whatTodel = document.form1.slcSubAddition.selectedIndex;
		}
	}
   //alert(whatTodel + 'isequal' + (document.form1.slcSubAddition.length -1))
   if (whatTodel != (document.form1.slcSubAddition.length-1))
	{
		for(i=whatTodel;i<document.form1.slcSubAddition.length-1;i++)
			{   //alert(i)
				document.form1.slcSubAddition.options[i].value = document.form1.slcSubAddition.options[i+1].value
				document.form1.slcSubAddition.options[i].text = document.form1.slcSubAddition.options[i+1].text
			}
			//document.form1.slcSubAddition.options[i].text = ''
			//document.form1.slcSubAddition.options[i].value = ''
			document.form1.slcSubAddition.length = i;
	} else 
			{
			 document.form1.slcSubAddition.length -1;
			document.form1.slcSubAddition.length =  document.form1.slcSubAddition.length -1;

			}
//document.form1.slcSubAddition.length = document.form1.slcSubAddition.length-1;
//alert(document.form1.slcSubAddition.length)
}


function delfromSubAdditionSelected(parameter)
{ 
  var i;
  var whatTodel;
  //alert(parameter);
  for(i=0;i<document.form1.slcSubAdditionSelected.length;i++)
	{
	 if (document.form1.slcSubAdditionSelected.options[i].value == parameter )
		{
		 whatTodel = document.form1.slcSubAdditionSelected.selectedIndex;
		}
	}
   //alert(whatTodel + 'isequal' + (document.form1.slcSubAdditionSelected.length -1))
   if (whatTodel != (document.form1.slcSubAdditionSelected.length-1))
	{
		for(i=whatTodel;i<document.form1.slcSubAdditionSelected.length-1;i++)
			{   //alert(i)
				document.form1.slcSubAdditionSelected.options[i].value = document.form1.slcSubAdditionSelected.options[i+1].value
				document.form1.slcSubAdditionSelected.options[i].text = document.form1.slcSubAdditionSelected.options[i+1].text
			}
			//document.form1.slcSubAdditionSelected.options[i].text = ''
			//document.form1.slcSubAdditionSelected.options[i].value = ''
			document.form1.slcSubAdditionSelected.length = i;
	} else 
			{
			 document.form1.slcSubAdditionSelected.length -1;
			document.form1.slcSubAdditionSelected.length =  document.form1.slcSubAdditionSelected.length -1;

			}
//document.form1.slcSubAdditionSelected.length = document.form1.slcSubAdditionSelected.length-1;
//alert(document.form1.slcSubAddition.length)
}



function hide_prof(id){
	document.getElementById(id).style.display="none";
}

function set_category_prof(oDiv, key){
	//alert(oDiv.innerHTML);
	//retArr=oDiv.innerHTML.split(';');
	//alert(retArr[1]);
	document.getElementById("_bName2").value = oDiv.innerHTML;
	if( document.getElementById("_bName21") )
		document.getElementById("_bName21").value = key;
	hide('_bName2_autocomplete');
}

function goUp_prof(){
	var _bName2_div = document.getElementById("_bName2_autocomplete");
	if( _bName2_div != null){
		if(_bName2_div.style.display == "block"){
			var cur_sel_hid = document.getElementById("cur_category");
			var cur_sel = cur_sel_hid.value;
			cur_sel = cur_sel.substr(8);
			var i = parseInt(cur_sel);
			i--;
			var previous_item = document.getElementById( "category" + i );
			if(previous_item != null){
				document.getElementById( cur_sel_hid.value ).style.backgroundColor = "White";
				previous_item.style.backgroundColor = "#F5A37E";
				cur_sel_hid.value = "category" + i;
			}
		}
	}
}

function goDown_prof(){
					//alert("goDown_prof");
	var _bName2_div = document.getElementById("_bName2_autocomplete");
					//alert("_bName2_div"+_bName2_div);
	if( _bName2_div != null){
					//alert("Not NULL");
		if(_bName2_div.style.display == "block"){
					//alert("_bName2_div.style.display=block");
			var cur_sel_hid = document.getElementById("cur_category");
					//alert("document.getElementById(cur_category)="+document.getElementById("cur_category")+"\ncur_sel_hid="+cur_sel_hid+"\ncur_sel_hid.value="+cur_sel_hid.value);
			var cur_sel = cur_sel_hid.value;
			cur_sel = cur_sel.substr(8);
					//alert("cur_sel="+cur_sel);
			var i = parseInt(cur_sel);
			i++;
			var next_item = document.getElementById( "category" + i );
			if(next_item != null){
				document.getElementById( cur_sel_hid.value ).style.backgroundColor = "White";
				next_item.style.backgroundColor = "#F5A37E";
				cur_sel_hid.value = "category" + i;
			}
		}
	}
}

function selectItem_prof(){
	var _bName2_div = document.getElementById("_bName2_autocomplete");
	if( _bName2_div != null){
		if(_bName2_div.style.display == "block"){
			var cur_sel_hid = document.getElementById("cur_category");
			var cur_sel = cur_sel_hid.value;
			var selected_item = document.getElementById( cur_sel );
			if(selected_item != null){
				document.getElementById( "_bName2" ).value = selected_item.innerHTML;
				category_div.style.display = "none";
				stopEvent();
			}
		}
	}
}
/*
function addEvent(obj,event_name,func_name){
	if (obj.attachEvent){
		obj.attachEvent("on"+event_name, func_name);
	}else if(obj.addEventListener){
		obj.addEventListener(event_name,func_name,true);
	}else{
		obj["on"+event_name] = func_name;
	}
}

function stopEvent(){
	evt = window.event;
	if (evt.stopPropagation){
		evt.stopPropagation();
		evt.preventDefault();
	}else if(typeof evt.cancelBubble != "undefined"){
		evt.cancelBubble = true;
		evt.returnValue = false;
	}
	return false;
}
*/
function closeAuto_prof(){
	var city_div = document.getElementById("_bName2_autocomplete");
	if( city_div != null ){
		city_div.style.display = "none";
	}
	/*var address_div = document.getElementById("Address_autocomplete");
	if( address_div != null ){
		address_div.style.display = "none";
	}*/
}

function up_down_select_prof(evtobj){
		//alert("up_down_select");
	var keycode= evtobj.charCode ? evtobj.charCode : evtobj.keyCode ;
		//alert("up_down_select keycode="+keycode);
	switch (keycode){
	case 38:
		goUp_prof();
		break;
	case 40:
		goDown_prof();
		break;
	case 13:
		selectItem_prof();
		break;
	case 9:
		closeAuto_prof();
		break;
	default:
		break;
	}
}

function get_category_prof(o, path){
	//alert("assaf");
	try{
		var key = event.charCode ? event.charCode : event.keyCode;
			//alert("key="+key);
		if( key==38 ){
			//alert("INN goUp");
			goUp_prof();
		}else if( key==40 ){
			//alert("INN goDown");
			goDown_prof();
		}else if( key==13 ){
			//alert("INN selectItem");
			selectItem_prof();
		}else if( key==9 ){
			//alert("INN closeAuto");
			closeAuto_prof();
		/*}else if( key==38 || key==40 || key==13 || key==9 ){
			alert("INN key="+key);
			up_down_select_prof(o);*/
		}else{
			if(key != 38 && key != 40 && key!= 13 ){
				var my_div = document.getElementById("_bName2_autocomplete");
				var myAjax = new Ajax.Request(
						path + 'ajax/category_autocomplete.php?q=' + o.value+"&c=1",
						{
							method: 'get', 
							onComplete: function(response) { 
								my_div.innerHTML = response.responseText;
								if(response.responseText != "")
									my_div.style.display = "block";
								else
									my_div.style.display = "none";
							}
							
						}                    );
			}
		}
	}catch(err){
		var my_div = document.getElementById("_bName2_autocomplete");
		var myAjax = new Ajax.Request(
			path + 'ajax/category_autocomplete.php?q=' + o.value+"&c=1",
			{
				method: 'get', 
				onComplete: function(response) { 
					my_div.innerHTML = response.responseText;
					if(response.responseText != "")
						my_div.style.display = "block";
					else
						my_div.style.display = "none";
				}
				
			}
		);			
	}
}

//addEvent(document,"keydown",up_down_select);
//addEvent(document,"click",close_auto);
