﻿var ItiIndex = new Object()

strNullFieldMessage = "Please enter a value for #FIELD_NAME#."

ItiIndex.ValidateForm = null

ItiIndex.SortBy = function()
{
    document.location = strURLReplaceQSPair("sortBy", getSelected(document.getElementById("SortBy")))
}

ItiIndex.DoAjax = function(url, postBody)
{
	var ajaxParams = new Object();
	ajaxParams.method = "post";
	ajaxParams.postBody = postBody

    ///Show a "please wait" dialog and spin icon and disable the screen so user doesn't resubmit, etc:
    ShowMask()
    
    ///Submit the request with AJAX:
	ajaxParams.onSuccess = function(t)
	{ 
	    ///Get rid of the "please wait" masker and dialog:
        HideMask()

        ///See if we got XML back:
        if(t.responseXML == null)
        {
            ///The server has not responsed with XML - probably this is a long, ugly HTTP error:
            ItiIndex.DisplayNotification("ErrorMessage", t.responseText)
            return
        }   
        
        //sResponse = t.responseXML.documentElement.innerText
        sResponse = t.responseXML.documentElement.childNodes.item(0).nodeValue        

        if(sResponse.indexOf("NEWURL:") == 0)
        {
            ///The result was successful and we should jump to the URL specified by the server:
            url = sResponse.substring(("NEWURL:").length)
            document.location = url
        }
        else if(sResponse.indexOf("SAMEURL:") == 0)
        {
            ///The result was successful and we should stay on the same page, displaying
            ///a notification:
            notification = sResponse.substring(("SAMEURL:").length)
            ItiIndex.DisplayNotification("NotifyMessage", notification)
        }
        else if(sResponse.indexOf("RELOAD:") == 0)
        {
            ///The result was successful and we should reload the page to show the changes:
            document.location.reload()
        }
        else
        {
            ///The server has responded with a nice friendly error message we should show:
            alert(sResponse)
            ItiIndex.DisplayNotification("ErrorMessage", sResponse)
        }
	};
	
	ajaxParams.onFailure = function(t)
	{
	    ///An HTTP or even lower level error occured. Let the user decide whether they want
	    ///to retry:
	    message = "The error '" + t.statusText + "' (" + t.status + ") occured. Details are shown below. Click OK to try again or Cancel to return to the form.\r\n" + t.responseText
	    if(confirm(message))
            new Ajax.Request( ItiIndex.AjaxUrl, ItiIndex.AjaxParams );
        else
	        ///Get rid of the "please wait" masker and dialog:
            HideMask()
	};
	
	ItiIndex.AjaxUrl = url
	ItiIndex.AjaxParams = ajaxParams
	
    new Ajax.Request( ItiIndex.AjaxUrl, ItiIndex.AjaxParams );
}

ItiIndex.AjaxUrl = ""
ItiIndex.AjaxParams = ""

ItiIndex.SubmitForm = function()
{
    ///Client-side validation of the form:
    if(ItiIndex.ValidateForm != null)
        if(!ItiIndex.ValidateForm())
            return
    
    ///Assemble the post:
    postBody = ""
    
    for(var i=0; i < document.getElementById("IndexForm").elements.length; i++)
    {
        element = document.getElementById("IndexForm").elements[i]

        ///Ignore ASP.NET automatic things:
        if(element.name == "__VIEWSTATE" || element.name == "__EVENTVALIDATION")
            continue
        
        if(element.type == "radio" && !element.checked)
            continue;
        
        if(postBody != "")
            postBody += "&"
        
        postBody += element.name + "="
        
        if(element.type == "text" || element.type == "hidden" || element.type == "password")
            postBody += escape(element.value)
        else if(element.type == "checkbox")
            postBody += (element.checked) ? "True" : "False"
        else if(element.type == "radio")
            postBody += escape(element.value)
        else
            postBody += escape(element.value)
    }
    
    ItiIndex.DoAjax(ItiIndex.PostUrl, postBody)
}

ItiIndex.DisplayNotification = function(className, message)
{
    if(navigator.userAgent.indexOf("MSIE" ) < 0)
        document.getElementById("Notification").style.width = "555px"

    if(typeof(className) == "undefined")
    {
        ///See if there is a pending cookie message to display:
        if(strGetCookie("Notification") == "")
            return
            
        className = strGetCookie("NotifyClass")
        message = strGetCookie("Notification")
        
        deleteCookie("Notification")
        deleteCookie("NotifyClass")
    }
    
    notifyDiv = document.getElementById("Notification")
    SetInnerText(notifyDiv, message)
    notifyDiv.className = className
}

ItiIndex.Logout = function()
{
    SetProgress("Logging out...")
    ItiIndex.DoAjax("Default.asmx/Logout", "")
}

ItiIndex.ResponseInput = new Object()

ItiIndex.ResponseInput.Over = function(id, choice)
{
    document.getElementById(id + "_ChoiceNA").className 
        = (choice == 0) ? "ChoiceNA_Hover" : "Choice"
    
    for(var i = 1; i <= 5; i++)
    {
        document.getElementById(id + "_Choice" + i).className 
            = (i <= choice) ? "Choice_Hover" : "Choice"
    }
}

ItiIndex.ResponseInput.Out = function(id)
{
    choice = document.getElementById(id + "_Value").value
    document.getElementById(id + "_ChoiceNA").className 
        = (choice == 0) ? "ChoiceNA_Active" : "Choice"

    ///Restore to previous value (if any)
    for(var i = 1; i <= 5; i++)
    {
        document.getElementById(id + "_Choice" + i).className
            = (i <= choice) ? "Choice_Active" : "Choice"
    }
}

ItiIndex.ResponseInput.Click = function(id, choice)
{
    document.getElementById(id + "_Value").value = choice
    
    if(choice < 0)
        choiceText = ""
    else if(choice == 0)
        choiceText = "(NA)"
    else
        choiceText = "(" + choice + ")"    
               
    document.getElementById(id + "_ValueText").innerHTML = choiceText
    ItiIndex.ResponseInput.Out(id)
}


function checkAlertNull2(element, strFieldName)
{
	if(element.value == "")
	{
	    message = strNullFieldMessage.replace("#FIELD_NAME#", strFieldName)
		alert(message)
		if(element.type != "hidden") //could happen in the case of node picker, color picker, etc.
			element.focus()
	    
	    ItiIndex.DisplayNotification("ErrorMessage", message)
	    
		return false
	}
	else
		return true
}

function showInputError(message)
{
    alert(message)
    ItiIndex.DisplayNotification("ErrorMessage", message)
}


function strGetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
    {
		if(!aCrumb[1])
		{
			//alert("returning nothing");
			return ""
		}
		else
		{
			//alert("returning: " + unescape(aCrumb[1]))
			return unescape(aCrumb[1]);
		}
    }
  }

  // a cookie with the requested name does not exist
  return "";
}

function setCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + ";"
}

