﻿function OpenModalWindow(URL)
{
	//if (window.showModalDialog) {
	//	window.showModalDialog(URL,'popup','dialogWidth:1000px;dialogHeight:600px');
	//} else {

	window.open(URL, 'popup', 'modal=yes,width=1000,height=600,menubar=1,resizable=1,scrollbars=1');
	return false;
}

function OpenWindow(URL)
{
	window.open(URL, 'popup', 'width=900,height=500,menubar=1,resizable=1,scrollbars=1');
	return false;
}

function h1toggle(elementID)
{
	if(document.getElementById(elementID).style['display']=='block') 
		document.getElementById(elementID).style['display']='none'; 
	else 
		document.getElementById(elementID).style['display']='block'; 
}

function imgtoggle(parentElementID)
{
	imgPlusMinus = getChildNodeByType(document.getElementById(parentElementID), "IMG")[0];
	imgFolder = getChildNodeByType(document.getElementById(parentElementID), "IMG")[1];
	
	if(imgPlusMinus.src.indexOf("t_plus.gif")>-1)
		imgPlusMinus.src = "/img/t_minus.gif";
	else
		imgPlusMinus.src = "/img/t_plus.gif";
	
	if(imgFolder.src.indexOf("i_folder.gif")>-1)
		imgFolder.src = "/img/i_folder_open.gif";
	else
		imgFolder.src = "/img/i_folder.gif";
}

function ShowPopup(divID)
{
	$('#' + divID).show();

	var winHeight = $(window).height();
	var divTopOffset = $('#' + divID).offset().top;
	var divHeight = $('#' + divID).height();
	var winWidth = $(window).width();
	var divLeftOffset = $('#' + divID).offset().left;
	var divWidth = $('#' + divID).width();
	
	//alert("win height" + winHeight + "\r\ndiv offset:" + divTopOffset+ "\r\ndiv height:" + divHeight + "\r\n\r\n\r\n" );

	$('#' + divID).animate({top:(divTopOffset-2), left:(divLeftOffset-2)}, 1);
	
	if(divTopOffset + divHeight > winHeight)
		$('#' + divID).animate({top:(winHeight - divHeight - 10)}, 5);
	if(divLeftOffset + divWidth > winWidth)
		$('#' + divID).animate({left:(winWidth - divWidth - 10)}, 5);
	
}
 
function ShowDiv(divID)
{
    if(document.getElementById(divID) != null)
        document.getElementById(divID).style.display='block';
}

function FocusCursor(txtID)
{
    document.getElementById(txtID).focus();
}
 
function CloseDiv(divID)
{
    if(document.getElementById(divID) != null)
        document.getElementById(divID).style.display='none';
}

function SetEditTime(timeControl,annotationControl,staticAnnotationControl,annotationID)
{
    var time = new Date();
    var amPM = "";
    var month = time.getMonth() + 1;
    var day = time.getDate();
    var year = time.getFullYear();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    
    minutes = ((minutes < 10) ? "0" + minutes : minutes);                 
    seconds = ((seconds < 10) ? "0" + seconds : seconds);
    
    if (hours > 12)
    {
        hours = hours - 12;
        amPM = "PM";
    }
    else
        amPM = "AM";
        
    var rightNow = month + '/' + day + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds + " " + amPM;
    document.getElementById(timeControl).innerHTML = rightNow;

	if(annotationID && annotationID!="")
	    GetAnnotation(annotationControl,staticAnnotationControl,annotationID, rightNow);
}

function GetAnnotation(annotationControl,staticAnnotationControl,annotationID,rightNow)
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) 
        self.xmlHttpReq = new XMLHttpRequest();
    // IE
    else if (window.ActiveXObject) 
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    var url = 'posthandler.aspx?annotationid=' + annotationID + '&editStartTime=' + rightNow;
    self.xmlHttpReq.open('GET', url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) 
            LoadAnnotation(self.xmlHttpReq.responseXML,annotationControl, staticAnnotationControl);
    }
        self.xmlHttpReq.send("");
}

function GetContent(contentSectionAssetID, recordStatus, version, destinationControlID)
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) 
        self.xmlHttpReq = new XMLHttpRequest();
    // IE
    else if (window.ActiveXObject) 
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    var url = '/posthandler.aspx?getContent=1&ContentSectionAssetID=' + contentSectionAssetID + '&RecordStatus=' + recordStatus + '&Version=' + version;
    self.xmlHttpReq.open('GET', url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) 
            LoadContent(self.xmlHttpReq.responseXML, destinationControlID);
    }
        self.xmlHttpReq.send("");
}

