function ValidateForm(validation, validateto) {
	var isFormValid = true;
	
	HideBalloon(validateto);

	for(i = 0; i < validation.length && i <= validateto; i++) {
		var field = validation[i][0];
		var fieldObj = document.getElementById(field);
		var isRequired = validation[i][1];
		var regExString = validation[i][2];
		var errorMessage = validation[i][3];
	    var theForm = document.forms[0];
	  
	
	
	if(fieldObj.type == "checkbox"){
		if (isRequired) {
			if(fieldObj.checked == false){
				ShowBalloon(GetLeft(fieldObj), GetTop(fieldObj), errorMessage);
				fieldObj.focus();
				return false;
				}
			}
		 }

		
		if (!fieldObj.disabled) {
			if (isRequired) {
				if (fieldObj.value == "") {
					// field is required and empty show error
					ShowBalloon(GetLeft(fieldObj), GetTop(fieldObj), errorMessage);
					fieldObj.focus();
					return false;
				}
			}
			

			if (fieldObj.value != "" && regExString != "") {
			
				var valid = true;
				eval('valid = ' + regExString.replace('[value]', fieldObj.value).replace('\\','\\\\'));
				if (!valid) {
					ShowBalloon(GetLeft(fieldObj), GetTop(fieldObj), errorMessage);
					fieldObj.focus();
					return false;
				}
			}
		}
	}

	return true;
}

function ShowBalloon(x, y, message) {
	var balloon = document.getElementById('balloon');
	var balloontext = document.getElementById('balloontext');
	var balloontable = document.getElementById('balloontable');
	
	balloontext.innerHTML = message;
	balloon.style.visibility = "visible";
	var leftTmp = x - 110;
	leftTmp = leftTmp + 40;
	leftTmp = leftTmp + 'px';
	balloon.style.left = leftTmp;
	var topTmp = y - balloontable.clientHeight + 5;
	topTmp = topTmp + 'px';
	balloon.style.top = topTmp;
	
	HideDropDowns(x - 110,y - balloontable.clientHeight + 5,balloontable.clientWidth,balloontable.clientHeight);	
	
}


function HideBalloon() {
	var balloon = document.getElementById('balloon');
	balloon.style.visibility = "hidden";	
	ShowDropDowns();
	
}

function GetLeft(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE

   var nLeftPos = eElement.offsetLeft;       // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy
      if(DL_bIE)                             // if browser is IE, then...
      {
         if(eParElement.tagName == "TD")     // if parent a table cell, then...
         {
            nLeftPos += eParElement.clientLeft; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nLeftPos += 1;             // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nLeftPos += nParBorder;       // append the border width to counter
            }
         }
      }

      nLeftPos += eParElement.offsetLeft;    // append left offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nLeftPos;                          // return the number calculated
}

function GetTop(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE

   var nTopPos = eElement.offsetTop;         // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy
      if(DL_bIE)                             // if browser is IE, then...
      {
         if(eParElement.tagName == "TD")     // if parent a table cell, then...
         {
            nTopPos += eParElement.clientTop; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nTopPos += 1;              // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nTopPos += nParBorder;        // append the border width to counter
            }
         }
      }

      nTopPos += eParElement.offsetTop;      // append top offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nTopPos;                           // return the number calculated
}

function IsDate(pValue) {
	// check to make sure that at least the characters
	// are in the right place
	var regEx = new RegExp("^(\\d{1,2}/\\d{1,2}/\\d{4})$", "i");
	if (regEx.exec(pValue) == null) {
		return false;
	}
	
	// parse out the different date parts
	var year = new Number(pValue.substr(pValue.lastIndexOf('/') + 1, 4));
 	var day = new Number(pValue.substring(pValue.indexOf('/') + 1, pValue.lastIndexOf('/')));
 	var month = new Number(pValue.substr(pValue, pValue.indexOf('/')));
	
	// do a quick data range check
	if (!(month > 0 && month < 13)) return false;
	if (!(day > 0 && day < 32)) return false;
	if (year > 2500) return false;
	if (year >= 1900 && year < 2000) year -= 1900
	
	// month argument must be in the range 1 - 12
	month = month - 1;  // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if ((tempDate.getYear() == year) &&
			(month == tempDate.getMonth()) &&
			(day == tempDate.getDate()) )
		return true;
	else 
		return false;
}

function RegExCheck(pValue, pRegExp)
{
	var regEx = new RegExp("^(" + pRegExp + ")$", "i");
	if (regEx.exec(pValue) == null) {
		return false;
	}
	return true;
}

function IsPassword(pValue) {
	if (pValue.length < 7) return false;
	
	var haslowerletter = true;
	var hasupperletter = true;
	var hasnumber = true;

	var regEx = new RegExp("[a-z]+");
	if (regEx.exec(pValue) == null) {
		haslowerletter = false;
	}
	
	var regEx = new RegExp("[A-Z]+");
	if (regEx.exec(pValue) == null) {
		hasupperletter = false;
	}

	var regEx = new RegExp("\\d+", "i");
	if (regEx.exec(pValue) == null) {
		hasnumber = false;
	}
	
	if ((haslowerletter && hasupperletter) 
			|| (haslowerletter && hasnumber) 
			|| (hasupperletter && hasnumber)) {
		return true;
	}
	
	return false;
}

function HideDropDowns(bleft,btop,bwidth,bheight){

   // the height & width of the dropdowns on the form must be set for this to work
 
   var theForm = document.forms[0]

   for(i=0; i<theForm.elements.length; i++){
  
   if(theForm.elements[i].type == "select-one"){
		var ddl = document.getElementById(theForm.elements[i].id);
		var ddleft = GetLeft(ddl);
		var ddtop  = GetTop(ddl);
	
		if ((bleft + bwidth < ddleft) || (btop + bheight < ddtop + 6) || (bleft > ddleft + ddl.style.posWidth) ||(btop > ddtop + ddl.style.posHeight))
			{
				ddl.style.visibility = "visible";
			}
		else
			{
			ddl.style.visibility ="hidden";
			}
			
		
      }
  
   
   }

} 

function ShowDropDowns(){

   var theForm = document.forms[0]

   for(i=0; i<theForm.elements.length; i++){
  
   if(theForm.elements[i].type == "select-one"){
		var ddl = document.getElementById(theForm.elements[i].id);
		
		ddl.style.visibility = "visible";
		
	}
}	
} 



