function str_replace(search, replace, subject)
{
return subject.split(search).join(replace);
}

//-------------------------------------------
// Обработка событий бокса воода кол-ва на кнопке корзины + события onfocus onblur



function LiveCheck() // для того чтобы отменять несохраненное кол-во на кнопках
{


	focused_id=($("*:focus").attr("id"));

	livecheck=document.getElementById('check_livebox_h');

	//alert(focused_id);
	if(livecheck.value!='')
	{

		obj=document.getElementById(livecheck.value);

		if(obj.lastClick==true)
		{
			hid_val=obj.id+'_hid';
			if(obj.id+'_btn'!=focused_id)
				obj.value=document.getElementById(hid_val).value;

			obj.lastClick=false;
		}
		else
		{

			if(obj.id+'_btn'!=focused_id)
				obj.value=document.getElementById(hid_val).value;
		}

	}

	 //$('#'+focused_id+'').focus();




}




function LiveBoxCheck(obj) // onfocus на cart_button
{
	obj.hasFocus=true;
	obj.select();
	obj.lastClick=true;
	upd_obj=document.getElementById('check_livebox_h');

	if ( (upd_obj.value!=obj.id) && (upd_obj.value!='') )
	{
		object=document.getElementById(upd_obj.value);

			hid_val=object.id+'_hid';
				object.value=document.getElementById(hid_val).value;

	}

	upd_obj.value=obj.id;
}

function LiveBoxCheckSmallBtn(obj) // onfocus на cart_button
{
	obj.hasFocus=true;
	obj.select();
	obj.lastClick=true;
	upd_obj=document.getElementById('check_livebox_h');

	/*
	if ( (upd_obj.value!=obj.id) && (upd_obj.value!='') )
	{
		object=document.getElementById(upd_obj.value);

			hid_val=object.id+'_hid';
				object.value=document.getElementById(hid_val).value;

	}
	*/
	//upd_obj.value=obj.id;
	//alert(obj.value);

}

//-------------------------------------------


function TableSpinClick(do_act,obj_id)
{
	obj=document.getElementById(obj_id);

	val=parseInt(obj.value);
	//alert(val);
	if(do_act=='add')
	{
		if(val>=0) val++;
		else val=0;
	}
	else
	{
		if(val>1) val--;
		else val=0;
	}


	obj.value=val;
//alert('act='+do_act+'  id='+obj_id+' val='+obj.value+' | newval='+val);
}

function timedMsg(obj)
{
setTimeout(function() { CartBtnOut(obj); }, 2000);

}

function CartBtnOut(obj)
{

	//alert('id='+obj.id);


	obj_name=obj.id+'_btn';

	obj_btn=document.getElementById(obj_name);
	alert('try'+obj_btn.hasFocus);

	if(obj_btn.hasFocus)
	{
		alert('button focus');
	}

	obj_name=obj.id+'_check';
	obj_check=document.getElementById(obj_name);
	if(obj.value!=obj_check.value)
		{
			//alert('DIFF');
			obj.value=obj_check.value;
		}


}


function fireEvent(element,event)
{

   if (document.createEvent) {
       // dispatch for firefox + others
       var evt = document.createEvent("HTMLEvents");
       evt.initEvent(event, true, true ); // event type,bubbling,cancelable
       return !element.dispatchEvent(evt);
   }
   else
    {
       // dispatch for IE
       var evt = document.createEventObject();
       return element.fireEvent('on'+event,evt)
   }
}




function PassReminderShow()
{

	VirtKeyBoard('pr_email_address');
	obj=document.getElementById('PassRemind_Btn');
	fireEvent(obj,'click');

}


function InstreamMainMenu (el,obj_id)
{
    var ul = null;
    var input_val=0;
    var input_field;

	//alert('el='+el);

    var childs = el.childNodes;
	//alert('childs='+childs.length);
    for (var i = 0; i<childs.length; i++)
    {
        if (childs[i].tagName == 'UL')
        {
            ul = childs[i];
        }
    }
    	if (ul)
	{

	inp_field='menudiv'+obj_id;

	 if (ul.style.display == '')
 	   {
            input_field1='inp'+obj_id;
	    input_val=document.getElementById(input_field1).value;
             if (input_val==2)
             {
		document.getElementById(inp_field).style.display='none';
		//alert('roll up')
		//alert('id='+obj_id+'  val='+input_val);

		childs = ul.getElementsByTagName('UL');
            	for (var i = 0; i<childs.length; i++)
	    	{
                	childs[i].style.display = 'none';
            	}
            	ul.style.display = 'none';
	     }
             else
	     {
		//alert('redirect');
	     }

            }
	  else
	    {


		document.getElementById(inp_field).style.display='';
		input_field1='inp'+obj_id;
		document.getElementById(input_field1).value='1';
		//alert('id='+obj_id+'  val='+input_val+'  inp_field='+inp_field);
		ul.style.display = '';

            }
    	}

	if (el.parentNode.id && el.parentNode.parentNode.tagName != 'UL' && el.parentNode.parentNode.tagName != 'LI')
	{
          childs = el.parentNode.childNodes;
        	for (var i = 0; i<childs.length; i++)
		{
            		if (childs[i].tagName == 'LI' && childs[i] != el)
			{
                		var uls = childs[i].getElementsByTagName('UL');
                		for (var j=0; j<uls.length; j++)
				{
                    			uls[j].style.display = 'none';
                		}
            		}

       	 	}

    	}


}






