// form.js - version 0.1 - SelCo2000
// For HERO
// Copyright (c) 2011. Selco2000
// All rights reserved.
//



function setGender() {
	if (document.getElementById("genderID").checked == true)
{
document.getElementById("Gender").value = document.getElementById("genderID").value;
document.getElementById("Gender").style.background = "#007FFF";
document.getElementById("Gender").style.color = "#FFFFFF";
}
else
{
document.getElementById("Gender").value = 'Female';
document.getElementById("Gender").style.background = "#FFE4E1";
document.getElementById("Gender").style.color = "#000000";
}}

//FORM VALIDATION

	
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false;}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(Businessname,"Business name must be filled out!")==false)
  {Businessname.focus();return false;}
}

{
if (validate_required(Phone,"Year started must be filled out!")==false)
  {Phone.focus();return false;}
}
}

function validateFormOnSubmit(theForm) {
var reason = "";

reason += validateEmpty(theForm.Firstname);
reason += validateEmpty(theForm.Lastname);
reason += validateEmpty(theForm.EmailAddress);
reason += validateEmpty(theForm.Phone);
reason += validateEmpty(theForm.Street);
reason += validateEmpty(theForm.City);
reason += validateEmpty(theForm.State);
reason += validateEmpty(theForm.Gender); 
reason += validateEmpty(theForm.Availability);
reason += validateEmpty(theForm.Occupation);      
reason += validateEmpty(theForm.Speciality);     

if (reason != "") {
 	$.msgbox("<h2>Form Errors</h2> <br><!--<em>-->Health & Educational Relief Organization <!--(info@heroglobal.org)</em>--><br/><p><h2>Some fields need your attention:</h2><br/></p>"  + reason  );
    return false;
  }
  return true;
}


function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Red'; 
		fld.style.color = '#FFFFFF';
        error = "<li>The required field ( " + "<b>" + fld.name + "</b>" + " ) has not been filled in.</li><br/>" 
    } else {
        fld.style.background = 'White';
		fld.style.color = '#000000';
    }
    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 resultblock = document.getElementById("resultblock");   
	resultblock.value = "";
	resultblock.classname = "blueText";
 
   var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   var validEmail = "";
    if (fld.value == "") {
        fld.style.background = 'Red';
        error = "You didn't enter an email address.\n";
					
			$.msgbox(error);
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
							
			$.msgbox(error);
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
						
			$.msgbox(error);
    } else {
        fld.style.background = 'White';
		Spry.Utils.loadURL("GET","checkusername.cfm?username="+(tfld), false, usercheck);
		 }	
   return error;

}
    
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
			$.msgbox(error);
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
			$.msgbox(error);
    } 
    return error;
}

function usercheck(request) {
   var result = request.xhRequest.responseText;
  var fld =  document.getElementById("EmailAddress");
   if(result == 0) {
   status("This email address is already in use! Please try another email address...");
 fld.style.background = 'Yellow';
}
   else {
	   	   var resultblock = document.getElementById("resultblock");   
	resultblock.value = "";
	fld.style.background = 'White';
	   status('');
	   setErrorDisplay();
   }
}
function status(str) {
   var resultblock = document.getElementById("resultblock");
   resultblock.innerHTML = str;  
   resultblock.className="redText";
   if (str != 0){
   Sexy.alert(str);
   }
}
function setErrorDisplay() {
   var resultblock = document.getElementById("resultblock");
   resultblock.className="blueText";  
}
function setFNFocus() {
   var fname = document.getElementById("Firstname");
  fname.focus();  
}






