﻿// JScript File
function ErrorDisplay(strErrorMessage)
{
var frameset;
var ifrTop;
var menuframe;
var menuframewindow;
var divErr;
var ulGen;
var ErrorArray;
var strErrorHTML;
strErrorMessage = strErrorMessage.substr(0,(strErrorMessage.length)-1); 
var ErrorArray = strErrorMessage.split("^");
var strErrorHTML = "";
for(i = 0; i<ErrorArray.length; i++)
{
	strErrorHTML +=  ErrorArray[i] + "\r\n";
}
if (strErrorHTML != "")
{
	alert(strErrorHTML);
}
else
{
	strErrorHTML = "Unspecified validation error";
	alert(strErrorHTML);
}
return false;
}


function PostData()
{
var frm = document.getElementById("frmData"); 
var arrValidateObjectRetval
var arrBusinessRulesRetval

arrValidateObjectRetval = ValidateObject(frm);
arrBusinessRulesRetval = ValidateBusinessRules();

if (arrValidateObjectRetval[0] == true && arrBusinessRulesRetval[0] == true)
{
	frm.action = "RequestForProposalUpdate.aspx";
	frm.submit();
}
else
{
	ErrorDisplay(arrValidateObjectRetval[1] + arrBusinessRulesRetval[1]);
}

}

function ValidateBusinessRules()
{
var valid = true;
var ErrorMessage = "";
var arrValidateBusinessRules = Array(true,"");

if(valid)
{
	arrValidateBusinessRules[0] = true;
	arrValidateBusinessRules[1] = "";
	return arrValidateBusinessRules;
}
	else
{
	
	
	arrValidateBusinessRules[0] = false;
	arrValidateBusinessRules[1] = ErrorMessage;
	return arrValidateBusinessRules;
}

}

 
 
function MapFieldInfo (objGen)
{
var arrRetval = new Array(8);
var ColumnName = 0;
var BusinessName = 1;
var Type = 2;
var Length = 3;
var Scale = 4;
var IsRequired = 5;
var RegEx = 6;
var RegExDisplay = 7;

var strID = "";
var objRegExShortDate = /^\d{1,2}\/\d{1,2}\/\d{4}/
var RegExShortDateDisplay = "MM/DD/YYYY";
var objRegExUSPhoneParens = /^\(\d{3}\)\s\d{3}-\d{4}/
var RegExUSPhoneParensDisplay = "(xxx) xxx-xxx";
var objRegExUSPhoneDashes = /^\d{3}-\d{3}-\d{4}/
var RegExUSPhoneDashesDisplay = "xxx-xxx-xxxx";
var objRegExUSPhoneDashesExt = /^\d{3}-\d{3}-\d{4}|^\d{3}-\d{3}-\d{4}-\d{4}/
var RegExUSPhoneDashesExtDisplay = "xxx-xxx-xxxx Or xxx-xxx-xxxx-xxxx";
var objRegEx19Or20Year = /^(19)|(20)\d{2}/
var RegEx19Or20YearDisplay = "19xx or 20xx";
var objRegExEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
var RegExEmailDisplay = "";
var objRegExUSZipCode  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
var RegExUSZipCodeDisplay = "xxxx Or xxxxx-xxxx";

strID = objGen.id;
if (strID.indexOf(".") != -1)
{
	arrID = objGen.id.split(".");
	strID = arrID[0];
}



switch(strID)
{
       case "txtEmailAddress":
        arrRetval[ColumnName] = "txtEmailAddress";
        arrRetval[BusinessName] = "Email Address";
        arrRetval[Type] = "String";
        arrRetval[Length] = "50";
        arrRetval[Scale] = "";
        arrRetval[IsRequired] = true;
        arrRetval[RegEx] = objRegExEmail;
        arrRetval[RegExDisplay] = "";
        break;
    	
}
return arrRetval;
} 



