﻿//------------------------------------------------------------------------------ APPLICATION FUNCTIONS --------------------------------------
function ShowAddForm()
    {
    document.getElementById("tbldialog").style.display = "";
    document.getElementById("tddialog").style.display = "none";
    document.getElementById("tdNewProject").style.display = "";
    document.getElementById("divtransparency").style.display = "";
    document.getElementById("trtemplatelist").style.display = "";
    document.getElementById("txtCopyFromProject").value = "";
    document.getElementById("spanheading").innerHTML = "New Project";
    document.getElementById("txtname").value = "";
    document.getElementById("txtname").focus();
    }

function CloseDialog()
    {
    document.getElementById("tbldialog").style.display = "none";
    document.getElementById("divtransparency").style.display = "none";
    document.getElementById("tdAddMessage").innerHTML = "";
    
    }

function PositionUploadBox()
	{
	var divPlaceHolder = document.getElementById("divUploadPlaceHolder")
	if (divPlaceHolder != null)
		{
		document.getElementById('divupload').style.top=divPlaceHolder.offsetTop;
		document.getElementById('divupload').style.left=divPlaceHolder.offsetLeft;
		document.getElementById('divupload').style.display="";
		}
	}
    
function showicons(idx)
	{
	document.getElementById("tr"+idx).style.backgroundColor="yellow";
	if (document.getElementById("imgdelete"+idx) != null) {document.getElementById("imgdelete"+idx).src="images/delete.gif"}
	if (document.getElementById("imgcopy"+idx) != null) {document.getElementById("imgcopy"+idx).src="images/copy.gif"}
	if (document.getElementById("imgedit"+idx) != null) {document.getElementById("imgedit"+idx).src="images/edit.gif"}
	}
	
function hideicons(idx)
	{
	document.getElementById('tr'+idx).style.backgroundColor="";
	if (document.getElementById("imgdelete"+idx) != null) {document.getElementById('imgdelete'+idx).src="images/delete_light.gif"}
	if (document.getElementById("imgcopy"+idx) != null) {document.getElementById('imgcopy'+idx).src="images/copy_light.gif"}
	if (document.getElementById("imgedit"+idx) != null) {document.getElementById('imgedit'+idx).src="images/edit_light.gif"}
	}

function deleteproject(sProjectName)
	{
	//ask the user if they're sure
	if(confirm("The project '"+sProjectName+"' will be permanently deleted.") == true)
		{
		//change the cursor to an hourglass
		document.body.style.cursor="wait";
		//Set the action to delete and submit the form
		document.getElementById('txtaction').value="deleteproject:"+sProjectName;
		//document.form1.submit();
		document.getElementById("form1").submit();
		}
	}
	
function copyproject(sProjectName)
	{
	//Show the create project dialog
    document.getElementById("tbldialog").style.display = "";
    document.getElementById("divtransparency").style.display = "";
    document.getElementById("trtemplatelist").style.display = "none";
    document.getElementById("txtCopyFromProject").value = sProjectName;
    document.getElementById("tddialog").style.display = "none";
    document.getElementById("tdNewProject").style.display = "";
    document.getElementById("spanheading").innerHTML = "Copy Project";
    document.getElementById("txtname").value = "Copy of " + sProjectName;
    document.getElementById("txtname").focus();
	}
	
