
function getElement(i)
{
	return document.getElementById(i) || document.getElementsByName(i).item(0);
}
	
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function UpdateStatus(TheDIV, result)
{
	try {
    if (result == 1) {TheDIV.style.display = "none";}
    else { TheDIV.style.display = "block";}
 	}
 	catch(E) {
  		alert(TheDIV.className);
 	}
  	
}

function ValidateTextArea(c, TheDIV, MinLength, MaxLength) {
	v = c.value;
	if (v == "") {
		if (MinLength < 1) {
			result = 1;
		} 
		else 
		{
			result = 0;
		}
	} 
	else 
	{
	  if (v.length >= MinLength) 
	  {
	  	if (MaxLength < 0) 
	  	{
	  		result = 1;
	  	} 
	  	else
	  	{
	  		if (v.length <= MaxLength) {
	  			result =1;
	  		} else {
	  			result = 0;
	  		}
	  	}
	  } 
	  else 
	  { 
	  	result = 0;
	  }
  }
  UpdateStatus(TheDIV, result);
  return result;	
}

function DisplayTextCounts(c, MinLength, MaxLength, TheDIV) {
	v = c.value;
	if (v=="") {
		s = "0";
	} else {
		s = v.length;
	}
	
	i = 0;
	if (MinLength >= 1) { i = 1 } 
	if (MaxLength >= 1) { i = i + 2 } 
	
	switch (i) {
		case 0:
		case 1:
		  s = "(" + s + " characters)";
		  break;
		case 2:
		case 3:
		  s = "(" + s + "/" + MaxLength + " characters)";
	}
	TheDIV.innerHTML = s;
}
	
function ValidateCheckbox(c, TheDIV, ValidOn) {
	result = 0;
	if (((c.checked==true) && (ValidOn==1)) || ((c.checked==false) && (ValidOn==0))) {
		result = 1;
  }
  else {
    result = 0;
  }
  UpdateStatus(TheDIV, result);
  return result;
}

//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z]{2,4})$/;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      return false;
    } 
    else
    {
    return true; 
    }
}

function ValidateValue(c, TheDIV) {
	
	result = 0;
  if (isEmptyField(c) == true) { result = 0; }
  else { result = 1; }
  	
  UpdateStatus(TheDIV, result);
  return result;
}

function ValidatePassword(c, TheDIV, CanBeEmpty)
{
	result = 1;
  if (isEmptyField(c) == true) {
  	if (CanBeEmpty==true) { result = 1;}
		else { result = 0; }
  }

  UpdateStatus(TheDIV, result);
  return result;	
}

function CountChars(ss, s) {
	r = 0;
	for (x=0; x <s.length; x++){
		if (ss.indexOf(s.charAt(x)) >= 0) {
			r = r + 1;
	  }
	}
	return r;
}

function CanMustCant(i, c){
  if (i == 0) { return 1; }
  if ((i == 1) && (c > 0)) { return 1; }
  if ((i == 2) && (c == 0)) { return 1; }
  return 0;
}
function ValidatePasswordCreate(c, c2, TheDIV, TheDIV2, CanBeEmpty, MinL, MaxL, LC, UC, Num, Sym) {
	result = 1;
  if (isEmptyField(c) == true) {
  	if (CanBeEmpty==true) { result = 1;}
		else { result = 0; }
  }
  else
  {
  	s = c.value;
  	L = s.length;
  	if ((MinL == -1) && (MaxL == -1)) { result = 1; }
  	if ((MinL == -1) && (MaxL != -1)) { 
  		if (L > MaxL) { result = 0; }
  		}
  	if ((MinL != -1) && (MaxL == -1)) { 
  		if (L < MinL) { result = 0; }
  		 }
  	if ((MinL != -1) && (MaxL != -1)) { 
  		if ((L < MinL) || (L > MaxL)) { result = 0; }
  	}
  	
  	
  	NumL = CountChars('abcdefghijklmnopqrstuvwxyz', s);
  	NumU = CountChars('ABCDEFGHIJKLMNOPQRSTUVWXYZ', s);
  	NumN = CountChars('0123456789', s);
  	NumS = L - NumL - NumU - NumN;

    if (CanMustCant(LC, NumL) == 0) { result = 0; }
    if (CanMustCant(UC, NumU) == 0) { result = 0; }
    if (CanMustCant(Num, NumN) == 0) { result = 0; }
    if (CanMustCant(Sym, NumS) == 0) { result = 0; }
  }
  
  UpdateStatus(TheDIV, result);
  if (c.value != c2.value) { result = 0 };
  UpdateStatus(TheDIV2, result);
  
  return result;	    
}


function ValidatePlainText(c, TheDIV, CanBeEmpty, MinLength, MaxLength)
{
	result = 1;
  if (isEmptyField(c) == true) {
  	if (CanBeEmpty==true) { result = 1;}
		else { result = 0; }
  }
  else
  {	
	  s = trim(c.value);
	  l = s.length;
	  if (MinLength > l) {
	  	result = 0;
	  }
	  else 
	  {
	    if (MaxLength > 0) {
		    if (l > MaxLength) {
			    result = 0;
		    }
	    }
	  }
  }
  UpdateStatus(TheDIV, result);
  return result;	
}

function ValidateTimeField(c, TheDIV) {
	v = c.value;
	if (v != "") {
		result = 1;
	} else {
		result = 0;
	}
  UpdateStatus(TheDIV, result);
  return result;	
}

