var baseClientID = 'mainMaster_cphMain_';

function validateForm() {
	var valid = DDM.Validation;
	
	if ($(baseClientID + "CompanyAddress1")) {
		if ($(baseClientID + "CompanyAddress1").value == "" && $(baseClientID + "CompanyAddress2").value != "") {
			$(baseClientID + "CompanyAddress1").value = $(baseClientID + "CompanyAddress2").value;
			$(baseClientID + "CompanyAddress2").value = "";
		}
	}

	var formElements = new Array();
	formElements[0] = new valid.formElementsToValidate(baseClientID + 'txtCompanyName', 'You must enter a Company Name');
	formElements[1] = new valid.formElementsToValidate(baseClientID + 'txtCompanyPhoneNumber', 'You must enter a company phone number');
	formElements[2] = new valid.formElementsToValidate(baseClientID + 'txtCompanyAddress1', 'You must enter a company address');
	formElements[3] = new valid.formElementsToValidate(baseClientID + 'txtCompanyCity', 'You must enter a company city');
	formElements[4] = new valid.formElementsToValidate(baseClientID + 'ddlCompanyState', 'You must enter a company state');
	formElements[5] = new valid.formElementsToValidate(baseClientID + 'txtCompanyPostalCode', 'You must enter a company zip code');
	formElements[6] = new valid.formElementsToValidate(baseClientID + 'txtFirstName', "You must enter the owner's first name");
	formElements[7] = new valid.formElementsToValidate(baseClientID + 'txtLastName', "You must enter the owner's last name");
	formElements[8] = new valid.formElementsToValidate(baseClientID + 'txtEmailAddr', "You must enter the owner's email address");
	formElements[9] = new valid.formElementsToValidate(baseClientID + 'txtConfirmEmail', "You must confirm contact's email address");
	formElements[10] = new valid.formElementsToValidate(baseClientID + 'txtUserName', "You must contact's enter a User Name");
	formElements[11] = new valid.formElementsToValidate(baseClientID + 'txtPassword', "You must contact's enter a Password");
	formElements[12] = new valid.formElementsToValidate(baseClientID + 'txtConfirmPassword', "You must confirm contact's Password");
	formElements[13] = new valid.formElementsToValidate(baseClientID + 'ckReadAgreement', "You must read the agreement");
	formElements[14] = new valid.formElementsToValidate((baseClientID + 'rblReferral').replace(/_/g, '$'), "You must tell us how you heard about us");

	var msg = valid.submitValidation(formElements);

	msg += valid.compareValidator(baseClientID + 'txtEmailAddr', baseClientID + 'txtConfirmEmail', "-Please verify your email addresses are the same.\n");

	msg += valid.compareValidator(baseClientID + 'txtPassword', baseClientID + 'txtConfirmPassword', "-Please verify Passwords are the same.\n");
	
	if (msg != "") {
		alert(msg);
		valid.setFocusFirstField();
		return false;
	} else {
		return true;
	}
}

function openDealerAgreementWindow() {
	openWindowMessages("Dealer Agreement", "../../DealerAgreement.htm", 450, 420)
}

function SendEmailToValidEmail(e, userID, fullName, emailAddress) {
	PageMethod.SendEmailToValidEmail(userID, fullName, emailAddress, SendEmailToValidEmail_callback); // asynchronous call
}

// This method will be called after the method has been executed
// and the result has been sent to the client.
function SendEmailToValidEmail_callback(res) {
	if (res.value) {
		$('panErrorMsg').update("An email has been sent you to validate your email address");
	} else {
		$('panErrorMsg').update("We were not able to send you an email");
	}
}


/******************
add onLoad events
*******************/
Event.observe(window, 'load', function() {if (window.setFocus) window.setFocus();});

function setFocus() {
	if (document.forms.length > 0) {
		if ($(baseClientID + "txtCompanyName") != null) {
			$(baseClientID + "txtCompanyName").focus();
		}
	}
}
