// CORE VALIDATION SCRIPTS 

function passOrRejectForm(form, pass) { 
 var form_valid = 0; 
 var errors = '';
  
 for (i = 0; i < pass.length; i++) {
 	if (true != pass[i]) {
		errors = errors + pass[i];
		form_valid++;
	} 
 } 
 errorList = "<div id=\"error_list\"><h3>ERRORS: </h3><ul>" + errors + "</ul></div>"; 
 if (0 != form_valid) {
 	if ($('error_list')) $('#error_list').remove();    
		$(form).before(errorList);		
 		return false;
 } else {
 	return true;
 }
}

function simpleStringLengthTest(length, operator, str, id, error, errorList) {	
 test = true;		
 switch (operator) {
	case ">": {
		if (length > str.length) test = false;
		break;
	}
	case "<": {
		if (length < str.length) test = false;
		break;
	}
	case "==": {
		if (length == str.length) test = false;
		break;
	}
	case "!=": {
		if (length != str.length) test = false;
		break;
	}
	default: {
		$test = false;
		break;
	}
 }
 if (test == false) {	
 	var errorListElement = "<li>" + error + "</li>";
 	$(id).css("background-color","#ffcccc");
	return errorListElement;	
 } else {
	$(id).css("background-color","transparent");
	return true;
 }
}

function simpleRegExpTest(str, pattern, id, error) {
	var test = str.match(pattern);
	if (null == test) {
		var errorListElement = "<li>" + error + "</li>";
		$(id).css("background-color","#ffcccc");
		return errorListElement;
	} else {
		$(id).css("background-color","transparent");
		return true;
	}
}

function validateZipCode(id) {		
	return simpleRegExpTest($(id).val(), /^[0-9]{5}$/, id, 'Invalid zip code');
}

function validateEmail(id) {
	return simpleRegExpTest($(id).val(), /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/, id, 'Invalid email');
}

function validatePhone(id) {	
	return simpleRegExpTest($(id).val(), /^[0-9.\s()-]{10,14}$/, id, 'Invalid phone');
}

function validateLogin() {
 
 $('#change_password').click(function() {
 	if ($('#change_password').attr('checked') == true) {
  		$('#jump_page').attr('disabled','disabled');
 	} else {
  		$('#jump_page').attr('disabled','');
 	}
 });
 
 $('#form1').submit(function() {   
 
  	var pass = new Array(3); 	
	
	pass[0] = simpleStringLengthTest(3, ">", $('#user_name').val(), '#user_name', 'Invalid username');
	pass[1] = simpleStringLengthTest(3, ">", $('#password').val(), '#password', 'Invalid password');
	if (true != $('#jump_page').attr('disabled')) {
		pass[2] = true;
		if ('Select One' == $('#jump_page').val()) {		
			$('#jump_page').css("background-color","#ffcccc");
			pass[2] = "<li>No manage page set</li>";
		}
	} else {
		pass[2] = true;
	}
	
 	return passOrRejectForm('#form1', pass); 
 });
}

function validateGetOutdoorsEvent() {
 
 $('#vendor_registration').submit(function() {        
 	var pass = new Array(3); 
	
	pass[0] = simpleStringLengthTest(3, ">", $('#company_name').val(), '#company_name', 'Company Name is required');
	pass[1] = simpleStringLengthTest(3, ">", $('#contact_name').val(), '#contact_name', 'Contact Company Name is required');
	pass[2] = validateEmail('#contact_email');
	
 	return passOrRejectForm('#vendor_registration', pass); 
 });
}

