/***************************************************
Javascript for HRR site
Jan 2009, Fraser Howard
****************************************************/

$(document).ready(function(){
		
		stripetables();
		
		// autocomplete for input forms
		$("#yname").autocomplete("../inc/namehint.php", { minChars:2 });
		$("#yemail").autocomplete("../inc/emailhint.php", { minChars:2 });
		$("#etitle").autocomplete("../inc/eventhint.php", { minChars:2 });
		
// end doc ready loop
});

function check4race() {

	var ttype = document.event_mod.type.value
 	if (ttype == "race") {
      document.event_mod.cc.disabled=false;
      document.event_mod.gp.disabled=false;
      //document.event_mod.postingpassword.disabled=true;
 	}
	else {
      document.event_mod.cc.disabled=true;
      document.event_mod.gp.disabled=true;
      document.event_mod.cc.checked=0;
      document.event_mod.gp.checked=0;
      document.event_mod.postingpassword.disabled=false;
 	}
return;
}

function validateFormOnSubmit(theForm) {
  
  var reason = "";
  
  // check text fields are completed
  reason += validateEmpty(theForm.yourname);
  //reason += validateEmpty(theForm.youremail);
  reason += validateEmpty(theForm.date);
  reason += validateEmpty(theForm.time);
  reason += validateEmpty(theForm.place);
  reason += validateEmpty(theForm.title);
  // password not required for race events
  if ( (theForm.type.value == "training") || (theForm.type.value == "other") ) {
   reason += validateEmpty(theForm.postingpassword);
  }

  // valid text?
  reason += validateText(theForm.time);
  reason += validateText(theForm.place);
  reason += validateText(theForm.title);
  reason += validateText(theForm.youremail);
  reason += validateText(theForm.yourname);
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
    
  return true;
}
function validateEmpty(fld) {
    var error = "";
    
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateNumb(fld,min,max) {
    var error = "";
    if (isNaN(fld.value)) {
	     fld.style.background = 'Yellow';
    	  error = "The required field is incomplete.\n";
    }
    else {
    		if ( (fld.value >= min ) && (fld.value <= max) ){
        		fld.style.background = 'White';
        	}
        	else {
        		fld.style.background = 'Yellow';
    	  		error = "The required field is out of range (" + min + " - " + max + ").\n";	
        	}    
    }

    return error;  
}

function validateText(fld) {
	var error = "";
	if (fld.value.length > 0 ) {
		if (fld.value.indexOf("<") != -1) {
			fld.style.background = 'Yellow';
    		error = "The required field is invalid.\n";
		}
		else {
			fld.style.background = 'White';
		}
	}
	
	return error;
}