/******************
* Required Field Validation
*******************/

function verifyRequiredFields(theForm, validateFields, fieldTitle)
{

 //	theForm = document.forms[0]  passed in from page
	var msg;
	var errorFound;
	var fileError = "";
	
// Declare and Initialize fields to be verified  - passed in
//	var validateFields = new Array();
//	validateFields[1] = "FIELD1";
//	validateFields[2] = "FIELD2";
//	validateFields[3] = "FIELD3";
//	validateFields[4] = "FIELD4";	
	
// passed in	
//  Declare and intitialize the field titles to be displayed to the user.  Netscape and IE do not have a common property to dynamically get this info
//	var fieldTitle = new Array();
//	fieldTitle[1] = "LABEL1";
//	fieldTitle[2] = "LABEL2";	
//	fieldTitle[3] = "LABEL3";	
//	fieldTitle[4] = "LABEL4";		
		
//  Loop through the array of fields and verify that they are not "" or "-Select One-
	for (var i = 0; i < theForm.length; i++) 
	{
		var e = theForm.elements[i];		
		
			for (var y=0; y<validateFields.length; y++) 
			{
		
				if (e.name == validateFields[y])
				{

//  Verify the text fields
				if (((e.type == "text") || (e.type == "hidden") || (e.type == "password") || e.type == "file")&& (e.value != ""))	
				{	
	
					fieldTitle[y] = "";
	      			continue;
				}
// Verify the "Select-One" fields

				if ((e.type == "select-one" ) && (e.selectedIndex !=0))
				{
					//	alert(e.type);
					//	alert(e.selectedIndex)
						fieldTitle[y] = "";
						continue;
				}
				
// Verify the checkbox and radio button fields
				if (e.type == "radio" || e.type == "checkbox")	
				{
					if (e.checked == true)  
					{
						fieldTitle[y] = "";
					}
				}					
			}	
		}
	}

//  Loop through the valid Fields array and add the fields that did not validate to the msg
	msg = "Please enter a value for the following fields.:\n\n";
	for (var i=0; i<fieldTitle.length; i++)
	{
		if (fieldTitle[i])  
		{
			msg += "     " + fieldTitle[i] + "\n\n";
			errorFound = "Y";
		}
	}

//  If an error is found then display message else save form	
	if (errorFound == "Y")
	{
		alert(msg);		
		return false;
	}
	else
	{

		return true;
	}
	
}
/*******************
*  Date Validation
*******************/

function checkDate(dateFields, dateLabels){

	var msg;
	var errorFound;

	for (var y=1; y<dateFields.length; y++) 
	{
		if (dateFields[y] != "") {
			if (isDate(dateFields[y])==true){ 
				dateLabels[y] = "";
			}
		}
		else {
			dateLabels[y] = "";
		}
	}

//  Loop through the valid Fields array and add the fields that did not validate to the msg
	msg = "The following fields have invalid dates.:\n\n";
	for (var i=0; i<dateLabels.length; i++)
	{
		if (dateLabels[i])  
		{
			msg += "     " + dateLabels[i] + "\n\n";
			errorFound = "Y";
		}
	}

//  If an error is found then display message else save form	
	if (errorFound == "Y")
	{
		alert(msg);		
		return false;
	}
	else
	{

		return true;
	}
 }
 
 
/********************
* Email Validation
*********************/
function checkEmail(emailFields, emailLabels){

	var msg;
	var errorFound;

	for (var y=1; y<emailFields.length; y++) 
	{
		if (emailFields[y] != "") {
			if (echeck(emailFields[y])==true){ 
				emailLabels[y] = "";
			}
		}
		else {
			emailLabels[y] = "";
		}
	}

//  Loop through the valid Fields array and add the fields that did not validate to the msg
	msg = "The following fields have invalid email addresses.:\n\n";
	for (var i=0; i<emailLabels.length; i++)
	{
		if (emailLabels[i])  
		{
			msg += "     " + emailLabels[i] + "\n\n";
			errorFound = "Y";
		}
	}

//  If an error is found then display message else save form	
	if (errorFound == "Y")
	{
		alert(msg);		
		return false;
	}
	else
	{

		return true;
	}
 }

