////////////////////////////////////////////////////////////////////////////////////////////////////////
// rfValidator()																					  //
// Checks for empty values in the given ElementReference and displays ErrorText "is a required field" //
// Argument Format: (ElementReference, "Element Error Text", ...)                                     //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function rfValidator()
{
	strErrors = "";
	myFields = rfValidator.arguments;
	for(var i=0; i<myFields.length; i=i+2)
	{
		if(StrTrim(myFields[i].value) == "")
		{
			strErrors += "<li>" + myFields[i+1] + " is a required field.</li>";
			FlagField(myFields[i]);		
		}
		else
		{
			UnflagField(myFields[i]);
		}
	}
	return strErrors;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// phoneValidator()																					  //
// Checks for empty values in the given ElementReference and displays ErrorText "is a required field" //
// Argument Format: (ElementReference, "Element Error Text", ...)                                     //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function phoneValidator()
{
	strErrors = "";
	myFields = phoneValidator.arguments;
	var regExpression = /\d\d\d-\d\d\d-\d\d\d\d/;
	for(var i=0; i<myFields.length; i=i+2)
	{
	if((myFields[i].value.length)>1){
		if (! regExpression.test(myFields[i].value))		
		{
			strErrors += "<li>" + myFields[i+1] + " is required in xxx-xxx-xxxx Format.</li>";			
			FlagField(myFields[i]);		
		}
		else
		{
			UnflagField(myFields[i]);
		}
	}	
	}
	return strErrors;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////
// rfCondChkValidator()																			      //
// if conditional element is checked, checks required field element									  //
// Argument Format: (condElementRef, rfElementRef, "Element Error Text", ...)						  //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function rfCondChkValidator()
{
	strErrors = "";
	myFields = rfCondChkValidator.arguments;
	for(var i=0; i<myFields.length; i=i+3)
	{
		if(myFields[i].checked)
		{
			if(StrTrim(myFields[i+1].value) == "")
			{
				strErrors += "<li>" + myFields[i+2] + " is a required field.</li>";
				FlagField(myFields[i+1]);		
			}
			else
			{
				UnflagField(myFields[i+1]);		
			}
		}
		else
		{
			UnflagField(myFields[i+1]);		
		}
	}
	return strErrors;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// rcValidator()																			       	  //
// Required checkbox validator                                  									  //
// Argument Format: (ElementRef, "Error Text", ...)                                                   //                                       						  
////////////////////////////////////////////////////////////////////////////////////////////////////////
function rcValidator()
{
	strErrors = "";
	myFields = rcValidator.arguments;
	for(var i=0; i<myFields.length; i=i+2)
	{
		if(!myFields[i].checked)
		{
			strErrors += "<li>" + myFields[i+1] + "</li>";			
		}
	}
	
	return strErrors;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// rrValidator()																			       	  //
// Required radio button validator                              									  //
// Argument Format: (Radio Name, "Error Text", ...)                                                   //                                       						  
////////////////////////////////////////////////////////////////////////////////////////////////////////
function rrValidator()
{
	strErrors = "";
	myFields = rrValidator.arguments;
	for(var i=0; i<myFields.length; i=i+2)
	{
		myRadArray = document.getElementsByName(myFields[i]);
		isRadSelected = false;
		for(var j=0; j<myRadArray.length; j++)
		{
			if(myRadArray[j].checked)
				isRadSelected = true;
		}
		if(!isRadSelected)
			strErrors += "<li>" + myFields[i+1] + " has not been selected.</li>";
	}
	
	return strErrors;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// rvPastDateValidator()																			  //
// Checks to see if the required value is not a past date											  //
// Argument Format: (DateValue, "Error Date", ...)													  //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function rvPastDateValidator()
{
	strErrors = "";
	var now = new Date();
	
	var strDay = now.getDate().toString();
	for(var x=0; x<2-strDay.length; x++)
		strDay = "0" + strDay;
		
	var strMonth = (now.getMonth() + 1).toString();	
	for(var y=0; y<2-strMonth.length; y++)
		strMonth = "0" + strMonth;

	var strYear = now.getFullYear().toString();
	
    strToday = strYear + strMonth + strDay;

	myFields = rvPastDateValidator.arguments;
	for(var i=0; i<myFields.length; i=i+2)
	{
		if(strToday > myFields[i])
			strErrors += "<li>" + myFields[i+1] + " cannot be a past date.</li>";
	}
	
	return strErrors;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// laValidator()																					  //
// Checks to see if the age meets "low age" requirements											  //
// Argument Format: (Age, Requirement, Text, ...)													  //	
////////////////////////////////////////////////////////////////////////////////////////////////////////
function laValidator()
{
	strErrors = "";
	myArgs = laValidator.arguments;
	for(var i=0; i<myArgs.length; i=i+3)
	{
		if(myArgs[i] < myArgs[i+1])
			strErrors += "<li>" + myArgs[i+2] + " - too young for this program.</li>";
	}
	
	return strErrors;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// haValidator()																					  //
// Checks to see if the age meets "high age" requirements											  //
// Argument Format: (Age, Requirement, Text, ...)													  //	
////////////////////////////////////////////////////////////////////////////////////////////////////////
function haValidator()
{
	strErrors = "";
	myArgs = haValidator.arguments;
	for(var i=0; i<myArgs.length; i=i+3)
	{
		if(myArgs[i] > myArgs[i+1])
			strErrors += "<li>" + myArgs[i+2] + " - too old for this program.</li>";
	}
	
	return strErrors;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// FlagField(obj)																			       	  //
// Highlights the specified field for error                      									  //                                      						 
////////////////////////////////////////////////////////////////////////////////////////////////////////
function FlagField(obj)
{
	obj.style.backgroundColor = "#990000";
	obj.style.color = "#FFFFFF";
	obj.style.fontWeight = "bold";	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// UnflagField(obj)																			       	  //
// De-Highlights the specified field for a fixed error             									  //                                      						 
////////////////////////////////////////////////////////////////////////////////////////////////////////
function UnflagField(obj)
{
	obj.style.backgroundColor = "#FFFFFF";
	obj.style.color = "#000000";
	obj.style.fontWeight = "normal";	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// DisplayErrors(strErrors)                                                                           //
// Displays any validation errors in a div tag                                                        //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function DisplayErrors(strErrors)
{	
	if(strErrors != "")
	{
		document.getElementById("divErrors").innerHTML = "<table><tr><td><table style='border: 1px solid #990000; border-collapse: collapse;'><tr><td style='background-color: #990000; color: white;' valign='bottom'>&nbsp;<img src='./images/error.bmp' />&nbsp;<b>Errors</b></td></tr><tr><td style='font-size: 14px; color: #990000;'><ul style='margin-left: 20px; padding-left: 0px;'>" + strErrors + "</ul></td></tr></table></td></tr></table>";
		scroll(0, 0);
		return false;
	}
	else
	{
		document.getElementById("divErrors").innerHTML = "";
		return true;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// StrTrim()                                                                                          //
// Trims whitespace from beggining and end of a string                                                //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function StrTrim(strObj)
{
	return strObj.replace(/^\s+/,'').replace(/\s+$/,'');
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//  currencyFormat(due)                                                                               //
// Formats currency                                           										  //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function currencyFormat(due){
sign = (due == (due = Math.abs(due)));
		due = Math.floor(due*100+0.50000000001);
		cents = due%100;
		due = Math.floor(due/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((due.length-(1+i))/3); i++)
		due = due.substring(0,due.length-(4*i+3))+','+
		due.substring(due.length-(4*i+3));
		due= ((sign)?'':'-') +  due + '.' + cents;	 
		return due;  
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//  checkType()                                                                               //
// Checks alarm type and sets due amount.                                         										  //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkType(){
     var due=0;
     document.getElementById("txttype").value="";
	 if((document.getElementById("chkFire").checked)&& !(document.getElementById("chkSecurity").checked)){
	 	due= 25;
	 	document.getElementById("txttype").value="F";	 	
	 	}
    if((document.getElementById("chkSecurity").checked)&& !(document.getElementById("chkFire").checked)){ 
       due = 25;
       document.getElementById("txttype").value="S";   
	   }
  if((document.getElementById("chkSecurity").checked)&& (document.getElementById("chkFire").checked)){	 
	    due = 50;	    
	    document.getElementById("txttype").value="B";
	   
	   }
   if((document.getElementById("chkCombo").checked)){
	    		due = 25;	
	      		document.getElementById("txttype").value="C";
	      		document.getElementById("chkSecurity").checked= true;
	      		document.getElementById("chkFire").checked= true;
	   }  
	   
	    document.getElementById("txtdue").value=currencyFormat(due);
}