var xmlDoc;
function LoadContent(xmlFile, destinationControlID)
{
  var xmlDoc = xmlFile;  

    if(xmlDoc!=null && xmlDoc.xml!="")
    {
        var error =  xmlDoc.getElementsByTagName("Error");
        if(error != null && error.hasChildNodes)
        {
            error = xmlDoc.getElementsByTagName("Error")[0].firstChild.nodeValue
            if(error != "")
                alert(error);
        }   

        if(xmlDoc.getElementsByTagName("Contents")[0].firstChild.nodeValue != null)
        {
			var contents = xmlDoc.getElementsByTagName("Contents")[0].firstChild.nodeValue;
            document.getElementById(destinationControlID).innerHTML = contents;
        }
    }
}

function LoadAnnotation(xmlFile,annotationControl, staticAnnotationControl)
{
  var xmlDoc = xmlFile;  

    if(xmlDoc!=null && xmlDoc.xml!="")
    {
        var error =  xmlDoc.getElementsByTagName("Error");
        if(error != null && error.hasChildNodes)
        {
            error = xmlDoc.getElementsByTagName("Error")[0].firstChild.nodeValue
            if(error != "")
                alert(error);
        }   

        if(xmlDoc.getElementsByTagName("Annotation")[0].firstChild.nodeValue != null)
        {
			var annotationContents = xmlDoc.getElementsByTagName("Annotation")[0].firstChild.nodeValue;
            document.getElementById(annotationControl).value = annotationContents;
			if(annotationContents.indexOf("<span ")>-1 || annotationContents.indexOf("<ol")>-1 || annotationContents.indexOf("<ul")>-1 || annotationContents.indexOf("<li>")>-1 || annotationContents.indexOf("<strong>")>-1 || annotationContents.indexOf("<em>")>-1 || annotationContents.indexOf("<u>")>-1 || annotationContents.indexOf("<a ")>-1 || annotationContents.indexOf("<h")>-1 || annotationContents.indexOf("<i>")>-1)
	        {
				document.getElementById(staticAnnotationControl).innerHTML = annotationContents;
				document.getElementById(staticAnnotationControl).style["display"] = 'block';
				document.getElementById(annotationControl).style["display"] = 'none';
			}
        }
    }
}
function verify()
{ 
 if (xmlDoc.readyState != 4)
 { 
  return false; 
 }
}
function PostAnnotation(contentAssetID,contentSectionID,annotationNameID,annotationDescriptionID,groupDropDownID,action,contentType,editTime,annotationID,divAnnotationTextID,imgAddAnnotationID,divErrorMessageID,closeDivID,showDivID)
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) 
        self.xmlHttpReq = new XMLHttpRequest();
    // IE
    else if (window.ActiveXObject) 
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    self.xmlHttpReq.open('POST', 'PostHandler.aspx', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) 
        {
           if(self.xmlHttpReq.responseText == "")
           {
                window.location = window.location;
                /*
                if(action == "update")
                window.location = window.location;
                    document.getElementById(divAnnotationTextID).innerText = document.getElementById(annotationDescriptionID).value;
                else if(action == "delete")
                {
                    //document.getElementById(imgAddAnnotationID).visible = true;
                }
                */
            }
            
            else
            {
				var divErrorMessage = document.getElementById(divErrorMessageID);

				if(divErrorMessage)
				{
					var response = self.xmlHttpReq.responseText;

					divErrorMessage.style.display = "block";
					divErrorMessage.style.border = "1px solid black";
					divErrorMessage.innerHTML = '<div style=\"background:#ccc;font-weight:bold;\">' + response.substring(response.indexOf('<Error>')+7, response.indexOf('</Error>')) +'</div>' +
						'<b>Current Annotation Description:</b><br/>'+
						response.substring(response.indexOf('<Annotation>')+12, response.indexOf('</Annotation>'));
						
					SetEditTime(editTime, "", "", "");
				}
            }
        }
            
    }
    var sendURL = GetAnnotationQueryString(contentAssetID,contentSectionID,annotationNameID,annotationDescriptionID,groupDropDownID,action,contentType,editTime,annotationID);

    if(sendURL != "")
        self.xmlHttpReq.send(sendURL);
}

