//validates email
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
	
	function isNonnegativeInteger (s)  
	   {   var secondArg = false;  
  
       if (isNonnegativeInteger.arguments.length > 1)  
        secondArg = isNonnegativeInteger.arguments[1];  
  
       // The next line is a bit byzantine.  What it means is:  
       // a) s must be a signed integer, AND  
       // b) one of the following must be true:  
       //    i)  s is empty and we are supposed to return true for  
       //        empty strings  
       //    ii) this is a number >= 0  
  
       return (isSignedInteger(s, secondArg)  
            && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );  
   }  
  
   function isPositiveInteger (s)  
   {   var secondArg = false;  
  
       if (isPositiveInteger.arguments.length > 1)  
          secondArg = isPositiveInteger.arguments[1];  
  
       // The next line is a bit byzantine.  What it means is:  
       // a) s must be a signed integer, AND  
       // b) one of the following must be true:  
       //    i)  s is empty and we are supposed to return true for  
       //        empty strings  
       //    ii) this is a positive, not negative, number  
  
       return (isSignedInteger(s, secondArg)  
          && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );  
   }  
  
   function isSignedInteger (s)  
  
   {   if (isEmpty(s))  
      if (isSignedInteger.arguments.length == 1) return false;  
      else return (isSignedInteger.arguments[1] == true);  
  
      else {  
         var startPos = 0;  
         var secondArg = false;  
  
         if (isSignedInteger.arguments.length > 1)  
            secondArg = isSignedInteger.arguments[1];  
  
         // skip leading + or -  
         if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )  
            startPos = 1;  
         return (isInteger(s.substring(startPos, s.length), secondArg))  
      }  
   }  
  
// isIntegerInRange (STRING s, INTEGER a, INTEGER b)  
   function isIntegerInRange (s, a, b)  
   {   if (isEmpty(s))  
         if (isIntegerInRange.arguments.length == 1) return false;  
         else return (isIntegerInRange.arguments[1] == true);  
  
      // Catch non-integer strings to avoid creating a NaN below,  
      // which isn't available on JavaScript 1.0 for Windows.  
      if (!isInteger(s, false)) return false;  
  
      // Now, explicitly change the type to integer via parseInt  
      // so that the comparison code below will work both on  
      // JavaScript 1.2 (which typechecks in equality comparisons)  
      // and JavaScript 1.1 and before (which doesn't).  
      var num = parseInt (s);  
      return ((num >= a) && (num <= b));  
   }  
  
   function isInteger (s)  
   {  
      var i;  
  
      if (isEmpty(s))  
      if (isInteger.arguments.length == 1) return 0;  
      else return (isInteger.arguments[1] == true);  
  
      for (i = 0; i < s.length; i++)  
      {  
 
 
         var c = s.charAt(i);  
  
         if (!isDigit(c)) return false;  
      }  
  
      return true;  
   }  
  
   function isEmpty(s)  
   {  
      return ((s == null) || (s.length == 0))  
   }  
  
   function isDigit (c)  
   {  
      return ((c >= "0") && (c <= "9"))  
   }  

	function validatePhone(fld) {
		var stripped = fld.replace(/[\(\)\.\-\ ]/g, '');    
	
	   if (fld == "") {
			return false;
		} else if (isNaN(parseInt(stripped))) {
			return false;
		} else if (!(stripped.length == 10)) {
			return false;
		}
		return true;
	}
	
function validateLogin() {
	var username = document.login.login_user.value;
	var password = document.login.login_pass.value;
	if(username == '' || !echeck(username)) {
		alert('Please enter a valid username.\nYour username is your email address.');
		return false;
	}
	if(password.length < 6 || password.length > 12) {
		alert('Your password must be between 6-12 characters.');
		return false;
	}
	return true;
}

function validateRegister() {
	var errors = new Array();
	var email = $("#UserEmail").val();
	var password1 = $("#UserPassword").val();
	var password2 = $("#UserPassword2").val();
	var firstname = $("#UserFirstName").val();
	var lastname = $("#UserLastName").val();
	var address1 = $("#UserAddress1").val();
	var city = $("#UserCity").val();
	var state = $("#UserState").val();
	var zip = $("#UserZip").val();
	var phone1 = $("#UserPhone1").val();
	var phone2 = $("#UserPhone2").val();
	var phone3 = $("#UserPhone3").val();
	var agree = $("input[type='radio']:checked").val();

	$(":input").removeClass("errorinput");
	$("#errorlist").hide();

	if(!echeck(email)) {
		$("#UserEmail").addClass("errorinput");
		errors.push("Please enter a valid email address.");
	}
	
	if(jQuery.trim(password1) == '') {
		$("#UserPassword").addClass("errorinput");
		errors.push("Please enter a password.");
	} else {
		if(password1.length < 6 || password1.length > 12) {
			$("#UserPassword").addClass("errorinput");
			errors.push("Password must be between 6-12 characters in length.");
		} else {
			if(password1 != password2){
				$("#UserPassword").addClass("errorinput");
				$("#UserPassword2").addClass("errorinput");
				errors.push("The two passwords entered do not match.");
			}
		}
	}
	
	if(jQuery.trim(firstname) == '') {
		$("#UserFirstName").addClass("errorinput");
		errors.push("Please enter your first name.");
	}
	
	if(jQuery.trim(lastname) == '') {
		$("#UserLastName").addClass("errorinput");
		errors.push("Please enter your last name.");
	}
	
	if(!validatePhone(phone1+phone2+phone3)) {
		$("#UserPhone1").addClass("errorinput");
		$("#UserPhone2").addClass("errorinput");
		$("#UserPhone3").addClass("errorinput");
		errors.push("Please enter a valid phone number.");
	} 

	if(jQuery.trim(address1) == '') {
		$("#UserAddress1").addClass("errorinput");
		errors.push("Please enter your address.");
	}

	if(jQuery.trim(city) == '') {
		$("#UserCity").addClass("errorinput");
		errors.push("Please enter your city.");
	}

	if(jQuery.trim(state) == '') {
		$("#UserState").addClass("errorinput");
		errors.push("Please enter your state.");
	}

	if(jQuery.trim(zip) == '' || zip.length != 5) {
		$("#UserZip").addClass("errorinput");
		errors.push("Please enter your zip code.");
	}

	if(agree != 'Y') {
		errors.push("You must agree to the terms to proceed.");
	}

	if(errors.length > 0) {
		$("#errorlist").html(errors.join('<br>')).show('slow');
		return false;
	}
	return true;
}