$(document).ready(function() {
  $("a.ckeckserial").click(function () {
    var id = $(this).attr("id");
//    $("#checkserial input[name=id]").val(id);
    open_popup('/popups/check-serial-'+id+'.html', 500, 100);
    return false;
  });
  $("a.directdownloads").click(function () {
    var id = $(this).attr("id");
    $("#checkserial input[name=id]").val(id);
    $("#checkserial input[name=serial]").val("-");
    $("#checkserial").submit();
    return false;
  });
  $("#checkserial").ajaxForm({
	clearForm: true,
	dataType: "html",
	beforeSubmit: function(formData, jqForm, options) {
 	  var success = true;
	  var form = jqForm[0];
	  messages = new Array();
	  for (var i=0; i < formData.length; i++) {
		var element = form.elements[formData[i].name];
		if (!formData[i].value && element.title != "") {
			messages.push("You should fill in " + element.title);
			success = false;
		}
	  }
	  if(!success) {
		alert(messages.join("\\n"));
	  }
      return success;
    },
    success: function(text){
	  if(text.substring(0, 6) == "<html>") {
		  if(opener == null) {
			  document.write(text);
		  } else {
		      opener.document.write(text);
  		      window.close();
		  }
	  } else if(text.substring(0, 9) == "DOWNLOAD:") {
		  if(opener == null) {
			  window.location = text.substring(9);
		  } else {
	        opener.location = text.substring(9);
    	    window.close();
		  }
      }
    }
	});
});

