var excludeList = new Array(
'pager',
'DataGridRPP*',
'cur_*',
'status_*',
'regions*',
'brandId',
'category',
'mb',
'cpu',
'ram',
'q_ram',
'hda',
'gpu',
'kb',
'mou',
'dvdw',
'cdw',
'mon',
'sc',
'spk',
'cs',
'fdd',
'mod',
'prnt',
'tvc',
'os',
'other',
'opt1',
'opt2',
'opt3',
'extra3',
'SearchBrands'
);

function IsDropDownExcluded(dropDownId) {
	if(dropDownId == null || dropDownId == '') {
		return true;
	}
	for(var i=0;i<excludeList.length;i++) {
		exclude = excludeList[i];
		if(dropDownId == exclude) {
			return true;
		}
		if (exclude.charAt(exclude.length-1) == '*') {
			excLikeStr = exclude.substring(0,exclude.length-1);
			if(dropDownId.search(excLikeStr) >= 0) {
				return true;
			}
		}
	}
	return false;
}

function ChangeDropDownStyle(elementId) {
	if(!IsDropDownExcluded(elementId)) {
    	//jQuery('#'+elementId).selectbox();
    	jQuery('#'+elementId).select_skin();
    } else {
    //	console.log('EXCLUDED');
    }
}

function ClosePageOver() {
    var ie = (document.all) ? 1 : 0;
    if (ie) {
        document.getElementById("PageOver").style.filter = "alpha(opacity=0)";
        document.getElementById("PageOver").style.display = "none";
    } else {
        document.getElementById("PageOver").style.opacity = 0;
    }
    document.getElementById("PageOver").style.zIndex = -5;
}

function ClosePageOverEffect() {
    var ie = (document.all) ? 1 : 0;
    document.getElementById("PageOver").style.zIndex = 1008;
    var eff = new Effect.Opacity('PageOver', {
        to: 0.0,
        duration: 0.3,
        transition: Effect.Transitions.linear,
        afterFinish: function () {
            document.getElementById("PageOver").style.zIndex = -5;
            document.getElementById("PageOver").style.visibility = "visible";
            if (ie) {
                document.getElementById("PageOver").style.display = "none";
            }
            document.getElementById("PageOver").style.height = getPageSize()[1] + "px";
            document.getElementById("PageOver").style.width = "100%";
            dropdowns = document.getElementsByTagName("select");
            for (i = 0; i < dropdowns.length; i++) {
                elem = dropdowns[i];
                if(IsDropDownExcluded(elem.getAttribute('id'))) {
                	Element.extend(elem);
                	elem.show();
                }
            }
        }
    });
    if (ie) {
        setTimeout("function() { " + "document.getElementById(\"PageOver\").style.display = \"none\";" + "document.getElementById(\"PageOver\").style.zIndex = -5;" + "document.getElementById(\"PageOver\").style.visibility = \"visible\";" + "}", 300);
    }
}

function OpenDialog(el) {
    var ie = (document.all) ? 1 : 0;
    dropdowns = document.getElementsByTagName("select");
    for (i = 0; i < dropdowns.length; i++) {
        elem = dropdowns[i];
        if(IsDropDownExcluded(elem.getAttribute('id'))) {
        	Element.extend(elem);
        	elem.hide();
        }
    }
    document.getElementById("PageOver").style.opacity = 0;
    if (ie) {
        document.getElementById("PageOver").style.filter = "alpha(opacity=0)";
        document.getElementById("PageOver").style.display = "block";
    }
    document.getElementById("PageOver").style.visibility = "visible";
    document.getElementById("PageOver").style.height = getPageSize()[1] + "px";
    document.getElementById("PageOver").style.width = "100%";
    document.getElementById("PageOver").style.zIndex = 1008;
    var eff = new Effect.Opacity('PageOver', {
        to: 0.8,
        duration: 0.3,
        transition: Effect.Transitions.linear,
        afterFinish: function (obj) {
            document.getElementById("PageOver").style.zIndex = 1008;
            var elem = document.getElementById(el);
            Element.extend(elem);
            var hg = getWindowHeight();
            var wd = getWindowWidth();
            elem.style.position = "absolute";
            elem.style.zIndex = 1009;
            elem.style.display = "block";
            scrpos = getScrollXY();
            elem.style.left = String((wd / 2) - (parseInt(elem.getWidth()) / 2) + scrpos[0]) + "px";
            elem.style.top = String((hg / 2) - (parseInt(elem.getHeight()) / 2) + scrpos[1]) + "px";
            elem.style.visibility = "visible";
            opendropdowns = $(el).getElementsByTagName("select");
		    for (i = 0; i < opendropdowns.length; i++) {
		        dropDown = opendropdowns[i];
		        ChangeDropDownStyle(dropDown.getAttribute('id'));
		        //elem.show();
		    }
		    if(elem.tagName != 'table' || !ie) {
				jQuery('#'+el).find('#closeButton').remove();
				var img = jQuery("<img id='closeButton' onclick='CloseDialog(\""+el+"\")' style='display: inline; position: absolute; right: -11px; top: -11px; z-index: 80; cursor: pointer;' src='images/formdialog/closebox.png'/>");
				jQuery('#'+el).append(img);
		    }
        }
    });
}

