/**
 * wcSubmit function for regular viewing of form (non ajax)
 */
function wcSubmit(f){
	f.submit();
}

/* 
function 1: checkvalid_blank (String Fieldname , String Description);
	checks to see if fieldname contains a value or not

function 2: checkvalid_zip (String Fieldname , String Description);
	checks to see if value in fieldname is 5 digits or not

function 3: checkvalid_ssn (String Fieldname , String Description);	
	checks to see if value in fieldname is 9 digits or not

function 4: checkvalid_password_match (String Fieldname1 , String Fieldname2 , String Description);	
	checks to see if two password fields have the same value
	returns true if yes or inserts description in fieldlist if false

function 5: checkvalid_ndigits (String Fieldname , Mixed NumberOfDigits , String Description);
	For checking a specified number of digits:
	return true if the NumberOfDigits is assigned to a integer value and the field contains the specified number of digits.
	
	For checking at least one digit (or digits only):
	return true if the NumberOfDigits is assigned to "any" and the field contains digits only.

function 6: checkvalid_email (String Fieldname , String Description);
	checks to see if value in fieldname is a valid email address or not
	
function 7: checkradio_value (String Fieldname , String SearchValue);	
	checks to see if the radio button value was selected or not (returns true or false)

function 8: checkselect_value (String Fieldname , String SearchValue);	
	checks to see if the select box value was selected or not (returns true or false)	
	
function 9: checkbox_selected (String Fieldname);	
	checks to see if the specified checkbox was checked or not (returns true or false)
	
function 10: checkvalid_digits_greaterthan (String Fieldname , Int Number , String Description);	
	checks to see if the fieldname value is a digit greater than Number passed in
	
function 11: checkvalid_digits_lessthan (String Fieldname , Int Number , String Description);	
	checks to see if the fieldname value is a digit less than Number passed in	
			
function 12: checkvalid_digits_between (String Fieldname , Int Number1 , Int Number2 , String Description);	
	checks to see if the fieldname value is a digit between two specified Numbers passed in	(inclusive)	
	
function 13: checkradio_blank (String Fieldname , String Description); 
//checks for blank radio button arrays and sets the focus on the first element of the blank array.	
	
*/


function setAvailableMethods()
{
        this.valid = true;
        this.fieldList = "";
        this.numericlist= "";
        this.focusField = null;

		this.checkvalid_blank = checkvalid_blank;
		this.checkvalid_email = checkvalid_email;
		this.checkvalid_zip = checkvalid_zip;
		this.checkvalid_ssn = checkvalid_ssn;
		this.checkvalid_password_match = checkvalid_password_match;
		this.checkvalid_3digits = checkvalid_3digits;
		this.checkvalid_4digits = checkvalid_4digits;
		this.checkvalid_digitsonly = checkvalid_digitsonly;
		this.checkvalid_digits_greaterthan = checkvalid_digits_greaterthan;
		this.checkvalid_digits_lessthan = checkvalid_digits_lessthan;
		this.checkvalid_digits_between = checkvalid_digits_between;
		this.checkradio_blank = checkradio_blank;
		this.checkradio_value = checkradio_value;
		this.checkselect_value = checkselect_value;
		this.checkbox_selected = checkbox_selected;
}

