/*****************************************
 * VALIDATION FUNCTION
 * Arguments:
 * str requirements: a comma separated string listing the ids of all required form fields
 *   - there are some special types of fields we want to validate - to flag these as special, 
 *     append |type to the end of their ids- specifically:
 *      - email address: append |email
 *		- checkbox: append: |checkbox (note: this will check to make sure the checkbox is checked)
 *      - credit card number: append |ccnumber
 *      - vcode: append: |vcode
 *      - compare: append: |compare (note: this is a special type of special type, where we want to confirm that 2 fields match - use format: field1!field2|compare)
 *		- expdate: append: |exp_date (note: this is a special type of special type, where we want to confirm that the date provided is in the future - use format: day!month!year|expdate - for no day or month use: 0!0!year|expdate)
 *
 * str error_message - optional error message for more specific applications
*****************************************/
function validate (requirements, form_id, error_message) {
	var submitForm = true;
	resetErrors(requirements);

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

	requirements_array = requirements.split(',');
	
	// iterate over all required fields
	for (var i=0; i < requirements_array.length; i++) {
		element_id = requirements_array[i];
		
		// first, check to see if it's a special case - ie, check that it has a | in the name
		if (element_id.indexOf('|') > 0) {
			// we have a special case, get the id of the element, and the type of element
			element_id = requirements_array[i].slice(0, requirements_array[i].indexOf('|'));
			element_type = requirements_array[i].slice(requirements_array[i].indexOf('|') + 1);
			
			if (element_type == 'compare') {
				if (!validate_compare (element_id)) {
					submitForm = false;
					show_error('compare'); // hard coded for now
				}
			
			} else if (element_type == 'expdate') {
				if (!validate_expdate (element_id)) {
					submitForm = false;
					show_error('expdate'); // hard coded for now
				}
			
			} else if (element_type == 'checkbox') {
				// verify the checkbox has been checked
				if (!validate_checkbox (element_id)) {
					submitForm = false;
					show_error(element_id);
				}
				
			} else { // ends if element_type = password
				element_value = document.getElementById(element_id).value;
	
				//alert ('element_id: ' + element_id + "\n" + 'element_type: ' + element_type + "\n" + 'element_value: ' + element_value);
	
				// before we do anything, make sure there is data in the element
				if (element_value == '') {
					submitForm = false;
					show_error(element_id);
					
				} else { // ends if (element_value)
					// validate the element with its specific function
					if (!window['validate_' + element_type](element_value)) {
						//alert ('Invalid ' + element_type);
						submitForm = false;
						show_error(element_id);
					}
				} // ends if/else element_value
			} // ends if/else element_type = password
			
		} else { // ends special type check
			// verify the element is not empty
			if (!document.getElementById(element_id).value) {
				submitForm = false;
				show_error(element_id);
			}
			
		} // ends type if/else
		
	} // ends for loop which iterates over all required fields

	// throw the error or submit the form
	if (submitForm == true) {
		document.getElementById(form_id).submit();
	} else {
		alert(error_message);
	}
}

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

// checks if the checkbox has been checked
function validate_checkbox (checkbox_id) {
	return document.getElementById(checkbox_id).checked;
}

// 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) {
	//alert('show_error: ' + element_id);
	document.getElementById('label-' + element_id).style.color = '#FF0000'; // turn the field label red
	//if (document.getElementById('required-' + element_id) !== null) document.getElementById('required-' + element_id).style.color = '#FF0000'; // turn the field required red
	document.getElementById(element_id).style.borderColor = '#FF0000'; // turn the input outline required red
}

function resetErrors (requirements) {
	ids = requirements.split(',');
	
	for (i = 0; i < ids.length; i++) {
		id = ids[i];
		if (id.indexOf('|') > 0) {
			if (id.slice(id.indexOf('|')+1) == 'compare') {
				id = 'compare';
			} else if (id.slice(id.indexOf('|')+1) == 'expdate') {
				id = 'expdate';
			} else {
				id = id.slice(0, id.indexOf('|'));
			}
		}

		document.getElementById('label-' + id).style.color = '';
		//if (document.getElementById('required-' + id) !== null) document.getElementById('required-' + id).style.color = '';
		document.getElementById(id).style.borderColor = '#999999';
	}
}