var val = new Object()
// The following function can be called to check form field
//    atLeastOneCheckBoxReq
//    between
//    bothFieldsRequired
//    character
//    checkBoxExcludesCheckBoxes
//    checkBoxExcludesFields
//    checkBoxInputPair
//    checkPhone
//    compareDate
//    Equal4TextBoxes
//    greaterthan
//    gte
//    gtzero
//    isEmpty
//    isEMail
//    isEqual
//    isNoEmpty
//    isNumeric
//    isNumber
//    nonnegative
//    nonnegative2
//    notCheckBoxNeedFields
//    oneFieldRequiresAll
//	   popUpWin
//    positive
//    replaceChars
//    replaceCharsback
//    required
//    requiredOption
//    selectRequired
//    selectRequiredNoZero
//    setFormElement
//    size
//    stripSpace
//    trim
//    TransformForm
//    confirmBox
//    Trim()
//   Copy Right: WExcel, Inc. 1999, 2000, 2001, 2002
//

val.name = "$Name:  $"
val.revision = "$Revision: 1.1 $"
val.rcsfile = "$RCSfile: val.js,v $"
val.date = "$Date: 2003-06-16 16:41:25-05 $"
val.author = "$Author: gnool $"

val.requiredRadioOption = function (field, message)
  { var rx=0;
    for( rx=0; rx<field.length; rx++)
       { if( field[rx].checked==true )
             return true;
       }
    alert(message);
    return false;
 }
val.size = function(field,s,message)
  { if( field.value.length>parseInt(s) )
      { if(message) alert(message);
        field.select();
        return false;
      }
    else return true;
 }
val.positive = function(field,message)
  { var f=field.value.replace(/[$,]/, "");
    var v=parseFloat(f);
    if( f.match(/[^0-9]/g) || v<=0 )
      { if(message) alert(message);
        field.select();
       return false;
      }
    else { field.value=f;
           return true
         }
 }
val.nonnegative = function(field,message)
  { var object_value;
    object_value = field.value;
    if (object_value.length == 0) return true;

    var start_format = " .+0123456789";
    var number_format = " .0123456789";
    var check_char;
    var decimal = false;
    var trailing_blank = false;
    var digits = false;

    check_char = start_format.indexOf(object_value.charAt(0))
    if(check_char==1) decimal = true;
    else if(check_char<1) { alert(message);
                            return false;
                          }

    //Remaining characters can be only . or a digit, but only one decimal.
    for( var i=1; i<object_value.length; i++)
       { check_char=number_format.indexOf(object_value.charAt(i))
         if( check_char<0 ) { alert(message);
                              return false; }
         else if(check_char==1) { if(decimal) { alert(message);     // Second decimal.
                                                return false; }
                                  else decimal=true;
                                }
         else if(check_char==0) { if(decimal||digits) trailing_blank=true; // ignore leading blanks
                                }
         else if(trailing_blank){ alert(message);
                                  return false;
                                }
         else digits = true;
       }
    //All tests passed, so...
    return true
 }
val.nonnegative2 = function(field,message)
  { var f=field.value.replace(/[$,]/, "");
    var v=parseFloat(f);
    if( f.match(/[^0-9]/g)||v<0 )
      { if(message) alert(message);
        field.select();
        return false;
      }
    else { field.value=f;
           return true;
         }
 }
val.required = function(field,message)
  { if( field.value=="" )
      { if(message) alert(message);
        field.select();
        return false;
      }
    else return true;
 }
val.requiredOption = function(field,message)
  { if( field.options[field.selectedIndex].value=="" )
      { if(message) alert(message);
         return false;
      }
    else return true;
 }

val.character = function(field,message)
  { if(field.value.length<=0) return true;
    if(field.value==0) return true;

    f=field.value.replace(/[$,]/, "");
    v=parseFloat(field.value);
    if( isNaN(v) || v!=field.value || v==0 || f.match(/\s/) )
      { alert(message);
        field.select();
        return false;
      }
    else return true;
 }

