// iMask is an open source (free) javascript tool for creating input and textarea masking. Copyright (c) 2007 Fabio Zendhi Nagao, http://zend.lojcomm.com.br/imask/, MIT Style License.
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('j 1h=C W({g:{1T:".1h",H:"2J",14:"2Z",1E:"30",1C:"2Y",1V:W.11,1O:W.11,1G:W.11,1F:W.11,1W:W.11},2U:k(1U){e.3m(1U);j 1S=$$(e.g.1T);1S.3f(k(o,i){o.g=2s.2x(o.2q);f(o.g.F=="T"){o.2o("1l-2p","2m")}o.P("2n",k(1j){1j=C O(1j);1j.M()});o.P("2H",k(12){12=C O(12);12.M();e.1N(12,o)}.U(e));o.P("2M",k(1d){1d=C O(1d);1d.M()});o.P("2D",k(1c){1c=C O(1c);e.1P(1c,o);e.L("1W",o,20)}.U(e));o.P("2E",k(1f){1f=C O(1f);e.24(1f,o)}.U(e));o.P("1Z",k(10){10=C O(10);10.M();e.25(10,o);e.L("1V",o,20)}.U(e));o.P("1R",k(15){15=C O(15);15.M();e.26(15,o);e.L("1O",o,20)}.U(e))}.U(e))},1N:k(2I,V){f(V.g.F=="1a"){j p=e.1m(V);e.G(V,p,(p+1))}n{f(V.g.F=="T"){e.1p(V)}}},1P:k(z,c){f(z.D==13){c.1R();e.2e(c)}n{f(!(z.D==9)){z.M();f(c.g.F=="1a"){j p=e.1m(c);Y(z.D){h 8:e.16(c);m;h 36:e.1b(c);m;h 35:e.1H(c);m;h 37:h 38:e.16(c);m;h 39:h 2j:e.18(c);m;h 1Q:e.19(c,p,e.g.H);m;Z:j l=e.1q(z);f(e.2h(c,p,l)){f(z.23){e.19(c,p,l.2l())}n{e.19(c,p,l)}e.L("1G",[z,c],20);e.18(c)}n{e.L("1F",[z,c],20)}m}}n{f(c.g.F=="T"){Y(z.D){h 8:h 1Q:e.2c(c);m;Z:j l=e.1q(z);f(e.g.14.I(l)>=0){e.2d(c,l);e.L("1G",[z,c],20)}n{e.L("1F",[z,c],20)}m}}}}}},24:k(A,c){f(!(A.D==9)&&!(A.23&&A.D==9)&&!(A.D==13)&&!(A.1B&&A.D==2t)&&!(A.1B&&A.D==3g)&&!(A.1B&&A.D==3i)){A.M()}},25:k(3b,c){f(c.g.1Y){c.s=e.29(c,c.s)}f(c.g.F=="1a"){e.1b.27(20,e,c)}n{e.1p.27(20,e,c)}},26:k(3n,c){f(c.g.1Y){c.s=e.1t(c)}},3p:k(c){e.G(c,0,c.s.t)},1b:k(c){N(j i=0,v=c.g.Q.t;i<v;i++){f(e.R(c,i)){e.G(c,i,(i+1));w}}},1H:k(c){N(j i=(c.g.Q.t-1);i>=0;i--){f(e.R(c,i)){e.G(c,i,(i+1));w}}},16:k(c,p){f(!$1X(p)){p=e.1m(c)}f(p<=0){e.1b(c)}n{f(e.R(c,(p-1))){e.G(c,(p-1),p)}n{e.16(c,(p-1))}}},18:k(c,p){f(!$1X(p)){p=e.1K(c)}f(p>=c.g.Q.t){e.1H(c)}n{f(e.R(c,p)){e.G(c,p,(p+1))}n{e.18(c,(p+1))}}},G:k(c,a,b){f(c.22){c.1Z();c.22(a,b)}n{f(c.21){j r=c.21();r.2V();r.1J("1g",a);r.1L("1g",(b-a));r.2Q()}}},19:k(c,p,l){j 1r=c.s;j X="";X+=1r.1z(0,p);X+=l;X+=1r.1w(p+1);c.s=X;e.G(c,p,(p+1))},1p:k(c){j v=c.s.t;e.G(c,v,v)},1m:k(c){j p=0;f(c.1D){f($F(c.1D)=="T"){p=c.1D}}n{f(1k.1e){j r=1k.1e.1M().1I();r.1L("1g",c.s.t);p=c.s.2X(r.1l);f(r.1l==""){p=c.s.t}}}w p},1K:k(c){j p=0;f(c.1v){f($F(c.1v)=="T"){p=c.1v}}n{f(1k.1e){j r=1k.1e.1M().1I();r.1J("1g",-c.s.t);p=r.1l.t}}w p},R:k(c,p){j 2g=c.g.Q.S();j l=2g.q(p);f("2y".I(l)>=0){w 1n}w 2a},2h:k(c,p,l){j 2f=c.g.Q.S();j 2i=2f.q(p);Y(2i){h"9":f(e.g.14.I(l)>=0){w 1n}m;h"a":f(e.g.1E.I(l)>=0){w 1n}m;h"x":f(e.g.1C.I(l)>=0){w 1n}m;Z:w 2a;m}},29:k(c,E){j 17=c.g.Q.S();j B="";N(j i=0,u=0,v=17.t;i<v;i++){Y(17.q(i)){h"9":f(e.g.14.I(E.q(u).S())>=0){f(E.q(u)==""){B+=e.g.H}n{B+=E.q(u++)}}n{B+=e.g.H}m;h"a":f(e.g.1E.I(E.q(u).S())>=0){f(E.q(u)==""){B+=e.g.H}n{B+=E.q(u++)}}n{B+=e.g.H}m;h"x":f(e.g.1C.I(E.q(u).S())>=0){f(E.q(u)==""){B+=e.g.H}n{B+=E.q(u++)}}n{B+=e.g.H}m;Z:B+=17.q(i);m}}w B},1t:k(c){j K=c.s;f(""==K){w""}j 1o="";f(c.g.F=="1a"){N(j i=0,v=K.t;i<v;i++){f((K.q(i)!=e.g.H)&&(e.R(c,i))){1o+=K.q(i)}}}n{f(c.g.F=="T"){N(j i=0,v=K.t;i<v;i++){f(e.g.14.I(K.q(i))>=0){1o+=K.q(i)}}}}w 1o},1q:k(1u){j l="";Y(1u.D){h 34:h 31:l="0";m;h 32:h 2R:l="1";m;h 2S:h 2P:l="2";m;h 2W:h 2T:l="3";m;h 3a:h 3l:l="4";m;h 3j:h 3k:l="5";m;h 3o:h 3e:l="6";m;h 3c:h 3h:l="7";m;h 2r:h 2u:l="8";m;h 2w:h 2v:l="9";m;Z:l=1u.2k;m}w l},2d:k(c,l){c.s=c.s+l;e.1s(c)},2c:k(c){c.s=c.s.1z(0,(c.s.t-1));e.1s(c)},1s:k(c){j J=e.1t(c);j y="";N(j i=0,v=J.t;i<v;i++){f("0"!=J.q(i)){y=J.1w(i);m}}J=y;y="";N(j v=J.t,i=c.g.1A;v<=i;v++){y+="0"}y+=J;J=y.1w(y.t-c.g.1A);y=y.1z(0,(y.t-c.g.1A));j 1x=C 2L("(\\\\d+)(\\\\d{"+c.g.2B+"})");2C(1x.2z(y)){y=y.2A(1x,"$1"+c.g.2G+"$2")}c.s=y+c.g.2O+J},1y:k(c){j 1i=c.2K();f(1i.2F()=="2N"){w 1i}n{w e.1y(1i)}},2e:k(c){j 2b=e.1y(c);2b.3d()}});1h.28(C 3q);1h.28(C 33);',62,213,'||||||||||||obj||this|if|options|case||var|function|chr|break|else|_3||charAt||value|length||len|return||_50|_f|_13|_40|new|code|str|type|_setSelection|maskEmptyChr|indexOf|_4f|_45|fireEvent|stop|for|Event|addEvent|mask|_isInputPosition|toLowerCase|number|bind|_d|Class|_2b|switch|default|_a|empty|_6||validNumbers|_b|_selectPrevious|_3f|_selectNext|_updSelection|fixed|_selectFirst|_8|_7|selection|_9|character|iMask|_55|_5|document|text|_getSelectionStart|true|_46|_setEnd|_chrFromEvent|_2a|_formatNumber|_stripMask|_49|selectionEnd|substr|re|_getObjForm|substring|decDigits|ctrl|validAlphaNums|selectionStart|validAlphas|onInvalid|onValid|_selectLast|duplicate|moveStart|_getSelectionEnd|moveEnd|createRange|_onMouseUp|onBlur|_onKeyDown|46|blur|_2|targetClass|_1|onFocus|onKeyDown|chk|stripMask|focus||createTextRange|setSelectionRange|shift|_onKeyPress|_onFocus|_onBlur|delay|implement|_wearMask|false|_57|_popNumber|_pushNumber|_submitForm|_3b|_36|_isViableInput|_3c|40|key|toUpperCase|right|mousedown|setStyle|align|alt|56|Json|67|104|105|57|evaluate|9ax|test|replace|groupDigits|while|keydown|keypress|getTag|groupSymbol|mouseup|_c|_|getParent|RegExp|click|form|decSymbol|98|select|97|50|99|initialize|collapse|51|lastIndexOf|abcdefghijklmnopqrstuvwxyz1234567890|1234567890|abcdefghijklmnopqrstuvwxyz|96|49|Options|48||||||52|_15|55|submit|102|each|86|103|88|53|101|100|setOptions|_17|54|_selectAll|Events'.split('|'),0,{}))

