/**
* a javascript class for form validation
* for now it's only validating required fields, you may add your own validation
* @author: markg
* @on: 5/25/2008
* @email: mglibres@dwebsite.com,markglibres@yahoo.com
*
*Example:
*
*<script src="/cad_js/FormValidator.js" type="text/javascript"></script>
*<script type="text/javascript">
*{literal}
*	var objM = new FormValidator();
*	
*	objM.addField('buildingName','Building Name');
*{/literal}
*</script>
*
*
*/
function FormValidator()
{
	this.requireFields = null;
	this.requireFieldDescription = null;
	this.excludeFields = null;
	this.nullValues = null;
	
	this.numericFields = null;
	this.numericFieldDesc = null;
	
};

FormValidator.prototype.clearFields = function()
{
	this.requireFields = new Array();
	this.requireFieldDescription = new Array();
	this.excludeFields = new Array();
	this.nullValues = new Array();
	
	this.numericFields = new Array();
	this.numericFieldDesc = new Array();
}

FormValidator.prototype.getFields = function()
{
	if(!this.requiredFields)
	{
		this.requiredFields = new Array();
		this.requireFieldDescription = new Array();
		this.nullValues = new Array();
	}
	
	if(!this.numericalFields){
		this.numericalFields = new Array();
		this.numericalFieldDesc = new Array();
	}
	this.excludeFields = new Array();
}
/** nullValue will determine the value to be considered as null such as on a select object**/
/* For example:
* <select>
*	<option value="0" selected>Select State</option>
*	<option value="NY">New York</option>
*</select>
* this will check if value selected is not equal to "0" as a required field.
*/
FormValidator.prototype.addField = function(fieldName, fieldDescription, validationType, nullValue)
{
	this.getFields();
	
	/**sets default validation type**/
	if(validationType == null)
		validationType = 'required';
		
	if(validationType == 'required')
	{
		this.requiredFields[this.requiredFields.length] = fieldName;
		this.requireFieldDescription[this.requireFieldDescription.length] = fieldDescription;
		if(nullValue != null)
			this.nullValues[this.nullValues.length] = nullValue;
		else
			this.nullValues[this.nullValues.length] = null;
			
	}
	
	if (validationType == 'numeric'){
		this.numericalFields[this.numericalFields.length] = fieldName;
		this.numericalFieldDesc[this.numericalFieldDesc.length] = fieldDescription;
	}
	

};

/**
* will add fields to be excluded on validation
* @author: markg
*/
FormValidator.prototype.excludeField = function(fieldName)
{
	this.getFields();
	var found = this.isExluded(fieldName);
	if(!found)
		this.excludeFields[this.excludeFields.length] = fieldName;
	
};

/**
* will add fields to be included again on validation
* @author: markg
*/
FormValidator.prototype.includeField = function(fieldName)
{
	this.getFields();
	for(i = 0 ;i < this.excludeFields.length; i++)
	{
		if(this.excludeFields[i] == fieldName)
		{
			this.excludeFields[i] = null;
			break;
		}
		
	}
	
};

FormValidator.prototype.isExluded = function(fieldName)
{
	var found = 0;
	for(var i = 0 ;i < this.excludeFields.length; i++)
	{
		if(this.excludeFields[i] == fieldName)
		{
			found = 1;
			break;
		}
		
	}
	return found;
}

FormValidator.prototype.validate = function()
{
	
	var notValidated = new Array();
	var notNumeric = new Array();
	var msg = "";
	
	if(this.requiredFields)
	{
		var excluded = 0;
		for(var i=0;i<this.requiredFields.length-1; i++)
		{
		
			excluded = this.isExluded(this.requiredFields[i]);
			obj = document.getElementById(this.requiredFields[i]);
			obj = obj.value;
			obj = obj.replace(/^\s+|\s+$/g, '');
			
			if( (!obj || this.nullValues[i] == obj) && !excluded)
			{
				notValidated[notValidated.length]= this.requireFieldDescription[i];
			}
			
		}
		
	}
	
	if (this.numericalFields){
		for(var i=0;i<this.numericalFields.length;i++){
			obj = document.getElementById(this.numericalFields[i]).value;
			if(isNaN(obj)){
				notNumeric[notNumeric.length] = this.numericalFieldDesc[i];
			}
		}
	}
	
	if(notValidated.length > 0)
	{
		msg = "The following fields are required:\n ";
		for(var i=0;i<notValidated.length;i++)
		{
			msg += "\n"+notValidated[i];
			
		}
		alert(msg);
		return false;
	}
	
	
	
	if(notNumeric.length > 0)
	{
		msg = "The following fields are not numeric:\n ";
		for(var i=0;i<notNumeric.length;i++)
			msg += "\n"+notNumeric[i];
		alert(msg);
		return false;
	}
	

	return true;
};

/*******************************************************************************************
** DIV Functions
** added by peter
*******************************************************************************************/
var LastOpenDiv='';
var ConfirmObjects='';
var alertScrollID='';

function DivOpen(myBox,myPosition,myLeft,myRight,myTop,myBottom,flags) { 
    	if(document.getElementById){ 
			var thisBox = document.getElementById(myBox);
			var thisMask = document.getElementById('Mask');

			if (thisMask) thisMask.style.visibility = 'visible';
			if (LastOpenDiv!='' && LastOpenDiv!=myBox && flags!='no close') {
				var hideBox = document.getElementById(LastOpenDiv);
				if (hideBox) hideBox.style.visibility = 'hidden';
			}

			var selectBoxesCnt=document.getElementsByTagName("select").length;
			if (selectBoxesCnt>0) {
				var selectBoxes=document.getElementsByTagName("select");
				for (i=0;i<selectBoxesCnt;i++) {
					var zbox=document.getElementById("ibox_"+ selectBoxes[i].name);
					var hbox=document.getElementById(selectBoxes[i].name);
					if (zbox) {hbox.style.display='none'; zbox.style.display='inline';}
				}
			}

			if (thisBox) {
				 thisBox.style.visibility = 'visible';
				 thisBox.style.display = 'block';
				 LastOpenDiv=myBox;

				if (myPosition) thisBox.style.position = myPosition;
				if (myLeft) thisBox.style.left = myLeft;
				if (myRight) thisBox.style.right = myRight;
				if (myTop) thisBox.style.top = myTop;
				if (myBottom) thisBox.style.bottom = myBottom;


			}
	}
} 
function DivClose(myBox) {
	var thisMask=document.getElementById('Mask');
	var thisBox = document.getElementById(myBox);
	var IfrRef = document.getElementById('DivShim');
	if (thisMask) thisMask.style.visibility = 'hidden';
	if (thisBox) { thisBox.style.visibility = 'hidden'; thisBox.style.display = 'none'; }
	
	var selectBoxesCnt=document.getElementsByTagName("select").length;
	if (selectBoxesCnt>0) {
		var selectBoxes=document.getElementsByTagName("select");
		for (i=0;i<selectBoxesCnt;i++) {
			var zbox=document.getElementById("ibox_"+selectBoxes[i].name);
			var hbox=document.getElementById(selectBoxes[i].name);
			if (zbox) { hbox.style.display='inline'; zbox.style.display='none'; }
		}
	}
	
	clearInterval(alertScrollID);
}