function ValidateObject(frm){
var objElement
var valid = true;
var ErrorMessage = "";
var arrRetval
var frmLength = frm.length;	
var ColumnName = 0;
var BusinessName = 1;
var Type = 2;
var Length = 3;
var Scale = 4;
var IsRequired = 5;
var RegEx = 6;
var RegExDisplay = 7;
var blnPassedRegEx = false;
var IsDateRetval
var arrValidateObject = Array(true,"");

//debugger;
//The backslash (\) is also used when you wish to match a special character literally.
//var objRegEx = /^\d{1,2}\/\d{1,2}\/\d{4}/
//var objRegEx = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
//var objRegEx = /^\(\d{3}\)\s\d{3}-d{4}/
//var objRegEx = /^\(\d{3}\)\s\d{3}-\d{4}/
//var objRegEx = /^(19)|(20)\d{2}/
//objRegEx = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*\s+<(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})>$|^(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})$/ 
//var objRegEx = /^\d{3}-\d{3}-\d{4}/
//var objRegEx = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
//var objRegEx  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

//var objRegEx = /^\d{3}-\d{3}-\d{4}|^\d{3}-\d{3}-\d{4}-\d{4}/
//var strToTest = "908-233-4545-1111";
//var blnPassed = objRegEx.test(strToTest);

	//initialize all testable elements with black text
	for(i=0; i<frmLength; i++)
	{
		if(frm.elements[i].type == "select-one" || frm.elements[i].type == "text")
		{
			frm.elements[i].style["color"] = "Black";
		}
	}
	
	//iterate through all elements in the object
	for(i=0; i<frmLength; i++)
    {
        // check the element is a textfield or select box
        if(frm.elements[i].type == "select-one" || frm.elements[i].type == "text")
        {
			//get information about the element from MapFieldInfo
			arrRetval = MapFieldInfo(frm.elements[i]);

			//Test to see whether the element is required
			if (arrRetval[IsRequired] == true)
			{

				//test element type - selects will have *0 as the "blank"
				//value associated with the *Select* text choice
				if(frm.elements[i].type == "text")
				{
					//Test to see whether a blank value is provided
					if(frm.elements[i].value == "")
					{
						valid = false;
						ErrorMessage += "The " + arrRetval[BusinessName] + " field can't be blank."	+ "^"
						frm.elements[i].style["color"] = "Red";
					}// end if (frm.elements[i].value == "")
				}
				else// frm.elements[i].type == "select-one"
				{
					//Test to see whether a blank value is provided
					if(frm.elements[i].value == "*0")
					{
						valid = false;
						ErrorMessage += "You must make a selection from the  " + arrRetval[BusinessName] + " dropdown list."	+ "^"
						frm.elements[i].style["color"] = "Red";
					}// end if (frm.elements[i].value == "")	
				}				


			}// end if (arrRetval[IsRequired] == true)



			// check value is provided
			if(frm.elements[i].value != "")
			{
				
				//Test for the data type of the column
				switch(arrRetval[Type])
				{
					case "String":
						//Test if the value provided is to many characters
						if(frm.elements[i].value.length > arrRetval[Length])
						{
							valid = false;
							var ExcessChars = parseInt(frm.elements[i].value.length) - parseInt(arrRetval[Length])
							ErrorMessage += "The " + arrRetval[BusinessName] + " field is " + ExcessChars.toString() + " characters too long." + "^"	
							frm.elements[i].style["color"] = "Red";
						}

						//Test whether a regular expression is assigned to the element
						if(arrRetval[RegEx] != "")
						{
							
							//Test whether the value supplied meets the constraints of the regular expression
							blnPassedRegEx = arrRetval[RegEx].test(frm.elements[i].value);
							if (blnPassedRegEx == false)
							{
								valid = false;
								if (arrRetval[RegExDisplay] != "") 
								{
									ErrorMessage += "The " + arrRetval[BusinessName] + " field must match the following pattern: " + arrRetval[RegExDisplay] + "^"	
									frm.elements[i].style["color"] = "Red";
								}
								else
								{
									ErrorMessage += "The " + arrRetval[BusinessName] + " field does not match the pattern defined for it.  Please contact the webmaster if you feel this pattern is in error. " + "^"
									frm.elements[i].style["color"] = "Red";
								}
								
							}
							else
							{
								blnPassedRegEx = false;
							}//end if passed regular expression test
						}// end if a regular expression is assigned to the element


						break;
					//end String
					case "DateTime":
					
					//Test to see whether the date supplied matches the desired pattern
					blnPassedRegEx = arrRetval[RegEx].test(frm.elements[i].value);
					if (blnPassedRegEx == false)
					{
						valid = false;
						ErrorMessage += "The " + arrRetval[BusinessName] + " must match the following pattern: " + arrRetval[RegExDisplay]	+ "^"
						frm.elements[i].style["color"] = "Red";						
					}
					else
					{
						//The date might have passed the format test but
						//still not be a valid date
						//DateParserHolder = Date.parse(frm.elements[i].value);
						IsDateRetval = isDate(frm.elements[i].value, 'MM/dd/yyyy');
						if (IsDateRetval != true)
						{
							valid = false;
							ErrorMessage += IsDateRetval + "^"
							frm.elements[i].style["color"] = "Red";	
						}//end if sDateRetval != true

						blnPassedRegEx = false;
					}//end if passed regular expression test
					break;
					//end DateTime
					case "Int32":
					break;
					
				}
                
			}// end if value is provided
		}// end if element type is text or select
	}// iteration through form elements

	if(valid)
	{
		arrValidateObject[0] = true;
		arrValidateObject[1] = "";
		return arrValidateObject;
		//return true;
    }
		else
	{
		
		//debugger;
		arrValidateObject[0] = false;
		arrValidateObject[1] = ErrorMessage;
		return arrValidateObject;

		
    }
}
//---Error IIWin Code
var gIIWinTest
function InvokeIIWin_Init(strHTML, strTitle, strType, strMemoDisplayButtonID)
{
var IsResizable;
var ResizerButtonClassName;
var MovableClickElement;
var CaptionBarContent;
var CaptionBarClassName;
var BodyContent;
var BodyClassName;  
var IIWinVisibility;
var IIWinClassName;
var IIWinTop;
var IIWinLeft;
var IIWinHeight;
var IIWinWidth;
var IIWinZIndex;
var HasWindowedControlBlocker;
var AutoNumberContentIDs;
var AutoSizeToContent;	
var Window_AfterInit;
var Window_AfterResize;
var Window_AfterMove;
var	Window_BeforeHide;
var Window_BeforeDestroy;
var ErrorDisplayType;
var TitleDiv;
var doc = window.document;
var objOrig;

var ulError; 

if(strType == "ErrorWindow")
{
	ulError = document.getElementById("ulError");
	ulError.innerHTML = strHTML;
	objOrig = doc.getElementById("tblError");
	BodyContent = objOrig.cloneNode(true);
	TitleDiv = document.getElementById("divCenter");
	TitleDiv.innerHTML = strTitle;
}
else
{
	objOrig = doc.getElementById("divMemoField");
	BodyContent = objOrig.cloneNode(true);
	AssignMemoEditorValue(strMemoDisplayButtonID, BodyContent);
	TitleDiv = document.getElementById("divCenter");
	TitleDiv.innerHTML = strTitle;
}



IsResizable = true;
ResizerButtonClassName = "";
MovableClickElement = "CaptionBar";
objOrig = doc.getElementById("divCaptionBar");
CaptionBarContent = objOrig.cloneNode(true);
CaptionBarClassName = "";

BodyClassName = "";
IIWinVisibility = "visible";
IIWinClassName = "";
IIWinTop = "100px";
IIWinLeft = "100px";
IIWinHeight = "";
IIWinWidth = "";
IIWinZIndex = "";
HasWindowedControlBlocker = false;
WindowedControlBlockerZIndex = "";
AutoNumberContentIDs = true;
AutoSizeToContent = true;
if(strType == "ErrorWindow")
{
	Window_AfterInit = CenterIIWin;
}
else
{
	Window_AfterInit = RegisterSaveCloseEventHandler;
}

Window_AfterResize = CaptionBarResize;
Window_AfterMove = LimitXAxis;
Window_BeforeHide = "";
Window_BeforeDestroy = "";
ErrorDisplayType = "alert";


gMyWindow =
IIWin_Instantiate
(
IsResizable,
ResizerButtonClassName,
MovableClickElement,
CaptionBarContent,		
CaptionBarClassName,
BodyContent,
BodyClassName,
IIWinVisibility,
IIWinClassName,
IIWinLeft,
IIWinTop,
IIWinHeight,
IIWinWidth,
IIWinZIndex,
HasWindowedControlBlocker,
WindowedControlBlockerZIndex,
AutoNumberContentIDs,
AutoSizeToContent,
Window_AfterInit,
Window_AfterResize,
Window_AfterMove,
Window_BeforeHide,
Window_BeforeDestroy,
ErrorDisplayType
);


}
function CaptionBarResize(IIWin, IIWinCaptionBar, IIWinBody, IIWinResizerButton)
{
if(IIWinCaptionBar == null)
{
	return;
}

var doc = document;
var intClientIDSuffix = parseInt(IIWin.attributes.getNamedItem("intClientIDSuffix").value);
var divLeft = doc.getElementById("divLeft." + intClientIDSuffix);
var divCenter = doc.getElementById("divCenter." + intClientIDSuffix);
var divCenterWidth;
var divRight= doc.getElementById("divRight." + intClientIDSuffix);
var IIWinCaptionBarWidth = IIWinCaptionBar.clientWidth;	
divCenterWidth = IIWinCaptionBarWidth - (divLeft.offsetWidth + divRight.offsetWidth);
if(divCenterWidth > 0)
{
	divCenter.style.width = IIWinCaptionBarWidth - (divLeft.offsetWidth + divRight.offsetWidth) + "px";
	divRight.style.left = (divLeft.offsetWidth + divCenter.offsetWidth) + "px";
}

}
function CenterIIWin(IIWin, IIWinCaptionBar, IIWinBody, IIWinResizerButton)
{
//Declare a variable to hold the function container object
var objFunctionContainer;
var objCoords = {};

CaptionBarResize(IIWin, IIWinCaptionBar, IIWinBody, IIWinResizerButton);

objFunctionContainer = GetFunctionContainerObject(IIWin);

//Test to see whether the function container object was created normally
//If there were any problems the ErrorMessage will be something other then ""
if(objFunctionContainer.ErrorMessage == "")
{
	//This call gets the coordinates object which enables centering the IIWin 
	//in the content area of the screen.
	objCoords = objFunctionContainer.GetCenteredCoordinates(IIWin);
	//If the ErrorMessage property is "" then it's ok to use
	//the Top and Left properties
	if (objCoords.ErrorMessage === "")
	{
		IIWin.style.top = objCoords.Top + "px";
		IIWin.style.left = objCoords.Left + "px";
	}
	
}

}

function LimitXAxis(IIWin, IIWinCaptionBar, IIWinBody, IIWinResizerButton)
{
var intTop
intTop = cbGetStringDimensionAsInteger(IIWin.style.top);
if(intTop < 0)
{
	IIWin.style.top = "0px";
}
}
function cbGetStringDimensionAsInteger(strDimension)
{
var objRegEx = /\D{2,}/;
var strRetval;
strRetval = strDimension.replace(objRegEx, "");
return parseInt(strRetval);
}



