// ************************
// WINDOW SUPPORT FUNCTIONS
// ************************
var _ASPFileName;
var BrowserVersion;

function init(aspFileName)
    {
    _ASPFileName = aspFileName;
    BrowserVersion = GetBrowserVersion();
    }

function window_onload()
    {
    initFocus();
    }

function initFocus()
    {
    var fieldType;
    if (document.forms[0])
	{
	for (var i = 0; i<document.forms[0].elements.length; i++)
		{
		fieldType = document.forms[0].elements[i].type;
	
		if ((fieldType == "text" || fieldType == "select-one") && !document.forms[0].elements[i].disabled)
			{
			document.forms[0].elements[i].focus();
			break;
			}
		}
	}
    }

// **********************
// FORM SUPPORT FUNCTIONS
// **********************

function FormCheckRequired(oForm)
    {
    var strMsg = "";
    var oChildren = oForm.elements;
    for (var i = 0; i<oChildren.length; i++)
        {
        if (oChildren[i].getAttribute("_REQUIRED") == "YES")
            {
            if (trim(oChildren[i].value) == "")
                {
                if (strMsg == "")
                    oChildren[i].setActive();
                else
                    strMsg = strMsg + "\n";
                strMsg = strMsg + DisplayName(oChildren[i]) + " required."
                oChildren[i].style.backgroundColor = "yellow";
                }
            else
                {
                oChildren[i].style.backgroundColor = "";
                }
            }
        }
    return strMsg;
    }

function GetBrowserVersion()
{
    var Browser;
    var Version;

    Version = parseFloat(navigator.appVersion);

    switch (navigator.appName)
    {
	case "Netscape":
		if (Version < 5)
			Browser = "NS4";
		else
			Browser = "NS6";
		break;

	case "Microsoft Internet Explorer":
		var versionIndex = navigator.userAgent.indexOf("MSIE") + 5;
		var IEVersion = parseFloat(navigator.userAgent.substring(versionIndex, versionIndex + 5));

		if (IEVersion < 5)
			Browser = "IE4";
		else
			Browser = "IE5";
		break;
	
	default:
		Browser = navigator.appName;
		break;
    }
    return Browser;
}

function FormDisable(oForm)
{
    var fieldType;
    for (var i = 0; i<oForm.elements.length; i++)
    {
        fieldType = oForm.elements[i].type;

        if ((fieldType == "button") || (fieldType == "submit"))
            oForm.elements[i].disabled = true;
        else
	    if (fieldType != "file")
		oForm.elements[i].disabled = false;
    }
}

// *************************
// ELEMENT SUPPORT FUNCTIONS
// *************************

function HTML_input_ondeactivate(objElement)
    {
    var strMsg = "";

    format(objElement);

    strMsg = validate(objElement);
    if (strMsg != "" )
        {
        alert(strMsg);
        objElement.setActive();
        return true;
        }
    return false;
    }

function HTML_input_submit(objForm)
    {
    var strMsg = "";
    if (objForm == null)                      
        return false;

    if (BrowserVersion == "IE5" || BrowserVersion == "NS6")
    {
	    strMsg = FormCheckRequired(objForm);
	    if (strMsg != "" )
        	{
	        alert(strMsg);
        	return true;
	        }
    }

    FormDisable(objForm);

    objForm.submit();
    return false;
    }

function format(objElement)
    {
    var strResult;

    if (objElement.value == "")  
        return "";

    strResult = "";

    switch(objElement.getAttribute("_FORMAT"))
        {
        case "DATE":
            strResult = FormatDate(objElement.value);
            break;
        }
    if (strResult != "" )
        objElement.value = strResult;

    return "";
    }

