function bookmarkURL(title)
{
    if (title == '')
        title = document.title;

    try{
        window.external.AddFavorite(document.location.href,title);
    }
    catch (e)
    {
        try{
            window.sidebar.addPanel(title, document.location.href,"");
        }
        catch(e1){}
    }
}
 /*
function $(id)
{
    return document.getElementById(id);
}
*/

function sendJAction()
{
    //arguments[0] - action
    //arguments[1] - module, if empty - current module
    //other arguments - parameters, ex. sendJAction('update', 'user', 'id', 5);
    //         sendJAction('update', '', 'id', 5, 'pid', 10);
    el = document.getElementById('xForm');
    if (el)
    {
        document.body.removeChild(el);
    }

    var xForm = document.createElement('FORM');
    var elemAction = document.createElement('INPUT');

    xForm.id = 'xForm';

    elemAction.setAttribute('type', 'hidden');
    elemAction.setAttribute('name', 'action');
    elemAction.setAttribute('value', arguments[0]);
    xForm.appendChild(elemAction);

    var Form = document.getElementById("mainform");
    var elemModule = document.createElement('INPUT');
    elemModule.setAttribute('name', 'module');
    elemModule.setAttribute('type', 'hidden');
    if (arguments[1])
        elemModule.setAttribute('value', arguments[1]);
    else
        elemModule.setAttribute('value', Form.module.value);
    xForm.appendChild(elemModule);


    for (var i=2; i<arguments.length; i+=2)
    {
        var elem = document.createElement('INPUT');
        elem.setAttribute('name', arguments[i]);
        elem.setAttribute('type', 'hidden');
        try{elem.setAttribute('value', arguments[i+1]); }catch(e){};
        xForm.appendChild(elem);
    }
    xForm.setAttribute('method', 'post');
    xForm.target = '_self';

    document.body.appendChild(xForm);

    xForm.submit();
}

function Trim(str)
{
var re1 = /^\s*/;
var re2 = /\s*$/;
return str.replace(re1, "").replace(re2, "");
}

function checkForm2()
{
    var Form;

    if (arguments[0])
    {
        if (typeof arguments[0] == 'object')
        {
            Form = arguments[0];
        }
    }
    if (!Form)
        Form = document.getElementById(arguments[0]?arguments[0]:'mainform');

    if (Form != null)
    {

        var tElement = Form.getElementsByTagName("INPUT");
        for (var i=0; i<tElement.length; i++)
        {
            if (tElement[i].type == "text" || tElement[i].type == "password")
            {
            	tElement[i].value=Trim(tElement[i].value);
                if ((tElement[i].getAttribute('req') != null) && (tElement[i].getAttribute('req') != '') && (tElement[i].value == '' && tElement[i].getAttribute('maynull') != '1'))
                {
                    alert(tElement[i].getAttribute('req'));
                    tElement[i].focus();
                    return false;
                }
                if ((tElement[i].getAttribute('req') != null) && (tElement[i].getAttribute('req') != '') && (tElement[i].type == 'checkbox') && !tElement[i].checked)
                {
                    alert(tElement[i].getAttribute('req'));


                    tElement[i].focus();
                    return false;
                }
                if ((tElement[i].getAttribute('equal') != null) && (tElement[i].getAttribute('equal') != ''))
                {
                    var t = tElement[i].getAttribute('equal');
                    eval("var tt = Form." + t);
                    if ((tt != null) && ((tt.value != tElement[i].value) && !(tElement[i].getAttribute('maynull') == '1' && tElement[i].value=='' && tt.value=='')))
                    {
                        alert(tElement[i].getAttribute('equaltext'));
                        tt.focus();
                        return false;
                    }
                }

                if ((tElement[i].getAttribute('is_email') != null) && (tElement[i].getAttribute('is_email') != ''))
                {
                    var re = /^[0-9a-zA-Z&]([-_.]?[0-9a-zA-Z&])*@[0-9a-zA-Z&]([-.]?[0-9a-zA-Z&])*\.[a-zA-Z]{2,4}?$/;//[a-wyz][a-z](g|l|m|pa|t|u|v|fo)?
                    if (!tElement[i].value.match(re)) {
                        alert(tElement[i].getAttribute('is_email'));
                        tElement[i].focus();
                        return false;
                    }
                }

                if ((tElement[i].getAttribute('minlength') != null) && (tElement[i].getAttribute('minlength') != '') && (tElement[i].value.length < tElement[i].getAttribute('minlength'))&& !(tElement[i].getAttribute('maynull') == '1' && tElement[i].value=='' && tt.value==''))
                {

                    alert(tElement[i].getAttribute('mintext'));
                    tElement[i].focus();
                    return false;
                }
            }
            else
            {
                if ((tElement[i].getAttribute('req') != null))
                {
                    var selected = false;
                    var all_group = document.getElementsByName(tElement[i].name);
                    for (var j=0; j<all_group.length; j++)
                        if (all_group[j].checked)
                            selected = true;

                    if (!selected)
                    {
                        var v;
                        if (tElement[i].required && tElement != 'undefined')
                            v = 'Required field';
                        else
                            v = tElement[i].getAttribute('req');
                        alert(v);
                        tElement[i].focus();
                        return false;
                    }
                }
            }
        }

        tElement = Form.getElementsByTagName("SELECT");
        for (var i=0; i<tElement.length; i++)
        {
            if ((tElement[i].getAttribute('req') != null) && (tElement[i].getAttribute('req') != '') && (tElement[i].value == ''))
            {
                alert(tElement[i].getAttribute('req'));
                tElement[i].focus();
                return false;
            }
        }


        tElement = Form.getElementsByTagName("TEXTAREA");
        for (var i=0; i<tElement.length; i++)
        {
            if ((tElement[i].getAttribute('req') != null) && (tElement[i].getAttribute('req') != '') && (tElement[i].value == ''))
            {
                alert(tElement[i].getAttribute('req'));
                tElement[i].focus();
                return false;
            }
        }
        return true;
    }
    else
    {
        return false;
    }

}