// When the page loads up
window.addEvent('domready', load_all);

// This is where we load stuff
function load_all(){
		
	// Loop through the required forms to add a form listener for ajax submission
	$$("form.required").each(function(el){

			// Form listener
			el.addEvent('submit', ajax_form_submit.bindWithEvent( el )	);

	});
	
	// This is the imask for form field
	 new iMask({  
		 onFocus: function(obj) {  
			 obj.setStyles({"background-color":"ff8", border:"1px solid 880"});  
		 },  
	   
		 onBlur: function(obj) {  
			 obj.setStyles({"background-color":"fff", border:"1px solid ccc"});  
		 },  
	   
		 onValid: function(event, obj) {  
			 obj.setStyles({"background-color":"8f8", border:"1px solid 080"});  
		 },  
	   
		 onInvalid: function(event, obj) {  
			 if(!event.shift) {  
				 obj.setStyles({"background-color":"f88", border:"1px solid 800"});  
			 }  
		 }  
	 });  

// function
}

var rule = {};
	rule["word"] 	=	/^\w+/i;

	rule["alphabetical"] 	=	/^[a-z]+$/i;
	rule["alphabet"] 	=	rule["alphabetical"];
	rule["alpha"] 	=	rule["alphabetical"];

	rule["numeric"] =	/^\d+$/;
	rule["number"] =	rule["numeric"];	
	rule["num"] =	rule["numeric"];		
	
	
	rule["alpha_numeric"] = /^[a-z\d]+$/i;
	rule["alphanumeric"] = rule["alpha_numeric"];
	rule["alphanum"] = rule["alpha_numeric"];

	rule["currency"] = /^(\$|\-|\$\-)?\d{1,3}([,]?\d{3})*(\.\d{2})?$/;
	rule["date_standard"] = /^([1-9]|0[1-9]|[1-2]\d|3[0-1])(\/|\.|-)([1-9]|0[1-9]|1[0-2])\2\d{4}$/;
	rule["time_civilian"] = /^([1-9]|0[1-9]|1[0-2]):[0-5]\d[\s]?(am|pm)$/i;
	rule["time_militart"] = /^([0-1]\d|2[0-3]):[0-5]\d$/;

	rule["phone"] = /^[\(]?\d{3}[\)]?[\s|\.|-]?\d{3}[\s|\.|-]?\d{4}$/;
	rule["phone_international"] = /^\d{1,3}[\s|\.|-]\d{7,20}$/;
	rule["postal_code"] = /^([a-z]\d[a-z])[\s|-]?(\d[a-z]\d)$/i;
	rule["zip_code"] = /^\d{5}(-\d{4})?$/;

	rule["email"] = /^([a-z0-9_-]+)(\.[a-z0-9_-]+)*@([a-z0-9_-]+)(\.[a-z0-9_-]+)*[\.]([a-z0-9_-]+)$/i;
	
	rule["username"] = /^[a-z0-9_-]+$/i;
	
	rule["http"] = /^((http|https|ftp):\/\/)?([a-z0-9_-]+)(\.[a-z0-9_-]+)+(\/\w+)*(\.[a-z0-9_-]+)*$/i;
	rule["ip_address"] = /^(\d{1,3})(\.\d{1,3}){3}$/;

