function createRequestObject(){
  var request_o; //declare the variable to hold the object.
  var browser = navigator.appName; //find the browser name
  if(browser == "Microsoft Internet Explorer"){
    /* Create the object using MSIE's method */
    request_o = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    /* Create the object using other browser's method */
    request_o = new XMLHttpRequest();
  }
  return request_o; //return the object
}

var http = createRequestObject();

function validDateField(fieldValue)
{
  return fieldValue != "";
}

function isValidMonth(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 1 || fieldValue > 12)){
    return false;
  }else{
    return true;
  }
}

function isValidDay(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 1 || fieldValue > 31)){
    return false;
  }else{
    return true;
  }
}

function isValidYear(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 2000)){
    return false;
  }else{
    return true;
  }
}

function isValidHour(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 1 || fieldValue > 12)){
    return false;
  }else{
    return true;
  }
}

function isValidMinute(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 0 || fieldValue > 60)){
    return false;
  }else{
    return true;
  }
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function getCityStateData(zip){
  /* Create the request. The first argument to the open function is the method (POST/GET),
    and the second argument is the url...
    document contains references to all items on the page
    We can reference document.form_category_select.select_category_select and we will
    be referencing the dropdown list. The selectedIndex property will give us the
    index of the selected item.
  */

//    alert("about to have trouble: " + zip);

var country = '';

if (document.getElementById('country') == null){
  country = 'USA';
}
else {
  country = document.getElementById('country').value;
}

if (country == 'USA' || country == ''){
  if (isInteger(zip)) {
    http.open('get', 'index.php?page_id=102&zip='+ zip);

  /* Define a function to call once a response has been received. This will be our
    handleProductCategories function that we define below. */
    http.onreadystatechange = handleCityStateData;
    current = zip;
  /* Send the data. We use something other than null when we are sending using the POST
    method. */
    http.send(null);
  }
  else {
   alert("Please enter a valid zipcode "+zip);
  }
  }
}

function Trim(inString) {
  var retVal = "";
  var start = 0;
  while ((start < inString.length) && (inString.charAt(start) == ' ')) {
    ++start;
  }
  var end = inString.length;
  while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
    --end;
  }
  retVal = inString.substring(start, end);
  return retVal;
}


/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleCityStateData(){
  /* Make sure that the transaction has finished. The XMLHttpRequest object
    has a property called readyState with several states:
    0: Uninitialized
    1: Loading
    2: Loaded
    3: Interactive
    4: Finished */
  if(http.readyState == 4){ //Finished loading the response
    /* We have got the response from the server-side script,
      let's see just what it was. using the responseText property of
      the XMLHttpRequest object. */
      if (http.responseText == "BAD ZIP"){
         alert("Please insert a valid zip " + http.responseText);
      }
      else {
            var cityState = Array();

      cityState = http.responseText.split(',');
    /* And now we want to change the product_categories <div> content.
      we do this using an ability to get/change the content of a page element
      that we can find: innerHTML. */
           var city = Trim(cityState[0]);
           var state = Trim(cityState[1]);

           document.getElementById('city').value = city;
           document.getElementById('state').value = state;
    }
  }
}
function makeStringFromSelect(selectCtrl) {
  var i;
  var j = 0;
  var outlist = "";
//	alert(selectCtrl.options.length);

  if (selectCtrl.options.length > 0)
  {
  	for (i = 0; i < selectCtrl.options.length; i++) {
	    if (j > 0) {
	      outlist = outlist + ", ";
	    }

	    outlist = outlist + selectCtrl.options[i].value;
	    j++;
  	}
  	return outlist;
  }
}
/*
function makeStringFromSelect(selectCtrl) {
  var i;
  var j = 0;
  var outlist = "";

  for (i = 0; i < selectCtrl.options.length; i++) {
    if (j > 0) {
      outlist = outlist + ", ";
    }

    outlist = outlist + selectCtrl.options[i].value;
    j++;
  }
  return outlist;
}
*/
function addItems(fromCtrl, toCtrl) {
  var i;
  var j;
  var itemexists;
  var nextitem;

  // step through all items in fromCtrl
  for (i = 0; i < fromCtrl.options.length; i++) {
    if (fromCtrl.options[i].selected) {
      // search toCtrl to see if duplicate
      j = 0;
      itemexists = false;
      while ((j < toCtrl.options.length) && (!(itemexists))) {
        if (toCtrl.options[j].value == fromCtrl.options[i].value) {
          itemexists = true;
          alert(fromCtrl.options[i].value + " found!");
        }
        j++;
      }
      if (!(itemexists)) {
        // add the item
        nextitem = toCtrl.options.length;
        toCtrl.options[nextitem] = new Option(fromCtrl.options[i].text);
        toCtrl.options[nextitem].value = fromCtrl.options[i].value;
      }
    }
  }
}

