/* Check the form values */
function checkEntry(evt){
    var theForm = yui.get('musical_instruments');
    if(!evt) evt = window.event; // Make sure we've got the event properly
    
    // The field names to check
    var arrInput = new Array('firstname','surname','telephone');
    var arrInputReset = new Array('firstname','surname','telephone','email_address','telephone_format','email_address_format');
    // Reset everything
    resetAlerts(arrInput);
    resetMessageAlerts(arrInputReset);
    hideit('errorBoxMain');
    
    //document.getElementById('boxError').style.visibility = 'hidden';	
    var arrErrors = new Array();
    var alertString = ''
    var errorFlag = 0;
    
    for (i=0; i < arrInput.length; i++){
        formName = arrInput[i];
        
        if (isVisible(theForm.elements[formName].parentNode) // It's visible within the form
            && theForm.elements[formName].value=='') { // And it's empty
            
            arrErrors[arrErrors.length] = arrInput[i] + '_error';
            arrErrors[arrErrors.length] = arrInput[i] + '_err_mess';
            errorFlag++;
        }
    }
   
    // Extras
    

    // Check the phone number format
    if (document.forms['musical_instruments'].telephone.value) {
      var emailRegex = /^(\(?[0-9\s\)]{9,16})$|[Nn][Oo][Nn][Ee]\s[Aa][Vv][Aa][Ii][Ll][Aa][Bb][Ll][Ee]|[Ee][Xx]\s[Dd][Ii][Rr][Ee][Cc][Tt][Oo][Rr][Yy]/;

	if (!document.forms['musical_instruments'].telephone.value.match(emailRegex) ) {
	    arrErrors[arrErrors.length] = 'telephone_error';
            arrErrors[arrErrors.length] = 'telephone_format_err_mess';
            errorFlag++;
        }
    }

    // Check email format
    if (document.forms['musical_instruments'].email_address.value) {
      var emailRegex = /^([\w]([-.\w]*[\w!\#\$%&'\*\+\-/=\?\^{}\|~])*@([\w][-\w]*[\w]\.)+[a-zA-Z]{2,9})$/;

      if (!document.forms['musical_instruments'].email_address.value.match(emailRegex) ) {
        arrErrors[arrErrors.length] = 'email_address_error';
        arrErrors[arrErrors.length] = 'email_address_format_err_mess';
        errorFlag++;
      }
    }
        
    if (errorFlag > 0) {
        showit('errorBoxMain');
        //document.getElementById('boxError').style.visibility = 'visible';     
        showErrors(arrErrors);
        // Stop the form submitting
        lib.cancelEvent(evt);
        return false;
    } else return true; // Allow the form to submit
}



lib.verbose = true; // Set the TamarLibrary verbose option to see all errors and log notes


/* Show the Ajax error message */
function showAjaxMessage() {
    yui.removeClass('less_than_ie6','showIE5only');
    window.location.hash = 'boxTop';
    return false;
}

/* Now we set everything up */
window.addListener('load',
    function(evt) { // Function to execute when page loads - basically contains everything
        // Reset the form
        document.forms.musical_instruments.reset();

        // set up the form check entry
        yui.get('musical_instruments').addListener('submit',checkEntry);
        
    }
);