/***********************
*
* Number validation
***********************/
/********************
* Email Validation
*********************/
function checkNumber(numFields, numLabels){

	var msg;
	var errorFound;

	for (var y=1; y<numFields.length; y++) 
	{

		if (numFields[y] != "") {
			if (!isNaN(numFields[y]) ) {
				numLabels[y] = "";
			}
		}
		else {
			numLabels[y] = "";
		}
	}

//  Loop through the valid Fields array and add the fields that did not validate to the msg
	msg = "The following fields are numeric and contain non-numeric characters.:\n\n";
	for (var i=0; i<numLabels.length; i++)
	{
		if (numLabels[i])  
		{
			msg += "     " + numLabels[i] + "\n\n";
			errorFound = "Y";
		}
	}

//  If an error is found then display message else save form	
	if (errorFound == "Y")
	{
		alert(msg);		
		return false;
	}
	else
	{

		return true;
	}
 }





function checkNumber2(theField, decallowed) {
alert(theField.name);
// decallowed = 2;  // how many decimals are allowed?
	if (isNaN(theField.value) ) {
		alert("Please enter a valid number.");
		theField.select();
//		thefield.focus();
	}
	else {
		if (theField.value.indexOf('.') == -1) theField.value += ".";
			dectext = theField.value.substring(theField.value.indexOf('.')+1, theField.value.length);
		if (dectext.length > decallowed)
		{
			alert ("Please enter a number with up to " + decallowed + " decimal places.");	
			theField.select();
//			theField.focus();
		}

   }
}



/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
//		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false
	}
return true
}


/**
* Email check
*/

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
//		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
//		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
//		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
//		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
//		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
//		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
//		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

/*
* This function sill prompt the user to confirm that they wish to delete records 
*  from the database
*/
function confirmDelete(theForm) {
	var deleteFound;

// Loop through the form and evaluate each object
	for (var i = 0; i < theForm.length; i++) 
	{
		var e = theForm.elements[i];	

// If the object is a checkbox and begins with the standard used for delete checkboxes
// then evealuate if it is checked
		if ((e.type == "checkbox") && (e.name.substring(0,2) == "D#"))	
		{	
// If one of the delete checkboxes is checked then flag the deletefound variable		
			if (eval("e.checked") == true) {
				deleteFound = "Y";

			}			
		}	
	}
// If one or more of the delete checkboxes are checked
// then prompt the user to confirm the deletion.
	if (deleteFound == "Y") {
		var agree=confirm("You have selected to DELETE one or more records." + "\n\n\n" + "Do you wish to continue?");
		if (agree){
			return true ;
		} else {
			return false ;
		}
	} else {
		alert("No records selected for deletion.");
		return false;
	}
		
}
	
	
function Trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}
	
/*
*  This function will take 2 dates, date format, and the condition and return true or 
*  false based on the condition evaluation.  It is always evaluated
*  firstDate <condition> secondDate
*/	

