﻿// JScript File
//This file should be rendamed to Common or Global, as its used
// for a number of purposes.
//Globals
var isIe = false;
var isBrowserSupported = false;

var myHighlightId = 0;
var myHighlightMenu = "";
var myHighlightChangeId;

if(navigator.appName == "Netscape")
{
    isIe = false;
    isBrowserSupported = true;
}

if(navigator.appName == "Microsoft Internet Explorer")
{
    isIe = true;
    isBrowserSupported = true;
}

var PageUpdate = new PageUpdateObject(PageRefresh);

   
function Refresh()
{
    PageUpdate.DoUpdate();
}

function PageRefresh()
{
    var reload = document.URL;
    if(reload.indexOf('#') > 0)
    {
        reload = reload.substr(0,reload.indexOf('#'));
    }
    document.location = reload;
}

function PageUpdateObject(method)
{
    this.DoUpdate = method;
}


function SearchHeader_OnSelectedIndexChangedHandler(item)
{
    __doPostBack('myHeaderSearchBtn','');
}



function StripHtml(stringIn)
{
    if(stringIn != null && stringIn.length > 0)
    {
        var re = new RegExp("<(.|\\n)*?>", "gim");
        var stringOut = stringIn.replace(re, "");
        return stringOut;
    }
    
    return "";
}

function CleanInputOfHtml(objInputElement)
{
    objInputElement.value = StripHtml(objInputElement.value);
}

function cancelEvent(e)
{
    if(isIe)
    {
        e.returnValue=false;
        e.cancel = true;
    }
    else
    {
        e.preventDefault();
    }
}

function OnDockClientCommand(docObj, eventArgs)
{
    var argVals = eventArgs.Name.split(":");
    var argName = argVals[0];
    
    switch(argName)
    {
        case "Editor":
            var editWindUrl  = GetEditLinkUrl(docObj.id);
            OpenEditWindow(editWindUrl,960, 560);
            break;
        case "ReviewContent":
            CallCommonServices_SubmitChangeForReview(argVals[1], "Content");
            break;
        case "ReviewProduct":
            CallCommonServices_SubmitChangeForReview(argVals[1], "Product");
            break;
       case "Publish":
            //Postback handler
            break;
       case "Reject":
            //Postback handler
            break;
       case "Delete":
           // Postback handler
            break;
       case "RelatedProductEditor":
            var editWindUrl  = GetEditorLink(argName, argVals[1]);
            OpenEditWindow(editWindUrl,815, 658);
            break;
    }
   
}

function GetEditLinkUrl(id)
{
    var sp = id.split('_');
    return GetEditorLink(sp[sp.length - 2],sp[sp.length - 1]);
}

function GetEditorLink(type, id)
{
    switch(type)
    {
        case "Prod":
            return "PopUpEditors/PopUpProductEditor.aspx?site=" + mySiteId + "&section=" + mySectionId + "&page=" + myPageId + "&product=" + id;
            break;
       case "Content":
            return "PopUpEditors/PopUpContentEditor.aspx?site=" + mySiteId + "&section=" + mySectionId + "&page=" + myPageId + "&content=" + id;
            break;
       case "Page" :
            return "PopUpEditors/PopUpPageEditor.aspx?site=" + mySiteId + "&section=" + mySectionId + "&page=" + id;
            break;
       case "Highlight" :
            return "PopUpEditors/PopUpHighlightEditor.aspx?site=" + mySiteId + "&section=" + mySectionId + "&page=" + myPageId + "&highlight=" + id;
            break;
       case "RelatedProductEditor" :
            return "PopUpEditors/PopUpProductAssociationForm.aspx?site=" + mySiteId + "&section=" + mySectionId + "&page=" + myPageId + "&product=" + id;
            break;
       case "Section" :
            var ret = "PopUpEditors/PopUpSectionEditor.aspx?site=" + mySiteId + "&section=" + id;
            if(id == 0)
                ret += "&new=true";
            return ret;
            break;
    }
}

