/*===========================================================================*/
// 20090513 AK fixed pass 2 filled with pass 1 in valid 1
// 20090323 AK added wiz 1 validation
// 20090316 AK added ajaxRegionCities
// 20090313 AK name changed from register.js
// 20090312 AK new code
/*===========================================================================*/
var message;

function getRegionCities(pref_city_id) {
	var objControl = null;
	// source
	objControl = new getObj('ddlPrefRegionId').obj;
	pref_region_id = (objControl.options[objControl.selectedIndex]).value;
	// destination
	objControl = new getObj('ddlPrefCityId').obj;
	ajaxRegionCities(objControl, pref_region_id, pref_city_id);
};

function setWizardPage(display, validate) {
	setPage(display, 'wizardPageToDisplay');
	setPage(validate, 'wizardPageToValidate');
};

function setPage(pageNumber, controlName) {
	var objControl = null;
	objControl = new getObj(controlName).obj;
	objControl.value = pageNumber;
};

function submitForm() {
	if (isValidForm()) {
		var objControl = null;
		objControl = new getObj('frmSalesRegister').obj;
		objControl.submit();
	} else {
		alert(message);
	};
};

function isValidForm() {
	var isValid = true;
	var objControl = null;
	message = '';

	objControl = new getObj('wizardPageToValidate').obj;
	intWizardPageToValidate = objControl.value;

	switch(intWizardPageToValidate) {
		case '1': // Contact Information
			isValid = isValidWizardPage1();
			break;
		case '2': // Home Preferences
			isValid = isValidWizardPage2();
			break;
		case '3': // Credit Application
			isValid = isValidWizardPage3();
			break;
		case '4': // Community Application
			isValid = isValidWizardPage4();
			break;
		case '5': // nothing to do - everything complete and the php will take us elsewhere
			;
			break;
		default:
			alert('Unknown wizard page number [' + intWizardPageToValidate + ']');
			break;
	};

	
	if (!isValid) {
		return false;
	} else {
		return true;
	};
};

function isValidWizardPage1() { // Contact Information
	var isValid = true;
	var objControl = null;
	var message = '';
	var strCache = null;
	var strCache2 = null;
	
//alert('isValidWizardPage1');

	objControl = new getObj('txtEMail').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (strCache == '') {
		isValid = false;
		addMessage('E-Mail address is a required field.');
	} else {
		if (strCache.length <= 100) {
			if (!isEMail(strCache)) {
				isValid = false;
				addMessage('Please enter a valid E-Mail address.');
			};
		} else {
			isValid = false;
			addMessage('E-Mail Address is limited to 100 characters.');
		};
	};

	objControl = new getObj('txtPass').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (trim(strCache) == '') {
		isValid = false;
		addMessage('Password is a required field.');
	} else {
		if (strCache.length > 32) {
			isValid = false;
			addMessage('Password is limited to 32 characters.');
		};
	};

	objControl = new getObj('txtPass2').obj;
	strCache2 = trim(objControl.value);
	objControl.value = strCache2;
	if (trim(strCache2) == '') {
		isValid = false;
		addMessage('Password re-entry is a required field.');
	} else {
		if (strCache2.length > 32) {
			isValid = false;
			addMessage('Password re-entry is limited to 32 characters.');
		};
	};
	
	if (strCache != strCache2) {
		isValid = false;
		addMessage('Password and Password re-entry do not match.');
	};
	
	objControl = new getObj('txtPhone').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (strCache != '') {
		if (strCache.length <= 21) {
			if (strCache.length >= 10) {
				if (!isPhoneNumber(strCache)) {
					isValid = false;
					addMessage('Please enter a valid Phone number.');
				};
			} else {
					isValid = false;
					addMessage('Phone number must contain at least 10 digits.');
			};
		} else {
			isValid = false;
			addMessage('Phone number is limited to 21 characters.');
		};
	};
	
	objControl = new getObj('txtFirstName').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (trim(strCache) == '') {
		isValid = false;
		addMessage('First name is a required field.');
	} else {
		if (strCache.length > 50) {
			isValid = false;
			addMessage('First name is limited to 50 characters.');
		};
	};
	
	objControl = new getObj('txtLastName').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (trim(strCache) == '') {
		isValid = false;
		addMessage('Last name is a required field.');
	} else {
		if (strCache.length > 50) {
			isValid = false;
			addMessage('Last name is limited to 50 characters.');
		};
	};
	
	objControl = new getObj('txtStreet1').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (trim(strCache) != '') {
		if (strCache.length > 100) {
			isValid = false;
			addMessage('Street 1 is limited to 100 characters.');
		};
	};
	
	objControl = new getObj('txtStreet2').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (trim(strCache) != '') {
		if (strCache.length > 100) {
			isValid = false;
			addMessage('Street 2 is limited to 100 characters.');
		};
	};
	
	objControl = new getObj('txtCity').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (trim(strCache) != '') {
		if (strCache.length > 100) {
			isValid = false;
			addMessage('City is limited to 100 characters.');
		};
	};
	
	objControl = new getObj('txtZipcode').obj;
	strCache = trim(objControl.value);
	objControl.value = strCache;
	if (trim(strCache) != '') {
		if (strCache.length <= 10) {
			if (!isZipcode(strCache)) {
				isValid = false;
				addMessage('Please enter a valid Zipcode.');
			};
		} else {
			isValid = false;
			addMessage('Zipcode is limited to 10 characters.');
		};
	};
	
	return isValid;
};

function isValidWizardPage2() { // Home Preferences
	var isValid = true;
	var objControl = null;
	var message = '';
	var strCache = null;
	
//alert('isValidWizardPage2');

	return isValid;
};

function isValidWizardPage3() { // Credit Application
	var isValid = true;
	var objControl = null;
	var message = '';
	var strCache = null;
	
//alert('isValidWizardPage3');

	return isValid;
};

function isValidWizardPage4() { // Community Application
	var isValid = true;
	var objControl = null;
	var message = '';
	var strCache = null;
	
//alert('isValidWizardPage4');

	return isValid;
};

function isValidWizardPage5() { // All
	var isValid = true;
	var objControl = null;
	var message = '';
	var strCache = null;
	
//alert('isValidWizardPage5');

	return isValid;
};

function addMessage(str) {
	if (message != '') {
		message += '\n';	
	};
	message += str;
};
