﻿
// JScript File
var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);

if (! isIE) {
  // ----- HTML objects -----
  HTMLElement.prototype.__defineGetter__("innerText", function () { return(this.textContent); });
  HTMLElement.prototype.__defineSetter__("innerText", function (txt) { this.textContent = txt; });
  HTMLElement.prototype.__defineGetter__("currentStyle", function() {return window.getComputedStyle(this, null);})
  HTMLElement.prototype.__defineGetter__("children", function () { return(this.childNodes); });
  HTMLElement.prototype.__defineGetter__("XMLDocument", function () { return ((new DOMParser()).parseFromString(this.innerHTML, "text/xml"));});

  HTMLElement.prototype.attachEvent = function (eventName, fHandler) {
    fHandler._wrapHandler = function (e) {
       window.event = e;
       fHandler();
       return (e.returnValue);
      };
    this.addEventListener(eventName.substr(2), fHandler._wrapHandler, false);
  };
  HTMLElement.prototype.detachEvent = function (eventName, fHandler) {
    if (fHandler._wrapHandler != null)
      this.removeEventListener(eventName.substr(2), fHandler._wrapHandler, false);
  };
  Event.prototype.__defineGetter__("srcElement", function () {
    var node = this.target;
    while (node.nodeType != 1) node = node.parentNode;
    if (node != this.target) alert("Unexpected event.target!");
    return node;
  });
  Event.prototype.__defineSetter__("cancelBubble", function (b) {if (b) this.stopPropagation();});
  Event.prototype.__defineGetter__("clientX", function(){return this.pageX;});
  Event.prototype.__defineGetter__("clientY", function(){return this.pageY;});
  Event.prototype.__defineSetter__("returnValue", function (b) {
    if (!b) this.preventDefault();
    this._returnValue = b;
    return(b);
  });
  Event.prototype.__defineGetter__("returnValue", function () {return (this._returnValue);});
  XMLDocument.prototype.selectSingleNode = function(xPath) {
    var doc = this;
    if (doc.nodeType != 9)
      doc = doc.ownerDocument;
    if (doc.nsResolver == null) doc.nsResolver = function(prefix) { return(null); };
    var node = doc.evaluate(xPath, this, doc.nsResolver, XPathResult.ANY_UNORDERED_NODE_TYPE, null);
    if (node != null) node = node.singleNodeValue;
    return(node);
  };
  Node.prototype.__defineGetter__("text", function () {
    return(this.textContent);
  });
}
	    var pageURL = document.location.href;
        function setURL(args,url)
        {   
        
            //url = document.location.href;
            
            if(url.indexOf("?") != -1)
            {  
                var script = url.substring(0,url.indexOf("?") )
                url = "&" + url.substr(url.indexOf("?")+1,url.length - url.indexOf("?"))
                
            }
            else
            {
                var script = url;
                url = "";
                
            }
            
            arrVars = args.split("&")
            
            for(i = 0; i < arrVars.length;i++)
            {
               
                if(url.indexOf("&" + arrVars[i].split("=")[0] + "=") != -1)
                {
                    var left = url.substr(0,url.indexOf("&" + arrVars[i].split("=")[0] + "="));
                    var right = url.substring(url.indexOf("&" + arrVars[i].split("=")[0] + "=")+1,url.length)
                    if(right.indexOf("&") == -1)
                        right = ""
                    else
                        right = right.substring(right.indexOf("&"),right.length);
                        
                   if(arrVars[i].indexOf("=null") == -1)
                        url = left + "&" + arrVars[i] + right;
                   else
                        url = left + right;
                }
                else
                    if(arrVars[i].indexOf("=null") == -1)
                        url = url + "&" + arrVars[i];
                    else
                        url = url;
            }
            
            return script + "?" + url.substring(1,url.length);
        } 
        var toolbarlevel4 = null;
        var toolbarlevel3 = null;
        var toolbarlevel2 = null;

        function bmo(e)
        {
            var el = null;
            if(isIE)
            {
                el=event.srcElement;
            }
            else
            {
                el=e.target;
            }
            if( el.tagName != "A")
            {
              if(el.id.indexOf("Template") > -1 || el.id == "")
              {
                  if(toolbarlevel4 != null)
                 {
                    document.getElementById ( toolbarlevel4).style.display = 'none';
                    toolbarlevel4 = null;
                 }
                 if(toolbarlevel3 != null)
                 {
                    document.getElementById ( toolbarlevel3).style.display = 'none';
                    toolbarlevel3 = null;
                 }
                 if(toolbarlevel2 != null)
                 {
                    document.getElementById ( toolbarlevel2).style.display = 'none';
                 }
              }
           }

        }
        function __tbmo(e)
        {
            if(isIE)
            {
                el=event.srcElement;
            }
            else
            {
                el=e.target;
            }
             //el = event.srcElement;
             if(el.tagName == "A")
                el = el.parentNode;
             
             
                           
             if(el.id.indexOf("_children") == -1)
             {
                //alert("hi"); 
                var children = document.getElementById(el.id + "_children");
                var level = el.getAttribute("level");
                
               
                   if(level==3)
                    {
                        if(toolbarlevel4 != null)
                        {
                            if(toolbarlevel4.indexOf(el.id + "_") == -1)
                            {
                                document.getElementById (toolbarlevel4).style.display = 'none';
                           }
                        }
                    }
                    else if(level==2)
                    {
                        if(toolbarlevel3 != null)
                        {
                          
                            if(toolbarlevel3.indexOf(el.id + "_") == -1)
                            {
                                 document.getElementById (toolbarlevel3).style.display = 'none';
                            }
                        }
                         if(toolbarlevel4 != null)
                        {
                            if(toolbarlevel4.indexOf(el.id + "_") == -1)
                            {
                                document.getElementById (toolbarlevel4).style.display = 'none';
                           }
                        }
                    }
                    else if (level ==1)
                    {
                        if(toolbarlevel2 != null)
                        {
                            if(toolbarlevel2.indexOf(el.id + "_") == -1)
                            {
                                document.getElementById (toolbarlevel2).style.display = 'none';
                            }
                        }
                        if(toolbarlevel3 != null)
                        {
                            if(toolbarlevel3.indexOf(el.id + "_") == -1)
                            {
                                document.getElementById (toolbarlevel3).style.display = 'none';
                            }
                        }
                        if(toolbarlevel4 != null)
                        {
                            if(toolbarlevel4.indexOf(el.id + "_") == -1)
                            
                            {
                                document.getElementById (toolbarlevel4).style.display = 'none';
                           }
                        }
                            
                    }   
                    
                if(children != null)
                {
                    if(level == 1)
                        children.style.left = getRealLeft(el);
                    else
                    {
                        //alert(getRealLeft(el));
                        children.style.left = el.offsetWidth - 15;
                    }
                    if(level ==1)
                        children.style.top = getRealTop(el) + el.offsetHeight;

                   
                    children.style.display = ""; 

                    if(level==3)
                    {                 
                        toolbarlevel4 = children.id;
                    }
                    else if(level==2)
                    {
                        toolbarlevel3 = children.id;
                    }
                    else if (level ==1)
                    {                   
                        toolbarlevel2 = children.id;                      
                    }
                } 
                 event.cancelBubble = true;
                 return false;        
              }    
        
        }
