$(document).ready(function() {
    //interstitial();
	loadBusy();
});

function validateFormOnSubmit(theForm) {
	var reason = "";

  reason += validateEmpty(theForm.agfname,'First Name');
  reason += validateEmpty(theForm.aglname,'Last Name');
  reason += validateEmail(theForm.agemail);
  reason += validateEmpty(theForm.agphone, "Phone");
  reason += validateEmpty(theForm.agpass, "Password");
  reason += validateEmpty(theForm.agconfpass, "Confirm Password");
  reason += validateEmpty(theForm.agimgcode,'Image Verification Code');
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validate(theForm) {
	if(validateFormOnSubmit(theForm)) {
		thForm.submit();
	}
}

function clearForm(theForm)
{
	alert("in clear Form");
	var i;
	for (i = 0; (i < theForm.length); i++) {
	theForm[i].reset();
	}
}


function validateFormOnSubmit1(theForm) {
var reason = "";

//  reason += validateUsername(theForm.username);
//  reason += validatePassword(theForm.pwd);
//  reason += validateEmail(theForm.email);
//  reason += validatePhone(theForm.phone);

  reason += validateEmpty(theForm.agencyemail,'Email');
//  reason += validatePhone(theForm.agencyphone);
//  reason += validateEmpty(theForm.agencyrecno,'Record Numbers');
reason += validateEmpty(theForm.agencyphone,'Phone Number');
//  reason += validateEmpty(theForm.agencydollar,'Dollar Amount');
  reason += validateEmpty(theForm.startdate,'Start Date');
  reason += validateEmpty(theForm.enddate,'End Date');
  //reason += validateEmpty(theForm.comments,'Comments');
  //reason += validateEmpty(theForm.agencyefile,'Agency File');
  reason += validateEmptyList(theForm,'Submission Months');
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateEmptyList(theForm,txtdet) {
    var error = "";
 
    if(theForm["agencyselmonths[]"].value == ""){
		    error = "The required field " +txtdet+ " has not been selected.\n"
	}

    return error;  
}
function validateEmpty(fld,txtdet) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = '#FFD4D4'; 
        error = "The required field " +txtdet+ " has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'lightblue'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#FFD4D4'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FFD4D4'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#FFD4D4';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#FFD4D4';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#FFD4D4';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = '#FFD4D4';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#FFD4D4';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFD4D4';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFD4D4';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#FFD4D4';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#FFD4D4';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#FFD4D4';
    }

    return error;
}
function passwordstrength () {
 // Replace areas in Caps in the line below with the NUMERICAL value you want to search for.
 // For example, if you want "Less than 4 characters" to be weak, replace "l=6 ;" with "l=4;"
 l=6 ; m=8 ; n=10 ;
 b=document.getElementById('Password').value ;
   if (b.length<l) {
     //You can type in whichever message you like for the below. Just edit the "Message" or "Length of Line" with the appropriate message/underscores
       c="Password Strength: <font color='red'><b>WEAK</b></font>" ;
       d="<font size='2'><font color='red'><b>_______________</b></font>" ;
    }
    else if (b.length<m) {
      c="Password Strength: <font color='#FF3300'><b>MODERATE-WEAK</b></font>" ;
      d="<font size='2'><font color='#FF3300'><b>_____________________</b></font>" ;
    }
    else if (b.length<n) {
      c="Password Strength: <font color='blue'><b>MODERATE</b></font>" ;
      d="<font size='2'><font color='blue'><b>_____________________</b></font>" ;
    }
    else {
      c="Password Strength: <font color='green'><b>STRONG</b></font>" ;
      d="<font size='2'><font color='green'><b>_____________________</b></font>" ;
    }
    //This area specifies which text areas the messages go to.  Replace 'Display' or 'Strength Visual' with the id of the area.

	if(b.length > 0) {
	    document.getElementById('Display').innerHTML=c ;
		document.getElementById('Strength Visual').innerHTML=d ;
	}
}

/*function interstitial() {
    $('#linkToTrans').click(function(e) {
        e.preventDefault();
        $("#transuserDialog").dialog("open");
    });
     $("#transuserDialog").dialog({ autoOpen: false, modal: true, width:520, height:220, resizable: false, draggable: false, zIndex: 3999, bgiframe: true, title: 'Transaction Deletion',  buttons: { "Continue": function() {window.location.href = $('#linkToTrans').attr("href");}}
    });
};*/

function loadBusy() {
    $('#linkToBusy').click(function(e) {
        //e.preventDefault();
        $("#busyDialog").dialog("open");

    });

	 $("#busyDialog").dialog({ autoOpen: false, modal: true, width:520, height:180, resizable: false, draggable: false, zIndex: 3999, bgiframe: true, title: 'Loading'});
};



