/*****************************************
 *	VALIDATION FUNCTIONS
 *
 *	This set of functions is useful for validating a form.  It will iterate over
 *	all form elements, and validate each based on its validate attribute.
 *
 *	We're also allowing for a unique special_validation() function, so we can do special
 *	validation based on the page itself.
*****************************************/

/*
 *	validate @brief
 *
 *	Summary: This function runs through each form element, and validates it according to
 *	its validate attribute.  It then looks for a specially defined special_validation() function,
 *	which allows for unique validation types on a per page basis.  If some element fails the validation,
 *	this function will then throw an error.
 *
 *	Arguments:
 *		str form_id - the id of the form to validate
 *		str error_message - optional, a custom message for the alert window if validation fails
 *
 *	Returns:
 *		true/false - based on the success of the validation
 */
function validate (form_id, error_message) {
	var submitForm = true;
	reset_errors(form_id);

	if (!error_message) error_message = 'There are errors with your information.  Please review, and re-submit.';

	var form = document.getElementById(form_id);
	
	// run through each form element, and validate it
	for (var i=0; i < form.elements.length; i++) {
		element = form.elements[i];
		if (element.getAttribute('valid') != null) {
			if (!window['validate_'+element.getAttribute('valid')](element.value)) {
				submitForm = false;
				show_error(element.id);
			}
		}
	}

	// run the special validation function, if found
	if (typeof special_validation == 'function') {
		if (!special_validation()) submitForm = false;
	}

	// throw the error or submit the form
	if (submitForm == true) {
		form.submit();
	} else {
		error_message_element = document.getElementById('error_message');
		if (error_message_element != null) {
			error_message_element.innerHTML = '<strong>Error:</strong> '+error_message;
			error_message_element.style.display = 'block';
		}

		alert(error_message);
	}
}

// This function checks that something has been filled in for an element
function validate_not_empty (value) {
	value = value.replace(/[\s]+/, '');
	
	if (value == null || value == '') return false;
	else return true;
}

// This function validates that a value is a number
function validate_number (value) {
	if (validate_not_empty (value)) {
		if (value.match(/[^0-9.,]+/)) return false;
		else return true;
	} else {
		return false;
	}
}

function validate_positive_number (value) {
	if (validate_not_empty(value) && validate_number(value)) {
		float = parseFloat(value);
		if (float > 0) return true;
		else return false;
	} else {
		return false;
	}
}

// This function validates that a url has been entered (requires that it begin with 'http://'
function validate_url (value) {
	if (value.indexOf('http://') == 0) return true;
	else return false;
}

// basic function that checks for an @ sign and a ., and that the @ is before the .
function validate_email (value) {
	if (value.indexOf('@') && value.indexOf('.')) { // check to see if it has both an @ and a .
		if (value.lastIndexOf('@') < value.lastIndexOf('.')) { // check that the @ is before the . - use lastIndexOf to avoid .s in the address itself
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

// basic validation function which checks that the card number supplied has 15 or 16 characters -
// note- we're not checking whether or not the supplied value is actually a number, just the length
function validate_ccnumber (ccnumber) {
	/*
	//alert ("validate_ccnumber: " + ccnumber);
	
	// strip dashes and whitespace from the number
	ccnumber = ccnumber.replace(/ /g, '');
	ccnumber = ccnumber.replace(/-/g, '');
	
	if (ccnumber.length == 15 || ccnumber.length == 16) {
		return true
	} else {
		return false;
	}
	*/
}

// basic validation function which checks that the cvcode supplied has 3 or 4 characters -
// note- we're not checking whether or not the supplied value is actually a number, just the length
function validate_vcode (vcode) {
	/*
	//alert ("validate_vcode: " + vcode);

	// strip whitespace from the number
	vcode.replace(/ /g, '');
	
	if (vcode.length == 3 || vcode.length == 4) {
		return true
	} else {
		return false;
	}
	*/
}

// function to compare 2 passwords, and see if they're the same
// - note: only will compare the 1st two, if there are more, they will be ignored
function validate_compare (fields) {
	/*
	field_array = fields.split('!');
	field1 = field_array[0];
	field2 = field_array[1];
	
	if (document.getElementById(field1).value === document.getElementById(field2).value) {
		return true;
	} else {
		return false;
	}
	*/
}

function validate_expdate (fields) {
/*
	field_array = fields.split('!');
	
	// run checks for day exists
	if (field_array[0] != 0) { day = document.getElementById(field_array[0]).value;
	} else { day = 1; }
	
	// run check to make sure month exists
	if (field_array[1] != 0) { month = document.getElementById(field_array[1]).value;
	} else { month = 1;}
	
	year = document.getElementById(field_array[2]).value;
	var exp_mil = Date.parse(month + '/' + day + '/' + year);
	
	//alert('exp_day: ' + day + "\nexp_month: "+month+"\nexp_year: "+year);

	current_date = new Date();
	current_day = current_date.getDate();
	current_month = current_date.getMonth() + 1;
	current_year = current_date.getFullYear();
	var current_mil = Date.parse(current_month + '/' + current_day + '/' + current_year);

	//alert('current_day: ' + current_day + "\ncurrent_month: "+current_month+"\ncurrent_year: "+current_year);
	//alert('exp: ' + exp_mil + "\n" + 'cur: ' + current_mil);
	
	if (exp_mil > current_mil) {
		return true;
	} else {
		return false;
	}
*/
}

function show_error (element_id) {
	error_element = document.getElementById(element_id);
	error_label = document.getElementById(element_id + '_label');
	error_element_message = document.getElementById(element_id + '_error_message');
	
	if (error_element != null) error_element.style.borderColor = '#FF0000';
	if (error_label != null) error_label.style.color = '#FF0000';
	if (error_element_message != null) error_element_message.style.visibility = 'visible';
}

function hide_error (element_id) {
	error_element = document.getElementById(element_id);
	error_label = document.getElementById(element_id + '_label');
	error_element_message = document.getElementById(element_id + '_error_message');
	
	if (error_element != null) error_element.style.borderColor = '';
	if (error_label != null) error_label.style.color = '';
	if (error_element_message != null) error_element_message.style.visibility = 'hidden';
}

function reset_errors (form_id) {
	var form = document.getElementById(form_id);
	
	// run through each form element, and hide its error
	for (var i=0; i < form.elements.length; i++) {
		if (form.elements[i].id != null && form.elements[i].id != '') hide_error(form.elements[i].id);
	}
	
	if (typeof special_validation_reset == 'function') special_validation_reset();
}

