
function Validator(frmname)
{  
   this.formobj=document.forms[frmname];
 
	if(!this.formobj)
	{
	  alert("Error: could not get Form object "+frmname);
		return;
	}
	
	if (!this.formobj.old_onsubmit) {
	    
		if (this.formobj.onsubmit==Validator_form_submit)
		   this.formobj.onsubmit = null;
		   
		if(this.formobj.onsubmit) {	 		    
		 	this.formobj.old_onsubmit = this.formobj.onsubmit;
			this.formobj.onsubmit=null;
		} else {
	 		this.formobj.old_onsubmit = null;
		}
	}
	this.formobj._sfm_form_name=frmname;
	this.formobj.clear_msgs = Validator_clear_msgs;	
	this.formobj.onsubmit=Validator_form_submit;
	this.addValidation = Validator_add_Validation;	
		
}

function Validator_add_Validation(aControl, aFunction, aMsg, aArgs){   
   var control = aControl;
         
   if (aControl.length) control = aControl[0];
     
   if (!control.validators)
    	control.validators = new Array();       	
   control.validators[control.validators.length]= new ValidatorItem(aControl, this.formobj, aFunction, aMsg, aArgs);
}

function Validator_control_validate(){
    var bResult = true;
	if (this.validators){
	   for(var i=0;i < this.validators.length;i++) 
			if (!this.validators[i].validate())
                          bResult = false;
	}
    return bResult;
}

function Validator_form_submit(){    
  var bResult = true;
  if ((this.old_onsubmit)&& (this.old_onsubmit!=null)) {
	  var bOldRes = this.old_onsubmit();
	
	  if ((bOldRes!=undefined) && (bOldRes==false) )
	      return false;
  }	  
  
   var el = this.elements;
   this.clear_msgs();   
	for(var i=0;i < this.elements.length;i++) {  
		if (this.elements[i].validate) if(!this.elements[i].validate()) bResult = false;
	}	

  return bResult;
}

function Validator_clear_msgs(){
   var el = this.elements;   
   for(var itr=0;itr <el.length;itr++) {   
	   if ((el[itr].type=='radio') || (el[itr].type=='checkbox') )
	      el[itr].parentNode.errorMsg = new Array();
       else el[itr].errorMsg = new Array();
	}   
}


function ValidatorItem(aControl, aForm, aFunction,aMsg, aArgs ){
   var control = aControl;
      
   if (aControl.length) control = aControl[0];  
		
    control.validate = function (){
       var bResult = true;
       this.className = ""; 
       for (var i=0; i< this.validators.length; i++){
          if (!this.validators[i].validate()) bResult = false;
       }
       return bResult;
    }
	
    this.validate = ValidatorItem_validate;
    this.validateFunction = aFunction;
    this.formobj = aForm;
    this.message = aMsg;
    this.control = aControl;
    if (aArgs)
    	this.args = aArgs;
}

function ValidatorItem_validate(){
	var args = new Array();	
	args[0] = this.control;
     if (this.args){
	var addtlArgs = this.args.split(";");
	for (var i=0; i<addtlArgs.length; i++){
    	var epos = addtlArgs[i].indexOf("="); 
		command  = addtlArgs[i].substring(0,epos); 
        cmdvalue = addtlArgs[i].substr(epos+1); 
		if ((cmdvalue.indexOf ("{")==0) && 
		    (cmdvalue.indexOf ("}")==cmdvalue.length-1)
		   ){
		  cmdvalue = this.formobj.elements[cmdvalue.substring(1,cmdvalue.length-1)].value;
		}    
		args[args.length] = cmdvalue;
	}
   }   
   var bResult =  this.validateFunction.apply(this, args);
   var errControl = this.control;
   
   if (this.control.length) errControl = this.control[0];   
   
   if ((errControl.type=='radio')||(errControl.type=='checkbox')) {
	   errControl = errControl.parentNode;
   }	   
	   
   if (!errControl.errorMsg)
        errControl.errorMsg = new Array();   	   
		
   var errMsg = errControl.errorMsg;   		
		
   if (!bResult) {     	    
        if (errControl.oldclassName==undefined) {		   
			errControl.oldclassName=errControl.className;		
		}	
		errControl.className = "erring";
		errControl.errorMsg[errMsg.length] =  this.message;
		errControl.oldmouseover = errControl.onmouseover;
		errControl.oldmouseout = errControl.onmouseout;
		errControl.onmouseover =  showError;    
		errControl.onmouseout = hideMsg;

   }else {
	 if (errControl.errorMsg.length<1) 
	     errControl.className=errControl.oldclassName;
     if ((errControl.oldmouseover) && (errControl.oldmouseover!=null)){
        errControl.onmouseover = errControl.oldmouseover
        errControl.oldmouseover = null;
     }
     
     if ((errControl.oldmouseout) && (errControl.oldmouseout!=null)){
        errControl.onmouseout = this.control.oldmouseout
        errControl.oldmouseout = null;
     }
   }
   return bResult;	 
}

function showError(){
   
   var msg = "";
   if (this.errorMsg.length>1){
      msg = "<ul>" 
      for (var i=0; i<this.errorMsg.length; i++){ 
        msg = msg+"<li>"+this.errorMsg[i]+"</li>";
      }
      msg = msg+"<ul>";
   } else {
       msg = this.errorMsg[0];
   }   
  if (!isBlank(msg))
     inlineMsg(this,msg);
}