function getCookie(c_name)
{
    if (document.cookie.length>0)
    { 
    c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
    }
    return ""
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}

function checkCookie()
{
    username=getCookie('username')
    if (username!=null && username!="")
      {alert('Welcome again '+username+'!')}
    else 
      {
      username=prompt('Please enter your name:',"")
      if (username!=null && username!="")
        {
        setCookie('username',username,365)
        }
      }
}
window.isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);
 
        function sp_ToolBoxClick(e)
        {
            if(isIE)
            {
                el=event.srcElement;
            }
            else
            {
                el=e.target;
            }
            if(el.tagName =="A")
                el = el.parentNode;

           if(el.tagName == "DIV")
           {
                var level = el.getAttribute("level");
                var parentControl = toolboxGetControl(el).id;

                var nextEl = el.nextSibling;
                if(nextEl != null)
                {
                    if(nextEl.tagName == null)
                    {
                        //alert("advance");
                        nextEl = nextEl.nextSibling;
                    
                    }
                }
                eval(el.getAttribute("action"));
                if(document.getElementById(parentControl).className == "toolbox") 
                 { 
                     var open = true;
                     
                     var rootLevel = parseInt(el.getAttribute("level")) + 1;
                    
                     if(nextEl.getAttribute("level") >= rootLevel)
                        open = false;
    
                     var path = ToolBoxPath(el);

                     if(open != true)
                        path = path.substring(0,path.lastIndexOf("/"))
                        
                     setCookie(parentControl,path,5);
                } 
            }
        }

        function LoadModule(ModuleID)
        {
           document.location.href = setURL("class=html&id=" + ModuleID,pageURL);
        }
        function ProductCategoryClick(ProductCategoryID)
        {
            var InventoryID = toolboxGetControl(event.srcElement).id.replace("Tools","");
            var CategoryID = event.srcElement.id;
            //alert(InventoryID);
            document.location.href = setURL("class=inventory&id=" + InventoryID + "&categoryid=" + CategoryID + "&productid=null&ip=null",pageURL);
        }
        
        function ToolBoxPath(el)
        {
            var level = el.getAttribute('level');
            var path = "";
            var lastvalue = 999;
            while (el.getAttribute('level') > 0)
            {
                if(lastvalue > el.getAttribute('level'))
                { 
                    path = "/" + el.id + path;
                    if(el.getAttribute('level') == 1)
                        return path;
                }
                lastvalue = el.getAttribute('level');
                el = el.previousSibling;
             
            } 
            return path;
        }
        
        function toolboxGetControl(el)
        {   
            el = el.parentNode;
            while(el.id == "")
            {
                el = el.parentNode;
            }
            return el;
        }

        function on(el)
        {
            el.style.display = '';
        }
        function off(el)
        {
            el.style.display = 'none';
        }
        function loadProductImage(e)
        {
            var path = e.src;
            var li = path.lastIndexOf(".");
            var right = path.substr(li,path.length - li);
            var left = path.substr(0,li-1);
            var newpath = left + right;            
            document.getElementById ("productimagelarge").src = newpath;
        
        }
        function getRealLeft(el) {
	        xPos = el.offsetLeft;
	        tempEl = el.offsetParent;
  	        while (tempEl != null) {
  		        xPos += tempEl.offsetLeft;
  		        tempEl = tempEl.offsetParent;
  	        }
	        return xPos;
        }

        function getRealTop(el) {
	        yPos = el.offsetTop;
	        tempEl = el.offsetParent;
	        while (tempEl != null) {
  		        yPos += tempEl.offsetTop;
  		        tempEl = tempEl.offsetParent;
  	        }
	        return yPos;
        }
 
    var ProductFeed =
   {
     Search : function(controlid,data)
     {
        document.location.href = document.location.href + "&filter=" + document.getElementById("search").value;
        
     }
   }  
   

