$(document).ready(function() {
  if($('#lead_edit').get(0)) {
    $('#lead_edit').draggable();
  }
  
  $('.menuroll').mouseover(function() {
    var src = $(this).attr("src");
    if(src.search(/hover/i) == -1) {
      var src1 = src.substring(0, src.length - 4);
      var src2 = src.substring(src.length - 4);
      $(this).attr({ src: src1 + "_hover" + src2 });
      active = 0;
    } else {
      active = 1;
    }
  }).mouseout(function() {
    var src = $(this).attr("src");
    if(src.search(/hover/i) > -1 && active == 0) {
      var src1 = src.substring(0, src.length - 10);
      var src2 = src.substring(src.length - 4);
      $(this).attr({ src: src1 + src2 });
    }
  });
  
  $('#menu_container a').mouseover(function() {
    $(this).css({ backgroundColor:'yellow' });
  }).mouseout(function() {
    $(this).css({ backgroundColor:'transparent' });
  });
  
  $('#stats_table tr:even td').css({ backgroundColor:'#eee' });
});

function showTab(idnum)
{
  for(var x=1; x<=30; x++) {
    $('#tab_' + x).css({ backgroundColor:"lightblue" });
    $('#tabbox_' + x).css({ display:"none" });
  }
  $('#tab_' + idnum).css({ backgroundColor:"yellow" });
  $('#tabbox_' + idnum).css({ display:"block" });
}

function showView(idnum)
{
  for(var x=1; x<=2; x++) {
    $('#view_' + x).css({ backgroundColor:"lightblue" });
    $('#viewbox_' + x).css({ display:"none" });
  }
  $('#view_' + idnum).css({ backgroundColor:"yellow" });
  $('#viewbox_' + idnum).css({ display:"block" });
}

function setCBValue(cbo, idx)
{
  var x = $('#' + idx);
  if(cbo.checked == true) {
    x.val('1');
  } else {
    x.val('0');
  }
}

function tglDisplay(objid)
{
  $('#' + objid).toggle();
}

function checkHuntContactInfo()
{
  var obj = $('#findahuntform');
  var msg = $('#errormessages');
  var fnm = $('#fah_fname');
  var lnm = $('#fah_lname');
  var sta = $('#fah_statepr_1');
  var phd = $('#fah_phoned');
  var phn = $('#fah_phonen');
  var phc = $('#fah_phonec');
  var ema = $('#fah_email');
  
  if(fnm.val().length > 0) {
    if(lnm.val().length > 0) {
      if(sta.val().length > 0) {
        if(ema.val().length > 0) {
          if(phd.val().length > 0 || phn.val().length > 0 || phc.val().length > 0) {
            obj.submit();
            return true;
          } else {
            msg.html('You need to provide atleast 1 phone number.');
            phd.focus();
          }
        } else {
          msg.html('Please provide an email address that we can send responses to.');
          ema.focus();
        }
      } else {
        msg.html('Please select your state/province.');
        sta.focus();
      }
    } else {
      msg.html('Please provide your last name.');
      lnm.focus();
    }
  } else {
    msg.html('Please provide your first name.');
    fnm.focus();
  }
  return false;
}

function checkFahStep1(max_locations, max_weapons)
{
  var obj = $('#findahuntform');
  var msg = $('#errormessages');
  var spe = $('#fah_species');
  var location_selected = 0;
  var weapon_selected = 0;
  
  if(spe.val().length > 0) {
    for(var x=1; x<=max_weapons; x++) {
      var weapon_ctl = document.getElementById('fah_weapon_'+x);
      if(weapon_ctl.type == "radio") {
        if(weapon_ctl.checked == true) {
          weapon_selected = 1;
        }
      } else {
        if(weapon_ctl.value.length > 0) {
          weapon_selected = 1;
        }
      }
    }
    if(weapon_selected == 1) {
      for(var x=1; x<=max_locations; x++) {
        if($('#fah_location_'+x).val().length > 0) {
          location_selected = 1;
        }
      }
      if(location_selected == 1) {
        obj.submit();
      } else {
        msg.html('Please select atleast 1 location.');
      }
    } else {
      msg.html('Please select a weapon.');
    }
  } else {
    msg.html('Please select a species.');
    spe.focus();
  }
  
  if(location_selected == 1 && weapon_selected == 1) {
    return true;
  }
  return false;
}

function trim(str)
{
  return str.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

function showAllFaqs()
{
  for(var x=0; x<100; x++) {
    $('#faq_' + x).css({ display:"block" });
  }
  $('#showallfaqs').css({ display:"none" });
  $('#hideallfaqs').css({ display:"block" });
}

function hideAllFaqs()
{
  for(var x=0; x<100; x++) {
    $('#faq_' + x).css({ display:"none" });
  }
  $('#hideallfaqs').css({ display:"none" });
  $('#showallfaqs').css({ display:"block" });
}

function ajaxRunFile(page)
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function() {
		if(ajaxRequest.readyState == 4) {
		  if(ajaxRequest.responseText.length > 0) {
		    alert(ajaxRequest.responseText);
		    return false;
		  }
		}
	}
	ajaxRequest.open("GET", page, true);
	ajaxRequest.send(null);
}

function ajaxGetFile(page, displayDiv)
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function() {
		if(ajaxRequest.readyState == 4) {
		  if(ajaxRequest.responseText.length > 0) {
        $('#' + displayDiv).html(ajaxRequest.responseText);
      }
		}
	}
	ajaxRequest.open("GET", page, true);
	ajaxRequest.send(null);
}

function showLeadEditor(content)
{
  var le = $('#lead_edit');
  if(content && content.length > 0) le.html(unescape(content));
  le.css({ display:"inline" });
}

function hideLeadEditor()
{
  var le = $('#lead_edit');
  le.html('');
  le.css({ display:"none" });
}

function saveLeadEditor()
{
  var user_input = "";
  var lef = document.lead_edit_form;
  for(var x=0; x<lef.elements.length; x++) {
    if(lef.elements[x].type != "button") {
      user_input += lef.elements[x].name + "=" + lef.elements[x].value.replace(/\n/g,"<br>") + "&";
    }
  }
  user_input = user_input.substring(0, user_input.length - 1);
  ajaxRunFile('reviewleads_edit_proc.php?' + user_input);
}

function checkFahStep3()
{
    var message = "Please correct the following errors:\n";
    var isValid = true;
    var partySize = p = $('#fah_party_size').val();
    var month = m = $('#fah_month').val();
    var year = y = $('#fah_year').val();
    var budget_range = $('#fah_budget_range').val();
    if(!budget_range.length > 0){
        message += '\nPlease select a budget range.';
        isValid = false;
    }
    if(!partySize.length > 0){
        message += '\nPlease select a party size.';
        isValid = false;
    }
    if(!month.length > 0){
        message += '\nPlease select a month.';
        isValid = false;
    }
    var mc = parseInt(m) - 1;
    var d = new Date();
    if(!((mc >= d.getMonth() && y == d.getFullYear()) || y > d.getFullYear() || (isNaN(m) && y >= d.getFullYear()))){
        message += '\nPlease check the date.  \n\t- Month/Season and Year are required.\n\t- The date must be in the future.';
        isValid = false;
    }
    if(isValid) {
        return true;
    } else {
        alert(message);
        return false;
    }
    return false;
}

