﻿//Moved Script from Master Page to a Include file
function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
win.window.focus();
}

function EmptySB(obj)
	{   
		var con=confirm("Do you really want to empty shopping basket")
		if (con==true)
		    window.location.replace =obj
		return false;
	}
function setCookie(NameOfCookie, value, expiredays) 
{
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function deleteCookie(NameOfCookie, expiredays) {
    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() - (expiredays * 24 * 3600 * 1000));
    document.cookie = NameOfCookie + "=;" + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function clearText(obj) {
    if (obj.value == 'Keyword Search') {
        obj.value = '';
    }
    obj.value = '';
    obj.className = 'keywrdsrchNormal'
}
function checkKeyword(id)
{
	var txtKey = document.getElementById(id);

	if (txtKey.value=='Put "multiple word" terms in quotes' || txtKey.value == "")
	{
		alert("Please enter keyword(s) to search.");
		return false;
	}
	return true;
}


   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 unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}   

function SaveToLightbox(trans_no,imgID)
{ 
    var strImageTitle = document.getElementById(imgID).title

    if ( strImageTitle.indexOf('already')>0)
    {
    alert("This image is in current LightBox already!")
    return;
    }

  	try
	{
		oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			oXMLHTTP = null;
		}
	}
	
	// Creating object of XMLHTTP in different browsers
	if (!oXMLHTTP && typeof XMLHttpRequest != "undefined")
	{
		oXMLHTTP = new XMLHttpRequest();
	}
	
	//Error handler 
	oXMLHTTP.onreadystatechange =function(){
		if (oXMLHTTP.readyState==4){
		   if (oXMLHTTP.status==200) {
			document.getElementById(imgID).title = "image in Lightbox already"
			document.getElementById(imgID).src = "../media/lightbox full_search.gif"
			//Increment Lightbox Count
			var lbl = document.getElementById("ctl00_lblLightboxImageCount");
			var iCartCount = parseInt(lbl.innerHTML,10)
			iCartCount = iCartCount+1 
			lbl.innerHTML = iCartCount}
		    else alert("There were problems adding the selected image to the LightBox. Please Try again later.") }
		}	
	
	//Make request to the SaveLightboxShoppingbasket.asp Page
	oXMLHTTP.open("POST", "../respages/SaveLightboxShoppingbasket.asp?trans_no="+ trans_no +"&Light=True", true)
    oXMLHTTP.send('');
}

function ADD_DeleteFromLightbox(trans_no, imgID, lblID, calledfrom)
{
   try
	{
		oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			oXMLHTTP = null;
		}
	}
	
	// Creating object of XMLHTTP in different browsers
	if (!oXMLHTTP && typeof XMLHttpRequest != "undefined")
	{
		oXMLHTTP = new XMLHttpRequest();
	}
	
	oXMLHTTP.onreadystatechange =function(){
		if (oXMLHTTP.readyState==4)
		{
		   if (oXMLHTTP.status==200) 
		   {
		   		if (document.getElementById(imgID).title == "Add to Lightbox")
				{
				    document.getElementById(imgID).title = "Remove Image from Lightbox"
				    
				    if (calledfrom == "PreviewPage")
				        document.getElementById(imgID).src = "../media/lightbox_preview_remove.gif"
				    else
				        document.getElementById(imgID).src = "../media/lightbox remove.gif"
				        
				document.getElementById(lblID).title = "Remove Image from Lightbox"
				document.getElementById(lblID).src = "../media/removefromlightbox.gif"
				
				var lbl = document.getElementById("ctl00_lblLightboxImageCount");
				var iLightboxCount = parseInt(lbl.innerHTML,10)
				iLightboxCount = iLightboxCount+1 
				lbl.innerHTML = iLightboxCount
				
				}
				else	
				{
				    document.getElementById(imgID).title = "Add to Lightbox"

				    if (calledfrom == "PreviewPage")
				        document.getElementById(imgID).src = "../media/lightbox_preview.gif"
				    else
				        document.getElementById(imgID).src = "../media/lightbox_search.gif"
				
				document.getElementById(lblID).title = "Add to Lightbox"
				document.getElementById(lblID).src = "../media/addtolightbox.gif"
				
				var lbl = document.getElementById("ctl00_lblLightboxImageCount");
				var iLightboxCount = parseInt(lbl.innerHTML,10)
				iLightboxCount = iLightboxCount-1 
				lbl.innerHTML = iLightboxCount
				}

			}
			else alert("There were problems performing this operation. Please Try again later.")
		 }
		}

	if (document.getElementById(imgID).title == "Add to Lightbox")
	{
		oXMLHTTP.open("POST", "SaveLightboxShoppingbasket.asp?trans_no="+ trans_no +"&Light=True", true)
	}
	else	
    {
      oXMLHTTP.open("POST", "SaveLightboxShoppingbasket.asp?trans_no="+ trans_no +"&DeleteLight=True", true)
    }
	
    oXMLHTTP.send('');
  }