function removeItems(fromCtrl) {
  var i = 0;
  var j;
  var k = 0;

  while (i < (fromCtrl.options.length - k)) {
    if (fromCtrl.options[i].selected) {
      // remove the item
      for (j = i; j < (fromCtrl.options.length - 1); j++) {
        fromCtrl.options[j].text = fromCtrl.options[j+1].text;
        fromCtrl.options[j].value = fromCtrl.options[j+1].value;
        fromCtrl.options[j].selected = fromCtrl.options[j+1].selected;
      }
      k++;
    } else {
      i++;
    }
  }
  for (i = 0; i < k; i++) {
    fromCtrl.options[fromCtrl.options.length - 1] = null;
  }
}
function enableDisableEnd()
{
  document.createEvent.end_date_y.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_m.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_d.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_h.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_min.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_p.disabled = document.createEvent.no_end.checked;

  document.createEvent.end_date_y.value = "";
  document.createEvent.end_date_m.value = "";
  document.createEvent.end_date_d.value = "";
  document.createEvent.end_date_h.value = "";
  document.createEvent.end_date_min.value = "";
  document.createEvent.end_date_p.value = "";
}

function validateFormData(use_v2){
  var isValid = 1;
  var isValidMsg = "";

  if ( use_v2 == 1 && false)
  {
    start_day = document.createEvent.day_start_date;
    start_month = document.createEvent.month_start_date;
    start_year = document.createEvent.year_start_date;
    end_day = document.createEvent.day_end_date;
    end_month = document.createEvent.month_end_date;
    end_year = document.createEvent.year_end_date;

    start_hour = document.createEvent.hour_start_date;
    start_min = document.createEvent.minute_start_date;
    end_hour = document.createEvent.hour_end_date;
    end_min = document.createEvent.minute_end_date;

    if(!validDateField(start_year.value) ||
       !validDateField(start_month.value) ||
       !validDateField(start_day.value) ||
       !validDateField(start_hour.value) ||
       !validDateField(start_min.value))
    {
      isValid = 0;
      isValidMsg = "Please enter a valid start date";
    }

    if(!document.createEvent.no_end.checked &&
      (!validDateField(end_year.value) ||
       !validDateField(end_month.value) ||
       !validDateField(end_day.value) ||
       !validDateField(end_hour.value) ||
       !validDateField(end_min.value)))
    {
      isValid = 0;
      isValidMsg = isValidMsg + "\nPlease enter a valid end date";
    }
  }
  else
  {
    start_day = document.createEvent.start_date_d;
    start_month = document.createEvent.start_date_m;
    start_year = document.createEvent.start_date_y;
    start_hour = document.createEvent.start_date_h;
    start_min = document.createEvent.start_date_min;
    end_day = document.createEvent.end_date_d;
    end_month = document.createEvent.end_date_m;
    end_year = document.createEvent.end_date_y;
    end_hour = document.createEvent.end_date_h;
    end_min = document.createEvent.end_date_min;

    if(!isValidYear(start_year.value) ||
       !isValidMonth(start_month.value) ||
       !isValidDay(start_day.value) ||
       !isValidHour(start_hour.value) ||
       !isValidMinute(start_min.value))
    {
      isValid = 0;
      isValidMsg = "Please enter a valid start date";
    }

    if(!document.createEvent.no_end.checked &&
      (!isValidYear(end_year.value) ||
       !isValidMonth(end_month.value) ||
       !isValidDay(end_day.value) ||
       !isValidHour(end_hour.value) ||
       !isValidMinute(end_min.value)))
    {
      isValid = 0;
      isValidMsg = isValidMsg + "\nPlease enter a valid end date";
    }
  }

if (document.createEvent.event_name.value == ""){
	isValid = 0;
	isValidMsg = isValidMsg + "\nPlease enter a name for this event";
}

  if(isValid==0)
  {
    alert(isValidMsg)
/*    if ( use_v2 ==1)
    {*/
      return false;
//    }
  }
  else
  {
    if ( use_v2 ==1)
  //  {
      return true;
    //}
    document.createEvent.submit();
  }
}