function checkvalid_blank(fieldname , description)
	{
		if(f.elements[fieldname].value=="") 
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}	

	
function checkvalid_zip(fieldname , description)
	{
		if(!(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="")
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}		

function checkvalid_ssn(fieldname , description)
	{
		if(!(/(^\d{9}$)|(^\d{3}-\d{2}-\d{4}$)/.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="") 
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}	


function checkvalid_password_match(fieldname1 , fieldname2 , description)
	{
		if((f.elements[fieldname1].value) != (f.elements[fieldname2].value)) 
		{
		if (valid) focusField = f.elements[fieldname1];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname1 + "does not match" + fieldname2 + "\n";
			}
		}
	}		
	
	
function checkvalid_3digits(fieldname , description)
	{
	checkvalid_ndigits(fieldname , "3" , description)
	}		

function checkvalid_4digits(fieldname , description)
	{
	checkvalid_ndigits(fieldname , "4" , description)
	}
	
function checkvalid_digitsonly(fieldname , description)
	{
	checkvalid_ndigits(fieldname , "any" , description)
	}		

	
function checkvalid_ndigits(fieldname , num_digits , description)
	{
		if (isNaN(num_digits))
			{
			pattern = eval("/^\\d+$/");
			}
		else
			{
			pattern = eval("/^\\d{"+num_digits+"}$/");
			}
		if(!(pattern.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="")
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}

function checkvalid_email(fieldname , description)
	{
		if(!validemail(f.elements[fieldname].value)) 
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}		
	
	

	
function checkradio_blank(fieldname, description)
 {
 for (i = 0; i < f.elements[fieldname].length; i++)
  {
  var arraychecked=0;
  if (f.elements[fieldname][i].checked)
   {
    arraychecked++;
    break;
   }
  }
 if (arraychecked==0)
  {
   
   if (valid) focusField = f.elements[fieldname][0];
   this.valid = false;
   if (description)
   {
    this.fieldList += description + "\n";
   }
   else
   {
    this.fieldList += fieldname + "\n";
   }
   return false;
  }   
  return true;
 }  
	
	
function checkradio_value(fieldname , checkvalue)
	{
	for (i = 0; i < f.elements[fieldname].length; i++)
		{
		if (f.elements[fieldname][i].checked)
			{
				if(f.elements[fieldname][i].value == checkvalue) 
				{
				return (true);
				}
			}
		} 
	return (false);	
	}		
		
		
function checkselect_value(fieldname , checkvalue)
	{
	for (i = 0; i < f.elements[fieldname].length; i++)
		{
		if (f.elements[fieldname][i].selected)
			{
				if(f.elements[fieldname].options[i].value == checkvalue) 
				{
				return (true);
				}
			}
		} 
	return (false);	
	}		
	
function checkbox_selected(fieldname,description )
	{
	if (f.elements[fieldname].checked)
		{
			return (true);
		}
	else
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;		
		if (description)
			{
			this.fieldList += description + "\n";
			}	
		return (false);		
		}
	
	return (false);		
	}		

var fpu = eval("/^(\\-)?(\\d)+(\\.)?(\\d)*$/");
function checkvalid_digits_greaterthan(fieldname , numvalue , description)
	{
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) <= numvalue || f.elements[fieldname].value == "") 
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}

function checkvalid_digits_lessthan(fieldname , numvalue , description)
	{
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) >= numvalue || f.elements[fieldname].value == "") 
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}		

function checkvalid_digits_between(fieldname , numvalue1 , numvalue2 , description)
	{
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) < numvalue1 || parseFloat(f.elements[fieldname].value) > numvalue2 || f.elements[fieldname].value == "") 
		{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		if (description)
			{
			this.fieldList += description + "\n";
			}
		else
			{
			this.fieldList += fieldname + "\n";
			}
		}
	}		

function isnumeric(n)
	{
    nums = /[\\0-9]/;
    if(nums.test(n))
       return true;
    }


    function validemail(c) {
	invalidChars = " /:,;"
	if (c == "") {
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (c.indexOf(badChar,0) != -1) {
			return false
		}
	}
	atPos = c.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (c.indexOf("@",atPos+1) != -1) {
		return false
	}
	periodPos = c.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > c.length)	{
		return false
	}
	if (atPos+1 == periodPos) {
		return false
	}		
	return true
	}	
	
	
// Trim leading spaces from a string
String.prototype.ltrim = function () {
  var s=this;
  while (s.charAt(0)==" ") s=s.substring(1,s.length);
  return s;
}

// Trim trailing spaces from a string
String.prototype.rtrim = function () {
  var s=this;
  while (s.charAt(s.length-1)==" ") s=s.substring(0, s.length-1);
  return s;
}

// Return the rightmost n characters of a string
String.prototype.left = function (n) {
  if (n < this.length) {
    return this.substring(0, n);
  } else
    return this;
}

// Return the rightmost n characters of a string
String.prototype.right = function (n) {
  if (n < this.length) {
    return this.substring(this.length-n, this.length);
  } else
    return this;
}

// Test a string to see if it's entirely digits
String.prototype.isNumeric = function() {
  for (var i=0; i<this.length; ++i)
    if ("0123456789".indexOf(this.charAt(i)) == -1) return false;
  return true;
}


function formatMoney(fld, show_error) {
  var amt=fld.value.ltrim().rtrim();
  var moneyChars="0123456789";
  var cents="00";
  var formatAmt="";
  var errorDesc="";

// If there is a dollar sign, remove it.
  if (amt.charAt(0) == "$") amt = amt.substring(1,amt.length).ltrim();

// Check for a decimal, and set a flag if there is one in the right place.
  if (amt.indexOf(".") != -1 ) {
    if (amt.indexOf(".") == amt.lastIndexOf(".") && amt.indexOf(".") >= amt.length-3) {
      if (amt.indexOf(".") < amt.length-1) cents = amt.substring(amt.indexOf(".")+1, amt.length);
      if (cents.length==1) cents=cents+"0";
      amt = amt.substring(0,amt.indexOf("."));
    } else
      errorDesc = "Decimal placement error";
  }

// Remove any commas.
  if (errorDesc.length == 0) {
    while (amt.indexOf(",") != -1) {
      amt = amt.substring(0,amt.indexOf(","))+amt.substring(amt.indexOf(",")+1, amt.length);
    }
  }

// Now, check for any invalid characters.  There should not be any characters other than digits.  
  for (var i=0; i<amt.length && errorDesc.length==0; ++i) {
    if (moneyChars.indexOf(amt.charAt(i)) == -1) {
      errorDesc = "Dollar amount contains at least one illegal character.";
    }
  }

  if (errorDesc.length==0) {
// The dollar amount now contains only digits, so we can format them.
    if (amt.length == 0) amt="0";
    formatAmt = "."+cents;
    while (amt.length > 3) {
      formatAmt = ","+amt.substring(amt.length-3,amt.length)+formatAmt;
      amt=amt.substring(0,amt.length-3);
    }
    formatAmt = "$"+amt+formatAmt;
//    formatAmt = amt+""+formatAmt;
  } else {
    if (show_error) {
      alert(errorDesc);
      fld.focus();
    } else {
      return false;
    }
  }
  fld.value = formatAmt;
  return true;
}