// This is the ajax form handler
function ajax_form_submit(e) {
					 
		var exit = {};
		
		// Prevent the submit event
		e = new Event(e).stop();
								 
		//--------------------------------------------------------------------//
		// This is where we do the form validation
				
			var i = 0;
		
			// Loop through the forms to add a form listener
			$$("iframe").each(function(e){
				
				// Check if this field is required
				if( e.getProperty('id') != null && e.getProperty('id').indexOf("mce_editor_") == 0 ){		
		
					var this_element = e.getProperty('id').replace("mce_editor_", "");
					var this_frame = window.frames[ i ].document.body.innerHTML;
					
					if( $(e.getProperty('id') + "_parent").getPrevious() ){
		
						$(e.getProperty('id') + "_parent").getPrevious().value = this_frame;
						
					// if
					}
						
				} i++;
			
			});

			// This is if form is required
			$each( this.getElementsBySelector('input, select'), function(el) {
																		 
				// Check if there's a class
				if( el.getProperty('class') != false && !exit.now){							 
										 
					el.rules = el.getProperty('class').split(' ');																		 
					
					for(i=0;i<el.rules.length;i++){
						
						// Check if this is a required field
						if( el.rules[i] == "required" && el.value == "" ){
						
							// If it has some text, say it
							if( el.getProperty('required') != false ){	
							
								alert( el.getProperty('required') );
								
							} else {
								
								alert( 'A required field is missing (id:' + el.getProperty('id') + ')' );
							
							// if
							}
							
							// Cancel the submission
							exit["now"] = true;
							
							// Break the loop
							break;
							
						// Now we'll check our other ruled
						} else {
							
							// Check if we have a rule
							if( rule[ el.rules[i] ] ){
								
								// Do some checking
								if( el.value.search( eval(rule[ el.rules[i] ] )) == -1 ){
							
									// If it has some text, say it
									if( el.getProperty('required') != false ){	
									
										alert( el.getProperty('required') );
										
									} else {
										
										alert( 'A required field is not valid (id:' + el.getProperty('id') + ')' );
									
									// if
									}
									
									// Cancel the submission
									exit["now"] = true;
									
									// Break the loop
									break;
								
								// RegEx Checking
								}
								
							// if
							}
							
						// if
						}
											
					// for
					}

				// if
				}
				
			// each
			});
			//--------------------------------------------------------------------//
				
			// Skip this function
			if( !exit.now  ){
									
				// This empties the log div and shows the spinning indicator
				var form_name = this.name;
		
				// Send takes care of encoding and returns the Ajax instance. onComplete removes the spinner from the log.
				var SubmitCall = this.send({
					method: 'post',
					update: $( this.name + '_log'),
					onComplete: function(){
						
						String.prototype.trim = function () {
							return this.replace(/^\s*/, "").replace(/\s*$/, "");
						}
						
						response = $( form_name + '_log').getText().trim();
		
						// Do something in success field
						if( ( response.lastIndexOf("true") > 0 || response == "true" ) && response.lastIndexOf("false") == -1 ){
		
							// Split the response into variables, based on line breaks
							tmp_response_id = response.split( new RegExp( "\\n", "g" ) );
		
							// If there is a response id, use it
							if( tmp_response_id.length > 1){
								
								var response_id = tmp_response_id[tmp_response_id.length-2];
								
							}
							
							// add additional javascript functions if need be
							if( $(form_name + '_on_success') ){
		
								// Eval the javascript in the form
								eval ( $(form_name + '_on_success').value );
		
							}
															
							// add additional forward function if need be
							if( $(form_name + '_redirect_on_success' ) && $(form_name + '_redirect_on_success' ).value ){
								
								document.location.href = $(form_name + '_redirect_on_success' ).value;
									
							}
		
						// Do something in failure field
						} else {
							
							// add additional javascript functions if need be
							if( $(form_name + '_on_failure') ){
								
								//Evel the javascript instruction to do on failure
								eval ( $(form_name + '_on_failure').value );
								
							}
							
							// add additional forward function if need be
							if( $(form_name + '_redirect_on_failure') && $(form_name + '_redirect_on_failure').value  ){
									
								document.location.href = $(form_name + '_redirect_on_failure').value;
								
							}
							
						}
						}
						
				}).request();
		
			//if
			}

}