function validateNewPassword() {
 
 $('#change_password').submit(function() {   
 
  	var pass = new Array(3); 	
	
	pass[0] = simpleStringLengthTest(3, ">", $('#old_password').val(), '#old_password', 'Your old password was not correct. Please enter the correct old password.');
	pass[1] = simpleRegExpTest($('#new_password').val(), /^[a-zA-Z0-9!@#$%^&+=_-]{5,16}$/, '#new_password', 'Invalid characters or password length. Password must be 5 - 16 charaters.')
	if ($('#new_password').val() != $('#confirm_password').val()) {
		$('#confirm_password').css("background-color","#ffcccc");
		pass[2] = "<li>Please confirm your new password</li>";
	} else {
		pass[2] = true;
	}
	
	return passOrRejectForm('#change_password', pass);
 });
}

function validateJustAddWater() {
 
 $('#conference_registration').submit(function() {        
 	var pass = new Array(6); 
	
	pass[0] = simpleStringLengthTest(3, ">", $('#name').val(), '#name', 'Name is required');
	pass[1] = simpleStringLengthTest(3, ">", $('#entity').val(), '#entity', 'Enitity is required');
	pass[2] = simpleStringLengthTest(3, ">", $('#address_1').val(), '#address_1', 'Address 1 is required');
	pass[3] = validateZipCode('#zip');
	pass[4] = validateEmail('#email');
	pass[5] = validatePhone('#phone');
	
 	return passOrRejectForm('#conference_registration', pass); 
 });
}

function validateContactUs() {
 
 $('#contact_us').submit(function() {    
     
 	var pass = new Array(5); 
	
	pass[0] = validateEmail('#from_email');
	pass[1] = simpleStringLengthTest(3, ">", $('#first_name').val(), '#first_name', 'First name is required');
	pass[2] = simpleStringLengthTest(3, ">", $('#last_name').val(), '#last_name', 'Last name is required');
	pass[3] = simpleStringLengthTest(3, ">", $('#subject').val(), '#subject', 'Subject is required');
	pass[4] = simpleStringLengthTest(10, ">", $('#comments').val(), '#comments', 'Comment is required');
	
 	return passOrRejectForm('#contact_us', pass); 
 });
}

function validateNewsletterSignup() {
 
 $('#signup').submit(function() {        
 	var pass = new Array(5); 	
		
	pass[0] = simpleStringLengthTest(3, ">", $('#name').val(), '#name', 'Name is required');
	pass[1] = simpleStringLengthTest(3, ">", $('#address').val(), '#address', 'Address is required');
	pass[2] = simpleStringLengthTest(3, ">", $('#city').val(), '#city', 'City is required');
	pass[3] = validateZipCode('#zip');
	pass[4] = validateEmail('#email');
		
 	return passOrRejectForm('#signup', pass); 
 });
}

function toggleBoth() {
	var mail = document.getElementById('mail_delivery');
	var email = document.getElementById('email_delivery');
	var both = document.getElementById('delivery_both');
	if (both.checked == true) {
		mail.checked = true;
		email.checked = true;
	}
}
function checkBoth() {
	var mail = document.getElementById('mail_delivery');
	var email = document.getElementById('email_delivery');
	var both = document.getElementById('delivery_both');
	if (mail.checked == true && email.checked == true) {
		both.checked = true;
	} else {
		both.checked = false;
	}
}



// BROWSER CHECK

function createCookie(name,value,days) {
 if (days) {
 var date = new Date();
 date.setTime(date.getTime()+(days*24*60*60*1000));
 var expires = "; expires="+date.toGMTString();
 }
 else var expires = "";
 document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
 var c = ca[i];
 while (c.charAt(0)==' ') c = c.substring(1,c.length);
 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}

function getInternetExplorerVersion() {
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).

 var rv = -1; // Return value assumes failure.
 if (navigator.appName == 'Microsoft Internet Explorer') {
 var ua = navigator.userAgent;
 var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
 if (re.exec(ua) != null) {
  rv = parseFloat( RegExp.$1 );
 }
 } else {
 rv = 10;
 }
 return rv;
}

function checkVersion() {
 var x = readCookie('good_browser');
 if (!(x)) {
 var ver = getInternetExplorerVersion();
 if ( ver > -1 ) {
  if ( ver < 7.0 ) {
  window.location = "/bad_browser.php";
  } else {
  createCookie('good_browser','1',30);
  }
 }
 }
}

$(document).ready(function() {
	checkVersion();
});
