/***** Variable Declaration *****/
var FormPage = new Array();

	//Variables
	FormPage.SubmitButtons = [];
	FormPage.EnableCookieBypass = false;
	FormPage.BypassCookieName = "SageFormPageBypass";
	FormPage.State2Url = "";
	
	//Methods
	FormPage.Init = fnInit;
	FormPage.EndRequestHandler = fnEndRequestHandler;
	FormPage.AddSubmitButton = fnAddSubmitButton;
	FormPage.SetBypassCookie = fnSetBypassCookie;
	FormPage.IsBypassCookieSet = fnIsBypassCookieSet;
	FormPage.ValidateCheckboxRequired = fnValidateCheckboxRequired;
	FormPage.ValidateCheckboxListRequired = fnValidateCheckboxListRequired;
	FormPage.ValidateCaptcha = fnValidateCaptcha;
	FormPage.ValidationGroupEnable = fnValidationGroupEnable;
	FormPage.ValidateEmail2 = fnValidateEmail2;
	FormPage.ValidateState = fnValidateState;

/***** FormPage Declarations *****/
function fnInit()
{
	if(FormPage.EnableCookieBypass || FormPage.State2Url != "")
	{
		//Check to see if cookie is already set
		if(FormPage.EnableCookieBypass && FormPage.IsBypassCookieSet() && (new Querystring()).get("EnableCookieBypass", "true") != "false")
		{
			if(FormPage.State2Url != "")
			{
				self.location.href =pt_3295.transformURL( CombineUrl(self.location.href, FormPage.State2Url));
			}
			else
			{
				//Disable Validation
				//FormPage.ValidationGroupEnable("Sage.CMS.Portlets.Forms", false);
			
				//Send Postback
				__doPostBack(FormPage.SubmitButtons[0], 'CookieBypass');
			}
		}
		
		//Attach to Form Update
		Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FormPage.EndRequestHandler);
	}
	
}
function fnEndRequestHandler(sender, args)
{
	var bFormPageSubmit = false;
	var sSourceControl = sender._postBackSettings.panelID.substr(sender._postBackSettings.panelID.indexOf("|")+1);
	
	//Check to see if update source is one of the submit buttons
	for (var i=0; i<FormPage.SubmitButtons.length; i++)
	{
		if(sSourceControl == FormPage.SubmitButtons[i])
			bFormPageSubmit = true;
	}
	
	//Whole page was submitted
	if(bFormPageSubmit)
	{
		//Set Cookie
		FormPage.SetBypassCookie();
	
		//Redirect
		if(FormPage.State2Url != "")
		{
			self.location.href =pt_3295.transformURL( CombineUrl(self.location.href, FormPage.State2Url));
		}			
	}
}
function fnAddSubmitButton(ButtonID)
{
	this.SubmitButtons[this.SubmitButtons.length] = ButtonID;
}
function fnSetBypassCookie()
{
	// set time, it's in milliseconds
	var Today = new Date();
	Today.setTime( Today.getTime() );

	var ExpiresDate = new Date( Today.getTime() + (6*30*1000*60*60*24) );
	
	document.cookie = this.BypassCookieName+"=true; path=/;expires=" + ExpiresDate.toGMTString();
}
function fnIsBypassCookieSet()
{
	var nameEQ = FormPage.BypassCookieName + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') 
			c = c.substring(1,c.length);
		
		if (c.indexOf(nameEQ) == 0)
		{
			return (c.substring(nameEQ.length,c.length) == "true") ? true : false;
		}
	}
	return false;
}


/***** Standard Page Methods *****/
function addLoadEvent(func)
{
	var oldonload = window.onload;   
	
	if (typeof window.onload != 'function') 
	{       
		window.onload = func;   
	}   
	else 
	{       
		window.onload = function() 
						{           
							oldonload();           
							func();       
						};
	}
}

function addClickEvent(func)
{
	var oldonclick = window.onclick;   
	
	if (typeof window.onclick != 'function') 
	{       
		window.onclick = func;   
	}   
	else 
	{       
		window.onclick = function() 
						{           
							oldonclick();           
							func();       
						};
	}
}


/***** Validation Methods *****/
function fnValidateCheckboxRequired(Sender, Args, ControlID)
{
	Args.IsValid = document.getElementById(ControlID).checked;
}
function fnValidateCheckboxListRequired(Sender, Args, ControlID)
{
	var chkControlId = ControlID;
	var options = document.getElementById(chkControlId).getElementsByTagName('input');
	var ischecked=false;    
	Args.IsValid =false;    
	for(i=0;i<options.length;i++)    
	{        
		var opt = options[i];        
		if(opt.type=="checkbox")        
		{            
			if(opt.checked)            
			{                
				ischecked= true;                
				Args.IsValid = true;                            
			}        
		}     
	}
}