function validate(objElement)
    {
    if (objElement.value == "")  
        return "";

    var strElement = trim(objElement.value);

    switch(objElement.getAttribute("_FORMAT"))
        {
        case "DATE":
            if (!isDate(strElement))
                return "Invalid Date ( " + strElement + " )";
            break;
        case "NUMERIC":
            if (!isNumeric(strElement))
                return "Value must be numeric. ( " + strElement + " )";
            break;
        case "CURRENCY":
            if (!isCurrency(strElement))
                return "Value must be an amount. ( " + strElement + " )";
            break;
        case "ZIP":
            if (!isZipCd(strElement))
                return "Invalid Zip Code. ( " + strElement + " )";
            break;
        case "FIPS":
            if (!isZipCd(strElement))
                return "Invalid FIPS Code. ( " + strElement + " )";
            break;
        case "EMAIL":
            if (!isEmailAddr(strElement))
                return "Invalid Email Address. ( " + strElement + " )";
            break;
        case "PHONE":
            if (!isPhoneNbr(strElement))
                return "Invalid Phone Number. ( " + strElement + " )";
            break;
        }
    return "";
    }

function FormatDate(strDate)
    {
    var intMonth;
    var intDay;
    var intYear;
    var arrDate;

    if (strDate == "")
        return "";

    if ((strDate.indexOf("/") > 0) || (strDate.indexOf("-") > 0))
        {
        if (strDate.indexOf("/") > 0)
            arrDate = strDate.split("/");

        if (strDate.indexOf("-") > 0)
            arrDate = strDate.split("-");

        intMonth = arrDate[0];
        intDay = arrDate[1];
        intYear = arrDate[2];
        }
    else
        {
        intMonth = strDate.substring(0,2);
        intDay = strDate.substring(2,4);
        intYear = strDate.substring(4,8);
        }

    if (parseInt(intYear) <= 30)
        intYear = "20" + intYear;
    else if (parseInt(intYear) < 100)
        intYear = "19" + intYear;
    else if (parseInt(intYear) < 1760)
        return ""

    var FormatDt = new Date(intMonth + "/" + intDay + "/" + intYear);

    if (FormatDt == "NaN")
        return "";

    if ((intMonth  != (FormatDt.getMonth()+1)  ) || 
        (intDay    != FormatDt.getDate()    ) ||
        (intYear   != FormatDt.getFullYear()) || (intYear.length != 4))
       return "";

    return (FormatDt.getMonth()+1 + "/" + FormatDt.getDate() + "/" + FormatDt.getFullYear());
    }

function isDate(strValue)
    {
    return (FormatDate(strValue) != "")
    }

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) 
    {
    if (string.search) 
        {
        if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
        }
    return true;
    }

function isEmailAddr(strValue)
    {
    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strValue)))
       return false;

    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if (strValue.match(illegalChars))
       return false;

    return true;
    }

function isPhoneNbr(strValue)
    {
    var PhoneFilter=/^(\(?\d\d\d\)?(\s|-)?)?\d\d\d-?\d\d\d\d\s?(((ext\.?\s?)|(x\.?))?\d\d?\d?\d?)?$/;
    if (!(PhoneFilter.test(strValue)))
       return false;

    return true;
    }

function isZipCd(strValue)
    {
    var ZipFilter=/^\d\d\d\d\d(-?\d\d\d\d)?$/;
    if (!(ZipFilter.test(strValue)))
       return false;

    return true;
    }

function isCurrency(strValue)
    {
    var CurrencyFilter=/^((\d+\.?\d?\d?)|(\.\d\d?))$/;
    if (!(CurrencyFilter.test(strValue)))
       return false;

    return true;
    }

function DisplayName (oElement)
    {
    if (oElement.getAttribute("LABEL") && oElement.getAttribute("LABEL") != 'Null')
	return (oElement.getAttribute("LABEL"));
    else
	return (oElement.name);

//    return (oElement.getattribute("NAME"));
    }

function ParentForm (oElement)
    {
    var oParent = oElement.parentElement;   // Get Parent
    while (oParent != null)
        {
        if (oParent.tagName == "FORM")
            return oParent;
        oParent = oParent.parentElement;    // Get Parent
        }
    return;
    }


// ************************
// COMMON SUPPORT FUNCTIONS
// ************************