function OpenEditWindow(strUrl, width, height)
{
	var props;
	var popUpWin;
	var x = 0, y = 0; // default values

    if (document.all) {
      x = window.screenTop;
      y = window.screenLeft;
      props= "width=" + width + ",height=" + height + ",scrollbars=1,resizable=1,top=" + x + ",left=" + y;
    }
    else if (window.screenX) {
      x = window.screenX + 25;
      y = window.screenY + 25;
      props= "width=" + width + ",height=" + height + ",scrollbars=1,resizable=1,screenY=" + y + ",screenX=" + x;
    }
	//props= "width=" + width + ",height=" + height + ",scrollbars=1,resizable=1,top=" + x + ",screenY=" + y + ",left=" + y + ",screenX=" + x;
	
	popUpWin = window.open(strUrl,'EditWin',props);
	popUpWin.window.focus();
}

function OpenPrintWindow(strUrl)
{
    OpenEditWindow(strUrl, 650, 850);
}

function LeftMenuClientContextClickHandler(node, itemText, itemID)
{
    var EditorLink = "";
     
     if(itemText == "New Section") 
     {
        OpenSectionEditor(0);
     }
     
    if(itemText == "New Page") 
    {
        if(node.Category == "Section")
        {
            //Create a root level page
            EditorLink = GetEditorLink("Page", 0);
            EditorLink += "&parent=0&newPage=true";
        }
        else if(node.Category == "Page")
        {
            EditorLink = GetEditorLink("Page", 0);
            EditorLink += "&parent=" + node.Value + "&newPage=true";
        }
        OpenEditWindow(EditorLink,615,398);
    }
    
     if(itemText == "New Product") 
    {
        if(node.Category == "Page")
        {
            var nVal = node.Value.split(":");
            EditorLink = "PopUpEditors/PopUpProductEditor.aspx?site=" + mySiteId + "&section=" + mySectionId + "&page=" + nVal[0] + "&product=0";
            
            OpenEditWindow(EditorLink,815,498);
        }
        else
        {   
            alert("Invalid node for this action");
        }
        
    }
    
     if(itemText == "New Content") 
    {
        if(node.Category == "Page")
        {
            var nVal = node.Value.split(":");
            EditorLink = "PopUpEditors/PopUpContentEditor.aspx?site=" + mySiteId + "&section=" + mySectionId + "&page=" + nVal[0] + "&content=0";
            OpenEditWindow(EditorLink,815,498);
        }
        else
        {   
            alert("Invalid node for this action");
        }
        
    }
    
    if(itemText == "Edit") 
    {
        if(node.Category == "Section")
        {
            alert("Under Construction");
            return;
        }
        else if(node.Category == "Page")
        {
            var nVal = node.Value.split(":");
            EditorLink = GetEditorLink("Page", nVal[0]);
        }
       
        OpenEditWindow(EditorLink,615,398);
    }
    
    if(itemText == "Publish" || itemText == "Reject" || itemText == "Delete") 
    {
        var nVal = node.Value.split(":");
        var link = "PublishRejectDeleteHandler.aspx?action=" + itemText + "&changeId=" + nVal[1] + "&target=" + node.Category + "&id=" + nVal[0];
        document.location = link;
    }
   
}
  
function OnClientDock(dockObj, eventArgs)
{
    
    var zone = RadDock_GetDockingZone("ctl00_MainContentArea_MainContentAreaZone");
    
    if(zone == null)
        return;
        
    var tblDocDesc = document.getElementById("ctl00_MainContentArea_MainContentAreaZone_myTableSaveDocDesc");
    var saveDocStateBtn= document.getElementById("ctl00_MainContentArea_MainContentAreaZone_mySaveDocState");
    var itemsMoved = document.getElementById("ctl00_MainContentArea_MainContentAreaZone_myItemsMoved");
    
    if(dockObj.id.indexOf("Prod") > 0)
        itemsMoved.value += dockObj.id.replace("ctl00_MainContentArea_MainProductZone_", "") + ";";
    else
        itemsMoved.value += dockObj.id.replace("ctl00_MainContentArea_MainContentAreaZone_", "") + ";";
    
    tblDocDesc.innerHTML = "To save the order changes, press the button to the right -->";
    saveDocStateBtn.style.visibility = "visible";
}
 
 /* AJAX Item Order Service
   
   Begin  */
