﻿// Открытие окна
function openNewWin(url, name, scr, w, h) {
	var w1 = window.open(url, name, 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,directories=no,scrollbars=yes,width=' + w + ',height=' + h + '');
	w1.focus();
}

function getCookie(name) {

	var offset = document.cookie.indexOf(name + '=');
	if (offset < 0) {
		return null;
	}

	var end = document.cookie.indexOf(';', offset);
	if (end < 0) {
		end = document.cookie.length;
	}

	return unescape(document.cookie.substring(offset + name.length + 1, end));
}

function showCalendar(calendar) {

	$find(calendar).showPopup();
}

function pressButton(buttonName, event) {

	if (window.event != null ? window.event.keyCode == 13 : event.keyCode == 13) {
		var button = $get(buttonName);
		button.click();
		return false;
    }
    return true;
}

function showPopup(ctrl, parentCtrl, leftShift, topShift, absoluteLevel) 
{ 
    var leftCoord = 0; 
    var topCoord = 0;
    
    parentCtrl = $get(parentCtrl);
    var parent = parentCtrl;
    while (parent) 
    {
        leftCoord += parent.offsetLeft;
        topCoord += parent.offsetTop;
        parent = parent.offsetParent;
    }

    leftCoord += parseInt(leftShift);
    topCoord += parseInt(topShift);
     
    var popupDiv = $get(ctrl);

    popupDiv.style.position = 'absolute';
    popupDiv.style.zIndex = absoluteLevel || 1000;
    popupDiv.onmouseover = function() { if (parentCtrl.timeout) clearTimeout(parentCtrl.timeout) }
    popupDiv.onmouseout = function() { parentCtrl.timeout = setTimeout('hidePopup("' + ctrl + '")', 500) };
        
    popupDiv.style.left = leftCoord + 'px';
    popupDiv.style.top = topCoord + 'px';
    popupDiv.className = '' ;
    parentCtrl.onmouseout = function() { parentCtrl.timeout = setTimeout('hidePopup("' + ctrl + '")', 500) };
    parentCtrl.onmouseover = function() { if (parentCtrl.timeout) clearTimeout(parentCtrl.timeout) }
    
	return void (0);
}

function hidePopup(ctrl) 
{
    $get(ctrl).className = 'DisplayNone';
}

function clearValues(ctrl) {

	for (var i = 0; i < ctrl.childNodes.length; i++) {
		var current = ctrl.childNodes[i];
		if (current == undefined) {
			continue;
		}

		if (current.tagName != 'INPUT') {
			clearValues(current);
			continue;
		}

		if (current.type == 'checkbox') {
			current.checked = false;
		}
		else {
			current.value = '';
		}

		clearValues(current);
	}

	return false;
}


function CheckTextareaValue(control) {

    if (control.attributes["maxLength"]) {
    	var maxLength = parseInt(control.attributes["maxLength"].value);
    	if (control.value.length > maxLength) {
    		control.value = control.value.substring(0, maxLength);
    	}
    }
}

function pressLink(id, e) {

    var evt = e || window.event;
    var parentNode = evt.target || evt.srcElement;
    
    while (parentNode != null) {
    
        if (parentNode.id == id) {
        
            return;
        }
        
        parentNode = parentNode.parentNode;
    }

    document.location = $get(id).href;
}

function toggleObjectRemains(divId, linkId, remainsText, hideText) {

    var divCtrl = document.getElementById(divId);
    if (divCtrl.style.display == 'none') {
        divCtrl.style.display = '';
        document.getElementById(linkId).innerHTML = hideText;
    }
    else {
        divCtrl.style.display = 'none';
        document.getElementById(linkId).innerHTML = remainsText;
    }
}

function setDefaultPage(cntrl) {
    cntrl.style.behavior = "url(#default#homepage)";
    cntrl.setHomePage(this.location);
    setDefaultPageDisplay(cntrl);
    return false;
}

function setDefaultPageDisplay(cntrl) {
    document.body.style.behavior = "url(#default#homepage)";
    if (document.body.isHomePage("http://www.gde24.ru/") || document.body.isHomePage("http://gde24.ru/") 
        || document.body.isHomePage(this.location)){
        cntrl.style.display = "none";
    }
}

function clearWhitespace(cntrl) {
    var current = cntrl.firstChild;
    while (current != null) {
        var nextChild = current.nextSibling;
        if (current.nodeType == 3 && !/\S/.test(current.nodeValue)) 
        {
            cntrl.removeChild(current);
        }
        else if (current.nodeType == 1) {
            clearWhitespace(current);
        }
        current = nextChild;
    }
}

function addEvent(eventName, func, object) {
    if (!object)
        object = window;
        
    if (object.addEventListener)
        object.addEventListener(eventName, func, false);
    else if (object.attachEvent)
        object.attachEvent("on" + eventName, func);
    else
        object["on" + eventName] = func;
}



function resizeOnloadIframe(iframe) {
    if (!iframe) {
        return;
    }
    iframe.style.height = 0; 
    resizeIframe(iframe)
    iframe.style.height =  iframe.height; 
}

function resizeIframe(iframe) {
    if (!iframe) {
        return;
    }

    iframe.style.display = "block";
    if (iframe.contentDocument && iframe.contentDocument.body.scrollHeight) { //ns6 syntax
        var getFFVersion = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
        var FFextraHeight = parseFloat(getFFVersion) >= 0.1 ? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers
        iframe.height = iframe.contentDocument.body.scrollHeight + FFextraHeight;
        iframe.width = iframe.contentDocument.body.scrollWidth;
    }
    else if (iframe.Document && iframe.Document.body.scrollHeight) //ie5+ syntax
        iframe.height = iframe.Document.body.scrollHeight;
}

function preventEventDefault(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    if(window.event) {
        window.event.returnValue = false;
    }
    return false;
}

function stopEventPropagation(e) {
    if (e && e.stopPropagation) {
        e.stopPropagation();
    }
    if (window.event) {
        window.event.cancelBubble = true;
    }
}

var Q_change_wmode = {
    replace_embed: function(embd) {
        var rplc;
        if (embd.outerHTML) {
            var html = embd.outerHTML;
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                rplc = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='opaque'");
            else rplc = html.replace(/<embed\s/i, "<embed wmode='opaque' ");
            embd.insertAdjacentHTML('beforeBegin', rplc);
            embd.parentNode.removeChild(embd);
        } else {
            rplc = embd.cloneNode(true);
            if (!rplc.getAttribute('wmode') || rplc.getAttribute('wmode') == 'Window')
                rplc.setAttribute('wmode', 'opaque');
            embd.parentNode.replaceChild(rplc, embd);
        }
    }
    , replace_objct: function(objct) {
        var rplc;
        if (objct.outerHTML) {
            var html = objct.outerHTML;
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                rplc = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='opaque' />");
            else rplc = html.replace(/<\/object\>/i, "<param name='wmode' value='opaque' />\n</object>");
            var chldrn = objct.childNodes;
            for (n = 0; n < chldrn.length; n++) {
                if (chldrn[n].getAttribute('name').match(/flashvars/i)) {
                    rplc = html.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + chldrn[n].getAttribute('value') + "' />");
                }
            }
            objct.insertAdjacentHTML('beforeBegin', rplc);
            objct.parentNode.removeChild(objct);
        }
    }
    , start: function() {
        var embds = document.getElementsByTagName('embed');
        for (var i = 0; i < embds.length; i++) {
            Q_change_wmode.replace_embed(embds[i]);
        }
        var objcts = document.getElementsByTagName('object');
        for (var i = 0; i < objcts.length; i++) {
            Q_change_wmode.replace_objct(objcts[i]);
        }
    }
}
