/*
	SIMPLE JQUERY FORM VALIDATION... 
	Jay Cominoli -- jcominoli@nexusdirect.com
	
	CURRENT:
	Version 0.2
		2011.08.03
		- Added functionality for radio buttons with the option of "other"
		+ currently radioValue checks for monitary values, this should be addressed in an updated version.
	
	Version 0.1
		2011.08.02
		- main code base written
		+ should this be a proper plugin vs. a lump of functions??? who knows.
*/




/*
*************************************
  prep settings & data
*************************************
*/


var returnval = true;
var validations = new Object;
validations["notblank"] = "isBlank";
validations["email"] = "isEmail";
validations["phone"] = "isPhone";
validations["zip"] = "isZipCodeUS";
validations["creditcard"] = "isCreditCard";
validations["radio"] = "radioValue";
validations["radiochecked"] = "radioChecked";




/*
*************************************
  base functions
*************************************
*/

$(document).ready(function() {
	if( $("form.validate").length>0 ){
		
		theid = "#" + $(this).attr("id");
		
		$("form.validate").submit(function(){
			// this
			
			
			theid = "#" + $(this).attr("id");
			
			returnval = true;
			returnval = validate_form(theid + " .required input, " + theid + " .required select", "err_highlight");
			
			if(!returnval && $("#error-message").length>0 ){
				$(theid + " #error-message").css("display", "block");
				window.location.hash = "#form-top";
			}
			
			return returnval;
		});
	}
	
	$("form.validate .required input, form.validate .required select").blur(function(){ validate_form(this, "err_highlight"); });
	$("form.validate .required input[type=radio]").click(function(){ validate_form(this, "err_highlight"); });
	
});

function validate_init(formid){
	
	$(formid).submit(function(){
		
		returnval = true;
		returnval = validate_form(formid + " .required input, " + formid + " .required select", "err_highlight");
		
		if(!returnval && $("#error-message").length>0 ){
			$(formid + " #error-message").css("display", "block");
			window.location.hash = "#form-top";
		}
		
		return returnval;
	});
	
}

function validate_form(obj, errorType){
	
	if( typeof obj == 'string' ){
		window["reset_" + errorType]();
	} else {
		window["reset_" + errorType](obj);
		
		/* MAKE ODD EXCEPTION FOR RADIO BUTTONS WITH "OTHER" */
		if( $(obj).attr("type")=="radio" && $("input." + $(obj).attr("class") + "[type=text]").length>0 ){
			window["reset_" + errorType]( $("input." + $(obj).attr("class") + "[type=text]") );
		}
		
	}
		
	$(obj).each(function(){
		for (var i in validations){
			if(eval('typeof ' + validations[i]) == 'function'){
				if( $(this).hasClass(i) && !window[validations[i]](this) ){
					window[errorType](this);
					returnval = false;
				}
			}
		}
	});
	return returnval;
}




/*
*************************************
  functions for error styling
*************************************
*/

function err_highlight(obj){
	$(obj).addClass("err-highlight");
}

function reset_err_highlight(obj){
	if(obj){
		$(obj).removeClass("err-highlight");
	} else {
		$(".err-highlight").removeClass("err-highlight");
	}
}




/*
*************************************
  functions for validating the data
*************************************
*/

function isBlank(obj){
	if(obj.value == ""){
		return false;
	} else {
		return true;
	}
	return false;
}

function isEmail(obj){
	if(obj.value.length>5){
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.value)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
	return false;
}

function isPhone(obj){
	var rval = true;
	phone = obj.value.replace(/[^0-9]/g, '');
	if(phone.length!=10){ rval = false; }
	if( rval ){ obj.value = "(" + phone.substr(0, 3) + ") " + phone.substr(3, 3) + " - " + phone.substr(6, 4); }
	return rval;
}

function isZipCodeUS(obj){
	zip = obj.value.replace(/[^0-9]/g, '');
	if(zip.length!=5){
		return false;
	} else {
		return true;
	}
	return false;
}

function isCreditCard(obj){
	var rval = true;
	cc = obj.value.replace(/[^0-9]/g, '');
	if(cc.length<8 || cc.length>20){ rval = false; }
	if( rval ){ obj.value = cc; }
	return rval;
}

function radioValue(obj){
	var n = $(obj).attr("name");
	var t = $(obj).attr("type");
	var v = $('input[name=' + n + ']:checked').val();
	var vo = $('input.radio[type=text]').val();
	
	if(v == "other" && !( /^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(v) )){
		if(/^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(vo)){
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
	return false;
}

function radioChecked(obj){
	var n = $(obj).attr("name");
	if( $('input[name=' + n + ']:checked').length > 0 ){
		return true;
	} else {
		return false;
	}
}