/* Saves the image to the Shopping basket by Ajax Method */
function SaveToShoppingBasket(trans_no,imgID,calledfrom)
{
    var strImageTitle = document.getElementById(imgID).title
    if ( strImageTitle.indexOf('already')>0)
    {
    alert('This Image is in the shopping basket already')
    return;
    }

   try
	{
		oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			oXMLHTTP = null;
		}
	}
	
	// Creating object of XMLHTTP in different browsers
	if (!oXMLHTTP && typeof XMLHttpRequest != "undefined")
	{
		oXMLHTTP = new XMLHttpRequest();
	}

	oXMLHTTP.onreadystatechange = function () {
	    if (oXMLHTTP.readyState == 4) {
	        if (oXMLHTTP.status == 200) {
	            document.getElementById(imgID).title = "image in shopping basket already"
	            if (calledfrom == "PreviewPage")
	                document.getElementById(imgID).src = "../media/basket_preview_full.gif"
	            else
	                document.getElementById(imgID).src = "../media/basketfull.gif"

	            //Increment the value of Shopping basket Count
	            var lbl = document.getElementById("ctl00_lblCartCount");
	            alert(lbl.value);
	            var iCartCount = 0 //parseInt(lbl.value, 10)
	            iCartCount = iCartCount + 1
	            lbl.innerHTML = iCartCount
	        }
	        else
	            alert("There were problems adding the selected image to the shopping basket. Please Try again later.")
	    }
	}
		
	oXMLHTTP.open("POST", "SaveLightboxShoppingbasket.asp?trans_no="+ trans_no +"&sb=True", true)
    oXMLHTTP.send('');
  }

   function SetScroll()
   {
    //TurnAutoCompleteOff();
    var PrevPage = document.referrer;
    
    if (PrevPage.indexOf("Preview.aspx",0) != -1)
        {
            var currPage = window.location.href;       
            if (currPage.indexOf("Search.aspx",0) !=-1)
            {
                var x = readCookie('xCoordHolder')
                var y = readCookie('yCoordHolder')
                document.getElementById("xCoordHolder").value = x
                document.getElementById("yCoordHolder").value = y
                if (x ==null)
                    x= 0;
                if (y==null)
                    y=0;
                window.scrollTo(x, y);
            }
        }
        return ;
    }


    function SetAdvCookie() {
        //TurnAutoCompleteOff();
        var CurrPage = window.location.href; ;
        if (CurrPage.indexOf("default.aspx", 0) >= 0) {
            setCookieLocal('showAdv', 0, 1);
        }
        return;
    }   
   
   function Scroll(x,y)
   {
      window.scrollTo(x, y);
   }
   
   function TurnAutoCompleteOff()
   {
    //document.forms[0].setAttribute("autocomplete", "off"); 
   }

   function SaveImageNotes(LightboxItmId, ctrlImageNotes, ctrlImage) {
       var sName = document.getElementById(ctrlImageNotes)
       if (sName.value.length >= 1200) {
           alert("You have reached the maximum length")
           return
       }
       document.getElementById(ctrlImage).src = "../media/wheel2.gif"
       try {
           oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch (e) {
           try {
               oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch (oc) {
               oXMLHTTP = null;
           }
       }

       // Creating object of XMLHTTP in different browsers
       if (!oXMLHTTP && typeof XMLHttpRequest != "undefined") {
           oXMLHTTP = new XMLHttpRequest();
       }


       oXMLHTTP.onreadystatechange = function() {
           if (oXMLHTTP.readyState == 4) {
               if (oXMLHTTP.status != 200)
                   alert("There were problems saving Image Notes. Please Try again later.")
               /*else
               {
               if (oXMLHTTP.responseText != "Done")
               alert(oXMLHTTP.responseText);
               }*/
           }
       }
       oXMLHTTP.open("POST", "Savelightboxnotes.asp?lightboxid=" + LightboxItmId + "&notes=" + encodeURIComponent(sName.value), true)
       oXMLHTTP.send('');
       setTimeout("BusyIcon('" + ctrlImage + "','0')", 2000);
   }

   function SaveLightboxNotes(LightboxId, ctrlNotes, ctrlImage)  // 17/01/2007 - SB removed parameter
   {

       var txtArea = document.getElementById(ctrlNotes);

       if (txtArea.value.length >= 1200) {
           alert("You have reached the maximum length")
           return
       }

       document.getElementById(ctrlImage).src = "../media/wheel3.gif"

       try {
           oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch (e) {
           try {
               oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch (oc) {
               oXMLHTTP = null;
           }
       }
       if (!oXMLHTTP && typeof XMLHttpRequest != "undefined") {
           oXMLHTTP = new XMLHttpRequest();
       }
       oXMLHTTP.onreadystatechange = function() {
           if (oXMLHTTP.readyState == 4) {

               if (oXMLHTTP.status != 200)
                   alert("There were problems saving Lightbox Notes. Please Try again later.")
               /*else
               {
               if (oXMLHTTP.responseText != "Done")
               alert(oXMLHTTP.responseText);
               }*/

           }
       }

       oXMLHTTP.open("POST", "Savelightboxnotes.asp?lightboxid=" + LightboxId + "&notes=" + encodeURIComponent(txtArea.value) + "&lightupdate=true", true)
       oXMLHTTP.send('');
       setTimeout("BusyIcon('" + ctrlImage + "','1')", 2000);
   }

   function BusyIcon(ctrlImage, LightboxNotes) {
       if (LightboxNotes == "1") {
           document.getElementById(ctrlImage).src = "../media/lightboxheadersave.gif"
       }
       else
           document.getElementById(ctrlImage).src = "../media/lightboxsave.gif"
   }

   function client_TreeNodeClicked(mEvent, CalledFrom) {
       var obj;
       var NodeExpand = false;
       var bAddtoList
       // Internet Explorer    
       if (mEvent.srcElement) {
           obj = mEvent.srcElement;
       }

       // Netscape and Firefox
       else if (mEvent.target) {
           obj = mEvent.target;
       }

       //When clicked on +/- image exit function
       if (obj.tagName == "IMG") {
           if (obj.alt == "Collapse Categories")
               return false;
       }
       if (obj.tagName == "INPUT" && obj.type == "checkbox") {
           if (obj.checked == true) {
               bAddtoList = true
           }
           else {
               bAddtoList = false
           }
       }

       do {
           if (obj.parentElement) {
               obj = obj.parentElement;
           }
           else {
               obj = obj.parentNode;
           }
       } while (obj.tagName != "TR")


       //Get Node text
       var link = obj.getElementsByTagName("A");
       var index = (link.length - 1)

       var sNodeText = link[index].innerHTML;
       sNodeText = sNodeText.replace(/\&amp;/g, '&');

       var sNodePath = link[index].href
       var tmp = "javascript:__doPostBack('ctl00$mainContent$TviewSubjectHiearchy','s"
       
       sNodePath = sNodePath.replace(tmp, "");

       sNodePath = sNodePath.replace("\')", "");
       sNodePath = sNodePath.replace("\\\\", "\\");
       sNodePath = sNodePath.replace("\\", "//");

       var dropDownListRef = document.getElementById('lstSelectedCategories');

       var option1 = document.createElement("option");
       option1.text = sNodeText
       option1.value = sNodePath
       option1.selected = true;

       for (i = dropDownListRef.length - 1; i >= 0; i--) {
           if (dropDownListRef.options[i].text == sNodeText) {
               bAddtoList = false
           }
       }

       if (bAddtoList == true)
           dropDownListRef.options.add(option1);
       else {
           for (i = dropDownListRef.length - 1; i >= 0; i--) {
               if (dropDownListRef.options[i].text == sNodeText) {
                   dropDownListRef.options[i] = null;
               }
           }
       }


       var optionsList = '';
       for (var i = 0; i < dropDownListRef.options.length; i++) {
           var optionText = dropDownListRef.options[i].text;
           var optionValue = dropDownListRef.options[i].value;

           if (optionsList.length > 0)
               optionsList += ';';

           optionsList += optionText + ':' + optionValue;
       }

       document.getElementById('hdnSelectedCategoryValues').value = optionsList;
       if (optionsList == '')
           document.getElementById('btnCategorySearch').title = "Please select a category to perform search";
       else
           document.getElementById('btnCategorySearch').title = "Click to perform search";

       return true;
   }

   function InitializeSideBar() {

       
       //Lightbox Header
       var objImgViwr = document.getElementById("LightboxHeaderOuter")
       if (objImgViwr != null) {
           var brwsrHeight = 0
           brwsrHeight = GetWindowHeight()
           objImgViwr.style.height = (brwsrHeight - 145) + 'px';
       }

      
       var c = Sys.Application.getComponents();
       var s = "";
       
       for (var i = 0; i < c.length; i++) {
           var id = ""
           id = c[i].get_id();
           var type = Object.getType(c[i]).getName();

           if (type == "Sys.Extended.UI.FloatingBehavior") {
               c[i].add_move(StartMoveHandler);
               c[i].add_propertyChanged(locationUpdatedHandler);
               
           }

           if (id == "CollapsePnlCategorySearch") {
               var type = Object.getType(c[i]).getName();
               if ((type == 'AjaxControlToolkit.CollapsiblePanelBehavior') || (type == 'Sys.Extended.UI.CollapsiblePanelBehavior')) {
                   c[i].add_expandComplete(cpeCategorySearch);
                   c[i].add_collapseComplete(cpcCategorySearch);
               }

           }

           if (id == "CollspPnlSidebar") {
               var type = Object.getType(c[i]).getName();
               if ((type == 'AjaxControlToolkit.CollapsiblePanelBehavior') || (type == 'Sys.Extended.UI.CollapsiblePanelBehavior')) {

                   c[i].add_expandComplete(cpeSidebar);
                   c[i].add_collapseComplete(cpcSidebar);
               }
           }
       }


       var scrollX, scrollY;

       if (document.all) {
           if (!document.documentElement.scrollLeft)
               scrollX = document.body.scrollLeft;
           else
               scrollX = document.documentElement.scrollLeft;

           if (!document.documentElement.scrollTop)
               scrollY = document.body.scrollTop;
           else
               scrollY = document.documentElement.scrollTop;
       }
       else {
           scrollX = window.pageXOffset;
           scrollY = window.pageYOffset;
       }

       var objImgViwr = document.getElementById("SidebarImgviewerLightboxDiv")

       if (objImgViwr != null) {
           var brwsrHeight = 0
           brwsrHeight = GetWindowHeight()

           var HeightDeduction = 230 - scrollY
           var NewHeight = brwsrHeight - HeightDeduction
           objImgViwr.style.height = NewHeight + 'px';

           var HeightDeduction = 201
           var NewHeight = brwsrHeight - HeightDeduction
           var objImgViwr = document.getElementById("SidebarImgviewerBasketDiv")
           objImgViwr.style.height = NewHeight + 'px';

           var objImgViwr = document.getElementById("SidebarCatTreeDiv")
           if (objImgViwr != null) {
               objImgViwr.style.height = (brwsrHeight - 185 + scrollY) + 'px';
           }

           var obj = document.getElementById("TreeviewDiv")
           if (obj != null) {
               obj.style.height = (brwsrHeight - 313 + scrollY) + 'px';
           }

           var obj = document.getElementById("hdnTreeviewDivHeight");
           if (obj != null) {
               obj.value = (brwsrHeight - 313 + scrollY)
           }
       }
  }

  function cpeSidebar(sender, arg) {
       var obj = document.getElementById("SearchResultsDiv")
       obj.setAttribute("Class", "SRwithSideBar")
       setCookie("SidebarOpen", "true", 1)
       document.getElementById("FooterSection").style.marginLeft = '204px';
       
   }

   function cpcSidebar(sender, args) {
       var obj = document.getElementById("SearchResultsDiv")
       obj.setAttribute("Class", "SRnoSideBar")
       setCookie("SidebarOpen", "false", 1)
       document.getElementById("FooterSection").style.marginLeft = '0px';
   }


   function cpeCategorySearch(sender, arg) {
       var obj = document.getElementById("ImgCloseHideCatSearch")
       obj.setAttribute("title", "Click here to close search options")
       var o = document.getElementById("Catsearchpan2")
       o.setAttribute("class", "CatSearchOptnsOpen");
       o.setAttribute("className", "CatSearchOptnsOpen");
   }

   function cpcCategorySearch(sender, args) {
       var obj = document.getElementById("ImgCloseHideCatSearch")
       obj.setAttribute("title", "Click here to expand search options")
       var o = document.getElementById("Catsearchpan2")
       o.setAttribute("class", "CatSearchOptnsClosed");
       o.setAttribute("className", "CatSearchOptnsClosed");
   }

   function GetWindowHeight() {
       var myWidth = 0, myHeight = 0;
       if (typeof (window.innerWidth) == 'number') {
           //Non-IE
           myWidth = window.innerWidth;
           myHeight = window.innerHeight;
       } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
           //IE 6+ in 'standards compliant mode'
           myWidth = document.documentElement.clientWidth;
           myHeight = document.documentElement.clientHeight;
       } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
           //IE 4 compatible
           myWidth = document.body.clientWidth;
           myHeight = document.body.clientHeight;
       }
       return myHeight;
   }

   function GetWindowWidth() {
       var myWidth = 0, myHeight = 0;
       if (typeof (window.innerWidth) == 'number') {
           //Non-IE
           myWidth = window.innerWidth;
           myHeight = window.innerHeight;
       } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
           //IE 6+ in 'standards compliant mode'
           myWidth = document.documentElement.clientWidth;
           myHeight = document.documentElement.clientHeight;
       } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
           //IE 4 compatible
           myWidth = document.body.clientWidth;
           myHeight = document.body.clientHeight;
       }
       return myWidth;
   }

   //Moved Script from Master Page to a Include file
