﻿// JScript File
function liValForm()
{
	var oEl = document.forms[0].elements;
	var i = 0;
	var retVal = true;
	
	var eStr = '';
	var optStr = '';
	var matchStr = '';
	var sExcType = '|button|image|submit|';
	
	//required fields
	for (i=0; i<oEl.length; i++)
	{
		if (oEl[i].getAttribute('type') != '' && sExcType.indexOf('|'+oEl[i].type.toLowerCase()+'|') == -1)
		{
			if ((oEl[i].getAttribute('req') != null && oEl[i].getAttribute('req') == 'yes') && oEl[i].value == '')
			{
				eStr += '--> ' + oEl[i].getAttribute('dispName') + '\n';
				oEl[i].style.backgroundColor = '#ffff00';
			}
			else
			{
				oEl[i].style.backgroundColor = '#ffffff';
			}			
		}
	}
	
	//optional fields
	for (i=0; i<oEl.length; i++)
	{
		if (oEl[i].getAttribute('type') != '' && sExcType.indexOf('|'+oEl[i].type.toLowerCase()+'|') == -1)
		{
		
			if ((oEl[i].getAttribute('opt') != null && oEl[i].getAttribute('opt') == 'yes') && oEl[i].value == '')
			{
				optStr += oEl[i].getAttribute('optMsg') + '\n';
				//oEl[i].style.backgroundColor = '#ffff00';
			}	
		}
	}	
	
	//match fields
	for (i=0; i<oEl.length; i++)
	{
		if (oEl[i].getAttribute('type') != '' && sExcType.indexOf('|'+oEl[i].type.toLowerCase()+'|') == -1)
		{
		
			if (oEl[i].getAttribute('mustMatch') != null && oEl[i].getAttribute('mustMatch') != '')
			{
				if (document.getElementById(oEl[i].getAttribute('mustMatch')) && oEl[i].value != document.getElementById(oEl[i].getAttribute('mustMatch')).value)
				{
					matchStr += oEl[i].getAttribute('matchMsg') + '\n';
				}
			}	
		}
	}			
	
	if (eStr.length > 0)
	{
		alert('The form fields listed below CANNOT be blank:\n\n' + eStr);
		retVal = false;
	}
	else if (matchStr.length > 0)
	{
		alert('The following errors need to be corrected:\n\n' + matchStr);
		retVal = false;
	}
	else if (optStr.length > 0)
	{
		retVal = confirm(optStr + '\n\nClick \'OK\' to continue, or \'CANCEL\' to return to the form.');
	}
	
	
	
	return retVal;
}


var headlinesRequest = false;

// this array holds default values for select lists and such
var fVals = new Array();

function liLoadSelect(sSelectID, sType, sVal, sText, sFilt, varHttp)
{
	if (document.getElementById(sSelectID))
	{
		document.getElementById(sSelectID).disabled = true;
	
		// prepare and send the request for the lookup info
		var sUrl = '/Handlers/LookupHandler.ashx?Type=1&val=2&text=3&filt=4';
		sUrl = sUrl.replace(/1/,sType).replace(/2/,sVal).replace(/3/,sText).replace(/4/,sFilt);
		//alert(sUrl);
		
		if(window.XMLHttpRequest) {
			varHttp = new XMLHttpRequest();
		}				
							
		if(headlinesRequest && headlinesRequest.readyState < 4) {
			varHttp.abort();
		}

		if(!window.XMLHttpRequest && window.ActiveXObject) {
			varHttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		varHttp.onreadystatechange = function() { liProcessLoadSelect(sSelectID, varHttp); }; //oHandler; //loadHeadlines;
		varHttp.open('GET', sUrl)
		varHttp.send(null);
	}
}

function liProcessLoadSelect(sSelectID, varHttp)
{
	if(varHttp.readyState != 4) { return false; }
	if (varHttp.status != 200) { return false; }
	if (varHttp.responseText == '') { return false; }
	
	var tString = 'Choose . . .:' + varHttp.responseText;
	//alert(tString);

	var select = document.getElementById(sSelectID); //document.createElement("select");   

	// remove any options
	if (select.options.length > 0)
	{
		while (select.options.length > 0)
		{
			select.options[0] = null;
		}
	}		
	/* setting an onchange event */   
	var option;  
	
	/* we are going to add two options */  
	/* create options elements */  
	var aryVal = tString.split('|');
	var i=0;
	var x=0;
	
	for (i=0; i<aryVal.length; i++)
	{
		var aryItem = aryVal[i].split(':');
		option = document.createElement("option"); 
		//alert(aryItem[1]);
		option.setAttribute("value", aryItem[1]);  
		option.innerHTML = aryItem[0];  
		try
		{
			if (aryItem[1] == fVals[select.id]) { option.selected = true; }
		}
		catch (e) {}
		
		select.appendChild(option);  			
	}
	
	select.appendChild(option);
		
	
	//document.getElementById('ddlStateSelect').style.left = liGetOffsetLeft(objStateTextBox);
	document.getElementById(sSelectID).disabled = false;
	varHttp = false;	
}


// This function is used to set the form values of all items EXCEPT select lists
// Select lists use the fVals[] array written out during load
function liSetFormValue(sElId, sVal)
{
	if (document.getElementById(sElId))
	{
		var oEl = document.getElementById(sElId);
		
		if (oEl.tagName.toLowerCase() == 'input')
		{
			switch (oEl.type.toLowerCase())
			{
				case 'text':
					oEl.value = sVal;
					break;
				case 'radio':
					liSetCheckRadio(sElId, sVal);				
					break;
				case 'checkbox':				
					liSetCheckRadio(sElId, sVal);
					break;				
			}
		}
		if (oEl.tagName.toLowerCase() == 'textarea')
		{	
			oEl.value = sVal;
		}		
	}
}

function liSetCheckRadio(sId, sVal)
{
	var oEl = document.getElementsByName(sId);
	var i = 0;
	
	for (i=0; i<oEl.length; i++)
	{
		if (oEl[i].value == sVal)
		{
			oEl[i].checked = true;
		}
	}
}

function liGetOffsetTop(oEl)
{
    var oT = oEl.offsetHeight;
    while ((oEl = oEl.offsetParent) != null) { oT += oEl.offsetTop; }
    return oT;
}

function liGetOffsetLeft(oEl)
{
    var oT = oEl.offsetLeft;
    while ((oEl = oEl.offsetParent) != null) { oT += oEl.offsetLeft; }
    return oT;
}