function imposeMaxLength(elementID, MaxLen)
{
  return (document.getElementById(elementID).value.length <= MaxLen);
}
var old_city;
var old_state;

function modifyCityState(state_id, city_id, country_id){
  country = document.getElementById(country_id).value;
  if (country != "USA" && country != ""){
    document.getElementById(state_id).style.display = 'none';
    document.getElementById(city_id).style.display = 'none';
    document.getElementById(city_id+2).style.display = 'block';
    document.getElementById(state_id+2).style.display = 'block';
    if (old_city != null){
      document.getElementById(city_id+2).value = old_city;
      document.getElementById(state_id+2).value = old_state;
    }
    old_city = document.getElementById(city_id).value;
    old_state = document.getElementById(state_id).value;
    document.getElementById(state_id).value = "";
    document.getElementById(city_id).value = "";
    document.getElementById(state_id+"_lbl").innerHTML = "State/Province:";
  }
  else {
    document.getElementById(state_id+2).style.display = 'none';
    document.getElementById(city_id+2).style.display = 'none';
    document.getElementById(city_id).style.display = 'block';
    document.getElementById(state_id).style.display = 'block';
    if (old_city != null){
      document.getElementById(city_id).value = old_city;
      document.getElementById(state_id).value = old_state;
    }
    old_city = document.getElementById(city_id+2).value;
    old_state = document.getElementById(state_id+2).value;
    document.getElementById(state_id+2).value = "";
    document.getElementById(city_id+2).value = "";
    document.getElementById(state_id+"_lbl").innerHTML = "State:";
  }

function createRequestObject(){
  var request_o; //declare the variable to hold the object.
  var browser = navigator.appName; //find the browser name
  if(browser == "Microsoft Internet Explorer"){
    /* Create the object using MSIE's method */
    request_o = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    /* Create the object using other browser's method */
    request_o = new XMLHttpRequest();
  }
  return request_o; //return the object
}

var http = createRequestObject();

function validDateField(fieldValue)
{
  return fieldValue != "";
}

function isValidMonth(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 1 || fieldValue > 12)){
    return false;
  }else{
    return true;
  }
}

function isValidDay(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 1 || fieldValue > 31)){
    return false;
  }else{
    return true;
  }
}

function isValidYear(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 2000)){
    return false;
  }else{
    return true;
  }
}

function isValidHour(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 1 || fieldValue > 12)){
    return false;
  }else{
    return true;
  }
}

function isValidMinute(fieldValue){
  if(!isInteger(fieldValue) || (fieldValue < 0 || fieldValue > 60)){
    return false;
  }else{
    return true;
  }
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function getCityStateData(zip){
  /* Create the request. The first argument to the open function is the method (POST/GET),
    and the second argument is the url...
    document contains references to all items on the page
    We can reference document.form_category_select.select_category_select and we will
    be referencing the dropdown list. The selectedIndex property will give us the
    index of the selected item.
  */

//    alert("about to have trouble: " + zip);

var country = '';

if (document.getElementById('country') == null){
  country = 'USA';
}
else {
  country = document.getElementById('country').value;
}

if (country == 'USA' || country == ''){
  if (isInteger(zip)) {
    http.open('get', 'index.php?page_id=102&zip='+ zip);

  /* Define a function to call once a response has been received. This will be our
    handleProductCategories function that we define below. */
    http.onreadystatechange = handleCityStateData;
    current = zip;
  /* Send the data. We use something other than null when we are sending using the POST
    method. */
    http.send(null);
  }
  else {
   alert("Please enter a valid zipcode "+zip);
  }
  }
}

function Trim(inString) {
  var retVal = "";
  var start = 0;
  while ((start < inString.length) && (inString.charAt(start) == ' ')) {
    ++start;
  }
  var end = inString.length;
  while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
    --end;
  }
  retVal = inString.substring(start, end);
  return retVal;
}


