/*
Author: Andrew Corbin
Last Modified: 5/3/11
Description: The following functions are used throughout the website for various features such as form validation.
*/

function IsEmail(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}


/* 
Description:
Call this plugin for any form and any elements with the attribute 'alt="req"' will be required before the form can be submitted

Attribute Values:
	alt=req (verifies that value of element is not null)
	alt=email (verifies that value of element is in valid email format)
 */
jQuery.fn.validate = function (callBackFunc) {
return this.each (function () {
		$(this).submit(function() { // on submit of signup form 
					err = ""; errCount = "";
					$.each($("input, select, textarea"), function(i,v) { // loop each form element inside of form 
					
						var tag = v.tagName;
						var element = $(v);
						var val = element.val();
						borderColor = "";
						$(this).blur();
						
						if($(this).css("border-left-color") != "rgb(255, 0, 0)") borderColor = $(this).css("border-left-color"); 
						//if color is not red then set border color to default value
						
						
						if($(this).attr("alt") == "req" && !$(this).val()){ //if alt attrib is "req" and val is null then err
								errCount = errCount + 1;
								if(errCount == 1)
								 $('html, body').animate({
									 scrollTop: $(this).offset().top
								 }, 2000);
							
							err="Fields marked with an asterisk to the left of them are required (*) "+$(this).attr("id");
							$(this).css("border-color","red"); //turn border red
							$(this).effect("shake", { times:1 }, 150); //shake elements		
						}
						//if alt attrib is "email" then verify email value
						else if($(this).attr("alt") == "email" && !IsEmail($(this).val())){ 
								err="You must enter a valid email address!";
								$(this).css("border-color","red"); //turn border red
								$(this).effect("shake", { times:1 }, 150); //shake elements		
						}		
										
						else {
								$(this).css("border-color","#ccc");
						}	
					});
			if(err) { 
				setTimeout('jAlert("'+err+'", "Whoops!")', 500); 
				//$( 'html, body' ).animate( { scrollTop: 250 }, 500 );
				return false;
			}
			rVal = false;
			if(typeof callBackFunc == 'function' && !err) {
				rVal = callBackFunc.call(this); //get return value from callback function
				return rVal; //use return value from callback function to determine whether or not to submit form
			}
			
		
		});

	
});


}



// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}







function in_array(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}