function fnValidateCaptcha(Sender, Args, ControlID1, ControlID2)
{


	Args.IsValid = false;
	
	//Get Control
	var oElm1 = document.getElementById(ControlID1);
	var oElm2 = document.getElementById(ControlID2);

	//alert(oElm1.value + ":" + oElm2.value);

	if (oElm1 == null || oElm2 == null)
		Args.IsValid = true;
	else
		Args.IsValid = (oElm1.value == oElm2.value);
}

function fnValidateEmail2(Sender, Args, ControlID1, ControlID2)
{
	Args.IsValid = false;

	//Get Controls
	var ctrlEmail1 = document.getElementById(ControlID1);
	var ctrlEmail2 = document.getElementById(ControlID2);

	if (ctrlEmail1 == null || ctrlEmail2 == null)
		Args.IsValid = true;
	else
		Args.IsValid = (ctrlEmail1.value == ctrlEmail2.value);

}

function fnValidateState(Sender, Args, ControlID1, ControlID2)
{
	Args.IsValid = false;

	//Get Controls
	var ctrlStateDropDown = document.getElementById(ControlID1);
	var ctrlStateTextBox = document.getElementById(ControlID2);
	
	if (ctrlStateDropDown != null && ctrlStateDropDown.value != "")
		Args.IsValid = true;
	else if(ctrlStateTextBox != null && ctrlStateTextBox.value != "")
		Args.IsValid = true;
	else
		Args.IsValid = false;
}


function HasPageValidators()
{            
	var hasValidators = false;                     
	
	try            
	{                
		if (Page_Validators.length > 0)                
		{                    
			hasValidators = true;                
		}            
	}            
	catch (error)            
	{            
	}   
	                   
	return hasValidators;        
}              

function fnValidationGroupEnable(validationGroupName, isEnable)        
{            
	if (HasPageValidators())            
	{             
		
		for(i=0; i < Page_Validators.length; i++)                
		{       
			
			if (Page_Validators[i].validationGroup == validationGroupName)                    
			{                        
				ValidatorEnable(Page_Validators[i], isEnable);                    
			}                
		}            
	}        
}


