/* 
    Document   : grassroots.js
    Created on : 01-Nov-2011
    Author     : Bev Love
    Description:
        Contains all the JavaScript functionality, 
        which relates to the XLVets Grassroots section.
*/

/**
 * Function which will display an error message
 * beside a specified field
 * 
 * @param string errorId - Id of the error to display
 * @param string fieldElement - form element containing the error
 */
function showErrorMessage(errorId, fieldElement) 
{
    //obtain the element which contains the error message
    var errorElement = document.getElementById(errorId);
    
    //display the error message and highlight the field with the error
    errorElement.style.display = "block";
    fieldElement.style.backgroundColor = "#FFFF99";
}

/**
 * Function which will hide an error message
 * beside a specified field
 * 
 * @param string errorId - Id of the error to hide
 * @param string fieldElement - form element no longer containing the error
 */
function hideErrorMessage(errorId, fieldElement) 
{
    //obtain the element which contains the error message
    var errorElement = document.getElementById(errorId);
    
    //hide the error message and wipe the highligt from the field
    errorElement.style.display = "none";
    fieldElement.style.backgroundColor = "white";
}

/**
 * Function which will validate the contact us form 
 * before it is passed to the server
 * 
 * @param object contactUsForm - the contact us form
 */
function validateContactUsForm(contactUsForm) 
{
	//define error count
	var errorCount = 0;
	//declare regular expressions used
	var regPostcode = /^(GIR[ ]?0AA)$|^([A-PR-UWYZ][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][0-9][A-HJKS-UW0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][ABEHMNPRVWXY0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$/;
	var regTelephone = /^0[0-9\s]+$/;
	
	//remove any whitespaces from the start and end of the name value
    var name = contactUsForm.name.value.replace(/^\s+|\s+$/g, '');
    //check to make sure that name has been populated    
    if (name == '') {
        //name has not been populated so show error message
        showErrorMessage('nameError', contactUsForm.name);
        //increase the error count
        errorCount++;
    }
    else {
        //name has been populated, so ensure error message is hidden
        hideErrorMessage('nameError', contactUsForm.name);
    }
	
	//remove any whitespaces from the start and end of the farm value
    var farm = contactUsForm.farm.value.replace(/^\s+|\s+$/g, '');
    //check to make sure that farm has been populated    
    if (farm == '') {
        //farm has not been populated so show error message
        showErrorMessage('farmError', contactUsForm.farm);
        //increase the error count
        errorCount++;
    }
    else {
        //farm has been populated, so ensure error message is hidden
        hideErrorMessage('farmError', contactUsForm.farm);
    }
	
	//remove any whitespaces from the start and end of the postcode value
    var postcode = contactUsForm.postcode.value.replace(/^\s+|\s+$/g, '');	
    //check to make sure that postcode has been populated    
    if (postcode != '') {
		//convert postcode to uppercase
		postcode = postcode.toUpperCase();
        //check to see if postcode is in the correct format
        if (!(regPostcode.test(postcode))) {
            //postcode is not in the correct format, so show error message
            showErrorMessage('postcodeError', contactUsForm.postcode);
            //increase the error count
            errorCount++;
        }        
        else {
            //postcode is in the correct format, so ensure error message is hidden
            hideErrorMessage('postcodeError', contactUsForm.postcode);
        }
    }
    else {
        showErrorMessage('postcodeError', contactUsForm.postcode);
    }
	
	//remove any whitespaces from the start and end of the telephone value
    var telephone = contactUsForm.telephone.value.replace(/^\s+|\s+$/g, '');
    //check to make sure that telephone has been populated    
    if (telephone != '') {
        //check to see if telephone is in the correct format
        if (!(regTelephone.test(telephone))) {
            //telephone is not in the correct format, so show error message
            showErrorMessage('telephoneError', contactUsForm.telephone);
            //increase the error count
            errorCount++;
        }        
        else {
            //telephone is in the correct format, so ensure error message is hidden
            hideErrorMessage('telephoneError', contactUsForm.telephone);
        }
    }
    else {
        showErrorMessage('telephoneError', contactUsForm.telephone);
    }
	
	//check the error count
    if (errorCount > 0) {
        //if the error count is greater than 0, 
        //then the form has failed validation
        return false;
    }
    else {
        //error count is 0,
        //so form has passed validation 
        return true;
    }
}

/**
 * Function to check if a field is blank
 * 
 * @param element field - field from the form
 * @param string errorId - the id of the span containing the error message
 */
function checkNotBlankField(field, errorId) 
{
    //remove any whitespaces from the start and end of the field
    var fieldValue = field.value.replace(/^\s+|\s+$/g, '');
    //check to make sure that field has been populated    
    if (fieldValue == '') {
        //field has not been populated so show error message
        showErrorMessage(errorId, field);
    }
    else {
        //field has been populated, so ensure error message is hidden
        hideErrorMessage(errorId, field);
    }
}

/**
 * Function to check if a field's value matches a regular expression
 * 
 * @param element field - field from the form
 * @param string regEx - the regular expression to match the field value to
 * @param string errorId - the id of the span containing the error message
 * @param bool allowNull - flag to indicate if a field can be null
 */
function checkField(field, regEx, errorId, allowNull) 
{
    //retrieve the desired regular expression
    switch (regEx) {
        case 'regPostcode':
            regEx = /^(GIR[ ]?0AA)$|^([A-PR-UWYZ][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][0-9][A-HJKS-UW0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][ABEHMNPRVWXY0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$/;
            break;
			
		case 'regTelephone':
			regEx = /^0[0-9\s]+$/;
			break;
            
        default:
            regEx = '';
            break;
    }
    
    //remove any whitespace from the start and end of the field
	var fieldValue = field.value.replace(/^\s+|\s+$/g, '');
	
	//check to see if the field has been populated
	if (fieldValue != '') {
		//field is populated 
		//check to see if field value is in the correct format
		if (!(regEx.test(field.value))) {
			//field is not in the correct format, so show error message
			showErrorMessage(errorId, field);
		}        
		else {
			//field is in the correct format, so ensure error message is hidden
			hideErrorMessage(errorId, field);
		}
	}
	else {
		//field has not been populated so hide the error message
		showErrorMessage(errorId, field);
	}
}

dojo.require("dojo.fx");

function wipeIn(para) 
{
    dojo.fx.wipeIn({
        node: para,
        duration: 300
    }).play();
}
