$(document).ready(function(){
	
	// every item with css .field class will be highlighted onfocus
	$(".field").focus(
		function() { 
			$(this).removeClass("field"); 
			$(this).addClass("fieldHighlight"); 
		}
	);
	
	// every item with css .field class will be unhighlighted onblur
	$(".field").blur(
		function() { 
			$(this).removeClass("fieldHighlight"); 
			$(this).addClass("field"); 
		}
	);
	
	// form validation
	$("form").submit(function() {
		// set submit variables							  
		var success = true;
		var curCheckName = "";
		var checkCounter = 0;
		$(':input').each(function() {
			// * required fields begin with req_
			if (this.name.indexOf("req_") >= 0) {
				// set input variables
				var name = this.name;
				var value = this.value;
				var type = this.type;
				var tag = this.tagName.toLowerCase(); 
				// check required text, password, hidden, and textarea fields
				if (type == "text" || type == "password" || tag == "textarea" || type == "file" || type == "hidden") {
					if (value.length <= 0) {
						var message;
						if (type == "hidden") {
							message = "You cannot process this form. There is missing required data.";
						} else {
							message = "You must enter a value.";
						}
						alert(message);
						this.focus();
						success = false;
						return false;
					} else if (name.indexOf("email")>=0) {
						// validate email address
						if ((value.indexOf("@") < 0) || (value.indexOf(".") < 0)) {
							alert("You must enter a valid e-mail address.");
							this.focus();
							success = false;
							return false;
						}
					}
				}
				// check required checkboxes
				else if (type == "checkbox") {
					if ((curCheckName != name) && (!this.checked)) {
						// check counter
						if (checkCounter == 0) {
							alert("You must check at least one option");
							this.focus();
							success = false;
							return false;
						}
						// reset counter
						checkCounter = 0;
					}
					if (this.checked) {
						checkCounter++;
					}
					curCheckName = name;
				}
				// check required drop down menus
				else if (tag == "select") {
					if (this.selectedIndex <= 0) {
						alert(this.name);
						alert("You must select at least one option");
						this.focus();
						success = false;
						return false;		
					}
				}
			}
		});
		return success;
	});
	
	// add back functionality to cancel button
	$("input[value='Cancel']").click(function() {
		history.back();						   
	});

});

// delete prompt
function deleteIt(id) {
	if (confirm("Are you sure that you wish to delete this item?")) {
		$("input[name='deleteID']").val(id);
		$("form").submit();
		return true;
	} else {
		return false;
	}	
}

function validDate(dateMonth,dateDay,dateYr) {
	var allMonths = new Array(null,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var formObj = document.forms[0].elements;
	var dateYrObj = formObj[dateYr].options[formObj[dateYr].options.selectedIndex];
	var dateDayObj = formObj[dateDay].options[formObj[dateDay].options.selectedIndex];
	var dateMonthObj = formObj[dateMonth].options[formObj[dateMonth].options.selectedIndex];
	var monthValue = parseInt(dateMonthObj.value);
	if ((dateYrObj.value.length > 0) && (dateDayObj.value.length > 0) && (dateMonthObj.value.length > 0)) {
		var testDate = new Date(monthValue + "/" + dateDayObj.value + "/" + dateYrObj.value + " 00:00:00");
		testDate = testDate.toUTCString();
		if (testDate.indexOf(allMonths[monthValue]) < 0) {
			alert("You must select an legitimate date.");
			return false;
		}
	}
}

function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode(encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function validate() {
	if (document.getElementById('addressInput').value.length <= 0) {
		alert("Please enter a zip/postal code or city, state");
		document.getElementById('addressInput').focus();
		return false;
	} else {
		return true;
	}
}

function repopulateStates() {
	var val = document.getElementById('req_country').value;
	if ($("#statesProvinces")) {
		$("#statesProvinces").load("/storelocator/inc/storesAjax.php", {country:val});
	}
}

function topMap() {
	document.getElementById('storelocations').scrollTop = 0;
}