function GetAnnotationQueryString(contentAssetID,contentSectionID,annotationNameID,annotationDescriptionID,groupDropDownID,action,contentType,editTimeID,annotationID) 
{
    if(document.getElementById(annotationNameID).value == "" && action != "delete")
    {
        alert('Please provide a valid Annotation name');
        return false;
    }
    else if(document.getElementById(annotationDescriptionID).value == "" && action != "delete")
    {
        alert('Please provide a valid Annotation description');
        return false;
    }
    else
    {
        var annotationName = document.getElementById(annotationNameID).value;
        var annotationDescription = document.getElementById(annotationDescriptionID).value;
        var groupID = groupDropDownID;
        editTime = document.getElementById(editTimeID).innerHTML;

        qstr = 'groupid=' + groupID + '&action=' + action + '&type=annotation&contentAssetID=' + escape(contentAssetID) + '&annotationID=' + annotationID + '&name=' + escape(annotationName) + '&text=' + escape(annotationDescription) + '&contentsectionid=' + contentSectionID + '&contenttype=' + escape(contentType) + '&edittime=' + escape(editTime); 
        return qstr;
    }
}

function PostBookmark(contentassetID,contentSectionID,bookmarkNameID,groupDropDownID,action,contentType, bookmarkID)
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) 
        self.xmlHttpReq = new XMLHttpRequest();
    // IE
    else if (window.ActiveXObject) 
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    self.xmlHttpReq.open('POST', 'PostHandler.aspx', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) 
        {
            UpdatePage(self.xmlHttpReq.responseText);
        }
    }
    
    var sendURL = GetBookmarkQueryString(contentassetID,contentSectionID,bookmarkNameID,groupDropDownID,action,contentType,bookmarkID);
    if(sendURL != "")
        self.xmlHttpReq.send(sendURL);
}

function GetBookmarkQueryString(contentassetID,contentSectionID,bookmarkNameID,groupDropDownID,action,contentType,bookmarkID) 
{
    if(document.getElementById(bookmarkNameID).value == "" && action != "delete")
    {
        alert('Please provide a valid Bookmark name');
        return false;
    }
    else
    {
        var groupDD = document.getElementById(groupDropDownID);
        var bookmarkName = document.getElementById(bookmarkNameID).value;
        var groupID = groupDD.options[groupDD.selectedIndex].value;
        
        qstr = 'groupid=' + groupID + '&action=' + action + '&type=bookmark&contentassetid=' + escape(contentassetID) + '&bookmarkid=' + bookmarkID + '&name=' + escape(bookmarkName) + '&contentsectionid=' + contentSectionID + '&contenttype=' + escape(contentType); 
        return qstr;
    }
}
function UpdatePage(str)
{
    if(str == "")
    {
        window.location = window.location;
    }
    else
    {
        alert(str);
    }
}

function AddEventsToTable(ContainerID)
{
	var objContainer = document.getElementById(ContainerID);
	if(objContainer)
	{
		var elements;
		if(objContainer.tagName == "TABLE")
		{
			var tBody = objContainer.getElementsByTagName('TBODY');
			if(tBody)
				elements = tBody[0].getElementsByTagName('TR');
			else
				elements = objContainer.getElementsByTagName('TR');
		}
		else
			elements = objContainer.getElementsByTagName('LI');

		for(var i=0;i<elements.length;i++){
			elements[i].onmouseover = highlightTableRow;
			elements[i].onmouseout = resetRowStyle;
		}
	}
}

function AddEventsToDivDLs(ContainerID)
{
	var objContainer = document.getElementById(ContainerID);
	if(objContainer)
	{
		var elements;
		if(objContainer.tagName == "DIV")
			elements = objContainer.getElementsByTagName('DL');

		for(var i=0;i<elements.length;i++){
			elements[i].onmouseover = highlightTableRow;
			elements[i].onmouseout = resetRowStyle;
		}
	}
}


function highlightTableRow()
{
 	this.className = this.className + " highlight";
}
function resetRowStyle()
{
	this.className = this.className.replace(" highlight", "").replace("highlight", "");
}
getChildNodeByType = function(obj,type){
	var thisArr = new Array();
	for(o=0;o<obj.childNodes.length;o++){if(obj.childNodes[o].nodeName==type){ thisArr[thisArr.length] = obj.childNodes[o]; } }
	return thisArr;
}

function daysInMonth(month,year) 
{
	var dd = new Date(year, month, 0);
	return dd.getDate();
} 

function actionOnEnter_NewBmk(e, ctl_save, ctl_cancel)
{
    var keynum;
    if(window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    if (keynum == 13)
    {
        document.getElementById(ctl_save).click();
        return false;
    }
    if (keynum == 27)
    {
        document.getElementById(ctl_cancel).click();
        return false;
    }
    return true;
}