var baseClientID = 'mainMaster_cphMain_';

function validateForm() {
	var valid = DDM.Validation;

	if ($(baseClientID + "txtAddress1")) {
		if ($(baseClientID + "txtAddress1").value == "" && $(baseClientID + "txtAddress2").value != "") {
			$(baseClientID + "txtAddress1").value = $(baseClientID + "txtAddress2").value;
			$(baseClientID + "txtAddress2").value = "";
		}
	}

	var FormElements = new Array();
	FormElements[0] = new valid.formElementsToValidate(baseClientID + 'txtFirstName', "You must enter the first name");
	FormElements[1] = new valid.formElementsToValidate(baseClientID + 'txtLastName', "You must enter the last name");
	FormElements[2] = new valid.formElementsToValidate(baseClientID + 'txtEmailAddr', "You must enter the email address");
	FormElements[3] = new valid.formElementsToValidate(baseClientID + 'txtConfirmEmail', "You must confirm email address");
	if ($(baseClientID + "txtPhoneNumber").value != '') {
		FormElements[4] = new valid.formElementsToValidate(baseClientID + 'ddlPhoneNumberType', "You must select the type of phone number");
		FormElements[5] = new valid.formElementsToValidate(baseClientID + 'txtAddress1', "You must enter an address");
		FormElements[6] = new valid.formElementsToValidate(baseClientID + 'txtCity', "You must enter a city");
		FormElements[7] = new valid.formElementsToValidate(baseClientID + 'ddlState', "You must select a state");
		FormElements[8] = new valid.formElementsToValidate(baseClientID + 'txtPostalCode', "You must enter the Zip Code");
		FormElements[9] = new valid.formElementsToValidate(baseClientID + 'txtUserName', "You must enter a User Name");
		FormElements[10] = new valid.formElementsToValidate(baseClientID + 'txtPassword', "You must enter a Password");
		FormElements[11] = new valid.formElementsToValidate(baseClientID + 'txtConfirmPassword', "You must confirm the Password");
		FormElements[12] = new valid.formElementsToValidate(baseClientID + 'ckReadAgreement', "You must read affiliate agreement");
		FormElements[13] = new valid.formElementsToValidate((baseClientID + 'rblReferral').replace(/_/g, '$'), "You must tell us how you heard about us.");
	} else {
		FormElements[4] = new valid.formElementsToValidate(baseClientID + 'txtAddress1', "You must enter an address");
		FormElements[5] = new valid.formElementsToValidate(baseClientID + 'txtCity', "You must enter a city");
		FormElements[6] = new valid.formElementsToValidate(baseClientID + 'ddlState', "You must select a state");
		FormElements[7] = new valid.formElementsToValidate(baseClientID + 'txtPostalCode', "You must enter the Zip Code");
		FormElements[8] = new valid.formElementsToValidate(baseClientID + 'txtUserName', "You must enter a User Name");
		FormElements[9] = new valid.formElementsToValidate(baseClientID + 'txtPassword', "You must enter a Password");
		FormElements[10] = new valid.formElementsToValidate(baseClientID + 'txtConfirmPassword', "You must confirm the Password");
		FormElements[11] = new valid.formElementsToValidate(baseClientID + 'ckReadAgreement', "You must read affiliate agreement");
		FormElements[12] = new valid.formElementsToValidate((baseClientID + 'rblReferral').replace(/_/g, '$'), "You must tell us how you heard about us.");
	}
	var msg = valid.submitValidation(FormElements);

	var emailAddr = $(baseClientID + "txtEmailAddr").value;
	var emailAddrConfirm = $(baseClientID + "txtConfirmEmail").value;
	if (emailAddr != '' && emailAddrConfirm != '') {
		if (emailAddr.toLowerCase() != emailAddrConfirm.toLowerCase()) {
			msg = valid.getErrorMessageTop(msg);
			msg += "-Please verify your email addresses are the same.\n";
			valid.setErrorColor($(baseClientID + "txtEmailAddr"));
			valid.setErrorColor($(baseClientID + "txtConfirmEmail"))
		} else {
			valid.resetColor($(baseClientID + "txtEmailAddr"));
			valid.resetColor($(baseClientID + "txtConfirmEmail"));
		}
	}

	var password = $(baseClientID + "txtPassword").value;
	var passwordConfirm = $(baseClientID + "txtConfirmPassword").value;
	if (password != '' && passwordConfirm != '') {
		if (password.toLowerCase() != passwordConfirm.toLowerCase()) {
			msg = valid.getErrorMessageTop(msg);
			msg += "-Please verify Passwords are the same.\n";
			valid.setErrorColor($(baseClientID + "txtPassword"));
			valid.setErrorColor($(baseClientID + "txtConfirmPassword"))
		} else {
			valid.resetColor($(baseClientID + "txtEmailAddr"));
			valid.resetColor($(baseClientID + "txtConfirmEmail"));
		}
	}

	if (msg != "") {
		alert(msg);
		valid.setFocusFirstField();
		return false;
	} else {
		return true;
	}
}

/*******************
add onLoad events
*******************/
Event.observe(window, 'load', function() {if (window.setFocus) window.setFocus();});

function setFocus() {
	if (document.forms.length > 0) {
		if ($(baseClientID + "txtFirstName") != null) {
			$(baseClientID + "txtFirstName").focus();
		}
	}
}