function submitenter(myfield,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
       {
       ProductFeed.Search()
       return false;
       }
    else
       return true;
}

var formdata = "";
var formerrormessage = "";
function __SPSubmit(el)
{
    //alert(event.srcElement.parentNode.parentNode.parentNode.parentNode.innerHTML);
    //el = event.srcElement;
    formdata = "";
    formerrormessage = "";
    while(el.getAttribute("formid") == null)
    {
        el = el.parentNode;
    }      
    
    fl = document.createElement("input");
    fl.type= "hidden";
    fl.name= "formdata";
    fl.value = __GetFormElement(el); 
    document.forms[0].appendChild(fl);
    

    
    fn = document.createElement("input");
    fn.type= "hidden";
    fn.name= "formid";
    fn.value = el.getAttribute("formid");
    document.forms[0].appendChild(fn);
    
    if(formerrormessage == "")
        document.forms[0].submit();
    else
        alert("Form cannot be submitted yet." + formerrormessage);
}
   var FormManager =
   {
        SaveForm : function(el, func)
        {
            formdata = "";
            formerrormessage = FormManager.Validate(el);
            //alert(el.outerHTML);
            while(el.getAttribute("formid") == null)
            {
                el = el.parentNode;
            }      
            
            fl = document.createElement("input");
            fl.type= "hidden";
            fl.name= "formdata";
            fl.value = __GetFormElement(el); 
            document.forms[0].appendChild(fl);
            //alert(fl.value);
            fn = document.createElement("input");
            fn.type= "hidden";
            fn.name= "formid";
            fn.value = el.getAttribute("formid");
            document.forms[0].appendChild(fn);
            
            if(formerrormessage == "")
                document.forms[0].submit();
            else
                alert("Form cannot be submitted yet." + formerrormessage);
        },
     Validate : function(el)
        {
            var AttributesTable = el.parentNode.parentNode.firstChild;
            var ValidationInfo = "";
            var validation = "";
            var value = "";
            for(i=0;i<AttributesTable.rows.length;i++)
            {
                if(AttributesTable.rows[i].cells[1].childNodes.length > 0)
                {
                if(AttributesTable.rows[i].cells[1].firstChild.tagName != "TABLE")
                {
                    value = AttributesTable.rows[i].cells[1].firstChild.value;
                    validation = AttributesTable.rows[i].cells[1].firstChild.getAttribute("validation");
                    switch(validation)
                    {
                        case "REQUIRED":
                            if(value.length < 1)
                            {
                               ValidationInfo += "\n" + AttributesTable.rows[i].cells[0].innerText + " must have a value.";
                            }
                            
                        break;              
                    }
                }
                }
            }
            return ValidationInfo;
        }
  }