var keystroke = {};
	keystroke["count"] = 0;
	
var ajax_calls = {};

function precheck_array_parse( array ){
	
	// Create record arrays
	tmp_return = Array();
	tmp_array = array.split("|");
	tmp_return[0] = tmp_array[0].split(",");

	// Create field arrays
	for(i=1;i<tmp_array.length;i++){
		
		tmp_return[i] = Array();
		tmp_array[i] = tmp_array[i].split(",");
		
		// Create field arrays
		for(j=0;j<tmp_return[0].length;j++){
	
			tmp_return[i][j] = tmp_array[i][j];
			
		}
	
	}

	return tmp_return;
	
// function
}

function precheck_result(result,toMatch,callback_match,callback_nomatch){

	result = result.replace("'","\'");
	result = result.replace(/\n/,"");
	
	// Check if we need to parse this
	if ( result.indexOf("|") > 0){
		
		// result = precheck_array_parse( result );
		
	// if
	}

	if( result == toMatch ){
	
		if( callback_match != "" ){
	
			(function(){

				callback_match = callback_match.replace("()","('" + result + "')");

				eval( callback_match );
				
			}).delay(10);

		}

	} else {
		
		if( callback_nomatch != "" ){
		
			(function(){

				callback_nomatch = callback_nomatch.replace("()","('" + result + "')");

				eval( callback_nomatch );
				
			}).delay(10);

		}

	}

// function 
}

