/* MediaPlus cmo35 Version cmo35A 04/01/08 12:01:39 delta js/login.js
** Copyright 2001 Publishing Business Systems, Inc.
** All Rights Reserved Worldwide.
*/
/* #177516 08.10.10 mickeyg Removed fieldcount declaration in valdmlogin().
** #176555 08.08.07 mickeyg Merge up cmo34 WR#176522.
** #176522 08.03.24 mickeyg Move var to top to resolve fieldcount not defined.
** #170822 08.02.26 mickeyg CR #29596 Use generic errormsg variable.
** #170822 08.02.25 mickeyg Restructure validatecmLogin "account" field checks.
** #168449 07.03.16 todd	Merge CMO 3.4b WR #168448 up to 3.4.
** #168448 06.10.03 todd    Put accountNumber validation in validate.js function
** #169343 07.02.06 mickeyg CR #27322 Adjust valdmlogin() phone/email validation
** #169745 07.02.01 mickeyg CR #27467 Add more code to valdmlogin().
** #169342 06.12.19 mickeyg Added valdmlogin().
**                          Removed obsolete phone tab related code.
** #160027 06.10.12 todd	CR #26803.  Use postalCodeName in password error.
** #160027 06.09.26 mickeyg Added zipcode; check if accountNum exists.
** #164790 06.09.26 mickeyg Comment out too many login/password entered errors. 
** #161592 06.09.19 mickeyg Removed duplicate CheckPhoneNumber() and updated 
**                          phone references.
** #167275 06.06.15 todd	Remove validateEmail procedure and use the one in
**							validate.js instead.
** #166951 06.05.19 todd	CR #25852. Remove unused validatecarrLogin function.
** #165671 06.03.10 robi	Merge 139440 up to 34.
** #139440 05.08.05 mickeyg RequireNumeric check.
** #160920 05.03.24 mickeyg Removed alert from WR#160905.
** #160905 05.03.23 mickeyg Fixed sendpassword stateinfo param; Added alert in 
**                          validateCMforgotpassword().
** #159472 05.02.24 mickeyg Add eval in checkLength(). 
** #156027 04.04.01 mickeyg CR#19586 Removed housenum numeric check
** #156027 04.03.26 mickeyg Added housenum validation and tabNext(), 
**							checkLength(), sendpassword().
** #149070 02.12.06 mickeyg Trim password 
** #       02.02.27 mickeyg Created (extracted from validate.js)
** js/login.js
** - Validation routines used for (Distribution & Subscriber) Login and Password
** -------------------------------------------------------------------------- */
var fieldcount = 0;

function sendpassword(){
    location.href = 
		"passwd.html?stateInfo=" + escape(document.login.stateInfo.value) +
	    "&phone=" + escape(document.login.phone.value) +
        "&email=" + escape(document.login.email.value);
    return false;
}

function mycount(myfield) {
    if (myfield != "") {
        return fieldcount++;
    }
}

// Distribution & Subscriber login validation
function validatecmLogin() {
	var myform = document.forms[0];
    var email = "";
    var phone = "";
    var accountNumber = "";
	var houseNumber;
	var zipCode;
	var zipCodeLabel;
    var password = myform.password.value;
	var requirenumeric = "yes"; /* default */

	// distribution module 
	if (myform.requirenumeric) {
		requirenumeric = myform.requirenumeric.value;
	}

	if (myform.postalCodeName) {
		zipCodeLabel = myform.postalCodeName.value;
	}

	if (myform.phone) {
		// clear out space only character entry
		isblank(myform.phone.value);
   		if (whitespace == true) {
			myform.phone.value = '';
		}
		mycount(myform.phone.value);
	}
	if (myform.email) {
		// clear out space only character entry
		isblank(myform.email.value);
   		if (whitespace == true) {
			myform.email.value = '';
		}
		mycount(myform.email.value);
	}

	// build login component error message
	if (fieldcount == 0) {
		errormsg = "Please enter one of the following fields: \n";
		// check for object existence and build associated error text
		builderrtext(myform.phone, "Phone Number");
		builderrtext(myform.email, "Email Address");
		if (errormsg != "") {
			alert(errormsg);
			return false;
		}
	}

	// initialize variables
	errormsg = "";
	fieldcount = 0;

	/* If we don't check for spaces, a user could login in using spaces for a
	   password which would match the database password value.
	   Refresh the screen and let account number/password handle the error.
    */
	/* At this point, we always expect password to be a field on the screen */
	isblank(password);
    if (whitespace == true) {
		password = '';
		myform.password.value = '';
	}
	else {
		mycount(password);
	}

	if (myform.accountNum) {
    	accountNumber = myform.accountNum.value;
		// clear out space only character entry
		isblank(accountNumber);
   		if (whitespace == true) {
			accountNumber = '';
			myform.accountNum.value = '';
		}
		mycount(accountNumber);
	}

	if (myform.housenumber) {
		houseNumber = myform.housenumber.value;
		// clear out space only character entry
		isblank(houseNumber);
    	if (whitespace == true) {
			houseNumber = '';
			myform.housenumber.value = '';
		}
		mycount(houseNumber);
	}
	if (myform.zipcode) {
		zipCode = myform.zipcode.value;
		// clear out space only character entry
		isblank(zipCode);
    	if (whitespace == true) {
			zipCode = '';
			myform.zipcode.value = '';
		}
		mycount(zipCode);
	}

	// build password error message
	if (fieldcount == 0) {
		errormsg = "Please enter one of the following fields: \n";
		// check for object existence and build associated error test
		builderrtext(myform.accountNum, "Account Number");
		builderrtext(myform.housenumber, "House Number");
		builderrtext(myform.zipcode, zipCodeLabel);
		builderrtext(myform.password, "Password");
		if (errormsg != "") {
			alert(errormsg);
			return false;
		}
	}

	// Validate against expected data types
    if (phone != "") {
		if(!checkPhoneNumber(myform.phone)) {
        	return false;
        }
    } 
    if (email != "") {
     	if(!validateEmail()) {
        	return false;
        }
    }
    if ((accountNumber != "") && (requirenumeric != 'no')) {
        if (!validateAccountNumber(accountNumber)) {
            return false;
    	}
    }
}