function OpenPopup(sUrl, w, h, scroll) {
    var modal;
    try {

        modal = $find('PopupExndrMain');
        if (scroll == 'no') {
            var frame1 = document.getElementById("framePopupWindowWithoutScroll");
            document.getElementById("framePopupWindowWithScroll").className = "PopupWindowHidden"
            frame1.className="PopupWindow"
        }
        else {
            var frame1 = document.getElementById("framePopupWindowWithScroll");
            document.getElementById("framePopupWindowWithoutScroll").className = "PopupWindowHidden"
            frame1.className = "PopupWindow"
        }
        frame1.style.height = h + 'px';
        frame1.style.width = w + 'px';
        frame1.src = sUrl

        if (modal != null) {
            modal.show();
        } 
    }
    catch (e) {
        alert(e);
    }
return false;
}

function ClosePopup() {
    try {
        window.parent.ClosePopupWindow();
    }
    catch (e) {
    }
    return false;
}

function locationUpdatedHandler(sender, eventargs) {
    try {

        var iNewX = sender.get_location().x
        var iNewY = sender.get_location().y

        var iOldX = document.getElementById("hdnDragStartLocX").value;
        var iOldY = document.getElementById("hdnDragStartLocY").value

        if ((iNewX != iOldX) && (iNewY != iOldY)) {
            var extndr = $find(sender.get_id())
            extndr.set_location(new Sys.UI.Point(parseInt(iOldX), parseInt(iOldY)));
        }

        if ((iNewX > -230) && (iNewX < 204)) {
            __doPostBack("btnAddToLightboxDragDrop", sender.get_element().src);

        }
    }
    catch (e) {
        alert(e);
        }
}

function StartMoveHandler(sender, eventargs) {
    document.getElementById("hdnDragStartLocX").value = sender.get_location().x;
    document.getElementById("hdnDragStartLocY").value = sender.get_location().y;
}

function SideBarActiveTabChanged(sender, args) {
try
{
    setCookie("SideBarActiveTab", sender.get_activeTabIndex(), null);
}
catch (e)
{
    alert(e);
}

}
function DoDownload(obj) {
    document.getElementById(obj).click();
}
