// The following code implements the UI elements of the form validator. Since
// there is no way to predict the UI requirements of each site that will use the
// validator API, this code will have to be re-implemented for each website
// that wishes to use the validation routines.

// Whether or not the user attempted to submit the form data
var attemptedSubmit = false;

// When a data element passes validation, the corresponding property of this
// object will be set to true
var passFields = {
	"targus_info": false
};

// This object keeps track of issue codes that apply to each data element
var issueFields = {
	"targus_info": []
};

var allowSubmit;

// An instance of the Validator class
var validator;

function initValidator() {
	
	val = new Validator();
	
	val.initData('targus_info', 'TargusInfo');

	document.getElementById('leadform').onsubmit = function () {
		if (!allowSubmit) {

            var email = document.getElementById('email').value;
            if (email.indexOf("@getmyonlinedegree.com") != -1) {
                document.getElementById('targus_info_error').style.display = 'none';
                return true;
            }

			attemptedSubmit = true;
			val.runValidator();
			document.getElementById('targus_info_error').style.display = 'inline';
			alert("Please correct any errors in your submission before continuing.");
			return false;
		} else {
			document.getElementById('targus_info_error').style.display = 'none';
			return true;
		}
		 // Run the original validation script
	};

	var runFunc = function () { val.runValidator(); };
	
	document.getElementById('firstName').onchange = runFunc;
	document.getElementById('lastName').onchange = runFunc;
	document.getElementById('phoneAreaCode').onchange = runFunc;
	document.getElementById('phoneExchange').onchange = runFunc;
	document.getElementById('phoneLast4').onchange = runFunc;
	document.getElementById('email').onchange = runFunc;
	//document.getElementById('zip').onchange = runFunc;
	//document.getElementById('state').onchange = runFunc;
};

function valfn_getFieldList(dataId) {

	switch (dataId) {

		case 'targus_info':
			fields = {
				"firstName": document.getElementById('firstName').value,
				"lastName": document.getElementById('lastName').value,
				"phoneNumber":
					document.getElementById('phoneAreaCode').value +
					document.getElementById('phoneExchange').value +
					document.getElementById('phoneLast4').value,
				"emailAddress": document.getElementById('email').value
				//"city": document.getElementById('city').value,
				//"state": document.getElementById('state').options[document.getElementById('state').selectedIndex].value,
				//"zipCode": document.getElementById('zip').value,
				//"state": document.getElementById('state').value
			};
			break;
			
		default:
			return {};
	}

	return fields;
}

function valfn_fieldIsEmpty(dataId) {
	switch (dataId) {

		case 'targus_info':
			if (document.getElementById('firstName').value &&
				document.getElementById('lastName').value &&
				document.getElementById('phoneAreaCode').value &&
				document.getElementById('phoneExchange').value &&
				document.getElementById('phoneLast4').value &&
				document.getElementById('email').value
				//document.getElementById('city').value &&
				//document.getElementById('zip').value &&
				//document.getElementById('state').value
			) {
				return false;
			}
			break;
	}
	return true;
}

function valfn_resultHandler(dataId, valResult) {
	var errspan = document.getElementById(dataId + '_error');
	
    var emailCheck = document.getElementById('email').value;
    if (emailCheck.indexOf("@getmyonlinedegree.com") != -1) {
        errspan.style.display = 'none';
        return;
    }
    
	switch (valResult) {
		case 1:
			passFields[dataId] = true;
			errspan.style.display = 'none';
			errspan.innerHTML = "";
			allowSubmit = true;
			break;
		case 2:
		case 3:
			passFields[dataId] = false;
			errspan.style.display = 'inline';
			break;
	}
	var allPass = true;
	for (var i in passFields) {
		if (passFields[i] == false) {
			allPass = false;
			break;
		}
	}
	if (allPass) {
		document.getElementById('targus_info_error').style.display = 'none';
	}
}

function valfn_issueHandler(dataId, issueList) {
	var errors = [];
	for (i = 0; i < issueList.length; i++) {
		errors.push(issueToText(issueList[i].code));
	}
	document.getElementById(dataId + '_error').innerHTML = errors.join('<br>') + '<br>';
}

function valfn_stdHandler(dataId, fieldList) {

	switch (dataId) {
		
		// TODO: Implement standardization from Targus info
		case 'targus_info':
			break;
	}
}

function issueToText(issueCode) {
	switch (issueCode) {
		case 1001:
			return 'Unable to validate';
		case 2001:
			return 'Country selection is not valid';
		case 3001:
			return 'Your email address is not formatted correctly';
		case 3002:
			return 'The first part of your email address is not valid';
		case 3003:
			return 'The domain name in your email address is not valid';
		case 3004:
			return 'Your email address ends with an invalid top-level domain';
		case 3005:
			return 'The domain name in your email address does not exist';
		case 4001:
			return 'This phone number is not formatted correctly';
		case 4002:
			return 'Your phone number has an invalid area code';
		case 4003:
			return 'The middle 3 digits of your phone number are not valid';
		case 5001:
			return 'First name is missing a vowel';
		case 5002:
			return 'Middle name is missing a vowel';
		case 5003:
			return 'Last name is missing a vowel';
		case 6001:
			return 'Invalid zip code';
        case 6002:
            return 'You have entered an invalid ZIP code';
        case 6003:
            return 'Your state selection is invalid';
        case 7001:
            return 'Phone number and/or address are invalid or do not match';
        case 7020:
        	return 'Phone number appears to have been disconnected';
        case 7022:
        	return 'Phone number is blank or invalid';
        case 7027:
        	return 'Phone number is not valid';
        case 7029:
        	return 'Name and address do not match';
        case 7030:
        	return 'Phone number is not valid';
        case 7031:
        	return 'Phone number is not valid';
        case "ISS_BAD_STATE_CODE":
            return 'State is not valid';
        default:
            return '';
	}
}
