// JavaScript Document

MB_Campaign = new Class({
												
	// INTITALIZE OBJECT
	initialize: function(options){
		
		this.blockedEmails    = new Array('championsg.com')
		this.postalExpression = /^[a-z](-?([0-9]|[a-z])+)*([0-9]|[a-z])+$/;
		this.emailExpression = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
		// ' 

		// GRABS DOM ITEMS AND SET PROPERTIES -- <form>, <select country>, <select state_au>, <select state_us>, <select state_ca>, <select postal_code>
		this.form 					= $$('form[name=Upd_Doc_Customization]');				
		this.required 			= '';
		
		this.valid  = 0;
		
		// DETECT IF FORM EXISTS
		if(this.form.length > 0) {
			this.form = this.form[0];
		} else {
			return;
		}		
		
		
		// DETECT FORM TYPE FROM HIDDEN FIELD		
		if($('mb_form_type') != null)  {
			this.formType       = $('mb_form_type').value
		} else {
			this.formType       = 'short-form';
		}
		
		// GET DYNAMIC FIELDS FOR MANIPULATION	
		this.state					= $$('input[name=State]');
		if(this.state.length > 0) {
			this.state					= this.state[0];
		} else {
			return;
		}
		
		this.country 				= $$('select[name=Country]');
		if(this.country.length > 0) {
			this.country 				= this.country[0];		
			this.country_tr 		= $(this.country).getParent('tr');
			this.country_tr_tds = this.country_tr.getElements('td[class=PropertySheetLeftColDesc]');
		} else {
			return;
		}
		
		//if(this.country) { this.country.options[this.country.selectedIndex].value		}
		
		this.state_au 			= $$('select[name=AU_State]');
		if(this.state_au.length > 0) {
			this.state_au 			= this.state_au[0];
			this.state_au_tr 		= $(this.state_au).getParent('tr');
			this.state_au_tds 	= this.state_au_tr.getElements('td[class=PropertySheetLeftColDesc]');
			this.state_au_help 	= this.state_au_tr.getElements('.HelpText');
			if(this.state_au_help.length > 0) { this.state_au_help = this.state_au_help[0]; }
		}
		
		this.state_us 			= $$('select[name=US_State]');
		if(this.state_us.length > 0) {
			this.state_us 			= this.state_us[0];
			this.state_us_tr 		= $(this.state_us).getParent('tr');
			this.state_us_tds 	= this.state_us_tr.getElements('td[class=PropertySheetLeftColDesc]');
			this.state_us_help 	= this.state_us_tr.getElements('.HelpText');
			if(this.state_us_help.length > 0) { this.state_us_help = this.state_us_help[0]; }
		} 

		this.state_ca 			= $$('select[name=CA_Province]');
		if(this.state_ca.length > 0) {
			this.state_ca 			= this.state_ca[0];
			this.state_ca_tr 		= $(this.state_ca).getParent('tr');
			this.state_ca_tds 	= this.state_ca_tr.getElements('td[class=PropertySheetLeftColDesc]');
			this.state_us_help 	= this.state_ca_tr.getElements('.HelpText');
			if(this.state_us_help.length > 0) { this.state_us_help = this.state_us_help[0]; }		
		}
		
		this.submitButton 	= $$('input[name=Button]');
		this.submitButton   = this.submitButton[1];		
		
		// GET SUBMIT TO OVERIDE MB FUNCTIONS
		this.submitButton.onclick = "";		
		this.submitButton.onclick = this.validationLong.pass(this.submitButton, this);
				
		// CONDITION FOR LONG FORM VS SHORT FORM
		if(this.formType == "long") {
			
			this.country.addEvent( 'change', function(event){ this.setFieldsLong(this.country.options[this.country.selectedIndex].value); }.bind(this) );
			this.state_au.addEvent('change', function(event){ this.setState(this.state_au.options[this.state_au.selectedIndex].value); }.bind(this) );
			this.state_ca.addEvent('change', function(event){ this.setState(this.state_ca.options[this.state_ca.selectedIndex].value); }.bind(this) );
			this.state_us.addEvent('change', function(event){ this.setState(this.state_us.options[this.state_us.selectedIndex].value); }.bind(this) );
					
			this.setFieldsLong(this.country.options[this.country.selectedIndex].value);
			
			this.setRequiredLong();
			this.getRequired();
		}	
		
		if(this.formType == "short-2-column") {

			this.country.addEvent( 'change', function(event){ this.setFieldsLong(this.country.options[this.country.selectedIndex].value); }.bind(this) );
			this.state_au.addEvent('change', function(event){ this.setState(this.state_au.options[this.state_au.selectedIndex].value); }.bind(this) );
			this.state_ca.addEvent('change', function(event){ this.setState(this.state_ca.options[this.state_ca.selectedIndex].value); }.bind(this) );
			this.state_us.addEvent('change', function(event){ this.setState(this.state_us.options[this.state_us.selectedIndex].value); }.bind(this) );
					
			this.setFieldsLong(this.country.options[this.country.selectedIndex].value);
			
			this.setRequiredLong();
			this.getRequired();
			
			var removeBr = $('Document_Table').getElements("br");
			for (i=0;i<removeBr.length;i++) {	removeBr[i].destroy(); }
			
		}			
		
		if(this.formType == "long-2-column") {
			
			this.country.addEvent( 'change', function(event){ this.setFieldsLong(this.country.options[this.country.selectedIndex].value); }.bind(this) );
			
			this.state_au.addEvent('change', function(event){ this.setState(this.state_au.options[this.state_au.selectedIndex].value); }.bind(this) );
			this.state_ca.addEvent('change', function(event){ this.setState(this.state_ca.options[this.state_ca.selectedIndex].value); }.bind(this) );
			this.state_us.addEvent('change', function(event){ this.setState(this.state_us.options[this.state_us.selectedIndex].value); }.bind(this) );
					
			this.setFieldsLong(this.country.options[this.country.selectedIndex].value);
			
			this.setRequiredLong();
			this.getRequired();
			
			var removeBr = $('Document_Table').getElements("br");
			for (i=0;i<removeBr.length;i++) {	removeBr[i].destroy(); }
			
		}	
	
	},
	
	getRequired: function(value){
		
		// GRAB ALL TD WITH CLASS OF PropertySheetLeftColDesc && CONTIANS * FOR REQUIRED
		this.required	= $$('td.PropertySheetLeftColDesc:contains("*")');
		
		// POPULATE REQUIRED COLLECTION WITH PARENT TR PROPERTY & FIELD PROPERTY 
		// this.required[].parentTr = parent <tr>
		// this.required[].field = <input> || <select>
		var requiredLength = this.required.length;
		
		for (i=0;i < requiredLength; i++) {
			this.required[i].parentTr 				= $(this.required[i]).getParent('tr');
			this.required[i].field						= $(this.required[i].parentTr).getElements('input');
			
			if(this.required[i].field == "") 	{	
				this.required[i].field	= $(this.required[i].parentTr).getElements('select'); 
			}
							
			if(this.required[i].field.length > 0) { this.required[i].field = this.required[i].field[0];}
			
			this.required[i].help = this.required[i].parentTr.getElements('.HelpText');
			if(this.required[i].help.length > 0) 		{ this.required[i].help = this.required[i].help[0]; }
		}

},
	
	// SETS THE VISIBLE CONFIGURATION BASED ON COUNTRY
	setFieldsLong: function(value){
				
		if( value.toLowerCase() == '') {
			this.state_us_tr.addClass('hide');			
			this.state_ca_tr.addClass('hide');
			this.state_au_tr.addClass('hide');
			this.state.set('value', '');
		} else if( value.toLowerCase() == 'us') {
			if(this.state_us_tr.hasClass('hide')) { this.state_us_tr.removeClass('hide'); }
			this.state_ca_tr.addClass('hide');
			this.state_au_tr.addClass('hide');
			this.state.set('value', '');
		}	else if( value.toLowerCase() == 'canada') {
			if( this.state_ca_tr.hasClass('hide') ) { this.state_ca_tr.removeClass('hide'); }
			this.state_us_tr.addClass('hide');
			this.state_au_tr.addClass('hide');
			this.state.set('value', '');
		}	else if( value.toLowerCase() == 'australia') {
			if( this.state_au_tr.hasClass('hide') ) { this.state_au_tr.removeClass('hide'); }
			this.state_us_tr.addClass('hide');
			this.state_ca_tr.addClass('hide');	
			this.state.set('value', '');
		} else {
			this.state_us_tr.addClass('hide');			
			this.state_ca_tr.addClass('hide');
			this.state_au_tr.addClass('hide');
			this.state.set('value', 'NULL');
		}
	},

	// SPOOF MB REQUIRED ON DYNAMIC FIELDS
	setRequiredLong: function(){
		this.state_au_tds[0].set('html', this.state_au_tds[0].get('text').trim() + '<font color="#FF0000">* </font><small>&nbsp;</small>');
		this.state_us_tds[0].set('html', this.state_us_tds[0].get('text').trim() + '<font color="#FF0000">* </font><small>&nbsp;</small>');
		this.state_ca_tds[0].set('html', this.state_ca_tds[0].get('text').trim() + '<font color="#FF0000">* </font><small>&nbsp;</small>');
	},
	
	setState: function(value){	
		this.state.set('value', value); 
	},

	// VALIDATE THE LONG FORM
	validationLong: function(obj){		
	
		// VARIABLE TO CHECK AGIANST FOR VALIDATION	
		this.valid = 0;
		var valid = 0;
		var fieldObj;
		var value;
			
		// LOOP THROUGH REQUIRED ARRAY/COLLECTION TO VALIDATE & SET CLASS = ERROR ON EACH REQIURED FIELD 
		var requiredLength = this.required.length;
				
		for (i=0;i < requiredLength; i++) {
			
			fieldObj 	= $(this.required[i].field);
			trObj 		= $(this.required[i].parentTr);
			helpObj 	= $(this.required[i].help);
			tdObj     = $(this.required[i]);
						
			if(fieldObj.get('tag').toLowerCase() == "input") { 
				
				if(fieldObj.get('value').length <= 0 && !trObj.hasClass('hide')) {
					this.setError(fieldObj, helpObj, tdObj.get('text').slice(0,(tdObj.get('text').length-3)) ,'input');
					continue;
				} else {
					
					//CHECK <INPUT NAME="EMIAL"> IS VALID AGAINST REG EX
					if(fieldObj.get('name').toLowerCase() == "email") {

						if(!fieldObj.get('value').toLowerCase().match(this.emailExpression)){ 
							this.setError(fieldObj, helpObj, tdObj.get('text').slice(0,(tdObj.get('text').length-3)) ,'valid');
							continue;
						}	else {
							// VALIDATE EMAIL AGIANST BLOCKED ARRAY
							var emailExt = fieldObj.get('value').split('@');
							emailExt = emailExt[1].toLowerCase();
							if(this.blockedEmails.indexOf(emailExt) > -1)  {
								this.setError(fieldObj, helpObj, tdObj.get('text').slice(0,(tdObj.get('text').length-3)) ,'valid');
								continue;
							} else {
								this.setValid(fieldObj, helpObj);
								continue;
							}
						}		
						
					}
					
					this.setValid(fieldObj, helpObj);
					continue;
				}		
			}
			
						
			if(fieldObj.get('tag').toLowerCase() == "select") { 
			
				
				if(!trObj.hasClass('hide')) {						
						
						//CHECK IF SELECT IS BLANK 
						if(fieldObj.options[fieldObj.selectedIndex].value == ''){ 
							this.setError(fieldObj, helpObj, tdObj.get('text').slice(0,(tdObj.get('text').length-3)) ,'select');
							continue;
						}	else {
							this.setValid(fieldObj, helpObj);
							continue;
						}	
						
						//CHECK COUNTRY CONDITIONS				
						if(fieldObj.get('name') == 'country' ){ 
							
							if(fieldObj.options[fieldObj.selectedIndex].value.toLowerCase() == 'us' && this.state_us.options[this.state_us.selectedIndex].value == '') { 
								if(this.state_us_tr.hasClass('hide')) {		this.field.state_us_tr.removeClass('hide')		}
								this.setError(this.state_us, this.state_us_help[0], this.state_us_tds[0].get(text).slice(0,(this.state_ca_tds[0].get('text').length-3)) ,'select');
								continue;
							} else {
								this.setValid(fieldObj, helpObj, 'select');
								continue;
							}	
							
							if(fieldObj.options[fieldObj.selectedIndex].value.toLowerCase() == 'canada' && this.state_ca.options[this.state_ca.selectedIndex].value == '') { 
								if(this.state_ca_tr.hasClass('hide')) {	this.field.state_ca_tr.removeClass('hide') }
								this.setError(this.state_ca, this.state_ca_help[0], this.state_ca_tds[0].get(text).slice(0,(this.state_ca_tds[0].get('text').length-3)) ,'select');
								continue;
							} else {
								this.setValid(fieldObj, helpObj, 'select');
								continue;
							}  
							
							if(fieldObj.options[fieldObj.selectedIndex].value.toLowerCase() == 'australia'  && this.state_au.options[this.state_au.selectedIndex].value == '') { 	
								if(this.state_au_tr.hasClass('hide')) {	this.state_au_tr.removeClass('hide')	}
								this.setError(this.state_au, this.state_au_help[0], this.state_au_tds[0].get(text).slice(0,(this.state_ca_tds[0].get('text').length-3)) ,'select');
								continue; 
							} else {
								this.setValid(fieldObj, helpObj, 'select');
								continue;
							}
						} else {
								this.setValid(fieldObj, helpObj, 'select');
								continue;
						}					
				} else {
					this.setValid(fieldObj, helpObj, 'select');
					continue; 
				}
			}
		}
		
		// FINAL LOOP TO CHECK IF EACH IS VALID...  IF NOT RETURN FORM NULL... ELSE FIRE MB setAction();
		for (i=0;i < this.required.length; i++) {			
			fieldObj 	= $(this.required[i].field);
			if(fieldObj.get('valid') == "1") {	this.valid = this.valid.toInt() + fieldObj.get('valid').toInt(); }
		}	
		if(this.valid == this.required.length) { 			
			setAction('Save'); 
		}
	},
	
	setError: function(fieldObj, helpObj, text, type){	
		
		if(type == 'select') {

			if($('helpText-select') != null) { 
				helpObj.set('html', $('helpText-select').get('value') + text);
				helpObj.addClass('error');
				helpObj.removeClass('hide');
				fieldObj.set('valid', '0');
				return
			}
		} 
		
		if(type == 'valid') {
			if($('helpText-input-valid') != null) { 
				helpObj.set('html', $('helpText-input-valid').get('value') + text);
				helpObj.addClass('error');
				fieldObj.addClass('error');
				helpObj.removeClass('hide');
				fieldObj.set('valid', '0');
				return
			}
		} 

		if(type = 'input') {
			if($('helpText-input') != null) { 
				helpObj.set('html', $('helpText-input').get('value') + text);
				helpObj.addClass('error');
				fieldObj.addClass('error');
				helpObj.removeClass('hide');
				fieldObj.set('valid', '0');
				return
			}
			
		}
	},
	
	setValid: function(fieldObj, helpObj){	
		helpObj.set('html', '&nbsp;'); 
		helpObj.removeClass('error');
		helpObj.addClass('hide');
		fieldObj.removeClass('error');
		fieldObj.set('valid', '1');
	}	
});	
