// JavaScript Document
// Myrsol Frontend Validation
// This file will provide an easay way to keep up with all the 
// Myrsol Administration Pages Validation.
// Here is how it works. You simply call this object onload of your form
// and pass in the forms name without the .ctp extension.
// There is a case statement for each page. That is all there is to it.

var gInputElemList = null;
var gElemCounter = [];

	function MyrsolFrontendValidation(aOptions)
	{
		var m_ovalidation = null;
		
		var m_view = aOptions.view || '';
		var m_formid = aOptions.formid || '';
		var m_ajaxlink = aOptions.ajaxlink || '';
		var m_reqPhone = aOptions.reqPhone || 1;
		
		MV_Init();
		//Locate item in array return position -1 means was not found
		///////////////////////////////////////////////////////////////
		this.MV_Init = MV_Init;
		function MV_Init()
		{
			MV_Views(m_view);
		}



		this.MV_Validate = MV_Validate;
		function MV_Validate()
		{
			if (!m_ovalidation) return false;
			return m_ovalidation.validate();
			
		}

		this.MV_Views = MV_Views;
		function MV_Views(m_view)
		{
			if (!m_view) return;
			
			switch (m_view) {

				//..................................................Start Below is an example
				case 'blogs/view':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html", 
						 rules: [ 
								   "length>2,BlogCommentAuthor,Please enter your name.",
								   "required,BlogCommentAuthor,Your name is required.",
								   "required,BlogCommentEmail,Email Address is required. (Will not be published)",
								   "valid_email,BlogCommentEmail,Please enter a valid email address. (Will not be published)", 
								   "required,BlogCommentBody,Please enter a comment.",
								   "length>1,BlogCommentBody,Please enter a comment greater than 1 character :)"
							  ] 
					});
				break;
				//.................................................End 

				//..................................................Start 
				case 'agents/view':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html", 
						 //onCompleteHandler: MV_ValidationComplete,
						 rules: [
								 	"required,AgentFullname,Please enter your fullname.",
									"required,AgentSubject,Please enter a subject for the message.",
									"required,AgentBody,Please enter a message.",
									"required,AgentEmail,Please enter your email address.",
									"valid_email,AgentEmail,Please enter a valid email address."
							  ] 
					});
				break;
				//.................................................End 

				//..................................................Start 
				case 'contact/index':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html",
						 rules: [
								 	"required,ContactFirstname,Please enter your firstname.",
									"required,ContactLastname,Please enter your lastname.",
									"required,ContactEmail,Please enter your email address.",
									"valid_email,ContactEmail,Please enter a valid email address."
							  ] 
					});
				break;
				//.................................................End 

				//..................................................Start 
				case 'subdivision/contact':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 displayType: "display-html",
						 rules: [
								 	"required,ContactFirstname,Please enter your firstname.",
									"required,ContactLastname,Please enter your lastname.",
									"required,ContactEmail,Please enter your email address.",
									"valid_email,ContactEmail,Please enter a valid email address."//,
									//"required,ContactCaptcha,Please enter a valid code."
							  ] 
					});
				break;
				//.................................................End 

				//..................................................Start 
				case '/users/login/':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html", 
						 onCompleteHandler: MV_ValidationComplete,
						 rules: [
								 	"required,UserUsername,Please enter your username.",
									"required,UserPassword,Please enter your password."
							  ] 
					});
				break;
				//.................................................End 

				//..................................................Start 
				case '/users/register/':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html", 
						 onCompleteHandler: MV_ValidationComplete,
						 rules: [
								 	"required,UserFirstname,Please enter your firstname.",
									"required,UserLastname,Please enter your lastname."
							  ] 
					});
				break;
				//.................................................End 

				//..................................................Start 
				case '/listings/tell_a_friend/':
				case 'listings/tellfriend':
				case '/custom_listings/tell_a_friend/':
				case 'custom_listings/tellfriend':

					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html",
						 onCompleteHandler: MV_ValidationComplete,
						 rules: [ 
								   "required,ListingSendername,Please enter your name.",
								   "required,ListingSenderemail,Your Email Address is required.",
								   "valid_email,ListingSenderemail,Please enter a valid email address.",
								   "required,ListingFriendsname,Please enter your friends name.",
								   "required,ListingFriendsemail,Your Friends Email Address is required.",
								   "valid_email,ListingFriendsemail,Please enter a valid email address for your friend"
							  ] 
					});
				break;
				//.................................................End 

				//..................................................Start listings/inquiry
				case 'listings/inquiry':
				case 'custom_listings/inquiry':
					if (m_reqPhone == 1)
					{
						m_ovalidation = new RSV({ 
							 errorFieldClass: "errorField", 
							 formID: m_formid,
							 //displayType: "display-html",
							 onCompleteHandler: MV_ValidationComplete,
							 rules: [ 
									   "required,ContactFirstname,Please enter your firstname.",
									   "length>2,ContactFirstname,Your firstname must be greater than 2 characters",
									   "required,ContactLastname,Please enter your lastname.",
									   "length>2,ContactLastname,Your lastname must be greater than 2 characters",
									   "required,ContactEmail,Your Email Address is required.",
									   "valid_email,ContactEmail,Please enter a valid email address.",
									   "required,ContactPhone,Please enter your phone number. If you wish for an alternate method of contact do so in the comments section.",
									   "function,ginquiry.MM_CustomValidation"
								  ] 
						});
					}
					else
					{
						m_ovalidation = new RSV({ 
							 errorFieldClass: "errorField", 
							 formID: m_formid,
							 //displayType: "display-html",
							 onCompleteHandler: MV_ValidationComplete,
							 rules: [ 
									   "required,ContactFirstname,Please enter your firstname.",
									   "length>2,ContactFirstname,Your firstname must be greater than 2 characters",
									   "required,ContactLastname,Please enter your lastname.",
									   "length>2,ContactLastname,Your lastname must be greater than 2 characters",
									   "required,ContactEmail,Your Email Address is required.",
									   "valid_email,ContactEmail,Please enter a valid email address."
								  ] 
						});
					}
				break;
				//.................................................End listings/inquiry

				//..................................................Start /listings/contact/
				case '/listings/contact/':
				case '/custom_listings/contact/':
					if (m_reqPhone == 1)
					{
						m_ovalidation = new RSV({ 
							 errorFieldClass: "errorField", 
							 formID: m_formid,
							 //displayType: "display-html",
							 onCompleteHandler: MV_ValidationComplete,
							 rules: [ 
									   "required,ContactFirstname,Please enter your firstname.",
									   "length>2,ContactFirstname,Your firstname must be greater than 2 characters",
									   "required,ContactLastname,Please enter your lastname.",
									   "length>2,ContactLastname,Your lastname must be greater than 2 characters",
									   "required,ContactEmail,Your Email Address is required.",
									   "valid_email,ContactEmail,Please enter a valid email address.",
									   "required,ContactPhone,Please enter your phone number. If you wish for an alternate method of contact do so in the comments section.",
									   "function,gMyrsolValidation.MV_CustomValidation"
								  ] 
						});
					}
					else
					{
						m_ovalidation = new RSV({ 
							 errorFieldClass: "errorField", 
							 formID: m_formid,
							 //displayType: "display-html",
							 onCompleteHandler: MV_ValidationComplete,
							 rules: [ 
									   "required,ContactFirstname,Please enter your firstname.",
									   "length>2,ContactFirstname,Your firstname must be greater than 2 characters",
									   "required,ContactLastname,Please enter your lastname.",
									   "length>2,ContactLastname,Your lastname must be greater than 2 characters",
									   "required,ContactEmail,Your Email Address is required.",
									   "valid_email,ContactEmail,Please enter a valid email address.",
									   "function,gMyrsolValidation.MV_CustomValidation"
								  ] 
						});
					}
				break;
				//.................................................End /listings/contact/

				//..................................................Start listings/inquiry
				case 'listings/schedule_showing':
					if (m_reqPhone == 1)
					{
						m_ovalidation = new RSV({ 
							 errorFieldClass: "errorField", 
							 formID: m_formid,
							 //displayType: "display-html",
							 onCompleteHandler: MV_ValidationComplete,
							 rules: [ 
									   "required,ContactFirstname,Please enter your firstname.",
									   "length>2,ContactFirstname,Your firstname must be greater than 2 characters",
									   "required,ContactLastname,Please enter your lastname.",
									   "length>2,ContactLastname,Your lastname must be greater than 2 characters",
									   "required,ContactEmail,Your Email Address is required.",
									   "valid_email,ContactEmail,Please enter a valid email address.",
									   "required,ContactPhone,Please enter your phone number. If you wish for an alternate method of contact do so in the comments section.",
									   "function,gschedule.MM_CustomValidation"
								  ] 
						});
					}
					else
					{
						m_ovalidation = new RSV({ 
							 errorFieldClass: "errorField", 
							 formID: m_formid,
							 //displayType: "display-html",
							 onCompleteHandler: MV_ValidationComplete,
							 rules: [ 
									   "required,ContactFirstname,Please enter your firstname.",
									   "length>2,ContactFirstname,Your firstname must be greater than 2 characters",
									   "required,ContactLastname,Please enter your lastname.",
									   "length>2,ContactLastname,Your lastname must be greater than 2 characters",
									   "required,ContactEmail,Your Email Address is required.",
									   "valid_email,ContactEmail,Please enter a valid email address."
								  ] 
						});
					}
				break;
				//.................................................End listings/inquiry

				case 'mortgage_calculator/listing':
				case '/mortgage_calculator/listing/':
				case 'mortgage_calculator/custom_listing':
				case '/mortgage_calculator/custom_listing/':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html",
						 onCompleteHandler: MV_ValidationComplete
						 /*rules: [ 
								   "required,ContactFirstname,Please enter your firstname.",
								   "length>2,ContactFirstname,Your firstname must be greater than 2 characters",
							  ] */
					});
				break;
				
				//..................................................Start property-search
				case 'property-search':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html",
						 //onCompleteHandler: MV_ValidationComplete,
						 rules: [ 
								   //"function,gMyrsolValidation.MV_CustomValidation"
							  ] 
					});
					
					if (typeof(gInputElemList) == 'undefined' || gInputElemList == null){
						gInputElemList = $(m_formid).getElementsByTagName("input");
						//gInputElemList = $$("input"); 
					}

					MV_ReselectItems(gInputElemList,'data[Listing][city][]');
					//Note these observers will watch cities and subdivision to make sure they are witing range at all times.
					//Event.observe($('cities'), 'change', function(){return gMyrsolValidation.MV_CheckBoxBetween('ListingSearchForm','data[Listing][city][]',0,2,'Please select fewer than [count] cities.',null);});
					//Event.observe($('subdivisions'), 'change', function(){return gMyrsolValidation.MV_CheckBoxBetween('ListingSearchForm','data[Listing][property][]',0,2,'Please select fewer than [count] subdivisions.',null);});

				break;
				//.................................................End property-search
				
				//..................................................Start property-search-schools
				case 'property-search-schools':
				//case 'search_by_subdivision':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html",
						 //onCompleteHandler: MV_ValidationComplete,
						 rules: [ 
								   "function,gMyrsolValidation.MV_CustomValidation"
							  ] 
					});
					
					if (gInputElemList == null){
						gInputElemList = $(m_formid).getElementsByTagName("input");
						//gInputElemList = $$("input"); 
					}

					if ($('data[Listing][school_elem][]')){
						MV_ReselectItems(gInputElemList,'data[Listing][school_elem][]');
					}

					if ($('data[Listing][school_middle][]')){
						MV_ReselectItems(gInputElemList,'data[Listing][school_middle][]');
					}
					
					if ($('data[Listing][school_jr_high][]')){
						MV_ReselectItems(gInputElemList,'data[Listing][school_jr_high][]');
					}
					
					if ($('data[Listing][school_high][]')){
						MV_ReselectItems(gInputElemList,'data[Listing][school_high][]');
					}
					
					if ($('data[Listing][school_district_name][]')){
						MV_ReselectItems(gInputElemList,'data[Listing][school_district_name][]');
					}


				break;
				//.................................................End property-search-schools

				//..................................................Start search_by_area
				case 'search_by_area':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 rules: [ 
								   "function,gMyrsolValidation.MV_CustomValidation"
							  ] 
					});
					
					if (gInputElemList == null){
						gInputElemList = $(m_formid).getElementsByTagName("input"); 
					}
					MV_ReselectItems(gInputElemList,'data[Listing][area][]');
				break;
				//.................................................End search_by_area

				//..................................................Start search_mls
				case 'search_mls':
					m_ovalidation = new RSV({ 
						 errorFieldClass: "errorField", 
						 formID: m_formid,
						 //displayType: "display-html",
						 //onCompleteHandler: MV_ValidationComplete,
						 rules: [ 
								   "required,mlssearch,Please enter an alphanumeric (0-9\\,) string."
							  ] 
					});
				break;
				//.................................................End search_mls

				//..................................................Start 
				//case '':
				//break;
				//.................................................End 
				//MV_CheckBoxNoMoreThan('ListingSearchForm','data[Listing][city][]',5,'Please select fewer than 5 Cities')

			default:
				alert('Function MV_Views:: ['+m_view+'] not set in file Myrsol_Frontend_Validation.js');
			break;
			}
		} // END MV_Views()

		this.MV_CustomValidation = MV_CustomValidation;
		function MV_CustomValidation(){
			if (!m_view) return;

			switch (m_view) {

				//..................................................Start Contact/Inquire
				case 'listings/inquiry':
				case '/listings/contact/':
				case 'listings/schedule_showing':
				case 'custom_listings/inquiry':
				case '/custom_listings/contact/':
					if (m_reqPhone == 1)
					{
						rtnFlag = MV_ValidatePhoneNumber(m_formid,'ContactPhone','Please enter a valid Phone Number.')
						return rtnFlag;
					}
					else 
						return true;
				break;
				//.................................................End Contact/Inquire

				//..................................................Start property-search
				case 'property-search':
					return MV_CheckBoxBetween(m_formid,'data[Listing][city][]',0,15,'Please Select Between 0 and 15 Cities','city_0')
				break;
				//.................................................End property-search

				//..................................................Start property-search
				case 'search_by_area':
					return MV_CheckBoxBetween(m_formid,'data[Listing][area][]',0,15,'Please Select Between 0 and 15 Areas','area_0')
				break;
				//.................................................End property-search

				case 'property-search-schools':
					return true;
				break;

				//..................................................Start property-search
				//case 'search_by_subdivision':
				//	return MV_CheckBoxBetween(m_formid,'data[Listing][property][]',0,15,'Please Select Between 0 and 15 Subdivisions','subdivision_0')
				//break;
				//.................................................End property-search

				//..................................................Start search_mls
				case 'search_mls':
					return MV_CheckCommanListLength($('mlssearch'),8,'mlssearch');
				break;
				//.................................................End search_mls

			default:
				alert('Function MV_CustomValidation:: ['+m_view+'] not set in file Myrsol_Frontend_Validation.js');
			break;
			}			
		}

		this.MV_ValidatePhoneNumber = MV_ValidatePhoneNumber;
		function MV_ValidatePhoneNumber(formid,szLookingForName,szmsg)
		{ 	
			var oEl = $(szLookingForName);
			//Allow empty.
			if (oEl.value == '')
				return true;	
				
			//If they don't have at least x numbers then this is not a valid number.
			numStr = oEl.value.replace(/[^0-9]+/g,'');
			euro = oEl.value.substring(0,1);
			if (euro == '+')
			{
				if (numStr.length < 15)
					return  [[oEl, 'Phone number not complete.']];
			}
			else
			{
				if (numStr.length < 7)
					return [[oEl, 'Phone number not complete.']];
			}
			
			var re = new RegExp(/^(([\+]{1}([0-9]){1,3}[- \.]{0,1})){0,1}[\\(\]]{0,1}([0-9]){3}[\\)\]]{0,1}[ -\.]?([^0-1]){1}([0-9]){2}[ ]?[-\.]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$/i);
	
			var regCheck = re.exec(oEl.value);
			if (regCheck != null)
				return true;
			else
				return [[oEl, szmsg]];
		}

		this.MV_ValidationComplete=MV_ValidationComplete;
		function MV_ValidationComplete()
		{
			if (!m_view) return;

			switch (m_view) {

				//..................................................Start listings/tellfriend 
				case '/users/login/':
				
				 new Ajax.Request(m_ajaxlink, {
					method: "post", 
					parameters: Form.serialize($('UserLoginForm')),
						onLoading: function() {
							//$('spinner').show();
							gInfoBoxes.MB_Open('errmsg1','Processing Login...');
							glogin.MM_Close();
						}, 
						onSuccess: function(oReq, oJSN) {
							if (oReq){
								var oData = oReq.responseText.evalJSON(true);
								gInfoBoxes.MB_Open('msgbox_shortime',oData.msg);
								if (oData.status == '1')
								{
									glogin.MM_Close();
								}
								else
								{
									setTimeout('glogin.MM_Open();',2000);
								}
							}
							//setTimeout('parent.Control.Modal.close();',2000);
							//$('status').update('<p class="success">An email has been sent to your friend.</p>');
						},
						onComplete: function() {
							gInfoBoxes.MB_Close('errmsg1');
							Field.clear("UserUsername");
							Field.clear("UserPassword");
							Field.clear("UserCookie");
						},
						onFailure: function() {
							gInfoBoxes.MB_Close('errmsg1');
							gInfoBoxes.MB_Open('msgbox_shortime','Unable to Login. Please Try Again.');
							setTimeout('glogin.MM_Open();',2000);
							//$('status').update('<p class="error">Error sending email to your friend. Please try again.</p>');
						}
					});
				break;
				//.................................................End users/login

				//..................................................Start listings/tellfriend 
				case '/users/register/':
				
				 new Ajax.Request(m_ajaxlink, {
					method: "post", 
					parameters: Form.serialize($('UserRegisterForm')),
						onLoading: function() {
							//$('spinner').show();
							gInfoBoxes.MB_Open('errmsg1','Processing Registration...');
							gregister.MM_Close();
						}, 
						onSuccess: function(oReq, oJSN) {
							if (oReq){
								var oData = oReq.responseText.evalJSON(true);
								gInfoBoxes.MB_Open('msgbox_shortime',oData.msg);
								if (oData.status == '1')
								{
									gregister.MM_Close();
								}
								else
								{
									setTimeout('gregister.MM_Open();',2000);
								}
							}
							//setTimeout('parent.Control.Modal.close();',2000);
							//$('status').update('<p class="success">An email has been sent to your friend.</p>');
						},
						onComplete: function() {
							gInfoBoxes.MB_Close('errmsg1');
							Field.clear("UserUsername");
							Field.clear("UserPassword");
							Field.clear("UserCookie");
						},
						onFailure: function() {
							gInfoBoxes.MB_Close('errmsg1');
							gInfoBoxes.MB_Open('msgbox_shortime','Unable to Register. Please Try Again.');
							setTimeout('gregister.MM_Open();',2000);
							//$('status').update('<p class="error">Error sending email to your friend. Please try again.</p>');
						}
					});
				break;
				//.................................................End users/login

				//..................................................Start listings/tellfriend 
				case '/listings/tell_a_friend/':
				case 'listings/tellfriend':
				case '/custom_listings/tell_a_friend/':
				case 'custom_listings/tellfriend':

				 new Ajax.Request(m_ajaxlink, {
					method: "post", 
					parameters: Form.serialize($('TellAFriendForm')),
						onLoading: function() {
							//$('spinner').show();
							gInfoBoxes.MB_Open('errmsg1','Processing Tell-A-Friend...');
							gtellafriend.MM_Close();
						}, 
						onSuccess: function(oReq, oJSN) {
							if (oReq){
								var oData = oReq.responseText.evalJSON(true);
								gInfoBoxes.MB_Open('msgbox_shortime',oData.msg);
								if (oData.status == '1')
								{
									gtellafriend.MM_Close();
								}
								else
								{
									setTimeout('gtellafriend.MM_Open();',2000);
								}
							}
							//setTimeout('parent.Control.Modal.close();',2000);
							//$('status').update('<p class="success">An email has been sent to your friend.</p>');
						},
						onComplete: function() {
							gInfoBoxes.MB_Close('errmsg1');
							Field.clear("ListingFriendsname");
							Field.clear("ListingFriendsemail");
							Field.clear("ListingBody");
							//$('spinner').hide();
						},
						onFailure: function() {
							gInfoBoxes.MB_Close('errmsg1');
							gInfoBoxes.MB_Open('msgbox_shortime','Unable to send Tell-A-Friend.');
							setTimeout('gtellafriend.MM_Open();',2000);
							//$('status').update('<p class="error">Error sending email to your friend. Please try again.</p>');
						}
					});
				break;
				//.................................................End listings/tellfriend 

				//..................................................Start 
				case 'listings/inquiry':
				case '/listings/contact/':
				case 'custom_listings/inquiry':
				case '/custom_listings/contact/':
					 new Ajax.Request(m_ajaxlink, {
						method: "post", 
						parameters: Form.serialize($('MoreInfoForm')),
							onLoading: function() {
								//$('spinner').show();
								gInfoBoxes.MB_Open('errmsg1','Processing Inquiry...');
								ginquiry.MM_Close();
							},
							onSuccess: function(oReq, oJSN) {
								if (oReq){
									var oData = oReq.responseText.evalJSON(true);
									gInfoBoxes.MB_Open('msgbox_shortime',oData.msg);
									if (oData.status == '1')
									{
										ginquiry.MM_Close();
									}
									else
									{
										setTimeout('ginquiry.MM_Open();',2000);
									}
								}
							},
							onComplete: function() {
								gInfoBoxes.MB_Close('errmsg1');

								Field.clear("ContactPhone");
								Field.clear("ContactBody");
								//$('spinner').hide();
							},
						onFailure: function() {
							gInfoBoxes.MB_Close('errmsg1');
							gInfoBoxes.MB_Open('msgbox_shortime','Unable to send Inquiry.');
							setTimeout('ginquiry.MM_Open();',2000);

							//$('status').update('<p class="error">Error sending email to your friend. Please try again.</p>');
						}							
						});
				break;
				//.................................................End 


				//..................................................Start 
				case 'listings/schedule_showing':
				case '/listings/schedule_showing/':
					 new Ajax.Request(m_ajaxlink, {
						method: "post", 
						parameters: Form.serialize($('ScheduleShowingForm')),
							onLoading: function() {
								gInfoBoxes.MB_Open('errmsg1','Processing Schedule a Showing Inquiry...');
								ginquiry.MM_Close();
							},
							onSuccess: function(oReq, oJSN) {
								if (oReq){
									var oData = oReq.responseText.evalJSON(true);
									gInfoBoxes.MB_Open('msgbox_shortime',oData.msg);
									if (oData.status == '1')
									{
										ginquiry.MM_Close();
									}
									else
									{
										setTimeout('ginquiry.MM_Open();',2000);
									}
								}
							},
							onComplete: function() {
								gInfoBoxes.MB_Close('errmsg1');

								Field.clear("ContactPhone");
								Field.clear("ContactBody");
								Field.clear("ContactDate1");
								Field.clear("ContactDate2");
								Field.clear("ContactDate3");
								Field.clear("ContactDate4");
							},
						onFailure: function() {
							gInfoBoxes.MB_Close('errmsg1');
							gInfoBoxes.MB_Open('msgbox_shortime','Unable to send Schedule a Showing Inquiry.');
							setTimeout('ginquiry.MM_Open();',2000);
						}							
						});
				break;
				//.................................................End 

				//..................................................Start 
				case 'mortgage_calculator/listing':
				case '/mortgage_calculator/listing/':
				case 'mortgage_calculator/custom_listing':
				case '/mortgage_calculator/custom_listing/':

					 new Ajax.Request(m_ajaxlink, {
						method: "post", 
						parameters: Form.serialize($('MortgageCalculatorForm')),
							onLoading: function() {
								$('spinner').show();
							},
							onSuccess: function() {
							},
							onComplete: function() {
								$('spinner').hide();
							}
						});
				break;
				//.................................................End 

			default:
				alert('Function MV_ValidationComplete:: ['+m_view+'] not set in file Myrsol_Frontend_Validation.js');
			break;
			}
		} //End MV_ValidationComplete()

		this.MV_ValidationSubmit=MV_ValidationSubmit;
		function MV_ValidationSubmit()
		{
			if (!m_view) return;

			switch (m_view) {
				//..................................................Start 
				case 'agents/view':
					var bvalidate = this.MV_Validate();
					return bvalidate;
				break;
				//.................................................End 

				//..................................................Start 
				case 'subdivision/contact':
					var bvalidate = this.MV_Validate();
					return bvalidate;
				break;
				//.................................................End 

			//break;
			}
		} //End MV_ValidationComplete()

		this.MV_CheckBoxBetween = MV_CheckBoxBetween;
		function MV_CheckBoxBetween(formid,szLookingForName,imin,imax,szmsg,szmsgidtoshowat,oElm)
		{
			if (!oElm) return;
			if (!oElm.checked)
			{
				MV_CheckBoxCount(oElm);
				return true;
			}

			var ihowmany = MV_GetCheckBoxCount(oElm);
		
			if (ihowmany >= imin && ihowmany < imax){
				MV_CheckBoxCount(oElm);
				return true;
			}
			else
			{
				if (szmsgidtoshowat == null){
					if (typeof(gInfoBoxes) != 'undefined')
					{
					szmsg = szmsg.replace(/\[count\]/gi,ihowmany);
					gInfoBoxes.MB_Add({fid:'validation_public',targetId:'center',openOnCreate:true,content:szmsg},{closeOnClick:true,width:250});
					return false;
					}
				}
				else
					return [[$(szmsgidtoshowat), szmsg]];
			}
			
		}

		this.MV_CheckBoxNr = MV_CheckBoxNr;
		function MV_CheckBoxNr(formid,szLookingForName,needed,szmsg,szmsgidtoshowat,oElm)
		{
			var ihowmany = MV_CheckBoxCount(oElm);
			
			if (ihowmany == needed)
				return true;
			else
			{
				if (szmsgidtoshowat == null){
					if (typeof(gInfoBoxes) != 'undefined')
					{
					szmsg = szmsg.replace(/\[count\]/gi,ihowmany);
					gInfoBoxes.MB_Add({fid:'validation_public',targetId:'center',openOnCreate:true,content:szmsg},{closeOnClick:true,width:250});	
					return false;
					}
				}
				else
					return [[$(szmsgidtoshowat), szmsg]];
			}
			
		}

		this.MV_CheckSetTo = MV_CheckSetTo;
		function MV_CheckSetTo(oSetArray,formid,szLookingForName,bchecked,ilimit)
		{
			cursor = 'wait';
			var ihowmany = 0;
			var oform = $(formid);
			if (oform)
			{
				for (var i = 0; i <oSetArray.length; i++)
				{
					 if (oSetArray[i].type == 'checkbox' && oSetArray[i].name == szLookingForName)
					 {
						el = oSetArray[i];
						if (el.checked != bchecked)
						{
							el.checked = bchecked;
							MV_CheckBoxCount(el)
							if (ilimit > 0)
								ihowmany++;
							if (ihowmany >= ilimit && ilimit != 0){
								return true;
							}
						}
					 }
				}
			
			}
			return true;
		}

		this.MV_UnSelAll = MV_UnSelAll;
		function MV_UnSelAll(formid,szLookingForName,ilimit)
		{
			if (gInputElemList == null){
				gInputElemList = $(formid).getElementsByTagName("input");
				//gInputElemList = $$("input"); 
			}

			//Clear all
			MV_CheckSetTo(gInputElemList,formid,szLookingForName,false,ilimit);
			
			return false;
		}

		this.MV_CheckSelAll = MV_CheckSelAll;
		function MV_CheckSelAll(formid,szLookingForName,ilimit)
		{
			document.body.style.cursor = "wait";

			if (gInputElemList == null){
				gInputElemList = $(formid).getElementsByTagName("input");
				//gInputElemList = $$("input"); 
			}
			var ihowmany = MV_GetCheckBoxCount(szLookingForName);
			
			//Clear all
			if (ihowmany > 0)
				MV_CheckSetTo(gInputElemList,formid,szLookingForName,false,ihowmany);
			//Check first ilimit
			MV_CheckSetTo(gInputElemList,formid,szLookingForName,true,ilimit);

			document.body.style.cursor = "default";

			return false;
		}

		this.MV_ReselectItems = MV_ReselectItems;
		function MV_ReselectItems(oSetArray,szLookingForName)
		{
			if (oSetArray == null) return false;
			//gInputElemList
			cursor = 'wait';
			var ihowmany = 0;
			for (var i = 0; i <oSetArray.length; i++)
			{
				 if (oSetArray[i].type == 'checkbox' && oSetArray[i].name == szLookingForName)
				 {
					el = oSetArray[i];
					if (typeof(gElemCounter[szLookingForName]) == 'undefined')
						gElemCounter[szLookingForName] = 0;
					
					if (el.checked)
						gElemCounter[szLookingForName]++;
				 }
			}
			return true;
		}

		this.MV_GetCheckBoxCount = MV_GetCheckBoxCount;
		function MV_GetCheckBoxCount(oElm)
		{
			if (oElm.name)
				var szLookingForName = oElm.name;
			else	
				var szLookingForName = oElm;
				
			if (typeof(gElemCounter[szLookingForName]) == 'undefined' || gElemCounter[szLookingForName] < 0)
				return gElemCounter[szLookingForName] = 0;
			else
				return gElemCounter[szLookingForName];
		}

		this.MV_CheckBoxCount = MV_CheckBoxCount;
		function MV_CheckBoxCount(oElm)
		{
			
			var szLookingForName = oElm.name;
			if (typeof(gElemCounter[szLookingForName]) == 'undefined')
				gElemCounter[szLookingForName] = 0;
			
			if (oElm.checked)
				gElemCounter[szLookingForName]++;
			else
				gElemCounter[szLookingForName]--;
				
			return gElemCounter[szLookingForName];

				
			
/*			var oform = $(formid);
			if (oform)
			{
				if (gElemList['input'] == 'undefined')
				    gElemList['input'] = oform.getElementsByTagName("input"); 

				alist = gElemList['input'];
				//var alist = $$("input");
				for (var i = 0; i <alist.length; i++)
				{
					 if (alist[i].type == 'checkbox' && alist[i].name == szLookingForName)
					 {
						el = alist[i];
						if (el.checked)
							ihowmany++;
					 }
				}
			
			}
*/			
		}


		this.MV_ValidateFixString = MV_ValidateFixString;
		function MV_ValidateFixString(oObject,valid,szmsgidtoshowat)
		{
			if (oObject.value == '')
				return true;

			var invalid="";
			var ok = "yes";
			for (var i=0; i<oObject.value.length; i++)
			{
				var temp = "" + oObject.value.substring(i, i+1);
				if (valid.indexOf(temp) == "-1")
				{
					ok = "no";
					invalid = invalid + oObject.value.substring(i, i+1);
				}
			}

			if (ok == "no")
			{
				var szreturnmsg = "[ " + invalid + " ] is not a valid character. \n\nValid Characters:\n["+valid+"]";
				var szNewStr="";
				szNewStr = oObject.value;
				szNewStr = MV_RemoveInvalidChars(szNewStr,valid);
				oObject.value = szNewStr;
				if (szmsgidtoshowat == null){
					if (typeof(gInfoBoxes) != 'undefined')
					{
					gInfoBoxes.MB_Add({fid:'validation_public',targetId:'center',openOnCreate:true,content:szmsg},{closeOnClick:true,width:250});	
					}
				return false;
				}
				else
					return [[$(szmsgidtoshowat), szreturnmsg]];
		  }
			return true;
		}

		//Given a comma list lets make sure none of the values are longer than
		//a given length.
		this.MV_CheckCommanListLength = MV_CheckCommanListLength;
		function MV_CheckCommanListLength(oObject,LenMax,szmsgidtoshowat)
		{
				var szsplit = oObject.value.split(",");
				var lvMsg = "";
				var ok = "yes";
				
				for (var iIndex = 0 ;iIndex < szsplit.length; iIndex ++){
					if (szsplit[iIndex].length > LenMax){
						lvMsg = lvMsg + "	Length Invalid for [" + szsplit[iIndex] + "]\n";
						ok = "no";
					}
				}
	
				if (ok == "no"){
					var szreturnmsg = 'The following character(s) are not allowed!\n' + lvMsg;
					oObject.focus();
					oObject.select();
					if (szmsgidtoshowat == null){
						if (typeof(gInfoBoxes) != 'undefined')
						{
						gInfoBoxes.MB_Add({fid:'validation_public',targetId:'center',openOnCreate:true,content:szmsg},{closeOnClick:true,width:250});	
						}
					return false;
					}
					else
						return [[$(szmsgidtoshowat), szreturnmsg]];
			  }
	
			return true;
	
		}

		//Given a string and the valid characters this function will remove
		//any invalid characters not in the valid list
		this.MV_RemoveInvalidChars = MV_RemoveInvalidChars;
		function MV_RemoveInvalidChars(szstr,valid)
		{
		if (szstr == '') return "";
		var vchr="";
		var badchr="";
		for (var i=0; i < szstr.length; i++)
		{
			var temp = "" + szstr.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
				badchr = badchr + szstr.substring(i, i+1);
			else
			vchr = vchr + szstr.substring(i, i+1);
		}
		
		return vchr;
		}	
	

	} //Close Myrsol Validation Class

