/*
* Copyright: 2005 - this.year() Karbonblack Creative

* This JS page controls all custom functions for the main webstie
* @page validations.js
* @version 2.14
* @author Greg Shiers, (Karbonblack.com)
* @copyright: Karbonblack 
*/

/* Function to check weather a field is empty
* RegEx includes validation to check if they have added multipe white spaces to the text field.
* if(emptyString(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
emptyString =  function ( field ) {
	var re = /^\s*$/;
	return re.test( $("#"+field).val() );
}
/**
* Check that the field contains no illegal charactors
* Allows "_", " " and "." otherwise other charactors are not allowed
* if(!validation.alphaNumeric(field)){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
alphaNumeric = function ( field ) {
	var re = /^[A-Za-z0-9_ \.]+$/
	return re.test( $("#"+field).val() );
}
/**
* Function to check for a valid email address, includes a check for @ and . in the string
* if(validation.isEmailAddress(field)){...}
* @param field
* @version 1.5
* @author Greg Shiers
*/
isEmailAddress = function ( field ) {
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/
	return re.test( $("#"+field).val() );
}
/**
* Function to check for a valid phone number
* Can include a number, spaces, dashes, and an extention
* if(!phoneNumber(field)){...}
* @param field
* @version 1.5
* @author Greg Shiers
*/
isphoneNumber = function( field ) {
	var re  = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3,4} ?\d{0,7} ?(x|xtn|ext|extn|extension)??\.? ?\d{1,5}?$/i;
	return re.test( $("#"+field).val() );
}
/**
* To check that input type is a word document
* if(!isWordDocument(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
isWordDocument = function ( field ) {
	var re = /(.doc?x$)/; // allow only word
	return re.test( $("#"+field).val() );
}
/**
* Checks that input has an image extention
* if(!safeWebImageInput(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
safeWebImageInput = function ( field ) {
	var re = /(.jpe?g$)|(.gif$)|(.png$)/; // allow jpg / jpeg / gif / png
	return re.test( $("#"+field).val() );
}
/**
* Checks that input is an safe web file upload
* if(!safeWebFileUpload(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
safeWebFileUpload = function ( field ) {
	var re = /(.pdf$)|(.txt$)|(.csv$)|(.doc$)/; // allow pdf / txt / csv / doc
	return re.test( $("#"+field).val() );
}
/**
* Checks that a checkbox is checked before passing validation
* if(!isChecked(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
isChecked = function ( field ) {
	return ( $("#"+field).checked ) ? false : true;
}
/**
* Checks that two fields match
* if(!areFieldsMatched(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
areFieldsMatched = function ( field, field2 ) {
	return ( $("#"+field).val() == $("#"+field2).val() ) ? false : true;
}
/**
* Checks if the first selected index of a select box is selected
* if(!isSelected(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
isSelected = function ( field ) {
	return ($("#"+field).selectedIndex == 0) ? true : false;
}
/**
* Checks for a certain selected index
* if(!selectedIndex(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
selectedIndex = function ( field , index) {
	return ($("#"+field).selectedIndex == index) ? true : false;
}
/**
* IE is we have a date function that spits out 2 as the month of february
* This funtion will make the value now 02.
* if(!friendlyDate(field){...}
* @param field
* @version 1.2
* @author Greg Shiers
*/
friendlyDate = function ( string ) {
	if ( string < 10 ) {
		string='0'+string;
	}
 	return string;
}
/**
* Method to validate that the date formatt has been entered correctly (dd/mm/yyyy)
* if(dateFormatted(field){...}
* @param field
* @version 1.0
* @author Greg Shiers
*/ 
dateFormatted = function ( field ) {
	var re = /^(\d{2,2})\/(\d{2,2})\/(\d{4,4})$/
	return re.test( $("#"+field).val() );
}
/********************************/
/* Create and remove errror 	*/
/********************************/

/**
* This function creates an error div
* @param field
* @version 1.0
* @author Greg Shiers
*/ 
createSimpleError = function ( target ) {
	$("#"+target).addClass('input-error');
}
/**
* This function removes an error div
* @param field
* @version 1.0
* @author Greg Shiers
*/ 
removeSimpleError = function ( target ) {
	$("#"+target).removeClass('input-error');
}
/**
* This function creates an error div
* @param field
* @version 1.0
* @author Greg Shiers
*/ 
createLabelError = function ( target, message ) {
	$("#"+target).addClass('input-error').parent().find('label').addClass('redshadow').text(message);
}
/**
* This function removes an error div
* @param field
* @version 1.0
* @author Greg Shiers
*/ 
removeLabelError = function ( target, message ) {
	$("#"+target).removeClass('input-error').parent().find('label').removeClass('redshadow').text(message);
}