function StipNonAlphaNumeric(s) {
  v = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var result = ""
  for (x=0; x < s.length; x++) {
  	if (v.indexOf(s.charAt(x))>=0) { result = result + s.charAt(x); }
  }
  return result;
}

function ConvertToPostal(usePostal, s) {
	if (usePostal==false) { return "" }
	if (s.length!=6) { return "" }
	s1 = s.charAt(0) + s.charAt(2) + s.charAt(4);
	s2 = s.charAt(1) + s.charAt(3) + s.charAt(5);
  c1 = CountChars('ABCDEFGHIJKLMNOPQRSTUVWXYZ', s1);
  c2 = CountChars('0123456789', s2);
  if ( (c1 != 3) || (c2 != 3) ) { return ""; }
  
  return  s.substr(0, 3) + " " + s.substr(3,3);
}

function ConvertToZIP5(useZIP5, s) {
  return ConvertToZIP(5, useZIP5, s);
}

function ConvertToZIP9(useZIP5, s) {
  return ConvertToZIP(9, useZIP5, s);
}

function ConvertToZIP(sz, useZIP, s) {
  if (useZIP==false) { return "" }
  if (s.length != sz) { return "" }
  c = CountChars('0123456789', s);
  if (c!=sz) { return "" }
  if (c == 9) {
    return s.substr(0, 5) + "-" + s.substr(4,4);
  } else {
 	  return s;
  }
}
function ValidatePostalZIP(c, TheDIV, CanBeEmpty, usePostal, useZIP5, useZIP9) {
	v = c.value;
	v = v.toUpperCase();
  v = StipNonAlphaNumeric(v);

	result = 0;
	if (isEmptyField(c) == true) {
		if (CanBeEmpty==1) {result = 1;}
	}
	else
	{
		if (v != "") { 
			r = ConvertToPostal(usePostal, v);
		  if (r != "") { 
		  	result = 1;
		  	c.value = r;
		  }
		  else {
  			r = ConvertToZIP5(useZIP5, v);
	  	  if (r != "") { 
		    	result = 1;
		    	c.value = r;
		    }
		    else {
    			r = ConvertToZIP9(useZIP9, v);
		      if (r != "") { 
		  	    result = 1;
		  	    c.value = r;
		     }
		    }
		  }
		}
  }		

  UpdateStatus(TheDIV, result);
  return result;
}

function ValidateEmailConfirm(c, c1, TheDIV, TheDIV1, CanBeEmpty)
{
	r  = ValidateEmail(c,  TheDIV,  CanBeEmpty, false);
	r1 = ValidateEmail(c1, TheDIV1, CanBeEmpty, false);
	
	ee  = c.value;
	ee1 = c1.value;
	if (ee != "") {
	  ee = ee.toUpperCase();
	}
	if (ee1 != "") {
	  ee1 = ee1.toUpperCase();
	}
	
	i = 0;
	if (r  == true) { i = i + 1 }
  if (r1 == true) { i = i + 2 }
  if (ee == ee1)  { i = i + 4 }
  
  result = 0;
  switch (i) {
  	case 0:  // neither are valid
  	  UpdateStatus(TheDIV , 0);
  	  UpdateStatus(TheDIV1, 0);
  	  break;
  	case 1:  // c is valid
  	  UpdateStatus(TheDIV , 1);
  	  UpdateStatus(TheDIV1, 0);
  	  break;
  	case 2:  // c1 is valid
  	  UpdateStatus(TheDIV , 0);
  	  UpdateStatus(TheDIV1, 1);
  	  break;
  	case 3:  // c and c1 are valid, but they are not equal
  	  UpdateStatus(TheDIV , 1);
  	  UpdateStatus(TheDIV1, 0);
  	  break;
  	case 4:  // they are the same, but neither are valid
  	  UpdateStatus(TheDIV , 0);
  	  UpdateStatus(TheDIV1, 0);
  	  break;
  	case 5:  // not likely to get here...they are the same, but c1 is not a valid email
  	  UpdateStatus(TheDIV , 0);
  	  UpdateStatus(TheDIV1, 1);
  	  break;
  	case 6:  // not likely to get here...they are the same, but c1 is not a valid email
  	  UpdateStatus(TheDIV , 1);
  	  UpdateStatus(TheDIV1, 0);
  	  break;
  	case 7:  // perfect
  	  UpdateStatus(TheDIV , 1);
  	  UpdateStatus(TheDIV1, 1);
  	  result = 1;
  	  break;
  	}

  return result;	
}
function ValidateEmail(c, TheDIV, CanBeEmpty, DoUpdateStatus)
{
	DoUpdateStatus = DoUpdateStatus || true;
	
	result = 0;
	if (isEmptyField(c) == true) {
		if (CanBeEmpty==1) {result = 1;}
		else { result =0;} 
	}
	else
	{
		if (isValidEmail(c.value)) { result = 1;}
		else { result =0;} 
  }		
  if (DoUpdateStatus==true) {
    UpdateStatus(TheDIV, result);
  }
  return result;
}

function isEmptyField(c) {
	s = trim(c.value);
	return (s == '');
}