// Returns true if the value of the field contains only numeric characters
val.isNumeric = function(field,message)
  { if(field.value.length<=0) return true;
    if(field.value==0) return true;

    f=field.value.replace(/[$,]/, "");
    v=parseFloat(field.value);
    if( isNaN(v) || v != field.value || v==0 || f.match(/\s/) )
      { alert(message);
        field.select();
        return false;
      }
    else return true;
 }

val.gtzero = function(field, message)
  { if(field.value.length<=0) return true;

    var v=field.value;
    if(v>0) return true;
    else { if(message) alert(message);
           field.select();
           return false;
         }
 }
val.between = function(field,low,high,message)
  { if(field.value.length<=0) return true;

    var v=parseFloat(field.value);
    if(isNaN(v)) { alert(message);
                   return false; }
    if( v>high || v<low )
      { if(message) alert(message);
        field.select();
        return false;
      }
    else return true;
 }
val.greaterthan = function(field,lower,message)
  { if(field.value.length<=0) return true;

    var v=parseFloat(field.value);
    if(isNaN(v)) { alert(message);
                   return false; }
    if( v<=lower )
      { if(message) alert(message);
        field.select();
        return false;
      }
    else return true;
 }
val.gte = function(field,lower,message)
  { if(field.value.length<=0) return true;

    var v=parseFloat(field.value);
    if(isNaN(v)) { alert(message);
                   return false; }
    if( lower>v)
      { if(message) alert(message);
        field.select();
        return false;
      }
    else return true;
 }
val.isEqual = function (n,s,ne)
  { if( n.value.length==0 || n.value!=ne )
      { window.alert(s);
        n.select();
        return false;
      }
    else return true;
 }
val.isEmpty = function (n,s)
  { if( n.value.length==0 )
      { if(s) window.alert(s);
	    n.select();
        return false;
      }
    else return true;
 }

val.isNotEmpty = function (n,s)
  { if( n.value.length!=0 )
      { if(s) window.alert(s);
        n.select();
        return false;
      }
    else return true;
 }
val.isEMail = function (field, message)
  { var emailregxvalid=/^[a-zA-Z0-9]+.*\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/  //valid email
    var emailregxinvalid=/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;                  // common invalid forms
    if( !emailregxvalid.test(field.value) || emailregxinvalid.test(field.value) )
      { window.alert(message);
        field.select();
        return false;
      }
    else return true;
 }

// If i exists, j must also exist, else print error s
val.bothRequired = function(i, j, s)
  { if( i.value.length>0 )
      { if( j.value.length>0 )
          { return true; }
        else
          { window.alert(s);
            j.select();
            return false;
          }
      }
   else return true;
}

// If i exists, j must also exist, else print error s
val.bothFieldsRequired = function(i,j,s)
  { if( i.value.length>0 )
      { if( j.value.length>0 )
          { return true; }
        else
          { window.alert(s);
            j.select();
            return false;
          }
      }
    else if( j.value.length>0 )
           { if( i.value.length>0 )
               { return true; }
             else
               { window.alert(s);
                 i.select();
                 return false;
               }
           }
    else return true;
 }

val.oneFieldRequiresAll = function()
  { var EMPTY=true;
    if( arguments.length<2) // must specify at least the error message and one form element
        return true;

    var x=arguments;
    for( var i=1; i<x.length; i++) // Check if at least one of?Ó³•o”¬“².×î?.??.?o?? the elements is not empyt
       { var fieldValue="";
         if( is.ns )
           { if( x[i].type=='select-one' )
               { fieldValue=x[i].options[x[i].selectedIndex].value; }
             else
               { fieldValue=x[i].value;
           }   }
         else fieldValue = x[i].value;
         if(fieldValue) { EMPTY=false;
                          break;
                        }
       }
    if(EMPTY) return true;
    else   // Make sure all elements have a value
      { for( var i=1; i<x.length; i++ ) // Check if at least one of the elements is not empyt
           { var fieldValue="";
             if( is.ns )
               { if( x[i].type=='select-one' )
                   { fieldValue=x[i].options[x[i].selectedIndex].value; }
                 else fieldValue=x[i].value;
               }
             else fieldValue=x[i].value;
             if(!fieldValue) { alert(x[0]);
                               x[i].select();
                               return false;
           }                 }
      }
    return true;   // At this point, non of the elements are empty
 }

