function disable(el) {  for (var i=0; i<el.length; i++) document.getElementById(el[i]).disabled=true; }
function enable(el) {  for (var i=0; i<el.length; i++) document.getElementById(el[i]).disabled=false; }
function showInline(element, state) {
  if (state==true) document.getElementById(element).className='visibleInline';
  else document.getElementById(element).className='hidden';
}

function showBlock(element, state) {
  if (state==true) document.getElementById(element).className='visibleBlock';
  else document.getElementById(element).className='hidden';
}

function uncomplete(id) { 
  document.getElementById(id).className='uncomplete';
  document.getElementById(id).scrollIntoView(false); 
}
function complete(id) { document.getElementById(id).className=''; }
function selectBoxValue(id) { 
  if (!document.getElementById(id)) { return ''; }
  else {
    return document.getElementById(id)[document.getElementById(id).selectedIndex].value; 
  }
}

function checkbox(id, items) {
  for (var i=1; i<=items; i++) {
    if (document.getElementById(id+i).checked==true) { 
      complete(id);
      return true;
    }
  }
  uncomplete(id);
  return false;
}
function textbox(id) {
	if (!document.getElementById(id)) alert(id);
	if (document.getElementById(id).value!='') { 
	  complete(id);
	  return true;	
	}
	uncomplete(id);
	return false;
}