function checkDate (dateVal) {
   if (/^(\d{2})([\/\.-]?)(\d{2})(\2)(\d{2}|\d{4})$/.test(dateVal) ||
      /^(\d{1,2})([\/\.-])(\d{1,2})(\2)(\d{2}|\d{4})$/.test(dateVal)) {
      this.month   = RegExp.$1 * 1;
      this.date    = RegExp.$3 * 1;
      this.year    = RegExp.$5 * 1;
      this.isValid = true;
   } else {
      this.isValid = false;
   } // end if-else
   return (this.isValid);
} // end fun checkDate

function parseDateOb2Str(dateOb)
//# returns date as a string
{
	var sMonth = String(dateOb.getMonth() + 1);
	var sDate = String(dateOb.getDate());
	var sYear = String(dateOb.getFullYear());
	
	if(sMonth.length == 1) sMonth = '0' + sMonth;
	if(sDate.length == 1) sDate = '0' + sDate;
	
	var newDate = sMonth + '/' + sDate + '/' + sYear;
        return   String(newDate);
}

// Validate and format date. Begin and end dates must be javascript date objects
// e.g. <input type="text" name="date" onchange="valDate(this, 'Date of Foo')">
function valDate(f, desc) {
var dateFlag, errorDesc = '';
   if (2 > arguments.length || arguments.length > 6) return;        
   dateFlag = new checkDate(f.value);
   if (dateFlag.isValid){
      var M = dateFlag.month;
      var D = dateFlag.date;
      var Y = dateFlag.year;
      if (Y < 100)
         Y = (Y <= 50) ? Y + 2000 : Y + 1900;
      if (M < 1 || M > 12) {
         errorDesc = " Invalid month.";
      } else {
         var ld = (Y % 400 == 0 || Y % 100 != 0 && Y % 4 == 0) ? "9" : "8";
         var maxdays = "312" + ld + "31303130313130313031";
         if (D < 1 || D > maxdays.substr ((M - 1) * 2, 2))
            errorDesc = " Invalid date.";
      } // end if-else
      var nDate = new Date(Y,(parseInt(M)-1),D);
      if (errorDesc.length == 0) {
         // Date is valid and in range; format it.
         f.value =  parseDateOb2Str(nDate);       
      } // end if
   } else {
      errorDesc = "You have entered an invalid date. Please re-enter the date in an accepted format, e.g. 01/01/2000 for January 1, 2000.";
   } // end if-else

             
   if (errorDesc.length > 0) {
      f.focus();
      f.select();
      alert(desc + ": " + errorDesc);
      return (false);
   } // end if
	return (true);
} // end fun valDate
  
  

// Validate and format phone number.
// e.g. <input type="text" name="phoneNum" onchange="valPhone(this, 'Foo Phone');">
function valPhone (f, desc) {
   if (/^\((\d{3})\)\s?(\d{3})[\s|\.|-](\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d{0,5}))?$/.test(f.value) ||
      /^(\d{3})[\s|\/|\.|-]?(\d{3})[\s|\.|-]?(\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d{0,5}))?$/.test(f.value)) {
      if (RegExp.$7) f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3+" ext."+RegExp.$7;
      else f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3;
   } else {
      f.focus ();
      f.select ();
	
	  var x10tionMesg;
	  if (desc == "Fax Number") {
		  x10tionMesg = "";
	  } else {
		  x10tionMesg = "\n If you wish to enter an extension (up to five digits), place it at the end of the main telephone number, and precede it with an 'x'.";
	  }	
	
      alert (desc + ": " + " You have entered an invalid number.  The number must be 10 digits in length and contain the 3 digit area code.  Please re-enter, formatting will be completed automatically.\n " + x10tionMesg);       
      return (false);
   } // end if-else
   return (true);
} // end fun valPhone		

