﻿function Index(strName)
{
    this.name = strName;
    
    var _this = this;
    var CONST_SEARCH_MIN_CHAR = 2;
    
    var string = function()
    {
        var s = new Array();
        for(var i=0; i<arguments.length; i++)
        {
            s.push(arguments[i]);
        }
        return s.join("");
    }
    
    function getTargetString(source, target, sign)
    {
	    try {
	        source = string(sign, source);
		    var p = new RegExp(string("[\\", sign, "]+(", target, "=){1}[^\\", sign, "]+"), "g");
		    var result = source.toString().match(p);
		    if(result != null)
			    return (result.toString().replace(new RegExp(string("^[\\", sign, "]+(", target, "=){1}"), "g"), ""));
	    }catch(e){}
	    
	    return "";
    }    
    
    var $ = function(id) {
        return document.getElementById(id);
    }    

    this.init = function()
    {
        try {
            this.State = $("hidd_state").value;
            this.ProductType1 = $("protype1");
            this.ProductType2 = $("protype2");
            this.ProductType3 = $("protype3");
            
            this.SetProductTypeEvent(this.ProductType1, "ProductType1");
            
            if (this.State == "products") 
                this.KeepProType();
        }catch(e) {}
    }
    
    Index.prototype.KeepProType = function()
    {
        var s = $("hidd_protype").value;
        
        if (s != "") {
            var _proType1 = getTargetString(s, "ProType1", "^");
            var _proType2 = getTargetString(s, "ProType2", "^");
            var _proType3 = getTargetString(s, "ProType3", "^");

            if (_proType1 != "") {
                var obj = $(string("pt1_", _proType1));
                if (obj != null) {
                    if (obj.attributes["key"].nodeValue != "NIL")
                        this.ProductType1_onclick(obj);
                    else
                        this.SelectedItemStyle(obj);
                }               
            }

            if (_proType2 != "") {
                var obj = $(string("pt2_", _proType2));
                if (obj != null) {
                    if (obj.attributes["key"].nodeValue != "NIL")
                        this.ProductType2_onclick(obj);
                    else
                        this.SelectedItemStyle(obj);
                }
            }
            
            if (_proType3 != "") {
                var obj = $(string("pt3_", _proType3));
                if (obj != null)
                    this.SelectedItemStyle(obj);
            }            
        }
    }
    
    Index.prototype.SetProductTypeEvent = function(obj, type)
    {
        obj.onclick = function(e) {
            e = window.event || e;   
            var obj = e.srcElement || e.target;
            eval(string("_this.", type, "_onclick(obj)"));
        }
        
        obj.onmouseover = function(e) {
            e = window.event || e;   
            var obj = e.srcElement || e.target;   
            eval(string("_this.", type, "_onmouseover(obj)"));
        }
        
        obj.onmouseout = function(e) {
            e = window.event || e;   
            var obj = e.srcElement || e.target;   
            eval(string("_this.", type, "_onmouseout(obj)"));
        }
    }

    Index.prototype.IsStopEvent = function(obj, type)
    {
        return (obj.id != string(type, "_title") && obj.id != type);
    }
    
    Index.prototype.SelectedItemStyle = function(obj)
    {
        obj.style.color = "#FFFFFF";
        obj.style.backgroundColor = "#0A246A";
    }  
    
    Index.prototype.NormalItemStyle = function(obj)
    {
        obj.style.color = "#000000";
        obj.style.backgroundColor = "transparent";
    }
    
    Index.prototype.OverItemStyle = function(obj)
    {
        obj.style.color = "#000000";
        obj.style.backgroundColor = "#f6f585";
    }    

    Index.prototype.ProductType1_onclick = function(obj)
    {
        if (this.IsStopEvent(obj, "protype1")) {
            if (this.Active1 != null && this.Active1 != obj)
                this.NormalItemStyle(this.Active1);
            if (this.Active1 == null || this.Active1 != obj) {
                this.SelectedItemStyle(obj);
                
                if (obj.attributes["key"].nodeValue != "NIL") {
                    var o = _Index.GetProType2(obj.attributes["key"].nodeValue).value;
                    if (o != null) {
                        this.ProductType3.innerHTML = '<div id="protype3_title" class="title">ProductType3</div>';
                        this.ProductType2.innerHTML = o;
                        this.SetProductTypeEvent(this.ProductType2, "ProductType2");
                    }
                }
                else
                    document.location.href = string("index.aspx?category=", obj.attributes["key"].nodeValue);                      
            }

            this.Active1 = obj;
        }
    }
    
    Index.prototype.ProductType1_onmouseover = function(obj)
    {
        if (this.IsStopEvent(obj, "protype1")) {
            if (this.Last1 != null && this.Last1 != this.Active1)
                this.NormalItemStyle(this.Last1);
            
            if (obj != this.Active1) {
                this.OverItemStyle(obj);
                this.Last1 = obj;
            }
        }
    }
    
    Index.prototype.ProductType1_onmouseout = function(obj)
    {
        if (this.IsStopEvent(obj, "protype1")) {
            if (obj != this.Active1)
                this.NormalItemStyle(obj);
        }
    }
    
    Index.prototype.ProductType2_onclick = function(obj)
    {
        if (this.IsStopEvent(obj, "protype2")) {
            if (this.Active2 != null && this.Active2 != obj)
                this.NormalItemStyle(this.Active2);
            
            if (this.Active2 == null || this.Active2 != obj) {
                this.SelectedItemStyle(obj);
                
                if (obj.attributes["key"].nodeValue != "NIL") {
                    var o = _Index.GetProType3(this.Active1.attributes["key"].nodeValue, obj.attributes["key"].nodeValue).value;
                    if (o != null) {
                        this.ProductType3.innerHTML = o;
                        this.SetProductTypeEvent(this.ProductType3, "ProductType3");
                    }
                }
                else {
                    this.ProductType3.innerHTML = '<div id="protype3_title" class="title">ProductType3</div>';
                    
                    var c = string(this.Active1.attributes["key"].nodeValue, ";", obj.attributes["key"].nodeValue);
                    document.location.href = string("index.aspx?category=", c); 
                }                
            }

            this.Active2 = obj;
        }
    }
    
    Index.prototype.ProductType2_onmouseover = function(obj)
    {
        if (this.IsStopEvent(obj, "protype2")) {
            if (this.Last2 != null && this.Last2 != this.Active2)
                this.NormalItemStyle(this.Last2);
            
            if (obj != this.Active2) {
                this.OverItemStyle(obj);
                this.Last2 = obj;
            }
        }
    }
    
    Index.prototype.ProductType2_onmouseout = function(obj)
    {
        if (this.IsStopEvent(obj, "protype2")) {
            if (obj != this.Active2)
                this.NormalItemStyle(obj);
        }
    }
    
    Index.prototype.ProductType3_onclick = function(obj)
    {
        if (this.IsStopEvent(obj, "protype3")) {
            if (this.Active3 != null && this.Active3 != obj)
                this.NormalItemStyle(this.Active3);
            
            if (this.Active3 == null || this.Active3 != obj) {
                this.SelectedItemStyle(obj);
                
                var c = string(this.Active1.attributes["key"].nodeValue, ";", this.Active2.attributes["key"].nodeValue, ";", obj.attributes["key"].nodeValue);
                document.location.href = string("index.aspx?category=", c);
            }

            this.Active3 = obj;
        }
    }
    
    Index.prototype.ProductType3_onmouseover = function(obj)
    {
        if (this.IsStopEvent(obj, "protype3")) {
            if (this.Last3 != null && this.Last3 != this.Active3)
                this.NormalItemStyle(this.Last3);
            
            if (obj != this.Active3) {
                this.OverItemStyle(obj);
                this.Last3 = obj;
            }
        }
    }
    
    Index.prototype.ProductType3_onmouseout = function(obj)
    {
        if (this.IsStopEvent(obj, "protype3")) {
            if (obj != this.Active3)
                this.NormalItemStyle(obj);
        }
    }
    
    Index.prototype.VerifySearchString = function()
    {
        if ($("txt_search").value.length < CONST_SEARCH_MIN_CHAR) {
            alert(string('Please enter a keyword with least ', CONST_SEARCH_MIN_CHAR, ' characters.'));
            $("txt_search").value = "";
            $("txt_search").focus();
            
            return false;
        }
        else
            document.location.href = string("./index.aspx?state=search&c=", escape($("txt_search").value));
    }
    
    Index.prototype.SubmitBasket = function(index)
    {
        var obj_1 = $(string("pn", index));
        var obj_2 = $(string("ig", index));
        var obj_3 = $("total_basket");
        
        if (obj_1 != null && obj_2 != null && obj_3 != null) {
            var prono = obj_1.attributes["prono"].nodeValue;
            if (obj_2.src.indexOf("addtobasket.jpg") != -1) {                
                $("hidd_submit_product").value += string(",", prono);
                obj_2.src = "./images/system/remove.jpg";
                obj_3.innerHTML = parseInt(obj_3.innerHTML) + 1;
            }
            else {
                var p = new RegExp(string("(,", prono, ",)"), "gi");
                var s = string(",", $("hidd_submit_product").value, ",");
                $("hidd_submit_product").value = s.replace(p, ",");
                obj_2.src = "./images/system/addtobasket.jpg";
                obj_3.innerHTML = parseInt(obj_3.innerHTML) - 1;            
            }

            window.frames["postback_data"].postback("SubmitBasketProduct", $("hidd_submit_product").value);
        }
    }
}
