﻿// ----------------  Common ------------------------

// register event for ie and ff
function addEvent(oElement, sEvent, func) {
    if (oElement.attachEvent) {
        oElement.attachEvent(sEvent, func);
    }
    else {
        sEvent = sEvent.substring(2, sEvent.length);
        oElement.addEventListener(sEvent, func, false);
    }
}

function createDelegate(instance, method) {
    return function() {
        return method.apply(instance, arguments);
    }
}

function redirectTo(url) {
    var objF = document.createElement("form");
    objF.method = "POST";
    objF.action = url;
    objF.target = "_self";
    objF.style.display = "none";
    document.body.appendChild(objF);

    objF.submit();
}

// ---------------- Military Shop Menu ------------------
function MSMenu(containerId, selectedMenuIndex) {

    this.menuContainer = document.getElementById(containerId);
    this.selectedMenuIndex = selectedMenuIndex | 0;
    this.menuContexts = [];
    this.menuItems = [
                        { Description: "首页",      Url: "Default.aspx" },
                        { Description: "公司介绍",  Url: "CompanyIntro.aspx" },
                        { Description: "产品展示",  Url: "Categories.aspx" },
                        { Description: "联系我们",  Url: "ContactUs.aspx" },
                        { Description: "用户留言",  Url: "GuestBookPage.aspx" }                        
                     ];

    // Create menu items
    for (var i = 0; i < this.menuItems.length; i++) {

        var curItem = this.menuItems[i];
    
        // Create MenuItem Holder
        var objHolder = document.createElement("div");
        objHolder.className = "menuItemHolder";
        this.menuContainer.appendChild(objHolder);

        // Create Link
        var objLink = document.createElement("a");
        objLink.href = curItem.Url;
        objLink.title = curItem.Description;
        objLink.hideFocus = true;
        objHolder.appendChild(objLink);

        // Create Menu Button Rect Div
        var objButton = document.createElement("div");
        objButton.className = "menuButton";
        objLink.appendChild(objButton);

        // Create Text Div
        var objTextDiv = document.createElement("div");
        objTextDiv.className = "menuText";
        objButton.appendChild(objTextDiv);

        if (i != this.selectedMenuIndex) {
            this.SetMenuStatus(i, objHolder, objTextDiv, false);
            addEvent(objButton, "onmouseover", this.CreateMouseEventFun(this.SetMenuStatus ,i, objHolder, objTextDiv, true));
            addEvent(objButton, "onmouseout", this.CreateMouseEventFun(this.SetMenuStatus, i, objHolder, objTextDiv, false));
        }
        else {
            this.SetMenuStatus(i, objHolder, objTextDiv, true);
        }
    }
}

MSMenu.prototype = {
    SetMenuStatus: function(menuIndex, menuHolder, menuText, isRollOver) {
        var holderHeight = 51;
        var textHeight = 20;

        if (isRollOver) {
            if (typeof (menuHolder.style.backgroundPositionY) != "undefined") {
                menuHolder.style.backgroundPositionY = (-1 * holderHeight) + "px";
                menuText.style.backgroundPositionY = -1 * (menuIndex * 2 + 1) * textHeight + "px";
            }
            else {
                menuHolder.style.backgroundPosition = "0px " + (-1 * holderHeight) + "px";
                menuText.style.backgroundPosition = "0px " + -1 * (menuIndex * 2 + 1) * textHeight + "px";
            }
        }
        else {
            if (typeof (menuHolder.style.backgroundPositionY) != "undefined") {
                menuHolder.style.backgroundPositionY = "0px";
                menuText.style.backgroundPositionY = -1 * (menuIndex * 2 + 0) * textHeight + "px";
            }
            else {
                menuHolder.style.backgroundPosition = "0px 0px";
                menuText.style.backgroundPosition = "0px " +  -1 * (menuIndex * 2 + 0) * textHeight + "px";
            }
        }
    },

    CreateMouseEventFun: function(handler, menuIndex, menuHolder, menuText, isRollOver) {
        return function() {
            handler(menuIndex, menuHolder, menuText, isRollOver);
        }
    }

}

function CheckDate(Inobj) {
    Inobj.value = trim(Inobj.value);
    if (Inobj.value != "") {
        var reg = /^\d{8}$/;
        if (Inobj.value.match(reg) != null) {
            Inobj.value = Inobj.value.substring(0, 4) + "-" + Inobj.value.substring(4, 6) + "-" + Inobj.value.substring(6, 8);
        }
        reg = /^([1-2]\d{3})-(([1][0-2])|(0?[1-9]))-(([3][0-1])|([1-2][0-9])|(0?[1-9]))$/;
        if (Inobj.value.match(reg) == null) {
            alert("输入日期的格式不正确！");
            Inobj.value = "";
            Inobj.focus();
        }
    }
}

//移除字符串中的空格
function trim(s) {
    var s2="";
    for(i=0;i<s.length;i++) {
	    if(s.charAt(i)!=" ") s2=s2+s.charAt(i);
    }
    return s2;
}
