// are regular expressions supported?
 var supported = 0;
 if (window.RegExp) {
  var tempStr = "a";
  var tempReg = new RegExp(tempStr);
  if (tempReg.test(tempStr))
     supported = 1;
 }

// function with thanks to webreference & javascriptkit
function isEmail(str) {

  if (!supported)
	  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

  var filter=/^([\w-]+(?:\.[\w-+]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  filter=/^[-+_.a-z0-9]+@(?:(?:(?:[a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])\.)+(?:[a-z]+)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i
  return (filter.test(str));
}


// - - - - - - ( event listener ) - - - - - - - -
// by Scott Andrew - http://scottandrew.com
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function addEvent(obj, evType, fn, useCapture) {
 if (obj.addEventListener)
  {
	obj.addEventListener(evType, fn, useCapture);
	return true;
  }
 else if (obj.attachEvent) {
	var r = obj.attachEvent('on'+evType, fn);
	return r;
  }
 else {
	return false;
  }
}


function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
    return false;
  }
}


var formID = 'new-project-req';


// ottomh
if(typeof(preventDefault) == 'undefined') {
function preventDefault() {}
}

function fd(e) {
   e.preventDefault();
   return false;
}

function preFlightCheck(e) {
// probably needs refinement - grouped error messages ?

  var f = $(formID);
  var input = $('start');
  var n = $('name');
  var pD = $('projectDescription');
  var pN = $('projectName');

  if(!pN.value || pN.length < 2 || pN.value.match(/(zilla)|[^a-z0-9]/gi)) {
    alert('Please check that project name is at least 2 characters, '
        + 'and contains no spaces, hyphens or underscores, '
        + 'nor the phrase "zilla".'); pN.focus();
    fd(e);
   return false;
  }


  if(!pD.value || !pD.value.match(/[\s]/g)) {
    alert('Please check that project description contains at least two words.');
    pD.focus();
    fd(e);
   return false;
  }
  if(pD.value.match(/(busca|search|suche)/gi) && pD.value.match(/(plug)-?(in)/gi)) {
    alert("Please review your project's purpose. Search plugins should be added to the Mycroft project.");
    pD.focus();
    fd(e);
   return false;
  }


  if(!n.value || !n.value.match(/[\s]/g)) {
    alert('Please enter your full name.'); n.focus();
    fd(e);
   return false;
  }

  if (!isEmail(f.email.value)) {
    alert("Please enter a valid email address");
    f.email.focus();
    fd(e);
   return false;
  }

  if(($('contrib-name-0').value != '' && $('contrib-email-0').value == '') ||
     ($('contrib-name-0').value == '' && $('contrib-email-0').value != '') ||
     ($('contrib-name-1').value != '' && $('contrib-email-1').value == '') ||
     ($('contrib-name-1').value == '' && $('contrib-email-1').value != '') ||
     ($('contrib-name-2').value != '' && $('contrib-email-2').value == '') ||
     ($('contrib-name-2').value == '' && $('contrib-email-2').value != '')) {
    alert("Please provide all contributor details");
    fd(e);
   return false;
  }

  if (!$('t-and-c').checked) {
    alert("Please ensure you have read and understand both the Terms of Use and the Copyright Policy.");
     $('t-and-c').focus();
    fd(e);
   return false;
  }


  input.disabled = true;

  return true;

}



function hideAllAJAX() {
 var resultsDiv  = $(resultsDivId);

	resultsDiv.style.display = 'none';
}



function unHide() {
var start = $('start');
var tandc = $('t-and-c');

if(tandc.checked) {
start.style.display = 'inline';
} else {
start.style.display = 'none';
}

}


function zeroth() {

var czech = $('t-and-c');
var n_p_form = $(formID);
var start    = $('start');

addEvent(czech, 'change', unHide, false);

addEvent(n_p_form, 'submit', preFlightCheck, false);
addEvent(n_p_form, 'click', hideAllAJAX, false);

// hide the submit button for the form
if(!czech.checked) {
start.style.display = 'none';
}

runLSPrep();

}

function zenith() {

var i, ip;
var czech = $('t-and-c');
var n_p_form = $(formID);
var searchInput = $(searchInputId);

removeEvent(n_p_form, 'submit', preFlightCheck, false);
removeEvent(n_p_form, 'click', hideAllAJAX, false);

removeEvent(czech, 'change', unHide, false);


removeEvent(document, 'keydown', escapeReset, false);

for(var i = 0; i < ips.length; i++) {

	searchInput = $(ips[i]);

	removeEvent(searchInput, 'focus', liveSearchHide, false);

	removeEvent(searchInput, 'keyup', liveSearchStart, false);

	removeEvent(searchInput, 'blur',  resetOnBlur, false);
	}

for(i = 0; i < a_ips.length; i++) {

	searchInput = $(a_ips[i]);

	removeEvent(searchInput, 'focus', liveSearchHide, false);
	}

}


window.onload   = zeroth;
window.onunload = zenith;