function call_precheck(field,criteria,result,toMatch,callback_match,callback_nomatch,co,func){

	ajax_calls[ keystroke["count"] ] = new XHR({
		method: 'get',
		headers: {'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'},
		onSuccess: function (response){ precheck_result(response,toMatch,callback_match,callback_nomatch) }
		});
	
	( function(){

		for(i=1;i<keystroke["count"];i++){
			
			if( ajax_calls[i] ){
			
				ajax_calls[i].cancel();
				
			}
		}
	
		$(result).setText("Searching...");
	
		ajax_calls[ keystroke["count"] ].send("sole.php","SOLE=true&" + func + "=true&field=" + $(  field  ).getProperty('name') + "&value=" + $(  field  ).value + criteria + "&co=" + co)
		
	}).delay(10);

// function
}

function precheck( field,criteria,toMatch,callback_match,callback_nomatch,co,func ){

	// This is the compare operator
	if( co == null ){
		
		co = "eq";
		
	}
	
	// This is the compare operator
	if( func == null ){
		
		func = "PRECHECK";
		
	}

	// Split up the crieria
	if( criteria != "" ){
	
		if( criteria.indexOf(",") != -1 ){
		
			criteria_array = criteria.split(",");
			tmp_criteria_string = "";
			
			// Loop through the additional 
			for(i=0;i<=criteria_array;i++){
			
				// Check if this element exists
				if( $(criteria_array[i]) ){
			
					tmp_criteria_string += "&" + $(criteria_array[i]).getProperty('name') + "=" + $(criteria_array[i]).value;
					
				}
			
			// looping through the criteria
			}	
			
			// reset the criteria
			criteria = tmp_criteria_string;			
		
		// There's no , so just one element
		}  else {
		
			if( $(criteria) ){
		
				// set the properly formattedcriteria
				criteria = "&" + $( criteria ).getProperty('name') + "=" + $(criteria).value;					
				
			}
			
		}

	// if, additional criteria	
	}

	var result = new Element('div', {'id': field + '_status', 'class': 'precheck'});
		$(result).injectAfter( field );
		
	// Remove autocomplete
	$( field ).setProperty("autocomplete", "off");

	$(  field  ).addEvent("keydown", function(event){

					// Count so we know what to cancel when
					keystroke["count"]++;
			
					event = new Event(event);
			
					// Check if these are acceptable keys
					if ( $(  field  ).value.length > -1 && event.key.length > 0 && ( (validChars.contains(event.key) || validCodes.contains(event.code)) || event.key == "backspace" || event.key == "delete"  )  )	{
					
						// We're good to submit
						call_precheck(field,criteria,result,toMatch,callback_match,callback_nomatch,co,func);
					
					// if
					}

				}
			
	);
	
	// This is for cookies if we're saving the last search
	if( Cookie.get( field ) != false && Cookie.get( field ) != "" ){
		
		// Fill in the form
		$( field ).value = Cookie.get( field );
		
		// Load the last search
		call_precheck(field,criteria,result,toMatch,callback_match,callback_nomatch,co,func);
	
	}

}

