﻿var containercomp = null;
var isIE = (navigator.appName == "Microsoft Internet Explorer");

function InitSearchMaterial(compid){
    var SM1 = new AJAXClass();
    SM1.url = "SearchMaterials.aspx";
    if(compid){
        containercomp = getComponent(compid);
        if(containercomp){
            containercomp.innerHTML = GetLoadingHtml();
        }
        SM1.onStatusOK = WriteContents;
        SM1.setRequest(false);
    }else{
        SM1.setRequest(true);
        document.write(SM1.request.responseText);
    }
}

function WriteContents(arg){
    if(containercomp){
        containercomp.innerHTML = arg.responseText;
    }
}

function toggleVisibility(compid,imgid,hdnid){
    var comp = getComponent(compid);
    var img = getComponent(imgid);
    var hdn = getComponent(hdnid);
    if(comp){
        var visible = new Boolean(false);
        if(comp.style){
            if(comp.style.display || comp.style.visibility){
                if(comp.style.display){
                    if(comp.style.display == "none"){
                        comp.style.display = "";
                        visible = true;
                    }else{
                        comp.style.display = "none";
                    }
                }
                if(comp.style.visibility){
                    if(comp.style.visibility == "hidden"){
                        comp.style.visibility = "visible";
                        visible = true;
                    }else{
                        comp.style.visibility = "hidden";
                    }
                }
            }else{
                comp.style.display = "none";
            }
        }else{
            comp.style.display = "none";
        }
        if(img!=null){
            var expand = img.attributes["expand"].value;
            var collapse = img.attributes["collapse"].value;
            if(visible==true){
                img.src = expand;
            }else{
                img.src = collapse;
            }
        }
        if(hdn!=null){
            if(visible==true){
                hdn.value = 1;
            }else{
                hdn.value = 0;
            }
        }
        hideHelp();
    }
}

function getVisible(){
    var comp = getComponent("trAdvanceOptions");
    var visible = false;
    if(comp){
        if(comp.style){
            if(comp.style.display || comp.style.visibility){
                if(comp.style.display){
                    if(comp.style.display != "none"){
                        visible = true;
                    }
                }                
                if(comp.style.visibility){
                    if(comp.style.visibility != "hidden"){
                        visible = true;
                    }
                }
            }else{
                visible = true;
            }
        }else{
            visible = true;
        }        
    }
    return visible;    
}

function getComponent(comp){
    return (typeof(comp)=="string") ? document.getElementById(comp) : comp;
}

function SearchMaterial(compid,rootpath){
    var currpage = window.location.href;
    var pagecomp = getComponent("ReffererPage");
    if(pagecomp){
        pagecomp.value = currpage;
    }
    var advanceserch = getComponent("EnableAdvanceSearch");
    if(advanceserch){
        if(getVisible()==true){
            advanceserch.value = 1;
        }
    }
    var comp = getComponent(compid);   
    var compclone = comp;
    compclone.style.display = "none";
    var loadingComp = document.createElement("p");
    loadingComp.style.height = "150px";
    loadingComp.style.verticalAlign = "middle";
    loadingComp.innerHTML = "<b>Please wait while searching...</b>";
    if(comp.parentNode) comp.parentNode.appendChild(loadingComp);
    var formcomp = document.createElement("form");
    formcomp.action = rootpath + "SearchResults.aspx";
    formcomp.method = "post";
    if(comp){formcomp.appendChild(compclone);}
    document.body.appendChild(formcomp);
    formcomp.submit();
}

function bindSearchResults(PageNumber){
    var comp = document.getElementById("lblMaterialList");
    if(comp){
        comp.innerHTML = GetLoadingHtml();
    }
    var SM1 = new AJAXClass();
    SM1.url = "SearchResults.aspx";
    SM1.parameters.Add("ChangePage","true");
    SM1.parameters.Add("PageNumber",PageNumber);
    SM1.onStatusOK = bindSearchResultsHandle;
    SM1.setRequest(false);       
}
function bindSearchResultsHandle(Arg)
{
    var comp = document.getElementById("lblMaterialList");
    if(comp){
        comp.innerHTML = Arg.responseText;
    } 
}
function GetLoadingHtml()
{
    var Str = "";
    Str += "<table width='100%' style='height:150px;'>";
    Str += "<tr><td align='center' valign='middle'><img src='images/loading.gif' alt='' /><br/>Loading..</td></tr>";
    Str += "</table>";
    return Str;
}