function isBlank(val) {
	if (val == null) {
		return true;
	}
	for (var i = 0; i < val.length; i++) {
		if ((val.charAt(i) != " ") && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r")) {
			return false;
		}
	}
	return true;
}

function validateRequired(aControl) {  
   var val = aControl.value;
   return !isBlank(val);
}

function validateEmail(aControl) {  
   if (isBlank(aControl.value)) return true;
   var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(aControl.value)) {
		return false;
	}
   return true;
}

function validateDate(aControl, aFormat) {
  var val = aControl.value;
  if (!isBlank(val))
    return isDate(val, aFormat);
  return true;
}

function validateRequiredRadio(aControl) {  
  var len = aControl.length
  for (i=0; i<len; i++){
	  if (aControl[i].type!="radio") {
		  alert(" Control is not a radio button");
		  return false;
	  } else {
		  if (aControl[i].checked) return true;
	  }
  }     
   return false;
}

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');

function isDate( val, format )  {
	var date=parseDate(val,format );
	if ( date==null ) {
		return false; 
	}
	return true;
}	

function parseDate(val, format ){
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getFullYear();
	var month=now.getMonth()+1;
	var date=1;
	var hh=now.getHours();
	var mm=now.getMinutes();
	var ss=now.getSeconds();
	var ampm="";
	while (i_format < format.length) {
	    // Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
				token += format.charAt(i_format++);
		}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			var yearLen =(""+year).length;
			year=_getInt(val,i_val,x,y);
			if (year==null) { return null; }
			i_val += yearLen;
			if (yearLen==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
			}
		} else if (token=="MMMM"||token=="MMM"||token=="NNN") {
			month=0;
			for (var i=0; i<MONTH_NAMES.length; i++) {
				var month_name=MONTH_NAMES[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
					if (token=="MMMM"||(token=="MMM"&&i>11)||(token=="NNN"&&i>11)) {
						month=i+1;
						if (month>12) { month -= 12; }
						i_val += month_name.length;
						break;
					}
				}
			}
			if ((month < 1)||(month>12)){return null;}
		} else if (token=="EE"||token=="E") {
			for (var i=0; i<DAY_NAMES.length; i++) {
				var day_name=DAY_NAMES[i];
				if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
					i_val += day_name.length;
					break;
				}
			}
		} else if (token=="MM"||token=="M") {
			month=_getInt(val,i_val,token.length,2);			
			if(month==null||(month<1)||(month>12)){return null;}
				i_val+=token.length;
		} else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return null;}
			i_val+=token.length;
		} else if (token=="hh"||token=="h") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>12)){return null;}
				i_val+=token.length;
		} else if (token=="HH"||token=="H") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>23)){return null;}
			i_val+=token.length;
		} else if (token=="KK"||token=="K") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>11)){return null;}
			i_val+=token.length;
		} else if (token=="kk"||token=="k") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>24)){return null;}
			i_val+=token.length;hh--;
		} else if (token=="mm"||token=="m") {
			mm=_getInt(val,i_val,token.length,2);
			if(mm==null||(mm<0)||(mm>59)){return null;}
			i_val+=token.length;
		} else if (token=="ss"||token=="s") {
			ss=_getInt(val,i_val,token.length,2);
			if(ss==null||(ss<0)||(ss>59)){return null;}
			i_val+=token.length;
		} else if (token=="a") {
			if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
			else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
			else {return null;}
			i_val+=2;
		} else {
			if (val.substring(i_val,i_val+token.length)!=token) {return null;}
			else {i_val+=token.length;}
		}
	}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) { return null; }
	// Is date valid for month?
	if (month==2) {
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
			if (date > 29){ return null; }
		}
		else { if (date > 28) { return null; } }
	}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return null; }
	}
		
	// Correct hours value
	if (hh<12 && ampm=="PM") { hh=hh-0+12; }
	else if (hh>11 && ampm=="AM") { hh-=12; }
	var newdate = new Date(year,month-1,date,hh,mm,ss);
	return newdate;
}

function _getInt(str,i,minlength,maxlength){
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) {
				return null; 
		}
		if (_isInteger(token)) { 
				return token; 
		}
	}
	return null;
}

function _isInteger(val){
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { 
				return false; 
		}
	}
	return true;
}

function validateNumber(aControl) {
  var val = aControl.value;
  if (!isBlank(val))
      return (parseNumber(val)!=null);
  return true;
}

function parseNumber(val){
    var digits="1234567890";
    var sNumber = "";
    var bIsEmpty=true;
    for (var i=0; i<val.length; i++){
	   if (val.charAt(i)!=' ')
	      bIsEmpty = false;

       if (((val.charAt(i)=='-') && i==0)||
     	   (val.charAt(i)=='.')||
     	   (digits.indexOf(val.charAt(i))!=-1)
     	  ) {
	       sNumber = sNumber+val.charAt(i);
       }else if (val.charAt(i)!=",")
       		return null;
    }     
			
	if (bIsEmpty) return null;// do nothing	  
    return parseFloat(sNumber);
}


// START OF MESSAGE SCRIPT //
var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(targetdiv,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;

  if ((targetdiv.tagName=="INPUT") || (targetdiv.tagName=="SELECT"))
      targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if ((msg!=null) && (!msg.timer)) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}