function substr_count( haystack, needle, offset, length )
     {
   		var pos = 0, cnt = 0;
   		if(isNaN(offset)) offset = 0;
   		if(isNaN(length)) length = 0;
   		offset--;
   		while( (offset = haystack.indexOf(needle, offset+1)) != -1 )
   		{
       		if(length > 0 && (offset+needle.length) > length)
       		{
        	   	return false;
       		}
       		else
       		{
           		cnt++;
       		}
   		}

   		return cnt;
	}



function fireEvent(element,event)
	  {
		//alert('fire');
		if (document.createEventObject)
		{
			// dispatch for IE
			//alert('fire');
			var evt = document.createEventObject();
			return element.fireEvent('on'+event,evt);

		}
		else
		{
			// dispatch for firefox + others
			//alert('fire2');
			var evt = document.createEvent("HTMLEvents");
			evt.initEvent(event, true, true ); // event type,bubbling,cancelable
			return !element.dispatchEvent(evt);
		}
       }




function CheckArray(el)
{
	var Arr=new Array();
	var i=0;
	Arr[i]=el;
	while(el.lastIndexOf('_')!=-1)
	{
		i++;
		el=el.substr(0,el.lastIndexOf('_'));
		Arr[i]=el;
	}
	return Arr;

}



function products_from_cat_sel(val)
{

  if(val>0)
  {
	   document.getElementById('prod_sel_input').value=val;
           document.forms["prod_sel_form"].submit();
  }
}


function search_sel_change(obj_val)
{
	//alert(obj_val);

	switch (obj_val)
	{
		case '1':
			outp_val='Введите название товара'
			break

		case '2':
			outp_val='Введите код товара'
			break

		case '3':
			outp_val='Введите P/N товара'
			break
 		default:

			outp_val='Введите название товара'
	}

	 document.getElementById('search_inp').value=outp_val;
	 document.getElementById('search_in_description').value=obj_val;
	//alert(outp_val);

}

function submit_prod_data(j,action,mod_name)
{


	var form_object='cart_quantity'+mod_name+j;
	var action_object='action'+mod_name+j;
	//alert('obj='+j+' act='+action+'   form='+form_object);

	document.getElementById(action_object).value=action;

	document.forms[form_object].submit();
	//alert('done');

}


function WishListAction(action)
{
	document.getElementById('action').value=action;
	document.forms['wishlist_form'].submit();
}


function HideFloatWin(obj_id)
{

	var obj=document.getElementById('obj_id');
	alert(obj);

}


function FloatWinClose(obj_id)
{


	obj=document.getElementById(obj_id);
	obj.style.display='none';

}

function FloatWinShow(obj_id)
{


	obj=document.getElementById(obj_id);
	obj.style.display='';

}


function LogOutWinClose()
{
	obj=document.getElementById('Window4');
	obj.style.display='none';
}

function PassRemindClose()
{
	obj=document.getElementById('Window1');
	obj.style.display='none';
}

function UpdateDsicountBoxData(prod_id,prod_name)
{
	//alert('prod_id='+prod_id+' prod_name='+prod_name);
}



function PageSelChange(flag)
{

	//alert(document.getElementById('page_sel').selectedIndex);
	var obj=document.getElementById('page_sel');
	var ind=parseInt(obj.selectedIndex);
	var max_ind=parseInt(obj.options.length);
	if(flag=='add')
	{
		ind++;
		//alert('max='+max_ind+'  ind='+ind);
		if(ind<max_ind)
		{
			//alert('INN');
			obj.selectedIndex=ind;
			fireEvent(obj,'change');
		}
  	}
	else
	{
		ind--;
		//alert('max='+max_ind+'  ind='+ind);
		if(ind>=0)
		{
			//alert('INN');
			obj.selectedIndex=ind;
			fireEvent(obj,'change');
		}

	}


}

function CartCurrChanged()
{
	obj=document.getElementById('windowOpen');
			fireEvent(obj,'click');
}

function CMPAutoShow(url)
{
	//obj=document.getElementById('OrderCompare_Btn2');
	//		fireEvent(obj,'click');
	//alert(url);
	CloseWindows(); FillCMPWindow(url);
}

function PassRemindClick()
{
	obj=document.getElementById('PassRemind_Btn');
			fireEvent(obj,'click');
}


function isNumberKeyOnly(evt)
{

	var charCode = (evt.which) ? evt.which : event.keyCode

        if (charCode > 31 && (charCode < 48 || charCode > 57 ) )
	            return false;

	return true;
}


function tel_data(myfield, e, dec)
{
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);
 //alert(key);
  // control keys
  if ((key==null) ||  (key==43) ||  (key==8) ||  (key==0))
    return true;
  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;

  else
    return false;
}