/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleCityStateData(){
  /* Make sure that the transaction has finished. The XMLHttpRequest object
    has a property called readyState with several states:
    0: Uninitialized
    1: Loading
    2: Loaded
    3: Interactive
    4: Finished */
  if(http.readyState == 4){ //Finished loading the response
    /* We have got the response from the server-side script,
      let's see just what it was. using the responseText property of
      the XMLHttpRequest object. */
      if (http.responseText == "BAD ZIP"){
         alert("Please insert a valid zip " + http.responseText);
      }
      else {
            var cityState = Array();

      cityState = http.responseText.split(',');
    /* And now we want to change the product_categories <div> content.
      we do this using an ability to get/change the content of a page element
      that we can find: innerHTML. */
           var city = Trim(cityState[0]);
           var state = Trim(cityState[1]);

           document.getElementById('city').value = city;
           document.getElementById('state').value = state;
    }
  }
}

function addItems(fromCtrl, toCtrl) {
  var i;
  var j;
  var itemexists;
  var nextitem;

  // step through all items in fromCtrl
  for (i = 0; i < fromCtrl.options.length; i++) {
    if (fromCtrl.options[i].selected) {
      // search toCtrl to see if duplicate
      j = 0;
      itemexists = false;
      while ((j < toCtrl.options.length) && (!(itemexists))) {
        if (toCtrl.options[j].value == fromCtrl.options[i].value) {
          itemexists = true;
          alert(fromCtrl.options[i].value + " found!");
        }
        j++;
      }
      if (!(itemexists)) {
        // add the item
        nextitem = toCtrl.options.length;
        toCtrl.options[nextitem] = new Option(fromCtrl.options[i].text);
        toCtrl.options[nextitem].value = fromCtrl.options[i].value;
      }
    }
  }
}

function removeItems(fromCtrl) {
  var i = 0;
  var j;
  var k = 0;

  while (i < (fromCtrl.options.length - k)) {
    if (fromCtrl.options[i].selected) {
      // remove the item
      for (j = i; j < (fromCtrl.options.length - 1); j++) {
        fromCtrl.options[j].text = fromCtrl.options[j+1].text;
        fromCtrl.options[j].value = fromCtrl.options[j+1].value;
        fromCtrl.options[j].selected = fromCtrl.options[j+1].selected;
      }
      k++;
    } else {
      i++;
    }
  }
  for (i = 0; i < k; i++) {
    fromCtrl.options[fromCtrl.options.length - 1] = null;
  }
}
function enableDisableEnd()
{
  document.createEvent.end_date_y.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_m.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_d.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_h.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_min.disabled = document.createEvent.no_end.checked;
  document.createEvent.end_date_p.disabled = document.createEvent.no_end.checked;

  document.createEvent.end_date_y.value = "";
  document.createEvent.end_date_m.value = "";
  document.createEvent.end_date_d.value = "";
  document.createEvent.end_date_h.value = "";
  document.createEvent.end_date_min.value = "";
  document.createEvent.end_date_p.value = "";
}

