/**
* COPYRIGHTED SOFTWARE
* PLEASE READ THE LICENSE AGREEMENT
* 
* @copyright		Copyright 2008-2010 Dilltree Inc.
* @package			tPanther
* @license 			http://dilltree.com/license/tPantherPro/2.0
* @author			Jeremy Dill
* 
* Form submission, formatting, and validation through panther->jcall.
* @module panther
* @submodule forms
* @requires jquery,panther_core,panther_jcall.  tPanther backend with jCall::validateForm.
/*
// CUSTOM TYPE EXAMPLE.
_PI.forms.customType.urltitle={};
_PI.forms.customType.urltitle.typeset=function(jinput){}
_PI.forms.customType.urltitle.vFormat=function(input){}
_PI.forms.customType.urltitle.getFormat=function(input,e,tabOn){
	var m=input;
	if(m.value!=m.value.spToDash()){m.value=m.value.spToDash();}
	if(m.value!=m.value.toLowerCase()){m.value=m.value.toLowerCase();}
}
**/
(function($){
panther.prototype.forms={};
panther.prototype.forms.customType={};
panther.prototype.forms.spopOpt={closeSpeed:200 , effect : 'fade' , position : 'top-right', offsetX:2, offsetY:-2, mouseTrap:false, popEvent : 'instant', popClass : 'err_pop', debug:false,initOnlyOnce:false};
panther.prototype.forms.spopNotice={closeSpeed:200 , effect : 'fade' , position : 'off-left', offsetX:-100, offsetY:-15, mouseTrap:false, popEvent : 'instant', popClass : 'umodal x_bnotice', debug:false, initOnlyOnce:false};
panther.prototype.forms.ipop=function(formSel,field,error) {
	var jfrm=$(formSel);
	var ap=jfrm.find("[name='" + field + "']");
	if(ap[0]){
		_PI.forms.lightDown(ap);
		var sp=ap.slidePop(
			$.extend(this.spopOpt,{
				popName : $.uid(jfrm)+ "_" + field,
				initContent : "<span class='vd_error'>" + error + "</span>",
				initOnlyOnce:false,
				popClass:'err_pop'			
			})
			)
			.addClass('vd_input_err')
			.bind("keyup.ipop",function(e){
				if($.inArray(e.keyCode,_PI.forms.ignoreKeys)<0) _PI.forms.lightDown(this);
			}).getPop();
		sp.box.data('ap',ap[0]).hoverClass("err_pop_hover").css('cursor','pointer').click(function(){_PI.forms.lightDown(ap);return false;});
	}
};
panther.prototype.forms.btnNotice=function(formSel,message,settings) {
	var jfrm=$(formSel);
	var ap=jfrm.data("sbtn");
	var spopts=$(_PI.forms.spopNotice).cloneObj($.extend(settings,{
					popName : $.uid(jfrm)+ "_notice_"+$.uid(this),
					initContent : "<span class='vd_notice'>" + message + "</span>"
				}));
	ap.each(function(){
			$(this).slidePop(spopts);
	});
};
panther.prototype.forms.cfm=function(formSel,field,error,type) {
	var jfrm=$(formSel);
	var ap=jfrm.find("[name='" + field + "']");
	if(ap[0]){
		_PI.forms.lightDown(ap[0]);
		var sp=ap.slidePop(
			$.extend(this.spopOpt,{
				popName : $.uid(jfrm)+ "_" + field,
				initContent : "<span class='vd_notice'>" + error + "</span>",
				initOnlyOnce:false,
				popClass:'msg_pop'
			})
			)
			.addClass('vd_input_err')
			.removeClass('vd_confirmed')
			.getPop();
		sp.box.data('ap',ap[0]).hoverClass("msg_pop_hover").css('cursor','pointer');
		sp.content.find('.x_yes').click(function(){
			_PI.forms.btnNotice(formSel,'<strong>Newly Confirmed Items. </strong>Please press SAVE to record changes.')
			var re = new RegExp("-"+type,"g");
			sp.anchor[0].name=sp.anchor[0].name.replace(re,"-"+type+"_cfm");
			_PI.forms.lightDown(ap);
			ap.addClass('vd_confirmed');
			return false;
		});
		sp.content.find('.x_no').click(function(){
			_PI.forms.lightDown(ap);
			ap.focus().select();
			return false;
		});
	}
};
// TESTS CAN BE A REGEX OR A FUNCTION RETURNING TRUE FOR PASS/FALSE FOR FAIL
panther.prototype.forms.regTest={
	em:new RegExp(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/),
	ph:new RegExp(/^([\(]{1}[0-9]{3}[\)]{1}[ ]{1}[0-9]{3}[\-]{1}[0-9]{4})$/),
	cc:new RegExp(/^[3|4|5|6]\([0-9]{15}$|[0-9]{12}$|[0-9]{13}$|[0-9]{14}$\)/),
	ssn:new RegExp(/^([0-9]{3}-[0-9]{2}-[0-9]{4})$/),
	num:new RegExp(/^([0-9]*)$/),
	date:new RegExp(/^([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})$/)
};
panther.prototype.forms.funcTest={
	req:function(el){ 
		var val=$(el).val();
		if(val!='') return true;
		return false;
	}
};
panther.prototype.forms.ignoreKeys=[0,8,9,13,16,17,18,27,37,38,39,40,46];
panther.prototype.forms.msg={
	em:"<strong>Invalid Email Address. </strong><br /> Please use 'name@domain.extension'.",
	ph:"<strong>Invalid Telephone Number Format. </strong><br />Please use '(###) ###-####'.",
	cc:"<strong>Invalid card number.</strong>",
	ssn:"<strong>Invalid Social Security Number. </strong><br />Please verify the SSN and re-enter.",
	num:"<strong>Invalid.</strong> Must be a number.",
	date:"<strong>Invalid Date Format. </strong><br />Please specify as MM/DD/YYYY.",
	req:"* required"
};
panther.prototype.forms.statesUsAndCanada=[{"abbr":"AL","name":"Alabama"},{"abbr":"AK","name":"Alaska"},{"abbr":"AZ","name":"Arizona"},{"abbr":"AR","name":"Arkansas"},{"abbr":"CA","name":"California"},{"abbr":"CO","name":"Colorado"},{"abbr":"CT","name":"Connecticut"},{"abbr":"DE","name":"Delaware"},{"abbr":"DC","name":"District Of Columbia"},{"abbr":"FL","name":"Florida"},{"abbr":"GA","name":"Georgia"},{"abbr":"HI","name":"Hawaii"},{"abbr":"ID","name":"Idaho"},{"abbr":"IL","name":"Illinois"},{"abbr":"IN","name":"Indiana"},{"abbr":"IA","name":"Iowa"},{"abbr":"KS","name":"Kansas"},{"abbr":"KY","name":"Kentucky"},{"abbr":"LA","name":"Louisiana"},{"abbr":"ME","name":"Maine"},{"abbr":"MD","name":"Maryland"},{"abbr":"MA","name":"Massachusetts"},{"abbr":"MI","name":"Michigan"},{"abbr":"MN","name":"Minnesota"},{"abbr":"MS","name":"Mississippi"},{"abbr":"MO","name":"Missouri"},{"abbr":"MT","name":"Montana"},{"abbr":"NE","name":"Nebraska"},{"abbr":"NV","name":"Nevada"},{"abbr":"NH","name":"New Hampshire"},{"abbr":"NJ","name":"New Jersey"},{"abbr":"NM","name":"New Mexico"},{"abbr":"NY","name":"New York"},{"abbr":"NC","name":"North Carolina"},{"abbr":"ND","name":"North Dakota"},{"abbr":"OH","name":"Ohio"},{"abbr":"OK","name":"Oklahoma"},{"abbr":"OR","name":"Oregon"},{"abbr":"PA","name":"Pennsylvania"},{"abbr":"RI","name":"Rhode Island"},{"abbr":"SC","name":"South Carolina"},{"abbr":"SD","name":"South Dakota"},{"abbr":"TN","name":"Tennessee"},{"abbr":"TX","name":"Texas"},{"abbr":"UT","name":"Utah"},{"abbr":"VT","name":"Vermont"},{"abbr":"VA","name":"Virginia"},{"abbr":"WA","name":"Washington"},{"abbr":"WV","name":"West Virginia"},{"abbr":"WI","name":"Wisconsin"},{"abbr":"WY","name":"Wyoming"},{"abbr":"BC","name":"British Columbia"},{"abbr":"ON","name":"Ontario"},{"abbr":"NL","name":"Newfoundland and Labrador"},{"abbr":"NS","name":"Nova Scotia"},{"abbr":"PE","name":"Prince Edward Island"},{"abbr":"NB","name":"New Brunswick"},{"abbr":"QC","name":"Quebec"},{"abbr":"MB","name":"Manitoba"},{"abbr":"SK","name":"Saskatchewan"},{"abbr":"AB","name":"Alberta"},{"abbr":"NT","name":"Northwest Territories"},{"abbr":"NU","name":"Nunavut"},{"abbr":"YT","name":"Yukon Territory"}];
panther.prototype.forms.acStateOpts=
{
		minChars: 1,
		width: 280,
		max:200,
		matchContains: "word",
		autoFill: false,
		formatMatch: function(row, i, max) {
			return row.name + " " + row.abbr;
		},	
	 	formatItem: function(item) {
	    	return item.abbr+" ("+item.name+")";
	  	},
		formatResult: function(item){
			return item.abbr;
		}
};
// sel - form elements, or container holding form elements to typeset.
panther.prototype.forms.typeset=function(sel){ 
	var jset=$(sel);
	var jfrm=jset;
	if(typeof jfrm[0]!='form') var jfrm=jset.parents("form:first");
	var tabON=jfrm.data('autoTab');
	jset.find('[name]').each(function(){
		var jinput=$(this);
		var vdtypes=null;
		var list = jinput.attr('class').split(" ");
	    for (var i = 0; i < list.length; i++) {
	        if (list[i].indexOf("vd-") >= 0) {
	            var cl = list[i].substring(3, list[i].length);
				vdtypes=cl.split("-");
	            break;
	        }
	    }
		if(vdtypes){
			jinput.data('vdtypes',vdtypes);
			
			// BIND EVENTS
			jinput
				.unbind('.typeset')
				.bind('keyup.typeset',function(e){_PI.forms.getFormat(this,e,'',tabON)});

			if (!(jinput).hasClass("ncvd")) jinput.bind('change.typeset blur.typeset',function(e){_PI.forms.vdate(this)})
			
				
			for (var i = 0; i < vdtypes.length; i++) {
				var vdtype=vdtypes[i];
				
				if(_PI.forms.customType[vdtype] && _PI.forms.customType[vdtype].typeset) {
					_PI.forms.customType[vdtype].typeset(jinput);
					return false;
				}
				// FORCE FORMAT OF ELEMENT BASED ON TYPE.
				switch(vdtype){
					case "ph":
					jinput
						.attr('maxlength','14')
						.attr('size','14')
					break;
					case "isodate":
						jinput
							.removeClass('hasDatepicker')
							.attr('maxlength','10')
							.attr('size','10')
							.datepicker({ 
								dateFormat: 'yy-mm-dd',
								duration: '',
								showOn: 'both', 
								buttonImage: '/_img/cal.png', 
								buttonImageOnly: true
							});
					break;
					case "def":
						jinput.defset();
					break;
				}
			}
		}
	});
};
panther.prototype.forms.lightDown=function(item){
	var pop=$.slidePop.getPop($.uid(item));
	if(pop && pop.isOpen()){
	     pop.close();
	}
	$(item)	
 		.removeClass("vd_input_err")
 		.unbind('.ipop');
};
panther.prototype.forms.vdate=function(sel,vdtype){
		var valid=true;
		var item=$(sel);
		var name=item.attr('name');
		var formobj=item.parents('form:first');
		// CLEAR ERRORS TO START
		try{
			_PI.forms.lightDown(item);
		} catch(e){
			_PI.console(e);
		}
		if (!vdtype) {
			var vdtypes=item.data('vdtypes');
			if(typeof(vdtypes)!="undefined"){
				for (var i = 0; i < vdtypes.length; i++) {
					var vdtype=vdtypes[i];
					if(!check(vdtype)) { 
						valid=false;
						break;
					}
				}
			}
		} else {
			valid=check(vdtype);
		}
		function check(type){
			_PI.forms.vFormat(type,item[0]);
			var pass=true;
			if(_PI.forms.funcTest[type]){
				 if(!_PI.forms.funcTest[type](item)) pass=false;
			} 
			if(_PI.forms.regTest[type]){
			 	if(item.val() && !_PI.forms.regTest[type].test(item.val()) ) pass=false;
			}
			if(!pass){
				try{
					if(item.is(":visible")) _PI.forms.ipop(formobj ,name,_PI.forms.msg[type]);
				} catch(e){
					_PI.console(e);
				}
				return false;
			} else {
				return true;
			}
		}
		return valid;
};
panther.prototype.forms.vFormat=function(type,p1){
	if(_PI.forms.customType[type] && _PI.forms.customType[type].vFormat) {
		_PI.forms.customType[type].vFormat(p1);
		return false;
	}
	var p;
	switch(type){
		case "isodate":
			if(p1.value!="") {
				p=p1.value.stripPunctuation();
				var a=p.substring(0,4);
				var b=p.substring(4,6);
				var c=p.substring(6,8);
				p=a;
				if (b!=""||a.length==4){
					p=p+"-"+b;
				}
				if (c!=""||b.length==2){
					p=p+"-"+c;
				}
				p1.value=p;
			}
		break;		
		case "ph":
			if(p1.value!="") {
				p=p1.value.stripPunctuation();
				/*if(p.substring(0,1)=="1") {
					p1.value="";
					return false;
				}*/
				var a=p.substring(0,3);
				var b=p.substring(3,6);
				var c=p.substring(6,10);
				if(a.length>0) p="("+a;
				if (b!=""||a.length==3){
					p=p+") "+b;
				}
				if (c!=""||b.length==3){
					p=p+"-"+c;
				}
				p1.value=p;
			}
		break;
		case "cc":
			if(p1.value!="") {
				if (p1.value.indexOf("*") >= 0) return; 
				p=p1.value.stripPunctuation();
				var ii=p.substring(0,2);
				switch(ii){
					case "34":
					case "37":
						this.cardtype="amex"; //3712 123456 12345
						p1.value=club(p,15);
					break;
					case "30":
					case "36":
					case "38":								
						this.cardtype="club"; //3012 123456 1234
						p1.value=club(p,14);
					break;				
					case "40":
					case "41":
					case "42":								
					case "43":
					case "44":
					case "45":								
					case "46":								
					case "47":								
					case "48":								
					case "49":								
						this.cardtype="visa"; //4112 1234 1256 1234
						p1.value=quadfour(p);				
					break;				
					case "50":
					case "51":								
					case "52":								
					case "53":								
					case "54":								
					case "55":								
						this.cardtype="mastercard"; //5112 1234 1256 1234
						p1.value=quadfour(p);
					break;				
					case "60":								
						this.cardtype="disc"; //6011 1234 1256 1234
						p1.value=quadfour(p);
					break;
					default:
						this.cardtype="unknown"; 
					break;
				}
			}
		break;
	}
	function club(p,len){
		var a=p.substring(0,4);
		var b=p.substring(4,10);
		var c=p.substring(10,len);
		p=a;
		if (b!=""||a.length==4){
			p=p+" "+b;
		}
		if (c!=""||b.length==6){
			p=p+" "+c;
		}
		return p;
	}
	function quadfour(p){
		var a=p.substring(0,4);
		var b=p.substring(4,8);
		var c=p.substring(8,12);
		var d=p.substring(12,16);
		p=a;
		if (b!=""||a.length==4){
			p=p+" "+b;
		}
		if (c!=""||b.length==4){
			p=p+" "+c;
		}
		if (d!=""||c.length==4){
			p=p+" "+d;
		}
		return p;
	}

};
//AUTOTABBING
panther.prototype.forms.autoTab=function(input,len, e, vdtype,pre) {
	var keyCode = e.keyCode; 
	if(input.value.length >= len && $.inArray(e.keyCode,_PI.forms.ignoreKeys)<0) {
		input.value = input.value.slice(0, len);
		this.nextFocus(input);
		//if (vdtype!="") setTimeout("vdate('"+input.id+"','"+vdtype+"',1,'"+pre+"');",20);
		return false;
	} //close if

	return true;
};
panther.prototype.forms.nextFocus=function(input){
	var nxt=false;
	$(input.form).find('[name]:visible:enabled').each(function(){
		if (nxt) {
			this.focus();
			if(this.select) this.select();
			return false;
		}
		if (this==input) nxt=true;
	});
	return false;
};
panther.prototype.forms.checkNumber = function(e){
	var key = e.keyCode;
	return ((key>47 && key<58)||(key>95&&key<106))
};
panther.prototype.forms.getFormat=function (m,e,type,tabON){
	if(undefined!=type && type!='') {
		var vdtypes=[type];
	} else {
		var vdtypes=$(m).data('vdtypes');
	}
	for (var i = 0; i < vdtypes.length; i++) {
		var vdtype=vdtypes[i];
		if(vdtype=="req") continue;
		var maxlen=m.getAttribute('maxlength');
		if(_PI.forms.customType[vdtype] && _PI.forms.customType[vdtype].getFormat) {
			_PI.forms.customType[vdtype].getFormat(m,e,tabON);
			continue;
		}
		switch(vdtype) {
			case "isodate":
				if (this.checkNumber(e)){	
					this.vFormat(vdtype,m);
					if(tabON)this.autoTab(m, maxlen, e, vdtype);			
				} else {
					if(m.value!=m.value.stripLetters()){m.value=m.value.stripLetters();}
				}
			break;
			case "ph":
				if (this.checkNumber(e)){	
					this.vFormat(vdtype,m);
					if(tabON)this.autoTab(m, maxlen, e, vdtype);
				} else {
					if(m.value!=m.value.stripLetters()){m.value=m.value.stripLetters();}
				}
			break;
			case "cc":
				if (this.checkNumber(e)){	
					this.vFormat(vdtype,m);
				//	if(tabON)autoTab(m, maxlen, e, vdtype);
				} else {
					if(m.value!=m.value.stripLetters()){m.value=m.value.stripLetters();}
				}
			break;		
			case "caps":
				if(m.value!=m.value.stripPunctuation()){m.value=m.value.stripPunctuation();}
				if(m.value!=m.value.toUpperCase()){m.value=m.value.toUpperCase();}
			break;
			case "lwr":
				if(m.value!=m.value.stripPunctuation()){m.value=m.value.stripPunctuation();}
				if(m.value!=m.value.toLowerCase()){m.value=m.value.toLowerCase();}
			break;
			case "num":
				if (!this.checkNumber(e)){
					if(m.value!=m.value.stripLetters()){m.value=m.value.stripLetters();}
				}
			break;	
		} 
	}
};

// ADDITIONAL STRING METHODS
String.prototype.stripBasic = function() {
	return this.replace( /[~`\.,;!@#\$%\^\*&\+=<>"\/:\?'\|\(\)\[\]\\]/g, '');
};
String.prototype.stripPunctuation = function() {
	return this.replace( /[~`\.,;!@#\$%\^\*&\+=<>"\/:\?'\|\(\)\[\]_\-\\\s]/g, '');
};
String.prototype.stripLetters = function() {
	return this.replace( /[A-Za-z]/g, '');
};
String.prototype.cleanQuotes = function() {
	return this.replace( /["']/g, '`');
};
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// PLUGINS
if(!$.fn.attention){
	$.fn.attention = function (ms,settings) {
		settings = $.extend({
			restoreBg : false //auto detect and restore bg color and image
		  }, settings);		
		return this.each(function(){
			var t=$(this)
			if(!t.is(":animated")&&ms>0){
				if(ms==undefined) ms=200;
				if (settings.restoreBg) {
					var color=t.css("background-color");
					var bgimg=t.css("background-image");
				}
				t
				.css({ backgroundColor:"yellow",backgroundImage:"none"})						 
		     	.fadeTo(ms,1,function(){
					if (settings.restoreBg) t.css({ backgroundColor:color,backgroundImage:bgimg});
					else t[0].style.backgroundColor="";
				}); 
			}
		});
	};
}
if (!$.fn.shake){
	$.fn.shake = function () {
		return this.each(function(){
			$(this).stop(1,1).animate( { marginLeft:"10px"}, 50 )
			.animate( { marginLeft:"0px" } , 50 )
			.animate( { marginLeft:"5px"}, 80 )
			.animate( { marginLeft:"0px" } , 120 );
		});
	};
}
$.fn.typeset = function () {
		return this.each(function(){
			_PI.forms.typeset(this);
		});
};
$.fn.defset = function () {
		return this.each(function(){
			var el=$(this);
			el.attr("defaultValue",el.val())
			.focus(function(){
				var el=$(this);
				if(el.val() == el.attr('defaultValue')) el.val(''); 
			})
			.blur(function(){
				var el=$(this);
				if(el.val() == '') el.val(el.attr('defaultValue'));
			});
		});
};
$.fn.hoverClass = function (cl) {
	if (cl==undefined) cl="odd";
	return this.each(function(){		
			$(this).hover(
				function(){
					$(this).addClass(cl);
					return false;
				},
				function(){
					$(this).removeClass(cl);
					return false;
				}
			)
			$(this).focus(
				function(){
					$(this).addClass(cl);
					return false;
				}
			);
			$(this).blur(
				function(){
					$(this).removeClass(cl);
					return false;
				}
			);	
	});
};
})(jQuery);