/***** QueryString Methods *****/
// optionally pass a querystring to parse
function Querystring(qs) 
{
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

	// Turn <plus> back to <space>
	// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
	// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) 
{
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) 
{
	var value = this.params[key];
	return (value != null);
}



/***** Reusable Form Functions *****/
function maxLength(field, maxChars)
{
	
	if(field.value.length > maxChars) 
	{
	
		field.value = field.value.substring(0,maxChars);
   
		event.returnValue=false;
		return false;
	}
   
	if(field.value.length == maxChars) 
	{
	
		event.returnValue=false;
		return false;
	}
	
	return true;
}  

function maxLengthPaste(field, maxChars)
{
   event.returnValue=false;
   if((field.value.length + window.clipboardData.getData("Text").length) > maxChars)
   {
	 field.value += window.clipboardData.getData("Text").substring(0,maxChars - field.value.length);
     return false;
   }
   event.returnValue=true;
}

function CombineUrl(sUrl1, sUrl2)
{
	var sUrl = "";
	var sFolder = "";
	var sPage = "";
	var sQueryString = "";

	//If Url2 is absolute, then return absolute
	if(sUrl2.substr(0,5).toLowerCase() == "http:") return sUrl2;
	
	//If Url2 is fully qualified, then return
	if(sUrl2.substr(0,1) == "/") return sUrl2;
	
	//Parse Url1, into Url, Querystring
	if(sUrl1.indexOf("?") != -1)
	{
		sQueryString = sUrl1.substr(sUrl1.indexOf("?")+1);
		sUrl = sUrl1.substr(0, sUrl1.indexOf("?"));
		sUrl1 = sUrl;
		//alert(sQueryString);
		//alert(sUrl);
	}
	else
		sUrl = sUrl1;
	
	//If Url2 starts with, ./, then remove
	if(sUrl2.substr(0,2) == "./") sUrl2 = sUrl2.substr(2);
	
	//Parse Url into Url, Page
	if(sUrl.indexOf(".aspx") != -1)
	{
		sPage = sUrl.substr(sUrl1.lastIndexOf("/")+1);
		sUrl = sUrl.substr(0, sUrl1.lastIndexOf("/"));

	}
	
	//Combine Url1 Folder, with Url2 Folder
	return sUrl + "/" + sUrl2 + "?" + sQueryString;
	
}







/****** DROP DOWN BOX CHECKBOX LSIT JS ******/
var CurrentDropDownCheckListID = "";

function CollapseCurrentDropDownCheckList()
{

	if(CurrentDropDownCheckListID != "")
		window.document.getElementById(CurrentDropDownCheckListID + '_' + 'divMain').style.display = 'none';
		
	CurrentDropDownCheckListID = "";
}

function CBDDL_ShowHideMultipleSelection(ControlClientID, evt)
{
	var textBoxMain = window.document.getElementById(ControlClientID + '_' + 'txtMain');
	var divisionMain = window.document.getElementById(ControlClientID + '_' + 'divMain');

	//Collapse Current Menus
	//if(!ForceClose || ForceClose == undefined)
	CollapseCurrentDropDownCheckList();
	
	//removes the 'Select' string txtMain
	switch(textBoxMain.value)
	{
		case 'Select':
			textBoxMain.value = '';
			break;
		case '':
			textBoxMain.value = 'Select';
			break;
		default:
			break;
	}	
	
	var displayStatus = divisionMain.style.display;
	if(displayStatus == 'block')
	{
		divisionMain.style.display = 'none';
	}
	else 
	{
		//Set Current Menu
		CurrentDropDownCheckListID = ControlClientID;
		
		divisionMain.style.display = 'block';
	}
	
	//event.cancelBubble = true;
	var e = (window.event) ? window.event : evt;
	if(e) e.cancelBubble = true;
}

function CBDDL_SetSelection( ControlID, ElementID, Event )
{
	var oCheckboxList = document.getElementById(ElementID);
	var aryLabels = oCheckboxList.getElementsByTagName('label');
	
	var sSelectionText = "";
	
	//Loop Through Labels
	for(i=0;i<aryLabels.length;i++)    
	{        
		var oLabel = aryLabels[i];      
		var oCheckbox = document.getElementById(oLabel.attributes["for"].value);
		
		if(oCheckbox != null && oCheckbox.type=="checkbox")        
		{            
			if(oCheckbox.checked)            
			{   
				sSelectionText += (document.all) ? oLabel.innerText.replace(/,/gi, "") + "," : oLabel.textContent.replace(/,/gi, "") + ",";
			}        
		} 

	}
		
	//Remove Trailing ,
	if(sSelectionText.substr(sSelectionText.length-1) == ",")
		sSelectionText = sSelectionText.substring(0, sSelectionText.length-1);
	
	//Set Selection Text
	var textBoxMain = window.document.getElementById(ControlID + '_' + 'txtMain');
	
	if(sSelectionText == "")
		textBoxMain.value = "Select";
	else
		textBoxMain.value = sSelectionText;

}

function CBDDL_getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null )
	{
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}

	return iReturnValue;
}

function CBDDL_getX( oElement )
{
	var iReturnValue = 0;
	while( oElement != null )
	{
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}

	return iReturnValue;
}

function CBDDL_HideOnFocusLost()
{
	var divCollection = window.document.getElementsByTagName('div');
	for (index = 0; index < divCollection.length; index++)
	{
		var divisionMain = divCollection[index];
		if (divisionMain.id.indexOf('divMain') >= 0 && divisionMain.style.display == 'block')
		{
			var iX1 = CBDDL_getX(divisionMain);
			var iX2 = iX1 + 250;
			var iY1 = CBDDL_getY(divisionMain);
			var iY2 = iY1 + 150;
			
			var iMouseX = event.clientX + document.body.scrollLeft;
			var iMouseY = event.clientY + document.body.scrollTop;
			
			if(!(iMouseX >= iX1 && iMouseX <= iX2 && iMouseY >= iY1 && iMouseY <= iY2))
				divisionMain.style.display = 'none';
			
		}
	}
}

function CBDDL_HideSelectionList(ControlClientID)
{
	var displayStatus = window.document.getElementById(ControlClientID + '_' + 'divMain').style.display;
	if(displayStatus == 'block')
	{
		window.document.getElementById(ControlClientID + '_' + 'divMain').style.display = 'none';
	}
	else 
	{
		window.document.getElementById(ControlClientID + '_' + 'divMain').style.display = 'block';
	}
}

function CancelBubbleEvent(evt)
{
	var e = (window.event) ? window.event : evt;
	e.cancelBubble = true;	
}