function validateFormData(use_v2){
  var isValid = 1;
  var isValidMsg = "";

  if ( use_v2 == 1 && false)
  {
    start_day = document.createEvent.day_start_date;
    start_month = document.createEvent.month_start_date;
    start_year = document.createEvent.year_start_date;
    end_day = document.createEvent.day_end_date;
    end_month = document.createEvent.month_end_date;
    end_year = document.createEvent.year_end_date;

    start_hour = document.createEvent.hour_start_date;
    start_min = document.createEvent.minute_start_date;
    end_hour = document.createEvent.hour_end_date;
    end_min = document.createEvent.minute_end_date;

    if(!validDateField(start_year.value) ||
       !validDateField(start_month.value) ||
       !validDateField(start_day.value) ||
       !validDateField(start_hour.value) ||
       !validDateField(start_min.value))
    {
      isValid = 0;
      isValidMsg = "Please enter a valid start date";
    }

    if(!document.createEvent.no_end.checked &&
      (!validDateField(end_year.value) ||
       !validDateField(end_month.value) ||
       !validDateField(end_day.value) ||
       !validDateField(end_hour.value) ||
       !validDateField(end_min.value)))
    {
      isValid = 0;
      isValidMsg = isValidMsg + "\nPlease enter a valid end date";
    }
  }
  else
  {
    start_day = document.createEvent.start_date_d;
    start_month = document.createEvent.start_date_m;
    start_year = document.createEvent.start_date_y;
    start_hour = document.createEvent.start_date_h;
    start_min = document.createEvent.start_date_min;
    end_day = document.createEvent.end_date_d;
    end_month = document.createEvent.end_date_m;
    end_year = document.createEvent.end_date_y;
    end_hour = document.createEvent.end_date_h;
    end_min = document.createEvent.end_date_min;

    if(!isValidYear(start_year.value) ||
       !isValidMonth(start_month.value) ||
       !isValidDay(start_day.value) ||
       !isValidHour(start_hour.value) ||
       !isValidMinute(start_min.value))
    {
      isValid = 0;
      isValidMsg = "Please enter a valid start date";
    }

    if(!document.createEvent.no_end.checked &&
      (!isValidYear(end_year.value) ||
       !isValidMonth(end_month.value) ||
       !isValidDay(end_day.value) ||
       !isValidHour(end_hour.value) ||
       !isValidMinute(end_min.value)))
    {
      isValid = 0;
      isValidMsg = isValidMsg + "\nPlease enter a valid end date";
    }
  }

if (document.createEvent.event_name.value == ""){
	isValid = 0;
	isValidMsg = isValidMsg + "\nPlease enter a name for this event";
}

  if(isValid==0)
  {
    alert(isValidMsg)
/*    if ( use_v2 ==1)
    {*/
      return false;
//    }
  }
  else
  {
    if ( use_v2 ==1)
  //  {
      return true;
    //}
    document.createEvent.submit();
  }
}

function imposeMaxLength(elementID, MaxLen)
{
  return (document.getElementById(elementID).value.length <= MaxLen);
}
var old_city;
var old_state;

function modifyCityState(state_id, city_id, country_id){
  country = document.getElementById(country_id).value;
  if (country != "USA" && country != ""){
    document.getElementById(state_id).style.display = 'none';
    document.getElementById(city_id).style.display = 'none';
    document.getElementById(city_id+2).style.display = 'block';
    document.getElementById(state_id+2).style.display = 'block';
    if (old_city != null){
      document.getElementById(city_id+2).value = old_city;
      document.getElementById(state_id+2).value = old_state;
    }
    old_city = document.getElementById(city_id).value;
    old_state = document.getElementById(state_id).value;
    document.getElementById(state_id).value = "";
    document.getElementById(city_id).value = "";
    document.getElementById(state_id+"_lbl").innerHTML = "State/Province:";
  }
  else {
    document.getElementById(state_id+2).style.display = 'none';
    document.getElementById(city_id+2).style.display = 'none';
    document.getElementById(city_id).style.display = 'block';
    document.getElementById(state_id).style.display = 'block';
    if (old_city != null){
      document.getElementById(city_id).value = old_city;
      document.getElementById(state_id).value = old_state;
    }
    old_city = document.getElementById(city_id+2).value;
    old_state = document.getElementById(state_id+2).value;
    document.getElementById(state_id+2).value = "";
    document.getElementById(city_id+2).value = "";
    document.getElementById(state_id+"_lbl").innerHTML = "State:";
  }
}
}