function __GetFormElement(el)
{
    if((el.tagName == "INPUT" || el.tagName == "TEXTAREA" || el.tagName == "SELECT") && el.type != "button" && el.id != "productclassform")
    {
        if(el.getAttribute("required") == "true")
        {
            if(el.value == "")
                formerrormessage += "\n - " + el.name + " field must have a value";
            
        }
        if(el.getAttribute("datatype") == "emailaddress")
        {
            __isEmail(el)
        }
        
        formdata += "&" + el.name + "=" + el.value;
    }
    for(var i=0;i<el.childNodes.length;i++)
    {
        
        __GetFormElement(el.childNodes[i]);
    }
    return formdata
}
function __isEmail(el)
{
    var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
    if (emailRegxp.test(el.value) != true)
        formerrormessage += "\n - " + el.name + " field must be a valid email address";

   
}


   var InventoryRotator =
   {
        Start: function(el)
        {
            //alert("start!" + products[0]);
              el =  document.getElementById("InventoryRotator").firstChild;
             
              InventoryRotator.LoadImage(el,0);
        },
        LoadImage: function(el,index)
        {
            if(index+1 > products.length)
                index = 0;            
            el.parentNode.setAttribute("productid",products[index].split('|')[2]);
            el.src=products[index].split('|')[0];

            document.getElementById("text").innerHTML =  "<a href='" + products[index].split('|')[3] + "'>" +  products[index].split('|')[1] + "</a>";
            InventoryRotator.FadeIn(el,0,index);
            
        },
        FadeOut: function(el,opacity,index)
        {
            opacity = opacity - .05;
            if(opacity > 0)
            {
                InventoryRotator.SetOpacity(el,opacity);
                 setTimeout(function(){InventoryRotator.FadeOut(el,opacity,index)}, 100)

            }   
            else
               InventoryRotator.FadeIn(el,0,index);
          
        },  
        FadeIn: function(el,opacity,index)
        {
            opacity += .05;
            //alert(opacity);
            if(opacity < 1)
            {
                InventoryRotator.SetOpacity(el,opacity);
                 setTimeout(function(){InventoryRotator.FadeIn(el,opacity,index)}, 100)

            }
            else
            {
             setTimeout(function(){InventoryRotator.LoadImage(el,index+1)}, 3000)
            }
            
        },
        SetOpacity: function(el, value)
        { //Sets the opacity of targetobject based on the passed in value setting (0 to 1 and in between)
            //alert("SETOP");
	        var targetobject=el
	        //alert(el.outerHTML);
	        targetobject.style.filter="alpha(opacity="+value*100+")"    
	        if (targetobject && targetobject.filters && targetobject.filters[0]){ //IE syntax
	            //alert("IE");
		        if (typeof targetobject.filters[0].opacity=="number") //IE6
			        targetobject.filters[0].opacity=value*100
		        else //IE 5.5
			        targetobject.style.filter="alpha(opacity="+value*100+")"
		        }
	        else if (targetobject && typeof targetobject.style.MozOpacity!="undefined") //Old Mozilla syntax
		        targetobject.style.MozOpacity=value
	        else if (targetobject && typeof targetobject.style.opacity!="undefined") //Standard opacity syntax
		        targetobject.style.opacity=value
	        targetobject.currentopacity=value
        }
  }

function __SPSubmitLogin()
{
    document.forms[0].submit();
}
function __SPLogout()
{
    var logout = document.createElement("input");
    logout.name = "logout";
    logout.value= "true";
    document.forms[0].appendChild(logout);
    document.forms[0].action = "default.aspx";
    document.forms[0].submit();
}

function __SPMemberHome()
{
    document.location.href = "default.aspx?class=memberhome";
}
        function nothing()
        {
            event.cancelBubble = true;
            
            return false;
        }  