function CallDisplayOrderService(type, id, index)
{
    DisplayOrder.SetItemOrder(type, id, index, CallDisplayOrderService_Complete, CallDisplayOrderService_Error);
}

function CallDisplayOrderService_Complete(ResponseAsJSON, ResponseAsXml, ResponseAsText)
{
    if(!ResponseAsJSON)
    {
        alert("Item Order Service Failed, please reload the page and try again.");
    }
}

function CallDisplayOrderService_Error(args)
{
    alert("Error:" + args);
}
/* End
   
   AJAX Item Order Service */

/* Send Review Request */
function CallCommonServices_SubmitChangeForReview(changeId, type)
{
    CommonServices.SubmitChangeForReview(changeId, type, CallCommonServices_SubmitChangeForReview_Complete, CallCommonServices_SubmitChangeForReview_Error);
}

function CallCommonServices_SubmitChangeForReview_Complete(ResponseAsJSON, ResponseAsXml, ResponseAsText)
{
    if(ResponseAsJSON)
    {
        alert("Review request successfully sent.");
    }
}
function CallCommonServices_SubmitChangeForReview_Error(args)
{
    alert("Error Sending Request for review message, Error was:"  + args.ErrorText); 
}
/* Highlihgt Edit Page Support used by multiple pages. */
function OpenHighlightEditor(id, highlightTypeId)
{
     var eLink = GetEditorLink("Highlight",id);
     eLink += "&highlightTypeId=" + highlightTypeId;
     OpenEditWindow(eLink, 850, 400);
}


function SubmitForReview(changeRefId)
{
    if( confirm("Submit this change for review?") )
    {
        alert("Under Construction");
        return false;
    }
}

function OpenRadDialog(dialogueUrl, title, width, height)
{
    var winManager = GetRadWindowManager();
    var dialogueWin = winManager.GetWindowByName("DialogWindow");
    dialogueWin.SetTitle(title);
    dialogueWin.SetSize(width, height);
    dialogueWin.SetUrl(dialogueUrl);
    dialogueWin.Show();
}


/* Section Editing */

function OpenSectionEditor(id)
{
    var eLink = GetEditorLink("Section",id);
     OpenEditWindow(eLink, 850, 400);
}

/* Content Editor Support Functions */
function showPopUp(strUrl)
{
	var props
	var popUpWin
		
	props= "width=700,height=500,left=10,screenX=40,screenY=40"
	//alert("Opening:" + strUrl)
	popUpWin = window.open(strUrl,'popUpWin',props)
	popUpWin.window.focus();
}

function showPopUp2(strUrl)
{
	var props
	var popUpWin
		
	props= "width=754,height=432,left=10,screenX=40,screenY=40"
	//alert("Opening:" + strUrl)
	popUpWin = window.open(strUrl,'popUpWin',props)
	popUpWin.window.focus();
}
	
function showPopUp2(strUrl, intHeight, intWidth )
{
	var props
	var popUpWin
		
	props= "width=" + intWidth + ",height=" + intHeight + ",left=10,screenX=40,screenY=40,toolbar=no"
	//alert("Opening:" + strUrl)
	popUpWin = window.open(strUrl,'popUpWin',props)
	popUpWin.window.focus();
}

/* Embed Flash Object */
function CreateFlashControl(DivID, WIDTH, HEIGHT, URL, ExtendedEmbedAttributes)
{
	var d = document.getElementById(DivID);

    var flashScript = " <embed height='" + HEIGHT + "' menu='false'" +
	                  " pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high'" +
	                  " type='application/x-shockwave-flash' WMODE='transparent' style='margin:0px; padding:0px'" +
	                  " src='" + URL + "' width='" + WIDTH + 
	                  "' " + ExtendedEmbedAttributes + " ></embed> ";
    
    d.innerHTML = flashScript;
    d.style.height = HEIGHT;
    d.style.width = WIDTH;
}
/* Search Box */