function compareDate(date1String, date2String, condition, dateType) {
/*
   function compareDate
   parameters: date1String date2String condition dateType
   returns: boolean
   
   date1String and date2String is a date passed as a string in the following
   formats:

   type 1 : 19970529
   type 2 : 970529
   type 3 : 29/05/1997
   type 4 : 29/05/97
   type 5 : 05/29/1997
   
   dateType is a numeric integer from 1 to 4, representing
   the type of dateString passed, as defined above.
   
   Condition is a string representation of the following conditions:
   		<  Date1 is less than Date2
   		>  Date1 is greater than Date2
   		=  Date1 is equal to Date2

   Returns true if the condition is met for Date1 and Date2
   Returns false dateType is not 1 to 4.
*/


    if (dateType == 1) {
        var date1 = new Date(date1String.substring(0,4),
                            date1String.substring(4,6)-1,
                            date1String.substring(6,8));
        var date2 = new Date(date2String.substring(0,4),
                            date2String.substring(4,6)-1,
                            date2String.substring(6,8));                            
    } else if (dateType == 2) {
        var date1 = new Date(date1String.substring(0,2),
                            date1String.substring(2,4)-1,
                            date1String.substring(4,6));
        var date2 = new Date(date2String.substring(0,2),
                            date2String.substring(2,4)-1,
                            date2String.substring(4,6));                            
    } else if (dateType == 3) {
        var date1 = new Date(date1String.substring(6,10),
                            date1String.substring(3,5)-1,
                            date1String.substring(0,2));
        var date2 = new Date(date2String.substring(6,10),
                            date2String.substring(3,5)-1,
                            date2String.substring(0,2));                            
    } else if (dateType == 4) {
        var date1 = new Date(date1String.substring(6,8),
                            date1String.substring(3,5)-1,
                            date1String.substring(0,2));
        var date2 = new Date(date2String.substring(6,8),
                            date2String.substring(3,5)-1,
                            date2String.substring(0,2));  
   } else if (dateType == 5) {
        var date1 = new Date(date1String.substring(6,10),
                            date1String.substring(0,2)-1,
                            date1String.substring(3,5));
        var date2 = new Date(date2String.substring(6,10),
                            date2String.substring(0,2)-1,
                            date2String.substring(3,5));
//alert("Date1=" + date1 + condition + " Date2=" + date2);                     
    } else {
    	alert("No date type passed into compareDate function");
        return false;
    }

	if (condition == "<") {
		if (date1 < date2)
			return true;	
	} else if (condition == ">") {
		if (date1 > date2)
			return true;	
	} else if (condition == "=") {
		if (date1.toString() == date2.toString())
			return true;	
	} else {
		alert ("An invalid condition was passed into compareDate function");
		return false;
	}
	
	// No conditions were met so return false
	return false;
}


/******************
* Field Length Validation
*******************/
function verifyLength(fNames, fLengths, fLabels){
	var msg;
	var errorFound;

	for (var y=1; y<fNames.length; y++) 
	{
		if ((fNames[y] != "") && (fNames[y] != 0)) {

			if (fNames[y].length == fLengths[y]) {
				fLabels[y] = "";
			}
		}
		else {
			fLabels[y] = "";
		}
	}

//  Loop through the valid Fields array and add the fields that did not validate to the msg
	msg = "The following fields do not meet the required number of characters.  The number in parenthesis indicates the required length of the field.:\n\n";
	for (var i=0; i<fLabels.length; i++)
	{
		if (fLabels[i])  
		{
			msg += "     " + fLabels[i] + " (" + fLengths[i] + ")\n\n";
			errorFound = "Y";
		}
	}

//  If an error is found then display message else save form	
	if (errorFound == "Y")
	{
		alert(msg);		
		return false;
	}
	else
	{

		return true;
	}
 

}



// Rounds to the dollar value for display
function dollarValue(n)
{
  var Bstr = "";
  var b = n * 100;
  var c = Math.round(b);
  var Astr = c.toString();
  var Alength = Astr.length;
  if (Alength <= 2) Bstr = Bstr + ".";
  var dot = Alength - 3;
  var counter = 0;
    while (counter < Alength )
    {
      Bstr = Bstr + Astr.charAt(counter);
      if (counter == dot) Bstr = Bstr + ".";
      counter = counter + 1;
    }
  var d = Bstr;
  return d;
}

function popUpTimer(){
	window.setTimeout(popupRR,2000);
}


function popupRR()     { 
	URL = 'pop_RetrieveResults.html'                     
	winName = "RETRIEVE_RESULTS" 
	winParam = "width=430,height=200,left=200,top=200,title=0,titlebar=0,menubar=0,location=0,status=0,toolbar=0,resizeable=1,title=0,scrollbar=1" 
	win = window.open( URL,winName,winParam) 
   
}
     
function closeRR() {
	if (win && win.open && !win.closed) win.close();
}

	
	