// If at least one check box is checked, return true, else print error (first argument)
val.atLeastOneCheckBoxReq = function()
  { if( arguments.length<2 )  // must specify at least the error message and one check box
        return true;

    var x=arguments;
    for( var i=1; i<x.length; i++)
       { if(x[i].checked) return true; }
    // At this point, no checked check boxes were found
    window.alert(x[0]);
    x[1].select();
    return false;
 }

val.checkBoxExcludesCheckBoxes = function()
  { if( arguments.length<2 )  // must specify at least the error message and one check box
        return true;

    var x=arguments;
    if(!x[1].checked) return true;
    for( var i=2; i<x.length; i++ )
       { if(x[i].checked) { window.alert(x[0]);
                            x[i].select();
                            return false;
       }                  }
    return true;
 }

val.checkBoxExcludesFields = function()
  { if( arguments.length<2 )  // must specify at least the error message and one check box
        return true;

    var x=arguments;
    if(!x[1].checked) return true;
    for( var i=2; i<x.length; i++ )
       { if(x[i].value) { window.alert(x[0]);
                          x[i].select();
                          return false;
       }                }
    return true;
 }

val.notCheckBoxNeedFields = function()
  { var bidEntered=false;
    var x=arguments;

    if( x.length<3 )  // must specify at least the error message and two check boxes
        return true;

    if(x[1].checked || x[2].checked) return true;
    for( var i=3 ; i<x.length; i++ )
       { if(x[i].value.length>0) { bidEntered = true;
                                   break;
       }                         }
    if( !bidEntered && !x[1].checked && !x[2].checked )
      { alert (x[0]);
        x[1].select();
        return false;
      }
    return true;
 }

// print error if no selection is made
val.selectRequired = function(field, message)
  { if(field.selectedIndex>=0) return true;
    else { if(message) alert(message);
           field.select();
           return false;
         }
 }

// print error if no selection is made
val.selectRequiredNoZero = function(field, message)
  { if(field.selectedIndex>0) return true;
    else { if(message) alert(message);
           field.select();
           return false;
         }
 }
val.replaceChars = function(f)
  { this.TransformForm(f,parent.util.escapeapos);
 }
val.replaceCharsback = function(f)
  { this.TransformForm(f,parent.util.unescapeapos);
 }
val.checkBoxInputPair = function (cB, field, message)
  { if( cB.checked )
      { if( field.value=="" )
          { if (message) alert(message);
            field.select();
            return false;
          }
        else return true;
      }
    else
      { if( field.value=="" )
          { return true; }
        else
          { cB.checked=true;
            return true;
      }   }
 }
val.Equal4TextBoxes = function (f1, f2, f3, f4, message)
  { var x=f1.value;
    if( x.length!=0 && (x==f2.value || x==f3.value || x==f4.value))
      { if(message) alert(message);
        f1.select();
        return false;
      }
    x=f2.value;
    if( x.length!=0 && (x==f1.value || x==f3.value || x==f4.value))
      { if (message) alert(message);
        f2.select();
        return false;
      }
    x=f3.value;
    if( x.length!=0 && (x==f1.value || x==f2.value || x==f4.value))
      { if (message) alert(message);
        f3.select();
        return false;
      }
    x=f4.value;
    if( x.length!=0 && (x==f1.value || x==f2.value || x==f3.value))
      { if (message) alert(message);
        f4.select();
        return false;
      }
    return true;
 }

// Do something to each form value of type hidden, text, textarea (user input)
val.TransformForm = function(f,operator)
  { for( var i=0; i<f.elements.length; i++)
       { myType=f.elements[i].type;
         if( myType=='hidden' || myType=='password' || myType=='text' || myType=='textarea' )
             f.elements[i].value=operator(f.elements[i].value);
       }
 }