function getBounds(tElement)
{
  var left = tElement.offsetLeft,
        top = tElement.offsetTop,
        parent;
  for (parent = tElement.offsetParent; parent; parent = parent.offsetParent)
  {
    left += parent.offsetLeft;
    top += parent.offsetTop;
  }
  return {left: left, top: top, width: tElement.offsetWidth, height: tElement.offsetHeight};
}

function setPosition()
{
    var scrollTop = document.body.scrollTop,
        tElement = arguments[0],
        tBounds = arguments[1],
        w = tElement.clientWidth,
        h = tElement.clientHeight,
        winW = document.body.clientWidth,
        winH = document.body.clientHeight,
        vPX = 0,
        tHeightNotSet = true;

    if ((w + tBounds.left > winW) && (winW - w + tBounds.left > w))
        tElement.style.left = tBounds.left - w + tBounds.width;
    else
        tElement.style.left = tBounds.left;

    if (h + tBounds.top > winH + scrollTop)
    {
        if (h - tBounds.top > 0)
            if (h + tBounds.top - winH - scrollTop > h - tBounds.top)
                tHeightNotSet = false;
    }

    if ((tBounds.top + h > winH + scrollTop) && tHeightNotSet)
        if (tBounds.top - h - scrollTop > 0)
            tHeightNotSet = false;

    if ((winH - tBounds.top + scrollTop - h - tBounds.height < 0) && (tBounds.top - scrollTop - h > winH - tBounds.top + scrollTop - h - tBounds.height))
        tHeightNotSet = false;

    if (tHeightNotSet)
        tElement.style.top = tBounds.top + tBounds.height + vPX
    else
        tElement.style.top = tBounds.top - h - vPX;
}

 	var latest_vertical_slide_number = null;

 	function hideLatestSlide(number) {
 		if(latest_vertical_slide_number != null) {
 			if(latest_vertical_slide_number != number) {
 	 			HideVerticalSlide(latest_vertical_slide_number);
 	 			latest_vertical_slide_number = null;
 			}
 		}
 	}

 	function ToggleVerticalSlide(number){
 		hideLatestSlide(number);
 		var myVerticalSlide = new Fx.Slide('vertical_slide_'+number);
 		myVerticalSlide.toggle();
 		latest_vertical_slide_number = number;
 	}

 	function ShowVerticalSlide(number){
 		hideLatestSlide(number);
 		var myVerticalSlide = new Fx.Slide('vertical_slide_'+number);
 		myVerticalSlide.show();
 		latest_vertical_slide_number = number;
 	}
 	function HideVerticalSlide(number){
 		var myVerticalSlide = new Fx.Slide('vertical_slide_'+number);
 		myVerticalSlide.hide();
 		if(latest_vertical_slide_number == number) {
 			latest_vertical_slide_number = null;
 		}
 	}

	function sub_product(id){    	var current_count = parseInt($('count_product_' + id).value);
    	if(current_count>0){
			var count_product = current_count-1;
	        add_product_ajax(id, count_product);
        }else{        	/*alert('Нет заказа по этой позици');*/        }	}

    function add_product(id){
    	var current_count = parseInt($('count_product_' + id).value);
		/*var count_product = current_count+1;*/
    	var count_product = parseInt(current_count);
    	if( count_product < 1 )
    		count_product = 1;
    	
        add_product_ajax(id, count_product);    }

 	function add_product_ajax(id, count){
		var url = "/buy_product/";

		var req = new Request({
			method: 'post',
			url: url,
			data: {
				'id': id,
				'count': count
			},
			onComplete: function(response) {
				window.location="/order.html";
				calculate_price(response);
			}
		}).send();
 	}

 	function calculate_price(response){
 		var pResponse = new Array(); 		pResponse = eval("(" + response + ")");

 		$('count_product_' + pResponse['product_id']).value = pResponse['count_product'];

 		if($('full_product_price'))
 			$('full_product_price').set('text', pResponse['full_product_price']);

 		if($('full_cart_price'))
 			$('full_cart_price').set('text', pResponse['full_cart_price']);

 		if($('cart_price'))
 			$('cart_price').set('text', pResponse['full_cart_price']);

 		if($('item_cart_cost_' + pResponse['product_id']))
 			$('item_cart_cost_' + pResponse['product_id']).set('text', pResponse['product_type_price']);
 	}


	function sub_product_cart(id){
    	var current_count = parseInt($('count_product_' + id).value);
    	if(current_count>0){
			var count_product = current_count-1;
	        add_product_ajax_cart(id, count_product);
        }else{
        	/*alert('Count Product Is Nool');*/
        }
	}

    function add_product_cart(id){
    	var current_count = parseInt($('count_product_' + id).value);
		var count_product = current_count+1;
        add_product_ajax_cart(id, count_product);
    }

 	function add_product_ajax_cart(id, count){

		var url = "/buy_product_cart/";

		var req = new Request({
			method: 'post',
			url: url,
			data: {
				'id': id,
				'count': count
			},
			onComplete: function(response) { calculate_price_cart(response); }
		}).send();

 	}

 	function calculate_price_cart(response){
 		var pResponse = new Array();
 		pResponse = eval("(" + response + ")");

 		$('count_product_' + pResponse['product_id']).value = pResponse['count_product'];

 		if($('full_cart_price'))
 			$('full_cart_price').set('text', pResponse['full_cart_price']);

 		if($('cart_price'))
 			$('cart_price').set('text', pResponse['full_cart_price']);

	 	if($('type_product_price_' + pResponse['product_id']))
	 			$('type_product_price_' + pResponse['product_id']).set('text', pResponse['product_type_price']);

		if($('full_product_price_' + pResponse['pid']))
	 			$('full_product_price_' + pResponse['pid']).set('text', pResponse['full_product_price']);
 	}
 	
 	jQuery(document).ready(function() {
 	      /*jQuery('div.doms_navigator ul li a').css('text-indent', '-44px');*/
 	});
 	
 	var autocompleter;
 	document.addEvent('domready', function() {
 	  autocompleter = new Autocompleter.Request.JSON(
 	    'search_product', '/search_product/', {
 	      postVar: 'str',
 	      maxChoices: 20,
 	      additional_options: {},
 	      indicatorClass: 'autocompleter-loading'
 	    }
 	  );

 	  if(false) {
 	    autocompleter.addEvent('onSelection', function(el, selected, value, input) {
 	      $('candidate_step_two_place_of_birth_id').value = selected.id;
 	    });
 	  }
 	});
 	
 	function product_dropdown_menu(){
		jQuery("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
		jQuery("ul.subnav a").each(function(){
			jQuery(this).removeAttr('title');
		});
		jQuery("ul.subnav li:first").addClass('first');
		jQuery("ul.subnav li:last").addClass('last');
		jQuery("ul.topnav li span").click(function() { //When trigger is clicked...  
	  
	        //Following events are applied to the subnav itself (moving subnav up and down)  
			jQuery(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
	  
			jQuery(this).parent().hover(function() {  
	        }, function(){  
	        	jQuery(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
	        });  
	  
	        //Following events are applied to the trigger (Hover events for the trigger)  
	        }).hover(function() {  
	        	jQuery(this).addClass("subhover"); //On hover over, add class "subhover"  
	        }, function(){  //On Hover Out  
	        	jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	    });
 	}
 	
 	function submit_form(form_id){
 		jQuery('#'+form_id).submit();
 	}
