﻿

//This function is used by the myaccountpage.
function ValidateRefillReminder() {
	var bValid = true;

	//First clear the old errors
	clearAllRefillErrors();
	
	var arrValids = [];

	var txtfn = getRRFirstName();
	var txtln = getRRLastName();
	var txteml = getRREmailAddress();
	var sm = getPrescriptionSelectMonth();
	var sd = getPrescriptionSelectDay();
	var sy = getPrescriptionSelectYear();
	var sl = getPrescriptionSelectLength();
	
	var fn = txtfn.value;
	var ln = txtln.value;
	var eml = txteml.value;
	var m = sm.selectedIndex;
	var d = sd.selectedIndex;
	var y = sy.selectedIndex;
	var l = sl.selectedIndex;
	
	//populate validation array: required fields
	arrValids.push(checkRefillRequiredField(txtfn, fn));
	arrValids.push(checkRefillRequiredField(txtln, ln));
	arrValids.push(checkRefillRequiredField(txteml, eml));
	arrValids.push(checkRefillRequiredDDL(sm, m));
	arrValids.push(checkRefillRequiredDDL(sd, d));
	arrValids.push(checkRefillRequiredDDL(sy, y));
	arrValids.push(checkRefillRequiredDDL(sl, l));
	
	//populate validation array: valid format
	arrValids.push(checkRefillAlphaField(txtfn, fn));
	arrValids.push(checkRefillAlphaField(txtln, ln));
	
	//populate validation array: email regex
	arrValids.push(checkRefillEmail(txteml, eml));
	
	//console.log(arrValids);

	for (var n in arrValids) {
		if (arrValids[n] == false) { bValid = false; }
	}
	
	//console.log(bValid);
	
//	console.log('bValid1: ' + bValid1);
//	console.log('bValid2: ' + bValid2);
//	console.log('bValid3: ' + bValid3);
//	console.log('bValid4: ' + bValid4);
//	console.log('bValid5: ' + bValid5);
//	console.log('bValid6: ' + bValid6);
	
	//bValid = bValid1 && bValid2 && bValid3 && bValid4 && bValid5 && bValid6 && bValid7;
	
	//return bValid;
	//temp - remove this...
	return bValid;
}


//Testing only

function testvalidation(id) {
	flagRefillError(id,'test_msg');
}

function testmainvalidation() {
	showRefillErrorSummary();
}

function clearmainvalidation() {
	clearAllRefillErrors();
}



//Flag required errors.
function checkRefillRequiredDDL(elem, value) {

//	console.log('checkRefillRequiredField: ' + elem + ', and ' + value);
//	console.log('checkRefillRequiredField: val.length: ' + value.length);
	
	var bValid = true;
	if (value <= 0) { //Set error.
		showRefillErrorSummary();
		flagRefillError(elem.id, ErrMsg_Required);
		bValid = false;
	}
	return bValid;
}

//Flag invalid errors.
function checkRefillAlphaField(elem, value) {
	var bValid = true;
	bValid = isAlphaNumeric(value);
	if (!bValid) {
		showRefillErrorSummary();
		flagRefillError(elem.id, ErrMsg_ValidType);
	}
	return bValid;
}

function checkRefillEmail(elem, value) {
	var bValid = true;
	bValid = isValidEmailAddress(value);
	if (!bValid) {
		showRefillErrorSummary();
		flagRefillError(elem.id, ErrMsg_ValidType);
	}
	return bValid;
}

function checkRefillRequiredField(elem, value) {

//	console.log('checkRefillRequiredField: ' + elem + ', and ' + value);
//	console.log('checkRefillRequiredField: val.length: ' + value.length);
	
	var bValid = true;
	if (value.length <= 0) { //Set error.
			showRefillErrorSummary();
			flagRefillError(elem.id, ErrMsg_Required);
			bValid = false;
		}
	return bValid;
}