function PlaceSearchBox()
{
    var hSearcFrm = document.getElementById("header_searchform");
    if(hSearcFrm != null)
    {
        //var menuContainer = document.getElementById("MainMenu");
        var frameSize = windowSize();
        var center =frameSize.width / 2;
        var leftPos = center + 240;
        hSearcFrm.style.left =  leftPos.toString() + "px";
    }
}

function windowSize() 
{
    var myWidth = 0, myHeight = 0;

    if(typeof(window.innerWidth ) == 'number' ) {
        myWidth = window.innerWidth - 30;
        myHeight = window.innerHeight;
    } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight; 
    } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth; 
        myHeight = document.body.clientHeight;
    }
    
    return { width: myWidth, height: myHeight };
}

window.onresize = PlaceSearchBox;

/* End Searchbox */

/* Dreamweaver support */
    function MM_jumpMenu(targ, selObj, restore) //v3.0
	{
		eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
		if (restore) selObj.selectedIndex = 0;
	}

	function MM_findObj(n, d) //v4.0
	{
		var p, i, x;

		if (!d) d = document;

		if ((p = n.indexOf("?")) > 0 && parent.frames.length)
		{
			d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
		}

		if (!(x = d[n]) && d.all) x = d.all[n];

		for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];

		for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);

		if (!x && document.getElementById) x = document.getElementById(n);

		return x;
	}

	function MM_jumpMenuGo(selName, targ, restore) //v3.0
	{
		var selObj = MM_findObj(selName);
		
		if (selObj) MM_jumpMenu(targ, selObj, restore);
	}

	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

	function MM_preloadImages() { //v3.0
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	
/* End Dreamweaver support */

function autoIframe(frameId)
{
    try
    {
        frame = document.getElementById(frameId);
        innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
        objToResize = (frame.style) ? frame.style : frame;
        objToResize.height = innerDoc.body.scrollHeight + 30;
    }
     catch(err){
        window.status = err.message;
    }
}

//Default Button Handlers
function AddKeyDownHandler(handler)
{
    if (document.addEventListener)
    {
       document.addEventListener('keydown',handler,false);
    }
    else if (document.attachEvent)
    {
       document.attachEvent("onkeydown", handler);
    }
    else
    {
        document.onkeydown = handler;
    }
}

function KeyDownHandler(e)
{
    // process only the Enter key
    if (e.keyCode == 13)
    {
        var btn = GetDefaultBtn();
        alert(btn);
        // cancel the default submit
        cancelEvent(e);
        // submit the form by programmatically clicking the specified button
        btn.click();
        return false;
    }
}

function clickButton(e, buttonid)
{ 
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt)
      { 
         if (evt.keyCode == 13)
        { 
            cancelEvent(evt);
            bt.click(); 
            return false; 
        } 
      } 
}
//helper function for IFRAME Pages. Allows the
//EMBEDED IFRAM page to change the location
//of the parent page.
function OpenParentUrl(url)
{
    window.parent.location = url;
}

var myLastHeight = 0;
function ResizeLeftManu()
{
    var leftMenu = document.getElementById("LeftMenu");
    var mainTable = document.getElementById("MainLayoutTable");
    if(mainTable && leftMenu)
    {
        var newHeight = mainTable.clientHeight - 12;
        var winLoc = window.location.pathname.toLowerCase();
        if(winLoc.indexOf("product") > -1)
            newHeight = mainTable.clientHeight;
        
        if(newHeight - myLastHeight > 15)
        {
            leftMenu.style.height = newHeight + "px";
            myLastHeight = newHeight;
        }
    }
}
window.onload = ResizeLeftManu;

function OpenRegionSelectWindow()
{
    var winManager = GetRadWindowManager();
    var dialogueWin = winManager.GetWindowByName("DialogWindow");
    dialogueWin.SetTitle('Please Select Region');
    dialogueWin.SetSize(972, 462);
    var path = "Dialogues/SelectRegionWin.aspx";
    dialogueWin.SetUrl(path);
    dialogueWin.Show();
}
