var errMsg1 = 'Data error:\n\n';
var errMsg2 = 'Registration cannot proceed:\n\n';
var errMsg3 = 'Information update cannot proceed:\n\n';
var errMsg4 = 'eMail address update cannot proceed:\n\n';
var errMsg5 = 'Password change cannot proceed:\n\n';
var errMsg6 = 'Sign In cannot proceed:\n\n';
var errMsg7 = 'Message format error:\n\n';
var errMsgTxt;
var gblField;
	
// Trim leading/trailing whitespace off string
function trim(str){
  return str.replace(/^\s+|\s+$/g, '');
}
	
// Ensure required fields aren't empty
function IsEmpty(str){   
  var emptyString = /^\s*$/;
  return(emptyString.test(str));
}
	
// Set focus to current field
function setFocusDelayed(){
  gblField.focus();
}

function setFocus(fld){
  // save fld in global variable so value retained when routine exits
  gblField = fld;
  setTimeout('setFocusDelayed()',100);
}
	
// Final UserID validation
function valEmail (fld1, fld2, reqd){
  var status = true;
  fld1.value = fld1.value.replace(/^\s*|\s*$/g,'');
  fld2.value = fld2.value.replace(/^\s*|\s*$/g,'');
  if (!IsEmpty(fld1.value)){
    var temp1 = trim(fld1.value);
    var temp2 = trim(fld2.value);
    if (valEmailFormat(fld1,false)){
      if (temp1 != temp2){
        errMsgTxt += '- eMail address confirmation does not match\n';
        status = false;
        gblField = fld2;
      }
    }
  }
  else {
    errMsgTxt += '- eMail address field cannot be blank\n';
    status = false;
    gblField = fld1;
  }
  return status;
}
	
	// val the email address format
	function valEmailFormat(fld, msg){
		var valid = true;
		var errMsg = '- Invalid email address format\n';
		var temp = trim(fld.value);
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		if (!(!reg1.test(temp) && reg2.test(temp))) {
			valid = false;
			if (msg){
				alert(errMsg1 + errMsg);
				setFocus(fld);
			}
			else {
				errMsgTxt += errMsg;
		        gblField = fld;
			}
		}
	return valid;
	}
	
	// Check Password
	function valPWord (fld1, fld2){
		var status = true;
		if (IsEmpty(fld1.value)){
			errMsgTxt += '- Password field cannot be blank\n';
			status = false;
		}
		else {
		  if (fld1.value.length < 6){
		    errMsgTxt += '- Password must be at least 6 characters in length\n';
		    status = false;
		  }
		  else {
        var temp1 = trim(fld1.value);
        var temp2 = trim(fld2.value);
        if (temp1 != temp2){
          errMsgTxt += '- Password confirmation does not match\n';
          status = false;
        }
      }
		}
		if (!status){
		  gblField = fld1;
			fld1.value = '';
			fld2.value = '';
		}
		return status;
	}

