/*
Serialize all form fields into one JSON object
$("#formId").serializeObject();
*/

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
}


$.fn.numericOnly = function(){
	$(this).keydown(function(event) {
			var k=event.keyCode;
			//alert(k);
	        /* delete 	=46
			backspace 	=8
			tab			=9
			0-9			=48-57
			numpad 0-9	=96-105
			home,end, arrows=35-40
			*/
	        if ( k == 46 || k == 8 || k == 9 || (k >= 48 && k <= 57 ) || (k >= 96 && k <= 105 ) || (k >= 35 && k <= 40 ) ) {
	                //Allow it to happen. Do nothing.
	        }
	        else {
	        	event.preventDefault();
				//prevent default action
	        }
	    });
}

function emailValidate(str){
    var  filter1=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})$/i;  //checks for format someone@somedomain
	
	var  filter2=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ; //checks for format someone@somedomainname.somedomain
    
	if (filter2.test(str))
        testresults=true;
    else{
       testresults=false;
    }

    return (testresults);
}

$.fn.emailOnly = function(){
   	var base = this;
	$(base).each(function(){
		$(this).keyup(function(event){
			validate(this);
		});
	
		$(this).change(function(event){
			validate(this);
		});
	});
	function validate(emailObj){
		str = $(emailObj).val();
		testresults = emailValidate(str);
		if(testresults) {
			$(emailObj).css("color","#4D5255");
		}
		else{
			$(emailObj).css("color","#f00");
		}
	}
   	
	//var defaultColor = $(base).css("background-color");
	
	
}   
    
$.fn.formValidate = function(obj){
	var base = this;
	var required = obj.required ? obj.required : ".req";
	var matched = obj.matched;
	var email = obj.email ? obj.email : ".email";
	var ret = true;
	$(base).find(required).each(function(){
		if($(this).val().length <= 0){
			if($(this).attr("title").length <= 0) {
				//alert("Please complete all required fields.");
				alert("Please complete "+$(this).attr("name"));
			}	
			
			else{
				alert("Please complete "+$(this).attr("title"));
			}
			$(this).focus();
			ret = false;
			return false;
		}
	});
	if(!ret) return ret;
	
	$(base).find(email).each(function(){
		value = $(this).val();
		if(!emailValidate(value)){
				alert("Invalid email address");
			$(this).focus();
			ret = false;
			return false;
		}
	});
	if(!ret) return ret;//if ret is false stop execution here;
	
	return ret;//if ret is true after all validations return true;
}

$.fn.anchorSetCurrent = function(){
	var base = this;
	$(base).each(function(){//set the current item active for all anchors
	  	baseHref = window.location.href;
		if(baseHref.indexOf("#")>0){
	  		baseHref = baseHref.substr(0,baseHref.indexOf("#"));
		}
		
		anchorHref = $(this).attr("href");
	  	if(baseHref == anchorHref || baseHref+"/" == anchorHref ){
			$(this).addClass('current');
		}
	 });
	 return $(base);
}	 

$.fn.hashLinkFix = function(){ 
	var base = this;
	$(base).each(function(){//adds the current url to all # links to solve the problem with base tag
	  	var href = $(this).attr("href");
			if(!href) return false;
			
	  	if(href.indexOf("#")==0){
			
			$(this).click(function(event){
				event.preventDefault();
				window.location.hash = href;
			});
			
		}
	 });
	 return $(base); 
}