function ltrim(argvalue) {

  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }

  return argvalue;
}


function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}

function trim(argvalue) {
  var tmpstr = ltrim(argvalue);

  return rtrim(tmpstr);

}

function WebRoot() 
    {
    var Url;
//    Url = document.location.href.substr (0, document.location.href.lastIndexOf ('?'));
//    return (Url.substr (0, Url.lastIndexOf ('/')));
    Url = document.location.href;
    if (Url.indexOf ('?') != -1)
        Url = Url.substr (0, Url.lastIndexOf ('?'));
    if (Url.indexOf ('/') != -1)
	Url = Url.substr (0, Url.lastIndexOf ('/'))
    return(Url);
    }

function ServerName() 
    {
    var Name;
    Name = document.location.href
    Name = Name.substr(Name.indexOf('://') + 3,Name.length - Name.indexOf('://') - 3);
    Name = Name.substr(0,Name.indexOf('/'));
    return(Name);
    }

function isSecure() 
    {
    var Protocol;
    Protocol = document.location.href
    Protocol = Protocol.substr(0,Protocol.indexOf('://'));
    if (Protocol == 'https')
	return(true)
    return(false);
    }

function OpenFileUpload(strQueryString)
{
	var newWin;
	var w = 360;
	var h = (screen.height)*0.25;
	LeftPosition=(screen.width)?(screen.width-w)/2:100;
	TopPosition=360;
	if (window.showModalDialog)
		newWin = window.showModalDialog(WebRoot() + "/FileUpload.aspx" + strQueryString,window,"dialogWidth=" + w + "px;dialogHeight=" + h + "px");
	else
		newWin = window.open(WebRoot() + "/FileUpload.aspx" + strQueryString,'Document','modal=0,width='+w+',height='+h+',screenX='+LeftPosition+',screenY='+TopPosition+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no');
}

function CenterElement(oElement)
{
	if (oElement)
	{
		var bodyWidth = document.body.offsetWidth;
		var elementWidth = oElement.offsetWidth;
		var elementOffset = ((bodyWidth - elementWidth)/2) - 10;
		if (elementOffset  > 0)
			oElement.style.left=elementOffset;
	}
}

function ReportCaseError(strQueryString)
{
	var strError = window.prompt("Report Case Error", "");
	
	if (strError != null)
	{
		if (strError == "")
			return;
			
		var frm;
		frm = top.body_frame.document.forms[0];	
		frm.ErrorText.value = trim(strError);
		frm.submit();
		
		alert ("Error Reported");		
	}
}

function RequeueCase(strCaseNbr, strWhichQueue, strQueryString)
{
	var bConfirmed = window.confirm("Requeue Case " + strCaseNbr + " to " + strWhichQueue + " Queue?");
	
	if (bConfirmed == true)
	{
		var locationString;
		locationString = WebRoot() + '/' + _ASPFileName + strQueryString
		top.body_frame.document.location = locationString;		
	}	
}

function OpenNewWindow(strQueryString)
{
	var newWin;
	var w = 800;
	var h = (screen.height)*0.75;
	LeftPosition=(screen.width)?(screen.width-w)/2:100;
	TopPosition=100;
	newWin = window.open(WebRoot() + '/' + _ASPFileName + strQueryString,'Document','modal=0,width='+w+',height='+h+',screenX='+LeftPosition+',screenY='+TopPosition+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no');
}

function ExitForm(fileUploadFlag)
{
	if (typeof(dialogArguments) == 'undefined')
	{
		if (window.opener == null)
			document.location.replace(document.location.href.substr (0, document.location.href.lastIndexOf ('/')));
		else
		{
			window.opener.Refresh();
			self.close();
		}
	}
	else 
	{
		window.close();
		dialogArguments.Refresh(fileUploadFlag);
	}

}

function ConfirmRequest(Msg, Location) 
    {
    if (confirm(Msg))
        {
        top.body_frame.document.location = WebRoot() + '/' + _ASPFileName + Location;
        }
    }
