var baseConferenceUrl = "/Pages/VideoConferenceCall.aspx";
var conferenceUrl = baseConferenceUrl + "?id=";
var declinedConferenceUrl = "/Pages/VideoConferenceDeclined.aspx?id="

/* Search */
$(document).ready(function() {
	$("#search input.searchBtn").bind("click",searchByString);
	$("#search input.searchString").bind("focus",function() { if ($(this).val() == 'поиск по товарам') $(this).val('');});
	$("#search input.searchString").bind("blur",function() { if ($(this).val() == "") $(this).val('поиск по товарам');});
	$("input.orderBySaver").bind("blur",saveOrderBy);
});

function saveOrderBy() {
	var inp = $(this);
	var categoryID = inp.attr("categoryID");
	var ord = inp.val();
	if (!parseInt(ord) && /\\s/.test(ord)) {
		alert("Введите число");
		return false;
	}
	$.post(
		"/products/editorderby",
		{"categoryID" : categoryID,"ord" : ord},
		function(resp) {
			if (resp != '') {
				alert(resp);return;
			}
			inp.parent().append("<div class='saveMsg' style='position:absolute;width:100px;height:50px;padding:5px;border:1px solid #bababa;background:#fff;'>сохранен!</div>");
			setTimeout(function() {
				$("div.saveMsg").remove();
			},1000);
		}
	);
}

function searchByString() {
	var str = $("#search input.searchString").val();
	if (!str || str == 'поиск по товарам') {
		return false;
	}
	var hr = "/s/"+str;
	location.href = hr;
}

function GetX(input) {
  var img=input;
  var X=0;
  do {X+=img.offsetLeft;} while (img=img.offsetParent);
  return X;
}
function GetY(input) {
  var img=input;
  var Y=0;
  do {Y+=img.offsetTop} while ((img=img.offsetParent)!=null);
  return(Y);
}

function getAncestorByAttribute(elt,attName,attValue,level)
{
	level = level || 1;
	var i = 0;
	while(elt && i< level)
	{
		elt = elt.parentNode;
		if (elt && elt.getAttribute(attName) == attValue)
			i++;
	}
	return elt;
}

function getAncestorByTagName(elt,tagName,level)
{
	level = level || 1;
	var i = 0;
	while(elt && i< level)
	{
		elt = elt.parentNode;
		if (elt && elt.nodeName == tagName)
			i++;
	}
	return elt;
}
function nextSiblingByTagName(elt,tagName,level){
    level = level || 1;
    var i = 0;
    while (elt && i < level){
        elt = elt.nextSibling;
        if (elt && elt.nodeName == tagName)
            i++;
    }
    return elt;
}
function previousSiblingByTagName(elt,tagName,level){
    level = level || 1;
    var i = 0;
    while (elt && i < level){
        elt = elt.previousSibling;
        if (elt && elt.nodeName == tagName)
            i++;
    }
    return elt;
}
var escChars = null
function HtmlEncode(str){
	var seqs = {"&":"amp",">":"gt","<":"lt",'"':'quot'}
	if (!escChars){
		qq = [];
		for(var i in seqs){
			qq[qq.length] = i;
		}
		escChars = new RegExp("[" + qq.join("") + "]","g");
	}
	return str.replace(escChars,function($0){ 
		return seqs[$0] ? "&" + seqs[$0] + ";" : $0;
	});
}

function HtmlDecode(str){
	var seqs = {"amp":"&","gt":">","lt":"<",'quot':'"'}
	str = str.replace(/&(\w+);/g,function($0,$1){return seqs[$1] || $0;})
	str = str.replace(/&#(\d+);/g,function($0,$1){ return String.fromCharCode(Number($1));});
	return str;
}

function openModalWindow(url,title,width,height){
   title=title || "&nbsp;";
   width = width || 480;
   height=height || 600;
   var h = document.getElementById("vcPopupHeader");
   var popup = h.parentNode;
   h.innerHTML = title;
   popup.style.display="block";
   ifr = document.getElementById("vcPopupFrame");
   if (window.opera || document.all){
    ifr.contentWindow.document.body.innerHTML = "";
   } else if (ifr) {
    ifr.parentNode.removeChild(ifr);
    ifr = null;
  }
   if (!ifr){
    ifr = document.createElement("IFRAME");
    popup.appendChild(ifr);    
    ifr.id = "vcPopupFrame";
    ifr.border=0;
    ifr.frameBorder=0;
    ifr.setAttribute("frameBorder","0");
    ifr.style.border="0px";
    ifr.style.padding="0px";
    ifr.style.margin = "0px";
  }
   ifr.src = url;   
   $find('vcPopupEx').show();
   ifr.width=width;
   ifr.height = height;
   popup.style.width = ifr.offsetWidth + "px";
   popup.style.height = (ifr.offsetHeight + h.offsetHeight)+ "px";
   $find('vcPopupEx').show();
}

function closeModalWindow(){
   ifr = document.getElementById("vcPopupFrame"); 
   if (!ifr)
        return;   
   $find('vcPopupEx').hide();
}
function closeThisModalWindow(){
    if (window.parent && window.parent.closeModalWindow){
        window.parent.closeModalWindow();
    }
}
function debug(){
    var txt = [];
    for(var i = 0;i < arguments.length ;i++){
        txt[i] = (arguments[i] == null ? "" : arguments[i]).toString();
    }
    if (document.body){
        div = document.createElement("DIV");
        div.innerHTML = txt.join(" : ");
        document.body.appendChild(div);
    }
}

function adjustIframeContainer(){
    var h = document.getElementById("vcPopupHeader");
    var popup = h.parentNode;
    var ifrelt = document.getElementById("vcPopupFrame");
    ifr = getDialogDocument();
    ifrelt.style.width = ifr.body.offsetWidth + "px";
    ifrelt.style.height = (ifr.body.offsetHeight + (window.opera ?50 : 0)) + "px";
    popup.style.width = ifrelt.offsetWidth + "px";
    popup.style.height = (ifrelt.offsetHeight + h.offsetHeight) + "px";
    $find('vcPopupEx').show();
    ifrelt.border=0;
    ifrelt.frameBorder=0;
    ifrelt.setAttribute("frameBorder","0");
}

function postDialogSizeAdjustment(){
    Sys.Application.add_load(
    function(){
        if (window.parent && window.parent.adjustIframeContainer){
            window.parent.adjustIframeContainer();
        }
    }
    );
}

function refreshCode(elt){
   var rnd = Math.random();
   elt.src = "/Pages/ConfirmationCodeImage.aspx?" + rnd;
}

function refreshImageCode(){
    var img = document.getElementById("confirmationAntibotCode");
    refreshCode(img);
}

function changeClassName(cclass,oldclass,newclass){
	if (typeof(newclass) == "string"){
		newclass = [newclass];
	} else if (!newclass.length){
		newclass = [];
	}
	if (typeof(oldclass) == "string"){
		oldclass = [oldclass];
	} else if (!oldclass.length){
		oldclass = [];
	}
	for(var i = 0; i < oldclass.length;i++){
		cclass = cclass.replace(new RegExp(oldclass[i],"g"),"");
	}
	cclass += " "  + newclass.join(" ");
	ccalss = cclass.replace(/\s+/," ").replace(/^ /,"").replace(/ $/,"");
	return cclass;
}

