$(document).ready(function(){
	/* global variables */
	var hs_builds = [
		{name: 'GRCh37' , value: '37'},
		{name: 'Build 36', value: '36'},
		{name: 'Build 35', value: '35'},
	];
	var mm_builds = [
		{name: 'Build 37', value: '37'},
		{name: 'Build 36', value: '36'},
	];
	var build_menu = $("#build");
	var chr_menu = $("#chr");
	/*check url parameters */
	var loc = location.search.substring(1, location.search.length);
	var params = loc.split("&");
	var i;
	var param_name;
	var param_value;
	for(i=0; i<params.length; i++){
		param_name = params[i].substring(0,params[i].indexOf('='));
		if(param_name== "taxid"){
			param_value = params[i].substring(params[i].indexOf('=')+1);
			if(param_value != "9606" && param_value != "10090"){
				alert("Unsupported taxid: " + param_value + " try 9606 (human) or 10090 (mouse)");
			}
			else{
				if(param_value == "9606"){
				  $("#organism").val(param_value);		
					populateBuild(build_menu, hs_builds);
					populateChr(chr_menu, param_value);
				}
				else{
					$("#organism").val(param_value)
					populateBuild(build_menu, mm_builds);
					populateChr(chr_menu, param_value);
				}
			}
		}
	}

/**event handlers */
/* change org */
	$("#organism").change(function(){
		/*need to populate build menu*/
		var item = $("#organism option:selected").val();
		if(item == 9606){
			/*human*/
			populateBuild(build_menu, hs_builds);
			populateChr(chr_menu, item);
		}
		else{
			/*mouse*/
			populateBuild(build_menu, mm_builds);
			populateChr(chr_menu, item);
		}
	});

/*change location input */
	$("form.grc_form input:radio").change(function(){
		var check = $("form.grc_form input:radio:checked").val();
		if(check == 'loc'){
			$("#chr_start_label").removeClass("deactivated");
			$("#chr_end_label").removeClass("deactivated");
			$("#chr_start").attr("disabled", false);
			$("#chr_end").attr("disabled", false);
			$("#first_acc_label").addClass("deactivated");
			$("#last_acc_label").addClass("deactivated");
			$("#first_acc").attr("disabled", true);
			$("#last_acc").attr("disabled", true);
		}
		else{
			$("#chr_start_label").addClass("deactivated");
			$("#chr_end_label").addClass("deactivated");
			$("#chr_start").attr("disabled", true);
			$("#chr_end").attr("disabled", true);
			$("#first_acc_label").removeClass("deactivated");
			$("#last_acc_label").removeClass("deactivated");
			$("#first_acc").attr("disabled", false);
			$("#last_acc").attr("disabled", false);
		}	
	});

});

/*this function doesn't work yet*/
function validateIssue(){
		/*validate*/
		var error = "";
		alert("submit pushed");
		if($("#organism :selected").val() == 0){
			error += "You must select an organism\n";
		}	
		if($("#email").val().length == 0){
			error += "Please provide a valid email address\n";
		}
		var e_val = emailValidate($("#email").val());
		if(e_val == false){
			error += "Please check your email address, is it valid?\n";
		}
		if($("#affiliation").val().length ==0){
			error += "Please provide your affiliation\n";
		}
		if($("#position :selected").text() == "select one"){
			error += "Please select a position\n";
		}
		if($("#desc").val().length ==0){
			error += "Please provide a detailed description of the problem\n";
		}
		alert("WTF: " + error); 
		if(error.val().length != 0){
			alert($error);
			return false;
		}
		return true;
}

/*this is old validation function, need to rework in jquery*/
function validateIssueSubmit() {
  var organism = document.getElementById("organism");
  var email = document.getElementById("email");
  var affiliation = document.getElementById("affiliation");
  var position = document.getElementById("position");
  var desc = document.getElementById("description");
  var loc_type = document.getElementsByName("range_type");
  var chr = document.getElementById("chr");
  var chr_start = document.getElementById("chr_start");
  var chr_stop = document.getElementById("chr_end");
  var error = "";

  if(organism.selectedIndex == 0){
    error += "You must select an organism.";
    alert("You must select an organism to continue");
    organism.focus();
    return false;
  }
  if(email.value.length == 0){
    error += "Please provide your email address\n";
  }
  if(email.value.length >0 && emailValidate(email) == false){
    error += "Check email address: is this valid?\n";
  }
  if(affiliation.value.length == 0){
    error += "Please provide you affiliation\n";
  }
  if(position.selectedIndex == 0){
    error += "Please select a Position\n";
  }
  if(error != ""){
    error += "\nPlease provide some information about yourself so that we may contact you.\n";
    alert(error);
    email.focus();
    return false;
  }
  if(desc.value.length == 0){
    error += "Please provide detailed description of the issues.\n";
    alert(error);
    desc.focus();
    return false;
  }
 return true;
}

validateUserMail=function(){
  if(!document.getElementById)
    return true; //no javascript;

  var institution = document.getElementById('institution');
  var position = document.getElementById('position');
  var mail = document.getElementById('mail');
  var message = document.getElementById('message');
  var error="";

  if(institution.value.length == 0){
    error += "Required: Institution.\n";
  }
  if(position.selectedIndex == 0){
    error += "Required: Position.\n";
  }
  if(mail.value.length == 0){
    error += "Required: email address.\n";
  }
  if(mail.value.length>0 && emailValidate(mail) == false){
    error += "Check email address: is this valid?\n";
  }
  if(message.value.length == 0){
    error += "Required: request. \n";
  }
  if(error != ""){
    alert(error);
    return false;
  }
  else{
    return true;
  }
}


function populateBuild(menu, data){
	if(data.length >0){
		menu.empty();
		$.each(data, function(){
			menu.append("<option value="+this.value+">"+this.name+"</option>");
			
		});
	}
}

function populateChr(menu, item){
	var url="/projects/genome/assembly/overlap/getchr.cgi?taxid="+item;
	$.ajax({
		url: url,
		type: "GET",
		dataType: "text",	
		timeout: 10000,
		error: function(){
			alert("failed to get chromosomes");
		},
		success: function(text){
			var chr_list = text.split(",");
			menu.empty();
			$.each(chr_list, function(){
			menu.append("<option value="+this+">chr"+this+"</option>");
			});
		}
	});
}

function emailValidate(elem){
	var reg = /^([A-Za-z0-9_\-\.])+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var address = elem.value;
	if(reg.test(address) == false){
		return false;
	}
	return true;
}