function frmHelpTopper(){
    var comp = getComponent("ifrmHelpTopper");
    if(comp==null){
        var newcomp = document.createElement("iframe");
        newcomp.id = "ifrmHelpTopper";
        newcomp.style.visibility = "hidden";
        newcomp.style.position = "absolute";               
        newcomp.style.left = "0px";
        newcomp.style.top = "0px";
        if(document.body){
            document.body.appendChild(newcomp);
        }
        comp = getComponent("ifrmHelpTopper");
    }
    return comp;
}

function HelpContainer(){
    var comp = getComponent("divHelpContainer");    
    if(comp==null){
        frmHelpTopper();
        var newcomp = document.createElement("div");
        newcomp.id = "divHelpContainer";
        newcomp.style.visibility = "hidden";    
        newcomp.className = "HelpContainer";
        newcomp.innerHTML = HelpContent();        
        if(document.body){
            document.body.appendChild(newcomp);
        }
        comp = getComponent("divHelpContainer");
    }
    return comp;
}

function showHelp(comp){
    comp = getComponent(comp);
    var container = HelpContainer();
    container.style.visibility = "visible";
    container.innerHTML = HelpContent(comp.attributes["HelpRef"].value); 
    PositionComponent(comp,container);
}

function getCompLeft(obj){
    return obj.offsetLeft + (obj.offsetParent ? getCompLeft(obj.offsetParent) : obj.x ? obj.x : 0);
}

function getCompTop(obj){
    return (obj.offsetParent ? obj.offsetTop + getCompTop(obj.offsetParent) : obj.y ? obj.y : 0);    
}

function PositionComponent(comp,container){
    comp = getComponent(comp);
    container = getComponent(container);
    var topper = frmHelpTopper();
    if(isIE==true){
        topper.style.visibility = "visible";
    }
    if(comp && container && topper){ 
        container.style.left = (getCompLeft(comp) + comp.offsetWidth) + "px";
        container.style.top = getCompTop(comp) + "px";
        topper.style.left = (getCompLeft(comp) + comp.offsetWidth) + "px";
        topper.style.top = getCompTop(comp) + "px";
        topper.style.width = container.offsetWidth + "px";
        topper.style.height = container.offsetHeight + "px";
    }
}

function hideHelp(){
    var container = HelpContainer();
    container.style.visibility = "hidden";
    var topper = frmHelpTopper();
    topper.style.visibility = "hidden";
}

var HelpArr = new Array();
HelpArr[HelpArr.length] = {name: "Class", header: "Classification", content: "Classification - defines what type of material it is, e.x. granite, porcelain, metal, etc."};
HelpArr[HelpArr.length] = {name: "Format", header: "Format", content: "Format - refers to the size option available for each product, e.x. slab, tile, and custom cut-to-size."};

function trim(str){
    str = (typeof(str)=="string") ? str : "";
    return str.replace(/^\s+|\s+$/,"");
}

function getHelp(name){
    name = trim(name).toLowerCase();
    for(i=0; i<HelpArr.length; i++){
        var test = new String();        
        var currname = trim(HelpArr[i].name.toLowerCase());        
        if(currname == name){
            return HelpArr[i];
        }
    }
    return null;
}

function HelpContent(name){
    var Help = getHelp(name);
    var str = "";
    var pathext = new String();
    pathext = (typeof(pathExt)=="string") ? pathExt : "";
    if(Help!=null){
        str += "<table cellspacing='0' cellpadding='3' border='0'>";
        str += "<tr class='header'>";
        str += "<td align='left'>" + Help.header + "</td>";
        str += "<td align='right'><a href='#' onclick='javascript:hideHelp();return false;'><img src='"+ pathext +"images/closebtn.jpg' alt='Close Window' width='15' height='15' /></a></td>"
        str += "</tr>";
        str += "<tr class='content'>";
        str += "<td colspan='2'>";
        str += Help.content;
        str += "</td>";
        str += "</tr>";        
        str += "</table>";
    }
    return str;
}

function onEnterPressedClick(ComponentId, event, KeyCode){    
    var e = window.event? window.event : event;
    var Key = new Number();    
    if(KeyCode){
       if(!isNaN(KeyCode)){
         if(parseFloat(KeyCode)>=0 && parseFloat(KeyCode)<=255){
            Key = parseFloat(KeyCode);
         }else{
            alert("invalid keycode...");
            return;
         }
       }
    }else{
        Key = 13;
    }
    var Component = getComponent(ComponentId);
    if(Component){
        var CurrKey = e.keyCode ? e.keyCode : e.which;        
        if(CurrKey==Key){
            Component.onclick();
            return false;
        }else{
            return true;
        }
    }
}