function validateEmail(obj) {
	if(obj.value != '') {
		var val = trimSpaces(obj,0); //remove whitespaces from front and back (if any)
		var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	 	if (!email.test(val)) {
			alert("Please enter a valid email address.");
			obj.value='';
			obj.focus();
		}
	}
}
function validateEmailonSubmit(obj) {
	if(obj != '') {
//		var val = trimSpaces(obj,0); //remove whitespaces from front and back (if any)
		var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	 	if (!email.test(obj))
			return false;
		else
			return true;
	}
}

function validateNumber(obj) {
	if(obj.value!='') { //if the field isn't blank
		var val = obj.value;
		var val = trimSpaces(obj,0); //remove any spaces from the beginning or end
		var pos=0;
		//remove any possible leading zeros (you never know)
        if (val.charAt(0)=="0") {
            while(val.charAt(pos)=="0")
                pos++;
            val = val.substr(pos);
        }
		if(obj.name == 'soc') //only need four digits for social security
			var numPattern = /^[0-9]{4}$/;
//		else if(obj.name == 'experience') //only need two digits for experience (years)
//			var numPattern = /^[0-9]{1}[0-9]{1}?$/;			
		else if(obj.name == 'C_zip') //zip code, we only need five digits
			var numPattern = /^[1-9]{1}[0-9]{4}?$/;
		else if(obj.name == 'C_cardnumber') //credit card number, 16 digits
			var numPattern = /^[0-9]{16}$/;
		else if(obj.name == 'C_cvv') //cvv number, 3 digits
			var numPattern = /^[1-9]{1}[0-9]{2}?$/;			
		else //otherwise, it's a phone number, so we need ten digits
			var numPattern = /^[2-9]{1}[0-9]{9}?$/;
	
		if(!numPattern.test(val)) {
			if((obj.name == 'phone') || (obj.name == 'unitphone'))
				alert("Please enter a valid phone number including the area code.");
			else if(obj.name == 'soc')		
				alert("Please enter the last four digits of your social security number.");
//			else if(obj.name == 'experience')
//				alert("Please enter the number of years you have been riding.");
			else if(obj.name == 'C_zip')
				alert("Please enter a valid five-digit zip code.");
			else if(obj.name == 'C_cardnumber')
				alert("Please enter a valid credit card number.");
			else if(obj.name == 'C_cvv')
				alert("Please enter the three-digit CVV number from the back of your credit card.");
			obj.value = '';
			obj.focus();
			return;
		}
		obj.value = val;
	}
}

function validateNoSpecChars(obj) {
//Prevents a user from entering special characters in a field (characters defined below in the iChars variable)
	if(obj.value!='') { //if the field isn't blank
		var val = obj.value;
		var val = trimSpaces(obj,0); //remove any spaces from the beginning or end
		
		var iChars = "!@$%^&*()+=[]\\;,./{}|\":<>?";
		for (var i = 0; i < val.length; i++) {
			if (iChars.indexOf(val.charAt(i)) != -1) {
				alert ("You have entered one more more special characters that are not allowed.\n\n Please try again.");
				obj.value = '';
				obj.focus();				
				return false;
  			}
		}
		obj.value = val;		
	}
}

function trimSpaces(obj,trimMode) {
//THIS FUNCTION WILL TRIM SPACES OFF THE FRONT AND BACK THE VALUE IN A TEXT FIELD, BUT LEAVES SPACES IN BETWEEN
// 0 = trim begin and end
// 1 = trim begin only
// 2 = trim after only
	var objValue = obj.value;
	var iPos;
	iPos=0;

    if (trimMode==0 || trimMode==1) {
        if (objValue.charAt(iPos)==" ") {
            while(objValue.charAt(iPos)==" ")
                iPos++;
            objValue = objValue.substr(iPos);
        }
    }
    iPos = objValue.length-1;

    if (trimMode==0 || trimMode==2) {
        if (objValue.charAt(iPos)==" ") {
            while(objValue.charAt(iPos)==" ")
	            iPos--;
            objValue = objValue.substr(0,iPos+1);
        }
    }
	return objValue;
}

function get_radio_value(field) {
	for (var i=0; i < field.length; i++) {
		if(field[i].checked)
	    	var val = field[i].value;
	}
	if(!val)
		var val = '';
	return val;
}

function validateDate(obj) {
//STEP 1 - VALIDATE THE FORMAT OF THE DATE ENTERED
	if(obj.value!='') { //if the field isn't blank
		var name = obj.name;  //name of the date field
		var val = obj.value; //value in the date field
		var val = trimSpaces(obj,0); //first, trim any spaces off the ends
		//the pattern on the next line only validates the date format and number ranges, no logic such as leap year
		//the pattern makes sure the format is 'mm/dd/yy' or 'm/d/yy' (no leading zero) where mm = 0-12, dd = 0-31, yy = 01-99
		var datePattern = /^(([0]?[0-9])|([1][0-2])){1}\/(([0]?[0-9])|([1]{1}[0-9])|([2]{1}[0-9])|([3]{1}[0-1])){1}\/([0-9]{1}[0-9]{1}){1}$/;
 		if (!datePattern.test(val)) {
			alert("Please enter a valid date in the form mm/dd/yy."); 
			obj.value='';
			obj.focus();
			return;
		}
		//if format is valid, pad zeros on the front of any single digit (1/1/07 => 01/01/07)
		var paddedDate = '';
		var parsedDate = val.split('/');
		for(x=0;x<parsedDate.length-1;x++) {
			if(parsedDate[x].length==1)
				parsedDate[x] = '0'+parsedDate[x];	
			paddedDate += parsedDate[x]+"/"
		}
		paddedDate += parsedDate[2]; //add the year on the end
		obj.value = paddedDate;
//STEP 2 - VALIDATE FOR LEAP YEARS AND # OF DAYS IN THE MONTH
		var finalDate = paddedDate.split('/');
		var month = finalDate[0];
		var day = finalDate[1];
		var year = finalDate[2];
		var leap = 0;
		var errFlag = 0;

		if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) //is it a leap year?
			leap = 1;
	  	if ((month == '02') && (leap == 1) && (day > 29)) //if so, check February (29 days in a leap year)
			errFlag = 1;
		if ((month == '02') && (leap == 0) && (day > 28)) //February, 28 days in a non-leap year
			errFlag = 1;
   		//Validation of other months
		if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12")))
			errFlag = 1;
		if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11")))
			errFlag = 1;
		if(errFlag==1) { //something is wrong with the date entered
			alert("Please enter a valid date in the form mm/dd/yy."); 
			obj.value='';
			obj.focus();
			return;
		}
	}
}

function popup(URLStr,wname,width,height,sbars,resize,toolbar) {
//This function is used to open a centered, popup window.
//This function's parameters include a URL, position of the window (left,top), the width and height of the window, and whether the window
//is resizeable and allows scrollbars.
	if (!left)
		var left=(screen.width/2) - (width/2);
	if (!top)
		var top=(screen.height/2) - (height/2);
	window.open(URLStr,wname,'toolbar='+toolbar+',location=no,directories=no,status=no,menubar=no,scrollbars='+sbars+',resizable='+resize+',width='+width+',height='+height+',left='+left+', top='+top+'');
}

function confirmMsg(source,action,id) {
	if(source == 'forms') {
		if(action == 'delete') {
			if(confirm("Are you sure you would like to delete this document?"))
				location.href='forms_list.php?action=delete&id='+id; //refresh the list, deleting the selected form
		}
	}
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i>d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