if( typeof tinyMCE != "undefined"){

	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		editor_selector : "mceEditor",
		plugins : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_buttons1 : "styleselect,formatselect,undo,redo",
		theme_advanced_buttons2 : "fontselect,fontsizeselect,help,code",
		theme_advanced_buttons3 : "bold,italic,underline,strikethrough,forecolor,backcolor,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,separator,outdent,indent,separator,link,unlink,separator,hr,separator,charmap",
		theme_advanced_buttons4 : "tablecontrols,separator,insertdate,inserttime,separator,image,flash,media,print",	
		plugin_insertdate_dateFormat : "%Y-%m-%d",
		plugin_insertdate_timeFormat : "%H:%M:%S",
		extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		external_link_list_url : "example_data/example_link_list.js",
		external_image_list_url : "example_data/example_image_list.js",
		flash_external_list_url : "example_data/example_flash_list.js"
	});
	
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		editor_selector : "mce_right",
		plugins : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,forecolor,backcolor,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist",
		theme_advanced_buttons2 : "numlist,separator,outdent,indent,separator,link,unlink,separator,hr,separator,charmap",
		theme_advanced_buttons3 : "tablecontrols,separator,insertdate,inserttime,separator,image,flash,media,print",	
		plugin_insertdate_dateFormat : "%Y-%m-%d",
		plugin_insertdate_timeFormat : "%H:%M:%S",
		extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		external_link_list_url : "example_data/example_link_list.js",
		external_image_list_url : "example_data/example_image_list.js",
		flash_external_list_url : "example_data/example_flash_list.js"
	});

	
}