var InventoryMan =
   {
        Add: function(inventoryId)
        {
            //alert("Add");
            document.location.href = document.location.href + "&action=add";
        },
        Edit: function(inventoryId, itemId)
        {
            if(itemId != 0)
                document.location.href = document.location.href + "&action=edit&item=" + itemId;
        },
        Delete: function(inventoryId, itemId)
        {
            if(itemId != 0)
                document.location.href = document.location.href + "&action=delete&item=" + itemId;
        }
        
   }
   
   
   var __SPDataGrid =
   {
        Click : function(el)
        {
            document.onselectstart = nothing;
            var row = event.srcElement.parentNode;
            var rowIndex = row.rowIndex;
            var newSelects = "";
            
            if(row.rowIndex == 0)
            {
            }
            else
            {
            __SPDataGrid.UnSelectAll(el);
            row.className = "selectedgriditem";
            el.setAttribute("s",row.rowIndex.toString());
            }
//            if(row.rowIndex == 0)
//            {
//                // Column Header Click Logic
//                var moduleid = document.getElementById('InventoryManager').getAttribute("targetcontrol");
//                var  onComplete = function (returnVal)
//                {
//                    document.getElementById("managercontrol").innerHTML = returnVal;
//                }
//                __doAJAX("SPFrame.UI.Controls.InventoryManagerClient","LoadManager$" + moduleid + "||" + event.srcElement.innerText,onComplete); 
//            }
//            else
//            {
//                if(event.button == 2)
//                {                           
//                    var x = event.clientX; var y = event.clientY;
//                    if(el.getAttribute("s") == null || ("," + el.getAttribute("s") + ",").indexOf("," + rowIndex + ",") == -1)
//                    {
//                       __SPDataGrid.UnSelectAll(el);
//                       row.className = "selectedgriditem";
//                       el.setAttribute("s",row.rowIndex.toString());
//                       if(el.getAttribute("__contextitems") != "" && el.getAttribute("__contextitems") != null)
//                       {
//                           var myWindow = __GetWindow("context", null, 150, 300, x-15, y-15, null,true);
//                           __WindowContent.appendChild(DrawContext2("imagemanager", row.cells[0].innerText, el.getAttribute("__contextitems"), el.getAttribute("__contexteventhandler")));
//                       }
//                    }   
//                    else
//                    {
//                       var myWindow = __GetWindow("context", null, 150, 300, x-15, y-15, null,true);
//                       __WindowContent.appendChild(DrawContext2("imagemanager", row.cells[0].innerText, el.getAttribute("__mcontextitems"), el.getAttribute("__mcontexteventhandler")));
//                    }
//                }
//                else
//                {
//                    // Row click logic
//                    if(event.shiftKey == true)
//                    {
//                         var start = 0; var end = 0;
//                         if(el.getAttribute("sIndex") < rowIndex){start = el.getAttribute("sIndex");end = rowIndex + 1;}else{start = rowIndex;end = el.getAttribute("sIndex");}
//                         for(i = start; i < end; i ++)
//                         {
//                            el.rows[i].className = "selectedgriditem";
//                            newSelects  = __SPDelimitedList.Add(newSelects,",",i);
//                         }  
//                         el.setAttribute("s",__SPDelimitedList.Add(el.getAttribute("s"),",",newSelects));
//                    }
//                    else if (event.ctrlKey == true)
//                    {
//                       row.className = "selectedgriditem";
//                         el.setAttribute("s",__SPDelimitedList.Add(el.getAttribute("s"),",",rowIndex));
//                    }
//                    else
//                    {
//                       __SPDataGrid.UnSelectAll(el);
//                       row.className = "selectedgriditem";
//                       el.setAttribute("s",row.rowIndex.toString());
//                    } 
//                }
//                el.setAttribute("sIndex",event.srcElement.parentNode.rowIndex);
           // }
        },
        
        UnSelectAll : function(el)
        {
           if(el.getAttribute("s")!= null)
           {
                    var arrSelects = el.getAttribute("s").split(',');
                    for(i=0; i < arrSelects.length;i++)
                        el.rows[arrSelects[i]].className = "griditem";                              
            }
        },
        GetKeys : function(el)
        {
            var indexes = el.getAttribute("s")
            var keys = "";
            if(indexes != "" && indexes != null)
            {
                for(i = 0;i < indexes.split(',').length;i++)
                {
                    if(keys == "")
                        keys = el.rows[indexes.split(',')[i]].cells[0].innerText;
                    else
                        keys += "," + el.rows[indexes.split(',')[i]].cells[0].innerText;
                }
            }
            return keys;
        }
   }