﻿// JScript File

        var strQuoteRequest="";
        var strSelect="";
        var Select = "Select";
        var Viewing = "ArrangeViewing";
        var Request = "RequestQuote";
        var isItemInCart= false;
        var req,wastage;
        var gCurrentContextBc = "";
        
        // Create a wastage for the form moving
        function setWastage(_wastage){
            wastage = _wastage;
            //alert(_wastage);
        }
        
        /*Delete from the basket*/
        /*It checks if the lnkRemove for a pirticular selection is created in the basket panel. If created then the second click on the same button
        means it is a request to unselect (delete) by calling the postback with the ID. If not created return false so that the main function can add to basket*/
        function unSelect(t){
            var _removeEvent = findObjWithClientId("lnkRemove" + t);
            var _removeLinkId;
            
            if(_removeEvent != null){
                _removeLinkId = _removeEvent.id.replace(/_/g,"$"); // Rectify the ID
                __doPostBack(_removeLinkId,''); // Calling the remove link's postback

                return true;
            }else{
                return false;
            }
            
        }
        
        function over(obj, over){
           //document.getElementById(obj).className = over;
        }
        function out(obj, out){
           //document.getElementById(obj).className = out;
        }
        
        function addQuoteRequest(type,id,name,data){
            var d;
            if(type == Request){
                d = document.getElementById("btnRequestQuote" + id);
                if(d != null){
                    d.disabled = true;
                    d.className = "quote-on";
                    //d.innerText = "Quote Requested";
                 }
                
                GetItemsBasket(type,id,name,data);
            }else if(type == Viewing){
                GetItemsBasket(type,id,name,data);
                d = document.getElementById("btnArrange" + id);
                if(d != null){
                    //d.innerText = "Viewing Arranged";
                    d.className = "visit-on";
                }
            }else if(type == Select){
                GetItemsBasket(type,id,name,data);
            }
            //alert(d.className);
            var checkbox = document.getElementById("chkBc" + id);
            if(checkbox != null){
                //checkbox.innerText = "OFFICE SELECTED"
                checkbox.className = "select-on";
                //checkbox.checked = true;
                checkbox.disabled = true;
             }
            
            //return true;
        }
        var removeMode="";
        function removeItem(id,type){
            removeMode = "remove"
            GetItemsBasket(type,id,"remove","");
            returnState_btnRequestQuote(id);
            returnState_btnArrange(id);
            returnState_chkBc(id);
        }
        
        function removeItem1(id,type){
            var d = document.getElementById('requestList');
            removeMode = "remove"
            GetItemsBasket(type,id,"remove","");
            //returnState(type,id);
            returnState_btnRequestQuote(id);
            returnState_btnArrange(id);
            returnState_chkBc(id);
        }
    
        function returnState(type,id){
            try {
                if(type == Request){
                    returnState_btnRequestQuote(id);
                }else if(type == Viewing){
                    returnState_btnArrange(id);
                }else{
                    returnState_chkBc(id);
                }
            } catch( e ) {
               // Ignoring error since an item couldve been added from a different page, 
               // would not be possible to set values back as page the context is lost
            }
        }
        function returnState_btnArrange(id){
            
            var d = document.getElementById("btnArrange" + id)
            if(d != null){
                d.disabled = false;
                d.className = "visit";
                //d.innerText = "Arrange Viewing"
                returnState_chkBc(id);
            }
        }
        function returnState_btnRequestQuote(id){
            var d = document.getElementById("btnRequestQuote" + id)
            if(d != null){
                d.disabled = false;
                d.className = "quote";
                //d.innerText = "Request Quote";
                returnState_chkBc(id);
            }
        }
        function returnState_chkBc(id){
            var d = document.getElementById("chkBc" + id)
            if(d != null){
                d.disabled = false;
                d.className = "select";
                //d.innerText = "SELECT OFFICE";
             }
        }
    
        var req;
        
        function GetItemsBasket(type,id,name,data){
            var url;
            if(type == ''){
                url = "../Registration/ClientSelectionDynamic.aspx";
                }else{
                if(removeMode == ''){
                    url = "../Registration/ClientSelectionDynamic.aspx?type=" + type + "&id=" + id + "&name=" + name + "&data=" + data;
                }else
                {
                    url = "../Registration/ClientSelectionDynamic.aspx?removeMode=" + removeMode + "&type=" + type + "&id=" + id + "&name=" + name + "&data=" + data;
                    removeMode = "";
                }
            }
            var currentTime = new Date();
            document.getElementById('frmBasket').src = url + "&time=" + currentTime;
            
        }
        

        function processReqChange(){
            if (req.readyState==4){
                var d = document.getElementById('requestList');
                if(req.responseText.length > 30){
                    if(findObjWithClientId("btnRegister") != null){
                        //findObjWithClientId("btnRegister").disabled = false;
                        isItemInCart = true;
                    }
                }else{
                    if(findObjWithClientId("btnRegister") != null){
                        //findObjWithClientId("btnRegister").disabled = true;
                        isItemInCart = false;
                    }
                }
                d.innerHTML = req.responseText;
            }
        }
        
        
        function onRegister(sitefor){
            //isItemInCart Variable is declared in SelectOffices.js and value assigned processReqChange() function
            // This logic also includes enabling/disabling of the submit button
            //alert(isItemInCart);
            //alert(sitefor)
            if(findObjWithClientId("pnlBasket").innerHTML.length >  30){
                location.href = "../" + sitefor + "/Registration";
            }
        }

        function adjustMyFrameHeight()
        {
            var frame = getElement("frmBasket");
            var frameDoc = getIFrameDocument("frmBasket");
            frame.height = frameDoc.body.offsetHeight;
            if(frameDoc.getElementById("requestList") != null){
                if(frameDoc.getElementById("requestList").innerHTML.length >  30){
                    isItemInCart = true;
                }else{
                    isItemInCart = false;
                }
            }
        }
    
    // Makes a server call to create basket list
    function SendSelectedToServer(isCurrentClickOn,isSelectOn,clickedObjectItemHeight,clickedObjectId,type,id,MovingformSiteMode,data,extended_data)
	{
		var url,maxBc,bottomPos; 
		var mydate= new Date()
		var bcFocusDummyDiv = document.getElementById("Dummy" + id);
		var isDeleteCommandEvent = false;
		
		
		maxBc = 10;
		
        /* On every click of each different types of button(Av, RQ, S), 
        the below condition checks if the the BC is already selected (S) and the current button click is not (R or AV). 
        If both combinations are there, then go ahead and remove from the basket otherwise dont remove but just add the new selecttion (RQ / AV)*/
		if(isSelectOn == 'true' && isCurrentClickOn == 'true'){
		    isDeleteCommandEvent = unSelect(id);  
		}
		
		// Get the ButtonPanel Id
		gCurrentContextBc = id;
		
		try {
		        // Move the form    		    
	        if(MovingformSiteMode == "1" || MovingformSiteMode == "2")
	        {
		        d = document.getElementById("FormRefresh");
			    
		        if(d != null){
			        if (clickedObjectId > 0){
    					
				        if(document.getElementById("basket").innerHTML.length >= 500 && clickedObjectId >= maxBc - 1){
					        d.style.top = (clickedObjectId * clickedObjectItemHeight) + (wastage-200) + "px";
				        }else{
					        d.style.top = (clickedObjectId * clickedObjectItemHeight) + wastage + "px"; 			
				        }
			        }
		        }
	        }
	        
	        // If the event is not to be deleted then go ahead and Add to basket
	        
            if(!isDeleteCommandEvent){	  
		        url = "../Process/SelectBusinessCentre.aspx";

    		    
                    if (window.XMLHttpRequest) {    // branch for native XMLHttpRequest object
                        req = new XMLHttpRequest();
                        
                    } else if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
                        try{
                            req = new ActiveXObject("Msxml2.XMLHTTP");
                        } 
                        catch (e) {
                            try {
                                req = new ActiveXObject("Microsoft.XMLHTTP");
                            } 
                            catch (e) {}
                        }
                    }
                   
                    url = url + "?bc=" + id + "&type=" + type + "&data=" + data + "&extended_data=" + extended_data + "&time=" + mydate;
                   
                    if(req){
                        req.onreadystatechange = submit;
                        //Asyncronous call by setting the 3rd parameter true, if false it will be dynchromous and will lock the browser
                        req.open("GET", url, true);
                        req.send("");
                    }
            }else{
                //alert("By unselecting you have removed the selection from the basket")
            }
        }catch( e ) {
            alert('Caught Exception: ' + e.description);
        }
    }

    function submit(){
        if (req.readyState==4){       
            //alert(gCurrentContextBc);
           __doPostBack(findObjWithClientId("AddButtonsPanel" + gCurrentContextBc).id,'');
           //__doPostBack(findObjWithClientId("UpdatePanelBasket").id,'');
           //alert(arrangevisit_req.responseText);
        }
    }
    
    var LastMouse = new Array(0, 0);
    var isIE = document.all ? true : false;

    function MouseCatch(e) 
    {
    //var divY = parseInt(document.getElementById("rightcolumn").style.top);
   // mouseY
      if (isIE)
      {       
        LastMouse = [event.clientX, (e||event).clientY + document.documentElement.scrollTop];       
      }
      else
      {
        LastMouse = [e.pageX,(e||event).clientY + document.documentElement.scrollTop];
      };
    }; 
 
  
    function MoveDiv(MovingformSiteMode) 
    {
    
        //alert(MovingformSiteMode);
        if(document.getElementById("ResultMap") != null)
        {
           if(document.getElementById("ResultMap").style.display=="block")
           {
                document.getElementById("ResultMap").style.display="none";
           }
        }
        
        if(MovingformSiteMode == "1")
        {
            d = document.getElementById("rightcolumn")
            //alert("Aptus/Comp/Mana");
        }
       else
       {
            d = document.getElementById("rightcol")
            //alert("Man/Lon");
       }                
       if(d != null)
       {      
       //alert(LastMouse[1]);
       if(document.getElementById("rbox") != null)
       {
            if(document.getElementById("rbox").style.display=="none")
            {
                document.getElementById("rbox").style.display="block";
            }
       }
        if(LastMouse[1] < 1000) // For first result <conditioning>
        {
             d.style.top = 0 + "px"; // fix the position always to fix place on the top as default
                 //alert("test");
                 }
        else if(LastMouse[1] > 1500 && LastMouse[1] < 3500) // For last results <conditioning>
        {
             d.style.top = (LastMouse[1]-990) + "px"; // In last results clicks slightly push back to middle
             //alert(d.style.top);
             if(MovingformSiteMode == "1")
             {
                    if(document.getElementById("rbox").style.display=="block")
                    {
                         document.getElementById("rbox").style.display="none";
                    }
             }
         }     
        else if(LastMouse[1] > 3500)  
        {
             d.style.top = (LastMouse[1]-1090) + "px";
              //alert(LastMouse[1]); 
              if(document.getElementById("rbox").style.display=="block")
                    {
                         document.getElementById("rbox").style.display="none";
                    }
        }
        else
        {          
             d.style.top = (LastMouse[1]-800) + "px";  // This is anything in between  
             }   
             }      
       // alert(LastMouse[1]);
    };   
    document.onmouseup = MouseCatch;
    

var isIE = document.all;
var mouseX = 0;
var mouseY = 0;

function getMouseXY(e)
{
if (!e) e = window.event;
if (e)
{
mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
mouseY = isIE ? (e.clientY + document.body.scrollTop) : e.pageY;
}
}

document.onmouseup = getMouseXY;