function CloseDialog(el) {
    var elem = document.getElementById(el);
    elem.style.display = "none";
    elem.style.visibility = "hidden";
    elem.style.zIndex = -2;
    ClosePageOverEffect();
}

function getWindowHeight() {
    var windowHeight = 0;
    if (typeof(window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        } else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function getWindowWidth() {
    var windowWidth = 0;
    if (typeof(window.innerWidth) == 'number') {
        windowWidth = window.innerWidth;
    } else {
        if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            if (document.body && document.body.clientWidth) {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    return windowWidth;
}

function getScrollXY() {
    var scrOfX = 0,
        scrOfY = 0;
    if (typeof(window.pageYOffset) == 'number') {
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function ShowMenu(el, target) {
    Element.extend(el);
    if (el.up().style.position != "absolute") {
        pos = Position.cumulativeOffset(el);
        tx = (pos[0] + el.getWidth() - 0) + "px";
        ty = pos[1] + "px";
    } else {
        tx = (parseInt(el.up().style.width) - 2) + 'px';
        hh = 0;
        hx = 0;
        while (el.previous(".MenuItem", hx) != undefined) {
            hh += 20
            hx += 1;
        }
        ty = hh + "px";
    }
    $(target).style.left = tx;
    $(target).style.top = ty;
    $(target).show();
}

function ShowDTMenu(target) {
    var myArr = target.split("_");
    re = /\d+/g
    myArr.each(function (s) {
        if (s.match(re)) {
            no = s.match(re);
        }
    });
    str = "";
    for (i = 0; i < myArr.length - 1; i++) {
        str = str + myArr[i] + "_";
    }
    img = "img_" + str.substr(0, str.lastIndexOf('_')) + "_" + no
    if ($(target).visible()) {
        if (src.indexOf("down")) {
            src = src.replace("down", "left");
        }
        $(img).src = src
        $(target).hide();
    } else if (!$(target).visible()) {
        $(target).show();
        src = $(img).src;
        if (src.indexOf("left")) {
            src = src.replace("left", "down");
        }
        $(img).src = src;
    }
}

function ShowMenu2(target) {
    $(target).show();
}

function HideMenu(target) {
    $(target).hide();
}

function ShowTooltip(event, source) {
    ttSource = $(source);
    if ($("IdeaToolTip") != null && $("IdeaToolTip").visible) {
        tt = $("IdeaToolTip");
        css = tt.classNames();
        for (i = 0; i < css.length; i++)
        tt.removeClassName(css[i]);
    } else {
        tt = document.createElement("div");
        Element.extend(tt);
        tt.id = "IdeaToolTip";
        tt.hide();
        document.body.appendChild(tt);
        Position.absolutize(tt);
    }
    tt.addClassName(ttSource.className);
    tt.update(ttSource.innerHTML);
    tt.setStyle({
        'width': ttSource.style.width,
        'height': ttSource.style.height,
        'display': 'block',
        'z-index': 1050
    });
    tt.style.left = (Event.pointerX(event) + 15) + "px";
    tt.style.top = (Event.pointerY(event)) + "px";
    tt.makePositioned();
    tt.show();
}

function HideTooltip() {
    $('IdeaToolTip').hide();
}

function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}