function isValidURL(s) {
	var re = /(^(((https?)|(ftp)):\/\/)([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i
  return re.test(s);
}


function ValidateURL(c, TheDIV, CanBeEmpty) {
  v = c.value;
	result = 0;
	if (isEmptyField(c) == true) {
		if (CanBeEmpty==1) {result = 1;}
	}
	else	{
		if (isValidURL(c.value)==true) { result = 1; }
		else {
			if (isValidURL("http://" + c.value)==true) { result = 1; }
		}
  }		
  UpdateStatus(TheDIV, result);
  return result;	
}


function getDateFromBoxes(cYear, cMonth, cDay)
{
  yv = cYear.value;
  mv = cMonth.value;
  dv = cDay.value;

  if (yv == "") { return "" }
  if (mv == "") { return "" }
  if (dv == "") { return "" }
 
  if (yv == "Year") { return "" }
  if (mv == "Month") { return "" }
  if (dv == "Day") { return "" }
 
  var td = new Date();
  td.setFullYear(parseInt(yv), parseInt(mv-1), parseInt(dv));
  return td;
	
}

//---------------------------------------------
// 
function VBtoJSDate(v) {
	vbzero = new Date("December 30, 1899 00:00:00");
	ns = v * 1000 * 60 * 60 * 24; // 1000 msec in Sec, in a min, in an hour, in a day
	return (Number(vbzero)  + ns);
}

//---------------------------------------------
//
function JStoVBDate(v) {
	vbzero = new Date("December 30, 1899 00:00:00");
	ns = Number( (Number(v) - Number(vbzero))  / (24000*3600 )); 
	return  Number(ns);
}
//---------------------------------------------

function ValidateDateField(cYear, cMonth, cDay, TheDIV, CanBeEmpty, hasMin, minDate, hasMax, maxDate, minNow, maxNow) {
 
  if (hasMin == true) {
  	if (minNow == true) {
	  		mim = new Date();
  	} 
  	else {
      mim = new Date(VBtoJSDate(minDate));
  	}
  }
  else {
  	mim = new Date();
  	mim.setMonth(mim.getMonth() - 120);
  }
  
  if (hasMax == true) {
  	if (maxNow == true) {
  		mam = Date();
  	} else {
      mam = VBtoJSDate(maxDate);
  	}
  }
  else {  // Add 10 Years to the CurrentDate
  	mam = new Date(); 
  	mam.setMonth(mam.getMonth() + 120);
  }
  
  dt = getDateFromBoxes(cYear, cMonth, cDay);

  result = -1;
  if (dt == "") {
  	if (CanBeEmpty == true) {
  		result = 1;
 		}
  	if (result == -1) { 
  		result = 0;
    }
  }
  else {
    if ((dt < mim) || (dt > mam)) {
   	  result = 0;
   	} else {
   		result = 1;
   	}
  }
  UpdateStatus(TheDIV, result);
  return result;
}

function ValidateDropDown(c, TheDIV, NoValue) {
	result = 0;
	if (c.value==NoValue) {result = 0; 	}
	else { result = 1;}
  UpdateStatus(TheDIV, result);
  return result;
}

function NoValidationRequired(TheDIV) {
	result = 1;
  UpdateStatus(TheDIV, result);
  return result;
}

function ValidateCheckboxArray(c, TheDIV, MinChecked, MaxChecked) {
var rv = "";
var num =0;
	for (var i=0; i < c.length; i++)
  {
    if (c[i].checked)
      {
      num++;
      }
  }
  result = 1;
  
  if (num < MinChecked) {
  	result = 0;
  }
  if (num > MaxChecked) {
  	if (MaxChecked !=0) {
  	  result = 0;
    }
  }
  UpdateStatus(TheDIV, result);
  return result;  	 
}

function ValidateProvState(c, TheDIV, CanBeEmpty, useCanada, useUSA, useOther) {
  v = c.value;
  
//  alert(c.name + '-->' + c.value);

  if ((v=="") || (v=="nil")) {
  	if (CanBeEmpty==true) {
  		result = 1;
  	}	else {
  		result =  0;
  	}
  } else {
  	result =  1;
  }
  UpdateStatus(TheDIV, result);
  return result;  
}

function ValidateCountries(c, TheDIV, CanBeEmpty) {
  v = c.value;
  
  if ((v=="") || (v=="nil")) {
  	if (CanBeEmpty==true) {
  		result = 1;
  	}	else {
  		result =  0;
  	}
  } else {
  	result =  1;
  }
  UpdateStatus(TheDIV, result);
  return result;  
}
function ValidateCombobox(c, TheDIV, hasOneValidValue, ValidValue , hasOneInvalidValue, InvalidValue) {
var rv = ""
if (c.name == "") { doit = true } else {doit=false}
	for (var i=0; i < c.length; i++)
  {
    if (c[i].selected==true)
      {
      rv = c[i].value;
      }
  }
  
if (doit == true) {  alert(c.name + ": " + hasOneValidValue +", " +ValidValue +", " +hasOneInvalidValue +", " + InvalidValue +", " +rv );}
  if (rv == "") {
  	result = 0;
if (doit == true) {    	alert("1");}
  }
  else
  {
  	if (rv==ValidValue) {
  		result = 1;
if (doit == true) {  alert("2");}
  	}
  	else
  	{
  		if (rv==InvalidValue) {
  			result = 0;
if (doit == true) {  alert("3");}
  		}
  		else 
  		{
  			if (hasOneValidValue==true) {
  				result = 0;
if (doit == true) {  alert("4");}
  			}
  			else
  			{
  			  result = 1;
if (doit == true) {  alert("5");}
  		  }
  		}
  	}
  }
  UpdateStatus(TheDIV, result);
  return result;
  
}


function ValidateRadioButtons(c, TheDIV, hasOneValidValue, ValidValue , hasOneInvalidValue, InvalidValue) {
var rv = ""
	for (var i=0; i < c.length; i++)
  {
    if (c[i].checked)
      {
      rv = c[i].value;
      }
  }
  
  if (rv == "") {
  	result = 0;
  }
  else
  {
  	if (rv==ValidValue) {
  		result = 1;
  	}
  	else
  	{
  		if (rv==InvalidValue) {
  			result = 0;
  		}
  		else 
  		{
  			if (hasOneValidValue==true) {
  				result = 0;
  			}
  			else
  			{
  			  result = 1;
  		  }
  		}
  	}
  }
  UpdateStatus(TheDIV, result);
  return result;
}
      
function ValidateCurrency(c, TheDIV, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, DecPts, ShowDollarSign) {
  // Remove $, if present
  s = trim(c.value);
  orig = s;
  s = s.replace("$", "");
  s = ConvertToNumber(s, DecPts);
  if (s=="") {
    if (CanBeEmpty==true) {
  		result = 1;
 		  s = "";
 	  }
 	  else
  	{
 	  	result = 0;
 		  s = "";
 	  }
 	}
  else
  {
  	if (s == "-") {
  		result = 0;
  		s = orig;
  	}
  	else {
  		if (((s<MinVal) && (hasMinVal==true))||((s>MaxVal) && (hasMaxVal==true))) {	result = 0;	}
  		else {result = 1; }
  		if (ShowDollarSign==true) {
  			s = "$ " + s;
  		}
  	}
  }

  if (result == 1) {  c.value = s; }
  UpdateStatus(TheDIV, result);
  return result;
}

function stringReverse(s) {
	outp =""
	for (i=0; i <=s.length;i++) {
		outp = s.charAt(i) + outp;
	}
	return outp;
}

function ReducePhoneNumber(num) {
	ph = num.toUpperCase();
	var r = ph.replace(/[\W]/g,'');
	if (r.charAt(0) == "1") { r = r.substr(1, r.length); }
  return r;
}

function JustNums(num) {
	ph = num.toUpperCase();
	var r = ph.replace(/[^\d]/g,'');
	if ((r.charAt(0) == "1") || (r.charAt(1)=="0")) { r = r.substr(1, r.length); }
  return r;
}
function ReturnExt(r) {
	if (r.length>=11) {
		if (r.charAt(10) == "X") {
			return r.substr(11, r.length);
		}
    if (r.charAt(7) == "X") {
 	  	return r.substr(8, r.length)
 	  } 
 	  if (r.length == 10) {
 	  	return "";
 	  }
    return r.substr(10, r.length);
  }
   
  if (r.length>=8) {
    if (r.charAt(7) == "X") {
	  	return r.substr(8, r.length)
	  } 
 	  if (r.length == 10) {
 	  	return "";
 	  }
	  return r.substr(7, r.length);
 	}
  
  return "";
}

function ReturnPhone(r) {
	if (r.length>=11) {
		if (r.charAt(10) == "X") {
		  return r.substr(0,10);
		}
		if (r.charAt(7) == "X") {
		  return r.substr(0, 7); 
		}
		if (r.length>=10) {
		  return r.substr(0, 10); 
		}
	}
  
  if (r.length>=8) {
    if (r.charAt(7) == "X") {
      return r.substr(0,7);
    }
		if (r.length>=10) {
		  return r.substr(0, 10); 
		}
    return r.substr(0,7);
  } 
  return r;
}

function PhoneReplaceChars(num) {
	var s = num;
	if (s == "") { return ""}
	
	var t = "";

  for (var i=0; i<s.length; i++) {
  	q = s.charAt(i);
	  switch (q) {
	  	case "A": case "B": case "C":
	  	  t = t + "2";
	  	  break;
	  	case "D":	case "E":	case "F":
	  	  t = t +  "3";
	  	  break;
	  	case "G":	case "H":	case "I":
	  	  t = t + "4";
	  	  break;
	  	case "J":	case "K":	case "L":
	  	  t = t + "5";
	  	  break;
	  	case "M":	case "N":	case "O":
	  	  t = t + "6";
	  	  break;
	  	case "P": case "Q": case "R": case "S":
	  	  t = t + "7";
	  	  break;
	  	case "T": case "U": case "V":
	  	  t = t + "8";
	  	  break;
	  	case "W": case "X": case  "Y": case  "Z":
	  	  t = t + "9";
	  	  break;
	  	default: t = t + s.charAt(i);
	  	 
	  }
	
  }
  return t;
	
}

function GetPhoneNumber(s, AreaCodeReqd, CanHaveExt,CanHaveChars, ConvertChars) {
	result = "";
  r = ReducePhoneNumber(s);
  pp = ReturnPhone(r);
  if (AreaCodeReqd == true) {
  	if (pp.length < 10) {
  		return "";
  	}
  }

  ee = ReturnExt(r);

  if (CanHaveExt==false) { ee = "" }
    
  if ((ConvertChars == false) && (CanHaveChars==false)) {
    i = JustNums(pp);     if (i != pp) { return "";	 }
    i = JustNums(ee);     if (i != ee) { return "";	 }
  }

  if (ConvertChars==true) {
	  pp = PhoneReplaceChars(pp);
	  ee = PhoneReplaceChars(ee);
  }
  
  if ((pp.length == 7) || (pp.length == 10)) {
  	valid = true;
  } else {
  	valid = false;
  }
  
  if (valid == true) {
  	if (pp.length==7) {
  		pp = pp.substr(0,3) + "-" + pp.substr(3,4);
  	}
  	else {
  		pp = "(" + pp.substr(0,3) + ") " + pp.substr(3,3) + "-" + pp.substr(6,4);
    }
    if (ee != "") {
    	ee = " x" + ee;
    }
    return pp + ee;
  }		
  return "";  
}  
function ValidatePhone(c, TheDIV, CanBeEmpty, AreaCodeReqd, CanHaveExt, ConvertChars, CanHaveChars) {
/*
  This is how its done:
    - pull off non-extension chars/digits
      - look for last occurance of ExtMarker, or "x"...if none, assume all are part of the phone number and ext.
*/	

  var ext = "";
  var phone = "";
  
  var s = trim(c.value);
  if (isEmptyField(c)==true) {
  	if (CanBeEmpty == true) {

  	  result = 1;
    }
    else
    { result = 0;
    }
  } 
  else {
    var ph = GetPhoneNumber(s, AreaCodeReqd, CanHaveExt,CanHaveChars, ConvertChars);
    if (ph == "") {
    	result = 0;
    }
    else
    {
    	result = 1;
    	c.value = ph;
    }
  }
  UpdateStatus(TheDIV, result);
	return result;  
}

function ValidateCreditCardExpiry(ExpMonth, ExpYear, TheDIV, CanBePast,CanBeEmpty) {
	mo = ExpMonth.value;
	yr = ExpYear.value;
	result = false;
	if ((mo=="") && (yr=="")) {
		if (CanBeEmpty==true) {
			result = 1;
		} else {
			result = 0;
		}
	}else {
		if ((mo=="") || (yr=="") ){
			result= 0;
		}
		else
		{
			var Today = new Date();
	    tm = Today.getMonth();
	    ty = Today.getFullYear();
	    if ((ty > yr) || ((ty == yr) && (mo < tm))) {
	    	if (CanBePast==true){
	    		result = 1;
	    	} else {
	    		result = 0;
	    	}
	    }
	    else {
	    	result = 1;
	    }
	  }
	}
	UpdateStatus(TheDIV, result);
	return result;
}
	
function ValidateCreditCardNumber(CCType, CCNum, CanBeEmpty) {
  var mask="2121212121212121";
  var f="";
  var l="";
  var iIndex=0;
  var validchars1="0123456789 -";
  var validchars2="0123456789";
  var result=0;
  var valid=true;
  
  // Make a Copy
  var num = CCNum.value;

  s = trim(CCNum.value);
  if (s=="") {
  	if (CanBeEmpty==true) {
  		valid = true;
  	}
  	else {
  		valid = false;
  	}
  }
  else
  {
    var JustNums = "";
    // strip out the spaces and dashes
    for (var i=0; i < CCNum.value.length; i++) 
    {
      var letter = CCNum.value.charAt(i).toLowerCase();
      if (validchars2.indexOf(letter) != -1) JustNums = JustNums + letter;
    }
    num = JustNums;
    
    switch(CCType.value)
    { case "VISA": // Visa
         	if (num.charAt(0)!='4') valid=false;
         	break;
      case "MasterCard": // Mastercard
         	if (num.charAt(0)!='5') valid=false;
         	break;
      case "AmericanExpress": // American Express
         	if (num.charAt(0)!='3') valid=false;
         	if (num.charAt(1)!='7') valid=false;
         	break;
      default:
          valid=false;
          break;
    }
    switch(num.length)
    {
      case 13: 
         	num = "000" + num;
         	break;
      case 14: 
         	num = "00" + num;
         	break;
      case 15: 
         	num = "0" + num;
         	break;
      case 16: 
         	break;
      default: 
          valid=false;
          break;
    }
  
    if (valid==true)
    { 
      var thisval=0;
      var totalval=0;
      var mv='012';
      var nv='0123456789';
      // Apply the mask
      for(i=0;i<16;i++)
      { 
        letter = num.charAt(i).toLowerCase();
        mult = mask.charAt(i).toLowerCase();
        thisval = mv.indexOf(mult) * nv.indexOf(letter);
        if (thisval > 9) 
        {  
          thisval -= 9;
        }
        totalval += thisval;
      }
  
      // check the mod10 state
      if (Math.floor(totalval/10)==(totalval/10)) 
      {  }
      else 
      { valid=false; }
    }
  }  
    
  if (valid==true)  {
  	result = 1;
  	if (num != "") {
  	  switch(JustNums.length) 
  	  {
  		  case 13: num = InsertSpaces(JustNums,4,8); break;
  		  case 14: num = InsertSpaces(JustNums,4,8,11);break;
  			case 15: num = InsertSpaces(JustNums,4,8,12);break;
   			case 16: num = InsertSpaces(JustNums,4,8,12);break;
    	}
		CCNum.value = num;
  	}
  }
  else  {  
  	result = 0; 
  }
  
  return result;
}
function ValidateCCFields(ctype, cnum, expyr, expmos, TheDIV, tDIV, nDIV, eDIV, CanBeEmpty, cards, doExpired, doNotExpired) {
	
 	resultExp = ValidateCCExpiryDate(expyr, expmos, eDIV, CanBeEmpty, doExpired, doNotExpired);
 	resultType = ValidateCCType(ctype, tDIV, CanBeEmpty, cards);
 	resultNum = ValidateCCNumber(cnum, ctype, CanBeEmpty, nDIV);
 	
 	if (((resultExp==1) && (resultType==1)) && (resultNum==1)) {
 		result = 1;
 	} else {
 		result = 0;
 	}
 	
 	i = 0;
 	if (ctype.value == "") {i++;}
 	if (cnum.value == "") {i++;}
 	if (expyr.value == "") {i++;}
 	if (expmos.value == "") {i++;}
 	
 	// 0 - All have content
 	// 4 - None have content
 	// 1-3 - some, but not all have content
 	if ((i > 0) && (i < 4)) {
 		if (CanBeEmpty==true) {
 			result = 0;
 		}
 	}
 	UpdateStatus(TheDIV, result); 
  return result;	
}

function ValidateCCNumber(cNumber, cType, CanBeEmpty, TheDIV) {
  
  result = ValidateCreditCardNumber(cType, cNumber, CanBeEmpty);
 	UpdateStatus(TheDIV, result); 
  return result;	
}

function ValidateCCExpiryDate(cYear, cMonth, TheDIV, CanBeEmpty, doExpired, doNotExpired) {
	YearVal = cYear.value;
	MonthVal = cMonth.value;
	
	
	result = 0;
	if (YearVal=="") {
		if (MonthVal =="" ) {
			if (CanBeEmpty==true) {
				result = 1;
      	UpdateStatus(TheDIV, result); 
        return result;
			}	
			else {
				result = 0;
				UpdateStatus(TheDIV, result);
				return result;
			}
		}
	}
  // it will only get here if not both are empty
	if ((YearVal=="") ||(MonthVal =="" )) {
		result = 0;
  	UpdateStatus(TheDIV, result);
		return result;
	}

	var dtm = new Date(YearVal, MonthVal, 1);
	var now = new Date();

	if (dtm > now) {
		ccExpired = false;
	} else {
		ccExpired = true;
	}

	var month = now.getMonth() + 1;
	var year = now.getYear();
	
	i = 0;
	if (doExpired    == true) {i = 1;}
	if (doNotExpired == true) {i = i + 2;}
	switch (i) {
	  case 0: result =1; break;
	  case 1: if (ccExpired==true) { result = 1; } break;
	  case 2: if (ccExpired==false) { result = 1; } break;
	  case 3: result = 1; break;
	}
	
	UpdateStatus(TheDIV, result);
  return result;	
}
      
function ValidateCCType(CCType, CCTypeDIV, CanBeEmpty, Cards) {
	result = ValidateDropDown(CCType,CCTypeDIV,"");
	if (result != 1) {
 		if ((CanBeEmpty==true) && ( isEmptyField(CCType)==true)) {
			result = 1;
		}
	}
	UpdateStatus(CCTypeDIV, result);
  return result;	
}

function InsertSpaces(s, p1, p2, p3) {
	result = s;
	p1 = p1 || -1;
	p2 = p2 || -1;
	p3 = p3 || -1;

	if (p3 != -1) {
	  result = s.substring(0,p3) + " " + s.substring(p3,s.length);
	}
	if (p2 != -1) {
	  result = result.substring(0,p2) + " " + result.substring(p2,result.length);
	}
	if (p1 != -1) {
	  result = result.substring(0,p1) + " " + result.substring(p1,result.length);
	}
	return result;
}
function ConvertToNumber(strVal, DecPts) {
  s = trim(strVal);
  if (s=="")
  {
  	return ""
  }
  else
  {
  	if (isNaN(s)==true) 
  	{ 
  		return "-"
  	}
  	else
  	{
  	  var num = parseFloat(s);
      return num.toFixed(DecPts);	
  	}
  }
}

function ValidateInteger(c, TheDIV, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat) {
	result = ValidateNumber(c, TheDIV, CanBeEmpty, 0, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat, false);
  UpdateStatus(TheDIV, result);
  return result;
}


function ValidateIntegerRangeFrom(c1, c2, TheDIV1, TheDIV2, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat) {
  var result = ValidateIntegerRange(c2, c1, c2, TheDIV1, TheDIV2, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat) ;
}
		
function ValidateIntegerRangeTo(c1, c2, TheDIV1, TheDIV2, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat) {
	var result = ValidateIntegerRange(c1, c1, c2, TheDIV1, TheDIV2, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat) 
}

function ValidateIntegerRange(c, c1, c2, TheDIV1, TheDIV2, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat) {
	var result = 0;
  var r1 = ValidateNumber(c1, TheDIV1, CanBeEmpty, 0, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat, false);
  var r2 = ValidateNumber(c2, TheDIV2, CanBeEmpty, 0, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat, false);
  
  if (r1==0) { 
  	UpdateStatus(TheDIV1, r1);
  }
  if (r2==0) {
  	UpdateStatus(TheDIV2, r2);
  }
  
	if (r1==r2) {
		if (r1==1) {
			var v1 = ConvertToNumber(c1.value, 0);
			var v2 = ConvertToNumber(c2.value, 0);
			if (v2-v1<0) {
			  if (SmartFormat==true) {
    			if (c.name == c1.name) { 
    				alert('a: ' + c.name + ', ' +  c1.name + ', ' + c2.name);
    				alert('v1: ' + v1 + ' v2:' +  v2);
		  	  	c1.value = v2;
    			} else {
    				alert('b: ' + c.name + ', ' +  c1.name + ', ' + c2.name);
    				alert('v1: ' + v1 + ' v2:' +  v2);
    				c2.value = v1;
		  	  }
  	  		UpdateStatus(TheDIV1, 1);
       	  UpdateStatus(TheDIV2, 1);
			  	return 1;
			  } else {
   				alert('c-> v1:' + v1 + '  v2:' +  v2);
			  	if (c==c1) { 
			  		UpdateStatus(TheDIV1, 1);
        	  UpdateStatus(TheDIV2, 0);
        	} else {
			  		UpdateStatus(TheDIV1, 0);
        	  UpdateStatus(TheDIV2, 1);
        	}
			  	return 0;
			  }
			} else {
	  		UpdateStatus(TheDIV1, 1);
     	  UpdateStatus(TheDIV2, 1);
				return 1;
			}
		}
	}
	return result;
}

function ValidateFloat(c, TheDIV, CanBeEmpty, hasMinVal, MinVal, hasMaxVal, MaxVal, DecPts, SmartFormat) {
	result = ValidateNumber(c, TheDIV, CanBeEmpty, DecPts, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat, false);
  UpdateStatus(TheDIV, result);
  return result;
}

function ValidateNumber(c, TheDIV, CanBeEmpty, DecPts, hasMinVal, MinVal, hasMaxVal, MaxVal, SmartFormat, UpdateTheStatus) {
	UpdateTheStatus = UpdateTheStatus || true;
	
dval="";
	
  s = trim(c.value);
  if (isEmptyField(c)==true) {
  	if (CanBeEmpty == true) {
  	  result = 1;
    }
    else
    { result = 0;
    }
  } 
  else 
  {  	
  if (isNaN(s)==true) {
  	result = 0;
  }
  else
  {
  	if (((s >= MinVal) || (hasMinVal==false)) && ((hasMaxVal==false) || (s <= MaxVal)) )
  	{
     if (DecPts == 0) {
  		if (s.indexOf('.') > -1) 
  		{
  			if (SmartFormat==true) {
  				s = Math.floor(s); // lose the dec pts
  				result = 1; 
  			}
  			else
  			{
  				result = 0;
  			}
  		}
  		else
  		{
 			 result = 1; }  // OK!!
  	 }
  	 else
  	 { // check/set the decimal pts
  	 	len = s.length;
      i = s.indexOf('.') + 1;
      if (((i< 1) && (DecPts>=0)) || ((len-i)<DecPts)) {// do dec vals in string
      	if (SmartFormat==true) {// add trailing zeros, if needed
     		  var num = parseFloat(s);
    		  s = num.toFixed(DecPts);
       		result = 1;
      	} else 
        { 
         	result = 0; }
      } 
      else
      { // we're here to truncate / round
      	if (DecPts==-1) { 
       		result = 1;
      	}
      	else 
        {
        	if ((len-i)==DecPts) {
         	  result = 1;
          }
          else
          {
        	  if (SmartFormat==true) 
        	  {
      		    var num = parseFloat(s);
      		    s = num.toFixed(DecPts);
      		    result = 1;
      	    }
      	    else 
            { 
          	  result = 0; 
          	}
          }
        }		
      }
     }
    }
    else
    {  // outside min/max range
    	result = 0;
    }
  }
}
  if (SmartFormat == true) {  
   	  
    c.value = s; 
  }
  if (UpdateTheStatus==true )
  { 
  	UpdateStatus(TheDIV, result); 
  }
  return result;
}

function ShowRequirements(TheDiv, w, h, FieldCaption, FieldRequirements){
  helpwindow=window.open('/field_requirements.asp','Field_Criteria','locationbar=0,location=0, toolbar=0, scrollbars=1,height=' + h + ',width=' + w);
	var helpcontent= helpwindow.document;
	helpcontent.write("<HTML><HEAD><TITLE>What's This?</TITLE></HEAD><BODY class=FormHelp>");
	helpcontent.write('<link rel="StyleSheet" href="/css/templates/MyStyles.asp" type="text/css">');
	helpcontent.write("<H1>" + FieldCaption + "</H1>");
  if (FieldRequirements=='') {
	  helpcontent.write('<BR><Center><DIV class=Requirements><b>Requirements:</b>  None.</DIV></CENTER>')
	} else {
		helpcontent.write('<BR><CENTER><DIV class=Requirements><b>Requirements:</b><BR>' + FieldRequirements + "</DIV></CENTER>");
	}
  helpcontent.write("<BR><BR><div style='text-align:center'><input type=Button value='Close' onClick='window.close();'></div><BR><BR>");
	helpcontent.write("</BODY></HTML>");
	helpcontent.close();
	if (window.focus) {helpwindow.focus();}
	return false;
}

function WhatsThis(TheDiv, w, h, FieldCaption, FieldRequirements){
  helpwindow=window.open('/whats_this.asp','Whats_This','locationbar=0,location=0, toolbar=0, scrollbars=1,height=' + h + ',width=' + w);
	var helpcontent= helpwindow.document;
	helpcontent.write("<HTML><HEAD><TITLE>What's This?</TITLE></HEAD><BODY class=FormHelp>");
	helpcontent.write('<link rel="StyleSheet" href="/css/templates/MyStyles.asp" type="text/css">');
	helpcontent.write("<H1>" + FieldCaption + "</H1>");
	helpcontent.write(TheDiv.innerHTML);
  helpcontent.write("<BR><BR><div style='text-align:center'><input type=Button value='Close' onClick='window.close();'></div>");
  if (FieldRequirements=='') {
	  helpcontent.write('<BR><Center><DIV class=Requirements><b>Requirements:</b>  None.</DIV></CENTER>')
	} else {
		helpcontent.write('<BR><CENTER><DIV class=Requirements><b>Requirements:</b><BR>' + FieldRequirements + "</DIV></CENTER>");
	}
	helpcontent.write("</BODY></HTML>");
	helpcontent.close();
	if (window.focus) {helpwindow.focus();}
	return false;
}

function StatusUpdatePanel(TheDIV, FormName) {
	TheDIV.innerHTML = TheDIV.innerHTML + "<DIV class=Heading3><DIV class=StatusButton onClick='Validate" +FormName+"();'>&nbsp;&nbsp;Update&nbsp;&nbsp;</DIV><DIV class=StatusButton onClick='ClearStatus" +FormName+"VALID();'>&nbsp;&nbsp;Hide&nbsp;&nbsp;</DIV>Incomplete Fields</DIV>";
}
function ClearStatus(TheDIV, FormName)
{
	TheDIV.innerHTML = "";
  TheDIV.style.display = "none";  
}

function AddStatus(TheDIV, fname, Msg)
{
	var s="";
	s = "<DIV class=OneStatusMsg "; 
	s = s + " onMouseOver=\"this.className=\'OneStatusMsgOver\'\"";
	s = s + " onMouseOut=\"this.className=\'OneStatusMsg\'\"";
  s = s + " onClick=\"window.location=\'#bookmark_" + fname + "\';\">" + Msg+ "</DIV>";
  									 
  TheDIV.innerHTML = TheDIV.innerHTML + s;
}


function DuplicateStatus(srcdiv, destdiv, validated)
{
	destdiv.innerHTML = srcdiv.innerHTML;
	if (validated==false) {
    srcdiv.style.display = "block";
    destdiv.style.display = "block";
  } else {
    srcdiv.style.display = "none";
    destdiv.style.display = "none";
  }  	

}

function PrePopulateCCExpiry(ExpiryMonth, ExpiryYear, YearsFuture, YearsPast)
{
	if (ExpiryMonth.options.count > 1)  {  // dont' repeat filling
	  return true;
	}
	YearsPast = YearsPast || 0;
	YearsFuture = YearsFuture || 10;
	
	// Add Months
	var optn = document.createElement("OPTION");
	optn.text = "Month";
	optn.value ="";
	optn.selected = true;
  ExpiryMonth.options.add(optn);	
	for (var x = 1; x <= 12; x++) 
	{
		s = x;
		if (x < 10) {s = "0" + s}
		var optn = document.createElement("OPTION");
		optn.text = s;
		optn.value = x;
		ExpiryMonth.options.add(optn);
  }
  // Add Years
  var Today = new Date();
  ty = Today.getFullYear();
	var optn = document.createElement("OPTION");
	optn.text = "Year";
	optn.value ="";
	optn.selected = true;
  ExpiryYear.options.add(optn);	
  fromyear = ty-YearsPast;
  toyear = ty + YearsFuture;

  for (var x=fromyear; x<=toyear; x++)
  {
  	var optn = document.createElement("OPTION");
  	optn.text = x;
  	optn.value = x;
  	ExpiryYear.options.add(optn);
  }
	
}
function CE_attachEvent2(editorname)
{
				// get the cute editor instance
				var editor1 = getElement('CE_' + editorname + '_ID');
				alert(editor1);
				//Get the editor content  
				var editdoc=editor1.GetDocument(); 

				// attach Event
				if(editdoc.attachEvent)
					editdoc.attachEvent('onkeypress',eval('validate'+editorname));
				else if(editdoc.addEventListener)
					editdoc.addEventListener('keypress',eval('validate'+editorname),true);
}					
			
function ValidateHTMLEditor(cname, TheDIV, CanBeEmpty, hasMin, MinLength, hasMax, MaxLength)
{
	
	var minl = -1;
	if (hasMin==true) 
	{
		minl = MinLength;
	}
	var maxl = -1;
	if (hasMax==true) 
	{
		maxl = MaxLentgh;
	}
   	
  // get the cute editor instance
  var editor1 = getElement('CE_' + cname + '_ID');
  // get the editor content  
//  var editdoc=editor1.GetDocument(); 
var v = getElement(cname).value;
	//var v = 'editdoc.body.innerHTML';

  
	if (v == "") {
		if (CanBeEmpty==true) {
			result = 1;
		}
		else 
		{
			if (MinLength < 1) {
			  result = 1;
		  } 
		  else 
		  {
		  	result = 0;
		  }
		}
	} 
	else 
	{
	  if (v.length >= MinLength) 
	  {
	  	if (MaxLength < 0) 
	  	{
	  		result = 1;
	  	} 
	  	else
	  	{
	  		if (v.length <= MaxLength) {
	  			result =1;
	  		} else {
	  			result = 0;
	  		}
	  	}
	  } 
	  else 
	  { 
	  	result = 0;
	  }
  }
  UpdateStatus(TheDIV, result);
  return result;	
}
	
function DisplayHTMLCounts(cname, hasMin, hasMax, MinLength, MaxLength, TheDIV) {
  // get the cute editor instance
}
function ddidsfkldfjs() {
	
	var editor1 = document.getElementById('CE_' + cname + '_ID');
  // get the editor content  
  var editdoc=editor1.GetDocument(); 
	var v = editdoc.body.innerHTML;
	
	if (v=="") {
		s = "0";
	} else {
		s = v.length;
	}
	
	i = 0;
	if (hasMin == true) { i = 1 } 
	if (hasMax == true) { i = i + 2 } 
	
	switch (i) {
		case 0:
		case 1:
		  s = "(" + s + " characters)";
		  break;
		case 2:
		case 3:
		  s = "(" + s + "/" + MaxLength + " characters)";
	}
	TheDIV.innerHTML = s;
}
	