// Distribution: District Manager login validation
function valdmlogin() {
	var myform = document.forms[0];

	if (myform.phone) {
		// clear out space only character entry
		isblank(myform.phone.value);
   		if (whitespace == true) {
			myform.phone.value = '';
		}
		mycount(myform.phone.value);
	}
	if (myform.email) {
		// clear out space only character entry
		isblank(myform.email.value);
   		if (whitespace == true) {
			myform.email.value = '';
		}
		mycount(myform.email.value);
	}

	// build login component error message
	if (fieldcount == 0) {
		errormsg = "Please enter one of the following fields: \n";
		// check for object existence and build associated error text
		builderrtext(myform.phone, "Phone Number");
		builderrtext(myform.email, "Email Address");
		if (errormsg != "") {
			alert(errormsg);
			return false;
		}
	}

    // initialize variables
	errormsg = "";
	fieldcount = 0;

	/* If we don't check for spaces, a user could login in using spaces for a
	   password which would match the database password value.
	   Refresh the screen and let account number/password handle the error.
    */
	/* At this point, we always expect password to be a field on the screen */
	isblank(myform.password.value);
    if (whitespace == true) {
		myform.password.value = '';
	}
	else {
		mycount(myform.password.value);
	}

	if (myform.districtid) {
		// clear out space only character entry
		isblank(myform.districtid.value);
   		if (whitespace == true) {
			myform.districtid.value = '';
		}
		mycount(myform.districtid.value);
	}

	// build password error message
	if (fieldcount == 0) {
		errormsg = "Please enter one of the following fields: \n";
		// check for object existence and build associated error text
		builderrtext(myform.districtid, "District ID");
		builderrtext(myform.password, "Password");
		if (errormsg != "") {
			alert(errormsg);
			return false;
		}
	}
	// Validate against expected data types
    if ((myform.phone) && (myform.phone.value != "")) {
		if(!checkPhoneNumber(myform.phone)) {
        	return false;
        }
    } 
    if ((myform.email) && (myform.email.value != "")) {
     	if(!validateEmail()) {
        	return false;
        }
    }
}

// Distribution & Subscriber password validation
function valforgotpwd(myform) {
	var myform = document.forms[0];
	var phone = "";
	var email = "";
	var fieldtext = "";

	// set phone variable and phone related error message.
	if (myform.phone) {
    	phone = myform.phone.value;
		fieldtext = 'a phone number';
	}

	// set email variable and email related error message.
	if (myform.email) {
		email = myform.email.value;
		if (myform.phone) 
			fieldtext = fieldtext + ' or ';

		fieldtext = fieldtext + 'an email address';
	}	

	// append period to error message
	fieldtext = fieldtext + '.';

	// validate required fields
	if ((phone == "") && (email == ""))  {
    	alert("Please enter " + fieldtext);
		if (myform.phone) {
			myform.phone.focus();
		}
		else if (myform.email) {
			myform.email.focus();
		}	
        return false;
	}
	if ((phone != "") && (email != ""))  {
    	alert("Please enter only " + fieldtext);
		if (myform.phone) {
			myform.phone.focus();
		}
		else if (myform.email) {
			myform.email.focus();
		}	
        return false;
	}

	// validate against expected data types/formats
    if (phone != "") {
		if(!checkPhoneNumber(myform.phone)) {
        	return false;
        }
    }
    if (email != "") {
     	if(!validateEmail()) {
        	return false;
        }
    }
}