// Check plain text
function valText (fld, req, test){
    var status = false;
    fld.value = fld.value.replace(/^\s*|\s*$/g,'');
    if (IsEmpty(fld.value)){
        if (req){
            errMsgTxt += '- ' + fld.alt + ' field cannot be blank\n';
        }
        else {
            status = true;
        }
    }
    else {
        var reg1 = /[^ a-zA-Z-']/;	            // allow alpha, space, dash or apostrophe
        var reg2 = /[^ a-zA-Z0-9-'/#\&\.,]/;    // allow alphanumeric, space and certain symbols
        var reg3 = /[^0-9-]/;                   // allow numbers and dash
        var reg4 = /[^0-9-+]/;                  // allow number, dash and plus
        var reg5 = /[^\x20-\x7e\x80-\xfe]/;     // allow any printable character, but no CR or LF
        var reg6 = /[^ a-zA-Z0-9-]/;               // allow alpha, space, or dash
        var regExp = eval('reg' + test);
        if (regExp.test(fld.value)) {
            errMsgTxt += '- ' + fld.alt + ' field contains invalid character(s)\n';
        }
        else {
            status = true;
        }
    }
    if (!status) {gblField = fld;}
    return status;
}

function valStateProvince(fldStateProvince, fldCountry) {
    var status = true;
    if (!fldStateProvince.value) {
        status = false;
        gblField = fldStateProvince;
        errMsgTxt += '- State/Province not selected\n';
    }
    return status;
}

// Used to validate presence of selection in select boxes.
// This function does not validate the actual data.
function valSelect(fld,req){
    var status = true;
    if (!fld.value){
        status = false;
        gblField = fld;
        switch(req) {
            case 1:
                errMsgTxt += '- Country not selected\n';
                break;
            case 2:
                errMsgTxt += '- "Send to" recipient not selected\n';
                break;
        }
    }
    return status;
}


function valPhone(fld,country) {
    var status = true;
    var regExp1 = /^\(\d{3}\) \d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4}|\d{3}[ -.]\d{3}[ -.]\d{4}$|^[\d]{10}/;
    var regExp2 = /^[\d]/;
    fld.value = fld.value.replace(/^\s*|\s*$/g,'');
    if (!IsEmpty(fld.value)) {
        switch(country.value) {
            // Use this for USA, Canada, and certain Atlantic and Caribbean islands.
            case 'US': case 'CA': case '02': case '05': case '53': case '78': case '81': case '88': case '89': case '94': case '97': case '98':
                if (!regExp1.test(fld.value)) {
                    errMsgTxt += '- Telephone number is not properly formatted\n   -- Telephone numbers must contain the complete\n       10-digit area code and number, and may be formated as:\n       (123) 456-7890, 123-456-7890, or 1234567890\n';
                    status = false;
                    gblField = fld;
                }
                break;
            // Use this for all other country codes
            default:
                if (!regExp2.test(fld.value)) {
                    errMsgTxt += '- Telephone number is not properly formatted\n   -- Enter number digits only (no spaces or other characters)\n'
                    status = false;
                    gblField = fld;
                }
                break;
        }    
        
    }
    gblField = fld;
    return status;
}
		
  function valSignIn(MyForm){
    var errs=0;
    errMsgTxt = '';
    if (!valPWord (MyForm.PWord, MyForm.PWord)) ++errs;
    if (!valEmailFormat (MyForm.UserID, false)) ++errs; 
    doErrs(MyForm, errs, 6);
		return (errs==0);
  }

  function valAddress(MyForm,FormType){
    var errs=0;
    errMsgTxt = '';
    if (!valPhone (MyForm.fld_Phone, MyForm.fld_Country)) ++errs; 
    if (!valSelect (MyForm.fld_Country,1)) ++errs;
    if (MyForm.fld_Country.value == 'US'){
        if (!valText (MyForm.fld_PostalCode, true, 3)) ++errs;
    }
    else {
        if (!valText (MyForm.fld_PostalCode, true, 6)) ++errs;
    }
    if (MyForm.fld_Country.value == 'US' || MyForm.fld_Country.value == 'CA'){
        if (!valStateProvince (MyForm.fld_StateProvince, MyForm.fld_Country)) ++errs; 
    }
    if (!valText (MyForm.fld_City, true, 1)) ++errs; 
    if (!valText (MyForm.fld_AddrLine2, false, 2)) ++errs; 
    if (!valText (MyForm.fld_AddrLine1, true, 2))  ++errs; 
    if (!valText (MyForm.fld_LName, true, 1))  ++errs;
    if (!valText (MyForm.fld_MIName, false, 1)) ++errs;
    if (!valText (MyForm.fld_FName, true, 1))  ++errs;
    if (FormType == 1){
        if (!valPWord (MyForm.fld_Password, MyForm.verifyPassword)) ++errs; 
        if (!valEmail (MyForm.fld_UserID, MyForm.verifyUserID, true)) ++errs;
    }
    doErrs(MyForm, errs, 2);
		return (errs==0);
  }

  function valRegUserChngSubAddr(MyForm){
    var errs=0;
    errMsgTxt = '';
    if (!valPhone (MyForm.fld_Phone, 'none')) ++errs; 
    if (!valText (MyForm.fld_PostalCode, true, 3)) ++errs; 
    if (!valState (MyForm.fld_State)) ++errs; 
    if (!valText (MyForm.fld_City, true, 1)) ++errs; 
    if (!valText (MyForm.fld_AddrLine2, false, 2)) ++errs; 
    if (!valText (MyForm.fld_AddrLine1, true, 2))  ++errs; 
    if (!valText (MyForm.fld_LName, true, 1))  ++errs;
    if (!valText (MyForm.fld_MIName, false, 1)) ++errs;
    if (!valText (MyForm.fld_FName, true, 1))  ++errs; 
    if (!valText (MyForm.old_fld_PostalCode, true, 3)) ++errs; 
    if (!valState (MyForm.old_fld_State)) ++errs; 
    if (!valText (MyForm.old_fld_City, true, 1)) ++errs; 
    if (!valText (MyForm.old_fld_AddrLine2, false, 2)) ++errs; 
    if (!valText (MyForm.old_fld_AddrLine1, true, 2))  ++errs; 
    if (!valText (MyForm.old_fld_LName, true, 1))  ++errs;
    if (!valText (MyForm.old_fld_MIName, false, 1)) ++errs;
    if (!valText (MyForm.old_fld_FName, true, 1))  ++errs; 

    doErrs(MyForm, errs, 3);
		return (errs==0);
  }

  function valChngSubAddr(MyForm){
    var errs=0;
    errMsgTxt = '';
    if (!valText (MyForm.NewAddr, true, 2))  ++errs;
    if (!valText (MyForm.CurrentAddr, true, 2))  ++errs;
    if (!valPhone (MyForm.fld_Phone)) ++errs; 
    if (!valEmail (MyForm.fld_UserID, MyForm.verifyUserID, true)) ++errs; 
    if (!valText (MyForm.fld_Name, true, 1))  ++errs;
    doErrs(MyForm, errs, 3);
		return (errs==0);
  }
  function valChngPWord(MyForm){
    var errs=0;
    errMsgTxt = '';
    if (!valPWord (MyForm.newPWord, MyForm.vfyNewPWord)) ++errs;
    doErrs(MyForm, errs, 5);
		return (errs==0);
  }

function valUserID(MyForm){
    var errs=0;
    errMsgTxt = '';
    if (!valEmail (MyForm.UserID, MyForm.vfyUserID, true)) ++errs; 
    doErrs(MyForm, errs, 4);
    return (errs==0);
}

function valShipAddr(MyForm){
    var errs=0;
    errMsgTxt = '';
    if (MyForm.AddrType.checked){
        if (!valText (MyForm.phone, false, 4)) ++errs; 
        if (!valText (MyForm.postalcode, true, 3)) ++errs; 
        if (!valText (MyForm.stateprovince, false, 1)) ++errs; 
    }
    else {
        if (!valPhone (MyForm.phone)) ++errs; 
        if (!valText (MyForm.postalcode, true, 2)) ++errs; 
        if (!valState (MyForm.stateprovince)) ++errs; 
    }
    if (!valText (MyForm.country, true, 1)) ++errs;
    if (!valText (MyForm.city, true, 1)) ++errs; 
    if (!valText (MyForm.address2, false, 2)) ++errs; 
    if (!valText (MyForm.address1, true, 2))  ++errs; 
    if (!valText (MyForm.suffix, false, 1))  ++errs; 
    if (!valText (MyForm.last_name, true, 1))  ++errs;
    if (!valText (MyForm.first_name, true, 1))  ++errs; 

    doErrs(MyForm, errs, 3);
    return (errs==0);
}

function clearFormValues(name) {
    var myType, myForm
    myForm = eval(name);
    for (var i=0, j=myForm.elements.length; i<j; i++) {
        myType = myForm.elements[i].type;
        if (myType == 'checkbox' || myType == 'radio')
            myForm.elements[i].checked = myForm.elements[i].defaultChecked;
        if (myType == 'hidden' || myType == 'password' || myType == 'text' || myType == 'textarea')
            myForm.elements[i].value = '';
        if (myType == 'select-one' || myType == 'select-multiple')
            for (var k=0, l=myForm.elements[i].options.length; k<l; k++)
                myForm.elements[i].options[0].selected = true;
    }
}  

function valContactForm(MyForm){
    var errs=0;
    errMsgTxt = '';
    if (!valSelect (MyForm.fld_Office, 2)) ++errs;
    if (!valText (MyForm.fld_FromName, true, 1)) ++errs;
    if (!valEmailFormat (MyForm.fld_FromEmail, false)) ++errs; 
    if (!valText (MyForm.fld_Subject, true, 5)) ++errs; 
    doErrs(MyForm, errs, 7);
    return (errs==0);
}



function doErrs(MyForm, errs, msgNum){
    if (errs > 0){
        alert(eval('errMsg' + msgNum) + errMsgTxt);
        setFocus(gblField);
    }
    MyForm.validated.value = true;
}
  
function confirmDelete(){
    return confirm('This action will permanently delete your user account.\n\nAre you sure you want to do this?\n\n');
}
