jQuery(document).ready(function() {

	jQuery("#clientLogin, #distributorLogin").click(function() {
		jQuery("#loginWindow").toggle();
		return false;
	});

	jQuery("#lnkAddToFavorites").click(function() {
		return AddToFavorites($(this));
//		addToFavorites("http://www.otclick.net", "OtClick.net");
	});


	jQuery(".modalClose").click(function() {
		jQuery('#loginWindow').hide();
	});

	var userAgent = navigator.userAgent.toLowerCase();

	jQuery.browser = {
		version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
		safari: /webkit/.test(userAgent),
		opera: /opera/.test(userAgent),
		msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
		mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
	};

});


function  stripHtml(v) {
	v = v + "";
	var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
	if (v) {
		v = v.replace(regexp, "");
		return (v && v !== '&nbsp;' && v !== '&#160;') ? v : "";
	} else {
		return v;
	}
}

function addToFavorites(url, title) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	} else if (window.external) { // IE Favorite
		window.external.AddFavorite(url, title);
	}
}

function AddToFavorites(link) {
	var url = window.location;
	var title = document.title;
	link.attr("href", url);
	link.attr("title", title);
	return bookmark(title, url);
}

function bookmark(title, url) {
	var os;
	if (document.layers)
		os = "n4";
	else if (document.getElementById && !document.all)
		os = "n6";
	else if (document.all)
		os = "ie";

	if (window.sidebar)// Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	else if (window.opera && window.print) {
		//		var elem = document.createElement('a');
		//		elem.setAttribute('href', url);
		//		elem.setAttribute('title', title);
		//		elem.setAttribute('rel', 'sidebar');
		//		elem.click();
		return true;

	}
	else if (os == "ie") {
		window.external.AddFavorite(url, title);
	} else {
		alert("Для добавления в закладки нажмите 'Ctrl+D'.")
	}
	return false;
}


        function setText(element, text)
        {
            if (typeof element.textContent != 'undefined')
            {
                element.textContent = text;
            }
            else if (typeof element.innerText != 'undefined')
            {
                element.innerText = text;
            }
        }



        function updateDialogTips(control, t) {
        	control.text(t).effect("highlight", {}, 1500);
        }

        function checkDialogLength(o, n, min, max, tipControl) {

        	if (o.val().length > max || o.val().length < min) {
        		o.addClass('ui-state-error');
        		updateDialogTips(tipControl, "Длина '" + n + "' должна быть между " + min + " и " + max + ".");
        		return false;
        	} else {
        		return true;
        	}

        }

        function checkDialogDdlLength(o, n, min, max, tipControl) {

        	if (o.val().length > max || o.val().length < min) {
        		o.addClass('ui-state-error');
        		updateDialogTips(tipControl, "Выберете значение для поля: '" + n + "'");
        		return false;
        	} else {
        		return true;
        	}

        }

        function checkDialogRegexp(o, regexp, n, tipControl) {

        	if (!(regexp.test(o.val()))) {
        		o.addClass('ui-state-error');
        		updateDialogTips(tipControl, n);
        		return false;
        	} else {
        		return true;
        	}
        }

        function ObjectInfo() {
        	this.propInfos = [];
        }

        function PropInfo(property, value) {
        	this.property = property;
        	this.value = value;
        }

        function FillDDProxy(url, ddl, paramArray, waitBlock, callback, errorFunction) {

        	if (!ddl)
        		return;

        	var pagePath = window.location.pathname;
        	//Call the page method
        	$.ajax({
        		type: "POST",
        		url: url,
        		contentType: "application/json; charset=utf-8",
        		data: $.toJSON(paramArray),
        		dataType: "json",
        		success: function(msg) {
        		fillDDSuccessFunction(msg.d, ddl, waitBlock, callback);
        		}

        	});
        	return false;
        }

        function fillDDSuccessFunction(values, ddl, waitBlock, callback) {
        	if (waitBlock)
        		waitBlock.hide();
        	//clear previously filled second drop down.
        	ddl.children().remove();
        	if (values != null && values.length > 0) {
        		for (var i = 0; i < values.length; i++) {
        			//Filling the second drop down on client side with returned json data
        			ddl.append('<option value=\"' + values[i].value + '">' + values[i].name + '</option>');
        		}
        	}
        	ddl.show();
        	//            ddl.show(1500, function() { });
        	if (callback)
        		callback(ddl);
        }