function StorePassword()
    {
    reqstring = "action=WriteAppPassword&AppPassword="+document.getElementById("txtManageProjectsPassword").value;
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") {http = new ActiveXObject("Microsoft.XMLHTTP");} else {http = new XMLHttpRequest();} 
    http.onreadystatechange=function() 
        {
        if(http.readyState == 4) 
            {
            if (http.status == 200) 
                {
                //If the password was successfully written to the server, refresh the form
    			document.getElementById("form1").submit();
                }
            else 
                {
                alert("Error: "+http.status+http.responseText);
                }
            }
        }

    //This function allows us to trap any errors
    http.open("POST","AjaxAction.aspx",true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(reqstring);
    
    }
			
function ShowAppPasswordPrompt()
    {
    //Build the form html
    var s="";
    s = s + "<table style='width:100%;height:100%;background-color:white' border=0 cellspacing=0 cellpadding=10>";
    s = s + "<tr style='height:10px'><td></td><td></td></tr>"
    s = s + "<tr style='height:20px'><td align='right'>New Password:</td><td><input type='password' id='txtNewAppPassword' style='width:200px' onKeyPress='return FormPassword_KeyPress(event);'></td></tr>";
    s = s + "<tr style='height:20px'><td align='right'>Confirm Password:</td><td><input type='password' id='txtNewAppPassword_confirm' style='width:200px' onKeyPress='return FormPassword_KeyPress(event);'></td></tr>";
    s = s + "<tr><td></td><td align='right'><input style='width:100px' type='button' value='OK' onclick='WriteAppPassword()'>&nbsp;&nbsp;&nbsp;</td></tr>"
    s = s + "</table>"
    
    //Show the create project dialog
    document.getElementById("tbldialog").style.display = "";
    document.getElementById("divtransparency").style.display = "";
    document.getElementById("tddialog").style.display = "";
    document.getElementById("tddialog").innerHTML = s;
    document.getElementById("tdNewProject").style.display = "none";
    document.getElementById("spanheading").innerHTML = "Assign Password";
    document.getElementById("txtNewAppPassword").focus();
    }

function FormPassword_KeyPress(e)
    {
    var key;
	if(window.event)
		{
		key = window.event.keyCode;     //IE
		}
	else
		{
		key = e.which;     //firefox
		}

	if(key == 13)
		{
		WriteAppPassword()
		return false;
		}
	else
		{
		return true;
		}
    }

function FormEnterPassword_KeyPress(e)
    {
    var key;
	if(window.event)
		{
		key = window.event.keyCode;     //IE
		}
	else
		{
		key = e.which;     //firefox
		}

	if(key == 13)
		{
		StorePassword();
		return false;
		}
	else
		{
		return true;
		}
    }

function RemoveFormPassword()
    {
    //Remove the form password
    reqstring = "action=AssignAppPassword";
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") {http = new ActiveXObject("Microsoft.XMLHTTP");} else {http = new XMLHttpRequest();} 
    http.onreadystatechange=function() 
        {
        if(http.readyState == 4) 
            {
            if (http.status == 200) 
                {
                alert("The 'Manage Projects Form' password has been removed.");
                //Reload the form
                window.location.href="default.aspx"
                }                
            else 
                {
                alert("Error: "+http.status+http.responseText);
                }
            }
        }

    //This function allows us to trap any errors
    http.open("POST","AjaxAction.aspx",true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(reqstring);    
    }

function WriteAppPassword()
    {
    //verify that the password is valid and the two match.
    var sPassword = document.getElementById("txtNewAppPassword").value;
    var sPassword_confirm = document.getElementById("txtNewAppPassword_confirm").value;
    if (sPassword != sPassword_confirm)
        {
        alert("The passwords do not match.")
        return false;
        }
    
    reqstring = "action=AssignAppPassword";
    reqstring = reqstring + "&AppPassword="+document.getElementById("txtNewAppPassword").value;
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") {http = new ActiveXObject("Microsoft.XMLHTTP");} else {http = new XMLHttpRequest();} 
    http.onreadystatechange=function() 
        {
        if(http.readyState == 4) 
            {
            if (http.status == 200) 
                {
                if (sPassword == "")
                    {
                    alert("The 'Manage Projects Form' password has been removed.");
                    }
                else
                    {
                    alert("The 'Manage Projects Form' is now password protected.");
                    //Reload the form
                    //window.location.href="default.aspx"
                    }
                //Hide the dialog
                document.getElementById("tbldialog").style.display = "none";
                document.getElementById("divtransparency").style.display = "none";
                document.getElementById("tddialog").innerHTML = "";
                }
            else 
                {
                alert("Error: "+http.status+http.responseText);
                }
            }
        }

    //This function allows us to trap any errors
    http.open("POST","AjaxAction.aspx",true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(reqstring);
    }
    
function uploadproject()
    {
    document.getElementById("tbldialog").style.display = "";
    document.getElementById("tddialog").style.display = "";
    document.getElementById("tdNewProject").style.display = "none";
    document.getElementById("divtransparency").style.display = "";
    document.getElementById("spanheading").innerHTML = "Project Upload";
    document.getElementById("tddialog").innerHTML = "<table style='width:100%;height:100%;background-color:white' cellspacing=0 cellpadding=10><tr><td align='center'><font size=3>Uploading Help Project...</font><br><br><br><img src='images/loading.gif'><br><br><br>Please be patient. This could take up to a couple of minutes depending on the size of your help system and the speed <br>of your server.</td></tr></table>";
    //Call the click method of btnupload to cause the selected file to be uploaded to the server.
    document.getElementById('btnUpload').click()
    }
    
function txtnameKeyPress(searchtext,e)
	{
    var key;
	if(window.event)
		{
		key = window.event.keyCode;     //IE
		}
	else
		{
		key = e.which;     //firefox
		}

	if(key == 13)
		{
		document.getElementById("btnAdd").click();
		return false;
		}
	else
		{
		return true;
		}
	}

function ShowAddProgress()
    {
    //alert("show add progress")
    document.getElementById("tddialog").style.display = "";
    document.getElementById("tddialog").innerHTML = "<table style='width:100%;height:100%;background-color:white'><tr><td align='center'>Creating New Project...<br><br><img src='images/loading.gif'><br></td></tr></table>";
    document.getElementById("tdNewProject").style.display = "none";
    }