function showRefillErrorSummary() {
	var errorpar = document.getElementById('parErrorSummary');
	document.getElementById('refill_error_summary').className = 'reg_main_err_msg';
	errorpar.innerHTML = DefaultErrorMessage; //located in validation.js.
}

function hideRefillErrorSummary() {
	var errorpar = document.getElementById('parErrorSummary');
	document.getElementById('refill_error_summary').className = 'reg_main_err_msg_off';
	errorpar.innerHTML = '';
}

function flagRefillError(id, msg) {
	var errorclass = 'rr_err_on';
	var validation_div = id.replace('ctl00_ctlRefillReminder_','v_');
	var errormsg_div = id.replace('ctl00_ctlRefillReminder_','e_');
	
	//See if we have an id from the M,D,Y dropdown...
	if (id.indexOf('ddlRRDolp') > -1) {
		validation_div = 'v_ddlRRDolp';
		errormsg_div = 'e_ddlRRDolp';
	}
	
	document.getElementById(validation_div).className = errorclass;
	document.getElementById(errormsg_div).innerHTML = msg;
	document.getElementById(errormsg_div).style.display = 'block';
}

function clearRefillError(id) {
	var errorclass = 'rr_err_off';
	var validation_div = id.replace('ctl00_ctlRefillReminder_','v_');
	var errormsg_div = id.replace('ctl00_ctlRefillReminder_','e_');
	document.getElementById(validation_div).className = errorclass;
	document.getElementById(errormsg_div).style.display = 'none';
}


function clearAllRefillErrors() {
	var prefix = 'ctl00_ctlRefillReminder_';
	var arrFields = [];
	arrFields.push('txtRRFirstName', 'txtRRLastName', 'txtRREmailAddress');
	arrFields.push('ddlRRDolp', 'ddlRRPrescriptionLength');
	
	//Clear the main error message.
	hideRefillErrorSummary();
	
	//Clear individual errors.
	for (var i in arrFields) {
		clearRefillError(prefix + arrFields[i]);
	}
}

//This function is used by the myaccountpage.
function switchPrescriptionDaysInMonth() {
	var sm = getPrescriptionSelectMonth();
	var sd = getPrescriptionSelectDay();
	var m = sm.selectedIndex;
	var d = sd.selectedIndex;
	//Not necessary to have a valid day and month. Should work with 
	//selectedIndex = 0. (Leave the following line commented out).
	//if (m <= 0 || d <= 0) { return; } 
	var select = sd;
	var lastday = select.options.length - 1;
	
	//Do the switch
	doDaysInMonthSwitch(m, select, lastday);
}

//This function is used by the myaccountpage.
function getPrescriptionSelectMonth() {
	return document.getElementById('ctl00_ctlRefillReminder_ddlRRDolpMo');
}

//This function is used by the myaccountpage.
function getPrescriptionSelectDay() {
	return document.getElementById('ctl00_ctlRefillReminder_ddlRRDolpDay');
}

//This function is used by the myaccountpage.
function getPrescriptionSelectYear() {
	return document.getElementById('ctl00_ctlRefillReminder_ddlRRDolpYr');
}

//This function is used by the myaccountpage.
function getPrescriptionSelectLength() {
	return document.getElementById('ctl00_ctlRefillReminder_ddlRRPrescriptionLength');
}

//This function is used by the myaccountpage.
function getRRFirstName() {
	return document.getElementById('ctl00_ctlRefillReminder_txtRRFirstName');
}

//This function is used by the myaccountpage.
function getRRLastName() {
	return document.getElementById('ctl00_ctlRefillReminder_txtRRLastName');
}

//This function is used by the myaccountpage.
function getRREmailAddress() {
	return document.getElementById('ctl00_ctlRefillReminder_txtRREmailAddress');
}

//This function is used by the myaccountpage.
function adjustPrescriptionDayValue() {
	var sm = getPrescriptionSelectMonth();
	var sd = getPrescriptionSelectDay();
	var m = sm.selectedIndex;
	var d = sd.selectedIndex;
	var selectday = sd;
	adjustDaySelectedValue(m, d, selectday);
}