val.setFormElement = function(formElement, setvalue)
  { if( formElement.type=='checkbox' && setvalue==true || setvalue=='checked')
        formElement.checked=true
    else if( formElement.type=='radio' && formElement.value==setvalue)
             formElement.checked=true
    else if( formElement.type=='select-one')
           { for( var i=0; i<formElement.options.length; i++)
                { if( formElement.options[i].value==setvalue)
                    { formElement.selectedIndex=i
                      break;
           }   }    }
 }

val.trim = function (obj_value){//trim left and right blank
	if(obj_value=="")
		return obj_vlaue=""
	for(var i=0;i<obj_value.length;i++)
		if(obj_value.substring(i,i+1)!=" "){
			for(var i=0;i<obj_value.length;i++)
				if(obj_value.substring(i,i+1)!=" ")
					break
			for(var j=obj_value.length;j>0;j--)
				if(obj_value.substring(j,j-1)!=" ")
					break
			return obj_value=obj_value.substring(i,j);
		}
	return obj_value=""        
}

val.stripSpace = function(obj_value){ //strip the blank
	var str=""
	var sChar
	if (obj_value ==null)
		return null
	for(var i=0;i<obj_value.length;i++){
		sChar=obj_value.substring(i,i+1)
		if(sChar!=" ")
			str+=sChar
	}
	return str;
}


val.compareDate = function (Date1,Date2,msg){
	// date formated as mm/dd/yyyy; date1 is expectd earlier than date2
	// tDate -- to date formated as mm/dd/yyyy
	var aFDate=Date1.split("/")
	var aTDate=Date2.split("/")

	var fY=aFDate[2]
	var fM=aFDate[0]-1
	var fD=aFDate[1]   

	var tY=aTDate[2]
	var tM=aTDate[0]-1
	var tD=aTDate[1]

	var FDate = new Date(fY,fM,fD)   
	var TDate = new Date(tY,tM,tD)

	if(FDate.getTime() > TDate.getTime()){
		alert(msg)
		return false
	}
	return true
}

val.isNumber = function (inputVal) {
   oneDecimal = false
   inputStr = "" + inputVal
   for (var i=0; i<inputStr.length; i++) {
      var oneChar = inputStr.charAt(i)
      if (i==0 && oneChar == "-") {
         continue
      }
      if (oneChar == "." && !oneDecimal) {
         oneDecimal = true
         continue
      }
      if (oneChar < "0" || oneChar > "9") {
         return false
      }  
   }
   return true
}

val.CheckPhone = function (inPhone) {
   var message = "Phone number must be formatted as 999/999-9999";
   var pnum = inPhone.value;

   if (pnum.length != 12) {
      alert(message);
      inPhone.select();
      return false;
   }
   
   if (pnum.indexOf("/") == -1 || pnum.indexOf("/") != 3) {
      alert(message);
      inPhone.select();
      return false;
   }
   
   if (pnum.indexOf("-") == -1 || pnum.indexOf("-") != 7) {
      alert(message);
      inPhone.select();
      return false;
   }
   return true;
}

// Opens a new browser window and loads the URL
// Input: URL - location of the page?Ó³•o”¬“².×î?.??.?o??, optionStr - properties of the new window
val.isSameValue = function (control1, control2, message) {
	if (control1.value != control2.value)
	{
		alert(message);
		control1.select();
		return false;
	}
	return true;
}

// Opens a new browser window and loads the URL
// Input: URL - location of the page, optionStr - properties of the new window
val.popUpWin = function (URL, optionStr) {
	var day = new Date();
	return window.open(URL, "_blank", optionStr);	
}

// Opens a new browser window and loads the URL
// Input: URL - location of the page, optionStr - properties of the new window
val.isLengthEnough = function (control, len, message) {
	if (control.value.length < len)
	{
		window.alert(message);
	    control.select();
        return false;
	}
	return true;
}

// Opens a new browser window and loads the URL
// Input: URL - location of the page, optionStr - properties of the new window
val.isEmptyWithoutAlert = function (control)
{
	return (control.value.length == 0);
 }
 
function confirmBox(message,filename)
{
	if (confirm(message))
	{
		window.location.href = filename;
		return true;
	}
}

String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}


