﻿jQuery.extend({
	is_email: function(str){
		if (window.RegExp) {
			return (
				!(new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)")).test(str)
				&& (new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$")).test(str)
			);
		}
		else if(str.indexOf("@") >= 0 && str.indexOf(".") >= 0) return true;
		return false;
	},
	is_creditcard: function(ccNumb){
		var valid = "0123456789",
			len = ccNumb.length,
			iCCN = parseInt(ccNumb),
			sCCN = ccNumb.toString().replace(/^\s+|\s+$/g,'');  // strip spaces
	
		var iTotal = 0,
			bNum = true,
			bResult = false,
			temp, calc;
		
		if(len < 15) return false;
		
		for (var j=0; j<len; j++) {
			if (valid.indexOf( "" + sCCN.substring(j, j+1) ) == "-1") return false;
		}
		
		for(var i=len;i>0;i-=2){
			iTotal += parseInt(parseInt(iCCN) % 10);
			
			iCCN = iCCN / 10;
			calc = (parseInt(iCCN) % 10)  * 2;
			switch(calc){
				case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
				case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
				case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
				case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
				case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
				default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
			}
			iTotal += calc;
	                                                
			iCCN = iCCN / 10;
		}
		if ((iTotal%10)==0) return true;
		return false;
	},
	is_phone: function(phone){
		return (isNaN(parseInt(phone = phone.replace(/[\(\)\.\-\ ]/g, ''))) || phone.length < 10);
	},
	is_zipcode: function(zipcode){
		return (isNaN(parseInt( zipcode )) || zipcode.length != 5);
	}

});


jQuery.fn.extend({
	validate: function(){ //modus validation adapted to jquery
		
		var passed = true;
		var f = this;
		var els = f.find(":input[validate], :password[validate], :radio[validate], :checkbox[validate], :submit[validate], :image[validate], :button[validate], :file[validate], :hidden[validate]");
		
		var exit = function(el){
			passed = false;
			$(el).focus();
			window.alert($(el).attr('message'));
			return false;
		}
		
		els.each(function(){
			var val = $(this).val();
			if($(this).filter("[validate='required'][value=''], [validate='true'][required='true'][value='']").length > 0) return exit(this);
			else if($(this).filter(":checkbox[validate='required']:not(:checked), :checkbox[validate='true'][required='true']:not(:checked)").length > 0
				&& f.find(":checkbox[name='" + $(this).attr("name") + "']:checked").length == 0) return exit(this);
			else if($(this).filter(":radio[validate='required']:not(:checked), :radio[validate='true'][required='true']:not([checked])").length > 0
				&& f.find(":radio[name='" + $(this).attr("name") + "']:checked").length == 0) return exit(this);
			else if($(this).filter("[match]").length > 0
				&& f.find("[name='" + $(this).attr("match") + "'][value='" + val + "']").length == 0) return exit(this);
			else if($(this).filter("[validate='email'], [email]").length > 0
				&& !jQuery.is_email(val)) return exit(this);
			else if($(this).filter("[minlength]").length > 0
				&& $(this).attr("minlength") > val.length) return exit(this);
			else if($(this).filter("[creditcard], [validate='creditcard'][value!='']").length > 0
				&& !jQuery.is_creditcard(val)) return exit(this);
			else if($(this).filter("[validate='phone'], [phone]").length > 0
				&& jQuery.is_phone(val)) return exit(this);
			else if($(this).filter("[validate='zipcode'], [zipcode]").length > 0
				&& jQuery.is_zipcode(val)) return exit(this);
		});
		
		return passed;
	},
	invalid: function(){ //modus validation adapted to jquery
		var f = this;
		var els = f.find(":input[validate], :password[validate], :radio[validate], :checkbox[validate], :submit[validate], :image[validate], :button[validate], :file[validate], :hidden[validate]");
		
		return els.filter(function(){
			if($(this).filter("[validate='required'][value=''], [validate='true'][required='true'][value='']").length > 0) return true;
			if($(this).filter("[validate='required'][type='checkbox']:not([checked]), [validate='true'][required='true'][type='checkbox']:not([checked])").length > 0
				&& f.find("[type='checkbox'][name='" + $(this).attr("name") + "'][checked]").length == 0) return true;
			if($(this).filter("[validate='required'][type='radio']:not([checked]), [validate='true'][required='true'][type='radio']:not([checked])").length > 0
				&& f.find("[type='radio'][name='" + $(this).attr("name") + "'][checked]").length == 0) return true;
			if($(this).filter("[match]").length > 0
				&& f.find("[name='" + $(this).attr("match") + "'][value='" + $(this).val() + "']").length == 0) return true;
			if($(this).filter("[validate='email'], [email]").length > 0
				&& !jQuery.modus.forms.is_email($(this).val())) return true;
			if($(this).filter("[minlength]").length > 0
				&& $(this).attr("minlength") > $(this).val().length) return true;
			if($(this).filter("[creditcard], [validate='creditcard'][value!='']").length > 0
				&& !jQuery.modus.forms.is_creditcard($(this).val())) return true;
			if($(this).filter("[validate='phone'], [phone]").length > 0
				&& (isNaN(parseInt(phone = $(this).val().replace(/[\(\)\.\-\ ]/g, ''))) || phone.length < 10)) return true;
			if($(this).filter("[validate='zipcode'], [zipcode]").length > 0
				&& (isNaN(parseInt( zipcode = $(this).val() )) || zipcode.length != 5)) return true;
			return false;
		});
		
	}
});