function check() {
  //replace characters that may cause problems
  var re=/["\\]/g;
  for (var k=0; k<document.forms[0].elements.length; k++){ 
	if(document.forms[0].elements[k].value) { 
	  document.forms[0].elements[k].value=document.forms[0].elements[k].value.replace(re,'?');
	}
  }

  var pass=true;
//part 5
  if (!textbox("password2")) pass=false;
  if (!textbox("password")) pass=false;
  if (!textbox("email2")) pass=false;
  if (!textbox("email")) pass=false;

  if (!textbox("city")) pass=false;
  if (!textbox("country")) pass=false;

  if (!textbox("adults")) pass=false;
  if (!textbox("children")) pass=false;
  if (!textbox("infants")) pass=false;
  if (document.getElementById("person6").checked) {
    if (!textbox("p6Nationality")) pass=false;
    if (!textbox("p6Surname")) pass=false;
    if (!textbox("p6Name")) pass=false;
  }
  if (document.getElementById("person5").checked) {
    if (!textbox("p5Nationality")) pass=false;
    if (!textbox("p5Surname")) pass=false;
    if (!textbox("p5Name")) pass=false;
  }
  if (document.getElementById("person4").checked) {
    if (!textbox("p4Nationality")) pass=false;
    if (!textbox("p4Surname")) pass=false;
    if (!textbox("p4Name")) pass=false;
  }
  if (document.getElementById("person3").checked) {
    if (!textbox("p3Nationality")) pass=false;
    if (!textbox("p3Surname")) pass=false;
    if (!textbox("p3Name")) pass=false;
  }
  if (document.getElementById("person2").checked) {
    if (!textbox("p2Nationality")) pass=false;
    if (!textbox("p2Surname")) pass=false;
    if (!textbox("p2Name")) pass=false;
  }
  if (!textbox("p1Nationality")) pass=false;
  if (!textbox("p1Surname")) pass=false;
  if (!textbox("p1Name")) pass=false;

//part 4
  if (selectBoxValue("nativeCurrency")=='') {
	  pass=false;
	  uncomplete("nativeCurrency");
  }
  else complete("nativeCurrency");

  if (selectBoxValue("budget")=='') {
	  pass=false;
	  uncomplete("budget");
  }
  else complete("budget");
  
  if (!textbox("returnCountry")) pass=false;
  if (!textbox("returnCity")) pass=false;
  var departureDate = null;
  if ((selectBoxValue("returnDay")=='00' || selectBoxValue("returnMonth")=='00') && (document.getElementById("tripLength").value=='# of days/weeks' || document.getElementById("tripLength").value=='')) {
	pass=false;
	uncomplete("returnDay");
	uncomplete("returnMonth");
	uncomplete("tripLength");
  }
  else {
	if (selectBoxValue("returnMonth")!='00') {
		departureDate = selectBoxValue("returnYear")+''+selectBoxValue("returnMonth");
	}
	complete("returnDay");
	complete("returnMonth");
	complete("tripLength");
  }
  if (!textbox("departureCountry")) pass=false;
  if (!textbox("departureCity")) pass=false;
  if ((selectBoxValue("departureDay")=='00' || selectBoxValue("departureMonth")=='00') && selectBoxValue("unknownDepartureMonth")=='00')	{
	pass=false;
	uncomplete("departureDay");
	uncomplete("departureMonth");
	uncomplete("unknownDepartureMonth");
  }
  else {
	if (departureDate != null) {
		var returnDate;
		if (selectBoxValue("departureMonth")!='00') returnDate = selectBoxValue("departureYear")+''+selectBoxValue("departureMonth");
		else returnDate = selectBoxValue("unknownDepartureYear")+''+selectBoxValue("unknownDepartureMonth");
		
		var depDT = new Date();
		var retDT = new Date();
		
		//depDT.setFullYear(parseInt(selectBoxValue("departureYear")), parseInt(selectBoxValue("departureMonth")), parseInt(selectBoxValue("departureDay")));
		//retDT.setFullYear(parseInt(selectBoxValue("returnYear")), parseInt(selectBoxValue("returnMonth")), parseInt(selectBoxValue("returnDay")));

		
//		alert(parseInt(returnDate));
//		alert(parseInt(departureDate));
//		alert(parseInt(returnDate) - parseInt(departureDate));
//		if (parseInt(returnDate) - parseInt(departureDate) < 0) {
		if ((Date.parse(departureDate)) >= (Date.parse(returnDate))) {//(parseInt(returnDate) - parseInt(departureDate) < 0) {
			alert("The departure date is after the return date, please check this before submitting");
			uncomplete("returnDay");
			uncomplete("returnMonth");
			uncomplete("tripLength");			
		}
	}
	complete("departureDay");
	complete("departureMonth");
	complete("unknownDepartureMonth");
  }

//part 3
  document.getElementById("airD").className='';
  document.getElementById("airT").className='';
  if (document.getElementById("airdom1").checked && !checkbox("airD",3)) pass=false;  
  if (document.getElementById("air1").checked && !checkbox("airT",3)) pass=false;  
//part 2
  if(!checkbox("bed",2)) pass=false;
  if(!checkbox("sm",2)) pass=false;
//part 1
  if(!checkbox("h",2)) pass=false;
  if(!checkbox("t",3)) pass=false;
  if(!checkbox("c",2)) pass=false;
  if(!checkbox("a",8)) pass=false;
  if(!checkbox("d",4)) pass=false;
//finished
  if (!pass) {
	  alert('You have missed filling in some required fields.\n\nThese have been marked in orange for you');
      return false;
  }
  else if (document.getElementById("email").value!=document.getElementById("email2").value) {
	  alert('Your confirmation email address does not match your original email address\n\nPlease enter them again');
	  document.getElementById("email").value='';
	  document.getElementById("email2").value='';
	  uncomplete("email");
	  uncomplete("email2");
	  return false;
  }
  else if (document.getElementById("password").value!=document.getElementById("password2").value) {
	  alert('Your confirmation password does not match your original password\n\nPlease enter them again');
	  document.getElementById("password").value='';
	  document.getElementById("password2").value='';
	  uncomplete("password");
	  uncomplete("password2");
	  return false;
  }
  else return true;
}

function init() {
  var hide = ["conventions","gayAccom","flights","flightlist","flightlist2","p3","p4","p5","p6","dis","evOther"];
  for (var i=0; i<hide.length; i++) document.getElementById(hide[i]).className='hidden';
}

//Get Selected Option from SelectList
function getOption(field) { return document.getElementById(field)[document.getElementById(field).selectedIndex].value; }
function getOptions(field) {  //gets multiple selections and appends them together and returns the result
  var list = document.getElementById(field);
  var string='';
  for (var i=0; i<list.length; i++) {
	  if (list[i].selected==true) string+=list[i].value;
  }
  return string;
}
function getCheckboxes(field, number) {
	var string='';
	for (var i=1; i<=number; i++) {
		var box = document.getElementById(field+i);
		if (box.checked == true) string+=box.value;
	}
	return string;
}

//Date Function - gets year-month-year hours:minutes:seconds format for hiden field
function updateDateTime(field) {
  var value = getOption(field+'Year')+'-'+getOption(field+'Month')+'-'+getOption(field+'Day');
  if (document.getElementById(field+'Hour')) value+=' '+getOption(field+'Hour')+':'+getOption(field+'Minute')+':00';
  document.getElementById(field).value = value;
}
function updateEvent(field) {
  var value = getOption(field+'Day')+' '+getOption(field+'Month')+' '+document.getElementById(field+'Year').value;
  document.getElementById(field).value = value;
}
