﻿
    function showHide(elem, camefrom) {
        if (document.getElementById(elem).style.display == 'none') {
            document.getElementById(elem).style.display = 'block'
        } else {
            document.getElementById(elem).style.display = 'none'
        }
//        $(".containerInner").height($(".mainContainer").height() - 40);
    }    
        
    function printNow(){
        window.print();
    }
    
    function noError(){return true;}
    //This script will stop all the errors alerts of javascript
    //window.onerror = noError;

    function openPrint(pageToView){
        //alert(pageToView);
        
        OpenWin = this.open(pageToView, "printWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=600,height=600,left=50,top=50");
        OpenWin.document.print();
    }

    function orderList(objName){
    
        var obj = document.getElementById(objName);
        var alternativeDisplay = "white";
        
        for(var j=0;j<obj.childNodes.length-1;j++)
        {
            for(var i=0;i<obj.childNodes.length-1;i++)
            {
                /*
                if (j % 2 == 0)
                    alternativeDisplay = "";
                else
                    alternativeDisplay = "white";
                */
                var LIContainer = new String(obj.childNodes[i].innerHTML);
                //alert(LIContainer)
                LIContainer.className = alternativeDisplay;
                var currText = stripHTML(LIContainer);
                                
                var LIContainer_Next = new String(obj.childNodes[i].nextSibling.innerHTML);
                var currText_Next = stripHTML(LIContainer_Next);
               
                if(currText>currText_Next)
                {
                    obj.insertBefore(obj.childNodes[i].nextSibling,obj.childNodes[i])
                }
            }
        }
        orderListbyCategory(objName);
    }
    
    
    function orderListbyCategory(objName){
    
        var obj = document.getElementById(objName);

        for(var j=0;j<obj.childNodes.length;j++)
        {
            for(var i=j+1;i<obj.childNodes.length;i++)
            {
                var currID = new String(obj.childNodes[j].id);
                var arrCurrID = new Array();
                arrCurrID = currID.split("_");
                var currTypeID = arrCurrID[0];
                //alert(currTypeID)
                var currID_Next = new String(obj.childNodes[i].id);
                var arrCurrID_Next = new Array();
                arrCurrID_Next = currID_Next.split("_");
                var currTypeID_Next = arrCurrID_Next[0];
               
                if(currTypeID==currTypeID_Next)
                {
                    obj.insertBefore(obj.childNodes[i],obj.childNodes[j])
                }
            }
        }
        
        displayCategory(objName);
    }
    
    
    function displayCategory(objName)
    {
        var obj = document.getElementById(objName);
        var allItemsType = document.getElementById("ctl00_ContentPlaceHolder1_allItemsType");
        //alert(allItemsType.length);
        var displayCategory = new String("");
        var checkingNext = new String("");
        var alternativeDisplay = "white";
        
        for(var i=0;i<obj.childNodes.length;i++)
        {
            var currID = new String(obj.childNodes[i].id);
            if (alternativeDisplay == "white")
                alternativeDisplay = "";
            else
                alternativeDisplay = "white";

            
            var arrCurrID = new Array();
            arrCurrID = currID.split("_");
            var currTypeID = arrCurrID[0];
            for(var j=0;j<allItemsType.length;j++)
            {
                if (currTypeID == allItemsType.options[j].value && checkingNext != currTypeID) {
                    alternativeDisplay = "";
                    checkingNext = currTypeID;
                    var oNewNode = document.createElement("LI");
                    oNewNode.setAttribute("className","Category");
                    oNewNode.innerHTML=allItemsType.options[j].innerHTML;
                    obj.insertBefore(oNewNode , obj.childNodes[i]);
                    i=i+1;
                }
            }
            
            obj.childNodes[i].className = alternativeDisplay;
            
        }
        
    }
    
    
    function stripHTML(strToStrip){
        
        var re= /<\S[^><]*>/g;
        var thestrToStrip = new String(strToStrip);
        thestrToStrip=thestrToStrip.replace(re, "");
        return thestrToStrip;
        
    }
    
    
	function NewWindow(page,width,height,top,left)
	{
		OpenWin = this.open(page, "CtrlWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width="+width+",height="+height+",left="+left+",top="+top+"");
	}

	function NavTo(url){window.location = url }

	function ChangeIframeHeight(theFrameName)
	{
		if (parent == window) return;
		else parent.setIframeHeight(theFrameName);
    }
	
	function getDocHeight(doc) 
	{
	  var docHt = 0, sh, oh;
	  if (doc.height) docHt = doc.height;
	  else if (doc.body) {
		if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
		if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
		if (sh && oh) docHt = Math.max(sh, oh);
	  }
	  if(docHt<=250)
	  	docHt=500;
	  return docHt;
	}
	
	function setIframeHeight(iframeName) {
	
	  var iframeWin = window.frames[iframeName];
	  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
	  if ( iframeEl && iframeWin ) {
		  
		iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
		var docHt = getDocHeight(iframeWin.document);
		//alert(docHt)
		// need to add to height to be sure it will all show
		if (docHt) iframeEl.style.height = docHt + 30 + "px";
	  }
	}
	
	
	function addToFavorites()
	{
	    var chr = 'CTRL-D';
        var agt=navigator.userAgent.toLowerCase();
        if(agt.substr(agt.indexOf('opera')+6,1) < 9) 
            chr = 'CTRL-T';
        if(window.external) 
            document.write('<a href="javascript:window.external.AddFavorite(self.location,document.title)">Add to Favorites<\/a>');
        else 
            document.write('Press '+chr+' to bookmark this page.');
    }


    function displayMoadafim(siteURL, pid, idToDisplay, fromHP) {
        var moadafim = new String(readCookie('moadafim'));
        //alert(moadafim)
        var arrmoadafim = new Array();
        arrmoadafim = moadafim.split('$$');
        var obj = document.getElementById(idToDisplay);
        var objisFavoritesPage = document.getElementById("isFavoritesPage");

        var stringSubjects = new String("");
        if (obj != null) {
            var counter = 0;
            //alert(arrmoadafim.length)
            if (arrmoadafim.length > 1) {
                for (var i = 0; i < arrmoadafim.length - 1; i++) {

                    /*
                    if (i % 2 == 0)
                        alternativeDisplay = "";
                    else
                        alternativeDisplay = "white";
                    */
                    counter++;
                    var arrItem = new Array();
                    var currMoadafim = new String(arrmoadafim[i]);
                    arrItem = currMoadafim.split('--');
                    if (counter == 1 && fromHP) {
                        stringSubjects = stringSubjects + "<div class=\"Subjects\">";
                    }
                    var strName = new String(arrItem[2]);
                    strName = unescape(strName);

                    if (!fromHP) {
                        stringSubjects = stringSubjects + "<li id=\"" + arrItem[0] + "_" + arrItem[1] + "\"><img src=\"" + siteURL + "/website/images/icons/DatabaseTypes/" + arrItem[0] + ".png\" align=\"middle\">";
                        stringSubjects = stringSubjects + "<div class=\"text\"><a href=\"" + siteURL + "/website/modules/Database/Item.aspx?pid=" + pid + "&type=" + arrItem[0] + "&id=" + arrItem[1] + "\">" + strName + "</a></div>";

                        if (objisFavoritesPage)
                            stringSubjects = stringSubjects + "<span class=\"remove_A\"><a href=\"javascript:RemoveItem('" + arrItem[0] + "_" + arrItem[1] + "')\">Remove</a></span>";

                        stringSubjects = stringSubjects + "</li>";
                    }
                    else {
                        if (strName.length > 16)
                            strName = strName.substring(0, 16) + "...";
                        stringSubjects = stringSubjects + "<div class=\"Subject\"><a href=\"" + siteURL + "/website/modules/Database/Item.aspx?pid=" + pid + "&type=" + arrItem[0] + "&id=" + arrItem[1] + "\">" + strName + "</a></div>";
                    }

                    if (counter == 3 && fromHP) {
                        stringSubjects = stringSubjects + "</div>";
                        counter = 0;
                    }
                }
                if (counter < 3) {
                    stringSubjects = stringSubjects + "</div>";
                }
            }
            else {
                stringSubjects = stringSubjects + "<div class=\"Subjects\" style=\"width:100%;\"><div class=\"add2fav\" title=\"In order to add an item to your favorites, please click the link 'Add to favorites' on the right bar, at the 'Item page'\">No Item where added to your favorites</div></div>";
            }
            obj.innerHTML = stringSubjects;
            //alert(stringSubjects)
        }

        if (objisFavoritesPage) {
            orderList(idToDisplay);
        }
    }


    function RemoveItem(obj) {
        RemoveItemFromCookie(obj);
        var objFavoritesPage = document.getElementById(obj);
        objFavoritesPage.parentNode.removeChild(objFavoritesPage);
    }


    function RemoveItemFromCookie(obj) {
        var moadafim = new String(readCookie('moadafim'));
        var strCookie = new String("");
        var arrmoadafim = new Array();
        arrmoadafim = moadafim.split('$$');

        for (var i = 0; i < arrmoadafim.length - 1; i++) {
            var arrItem = new Array();
            var currMoadafim = new String(arrmoadafim[i]);
            arrItem = currMoadafim.split('--');
            if (arrItem[0] + "_" + arrItem[1] != obj) {
                strCookie = strCookie + arrmoadafim[i] + "$$";
            }
        }

        eraseCookie('moadafim');
        createCookie('moadafim', strCookie, 360);
    }



    function createCookie(name,value,days) {
        if (days) {
	        var date = new Date();
	        date.setTime(date.getTime()+(days*24*60*60*1000));
	        var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }


    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1,c.length);
	        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
