//SUPPRESS ALL JAVASCRIPT ERRORS
function silentErrorHandler() {return true;}
window.onerror=silentErrorHandler;


//ACTION COMFIRMATION
function confirmAction(myURL,myMES){
	var res = confirm(myMES);
	if(res){
		location.href=myURL;
	}
}

//Add more fields dynamically.
function addField(area,field,limit) {
	if(!document.getElementById) return; //Prevent older browsers from getting any further.
	var field_area = document.getElementById(area);
	var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area.
	//Find the count of the last element of the list. It will be in the format '<field><number>'. If the 
	//		field given in the argument is 'friend_' the last id will be 'friend_4'.
	var last_item = all_inputs.length - 1;
	var last = all_inputs[last_item].id;
	var count = Number(last.split("_")[1]) + 1;
	
	//If the maximum number of elements have been reached, exit the function.
	//		If the given limit is lower than 0, infinite number of fields can be created.
	if(count > limit && limit > 0) return;
 	
	if(document.createElement) { //W3C Dom method.
		var li = document.createElement("li");
		var input = document.createElement("input");
		input.id = field+count;
		input.name = field+count;
		input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc.
		li.appendChild(input);
		field_area.appendChild(li);
	} else { //Older Method
		field_area.innerHTML += "<li><input name='"+(field+count)+"' id='"+(field+count)+"' type='text' /></li>";
	}
}

//Add more stops address fields dynamically.
function addStops(area) {
	if(!document.getElementById) return;
	var limit = 5;
	var field_area = document.getElementById(area);
	var counthidden = document.getElementById("stops_counter").value*1;
	var count = counthidden + 1;
	if(count > limit && limit > 0) return;
	field_area.innerHTML += "<li>Address "+count+": <input name='stops_address_"+count+"' id='stops_address_"+count+"' type='text' style='width:82%' />  <a onclick=\"document.getElementById('stops_address_"+count+"').value='';\">[X]</a></li>";
	//field_area.innerHTML += "<li><input name='stops_address_"+count+"' id='stops_address_"+count+"' type='text' /> <input name='stops_postcode_"+count+"' id='stops_postcode_"+count+"' type='text' /></li>";
	document.getElementById("stops_counter").value = count;

}

//wait
function wait(delay){
	string="pauseforalert("+delay+");";
	setTimeout(string,delay);
}
//AJAX CALLS
//update address dropdown in booking form
function updateAddressSelect(aDiv) {
	callAHAH('js_select_address.php?adiv='+aDiv,aDiv,'<span class="ajaxmessage">Updating list - please wait</span>', 'Error');
}

//update card dropdown in booking form
function updateCardSelect(aDiv) {
	callAHAH('js_select_card.php',aDiv,'<span class="ajaxmessage">Updating list - please wait</span>', 'Error');
}

//get address information
function showMyAddress(aDiv,aid,inp) { // aDiv=id of div and aid = addressid from db / inp is the text input
	callAHAH('js_get_address.php?inp='+inp+'&aid='+aid,aDiv,'<span class="ajaxmessage">Loading - please wait</span>', 'Error');
}

//view address information
function showMyAddressView(aDiv,aid) { // aDiv=id of div and aid = addressid from db
	callAHAH('js_view_address.php?aid='+aid,aDiv,'<span class="ajaxmessage">Loading - please wait</span>', 'Error');
}

//update contact dropdown in booking form
function updateContactSelect(aDiv) {
	callAHAH('js_select_phone.php',aDiv,'<span class="ajaxmessage">Updating list - please wait</span>', 'Error');
}


//BOOKING FORM
function makeTwoChars(inp) {
  return String(inp).length < 2 ? "0" + inp : inp;
}

function initialiseDatePickers() {
  var Spickdate = datePickerController.datePickers["pickdate"];
// For Internet Explorer: If they are not created then call this function 500 milliseconds later
  if(!Spickdate) {
		setTimeout("initialiseDatePickers()", 200);
		return;
  }
  var today = new Date();
  today = String(today.getFullYear()) + makeTwoChars(today.getMonth()+1) + makeTwoChars(today.getDate());
  Spickdate.setRangeLow(today);
}

function verifyTime(timeBox){
   time=timeBox.value
   if (!/\d{2}:\d{2}/.test(time)){
		alert('Oops! invalid time format - please try again')
		timeBox.value='';
		timeBox.focus();
		return
   }
   time=time.split(':')
   if (time[0] >23 || time[1]>59) {
		alert('Oops! invalid time format - please try again')
		timeBox.value='';
		timeBox.focus();
		return
   }
}
function showFlight(txt,div) {
	obj = document.getElementById(div);
	var str = document.getElementById(txt).value;
	if (str.search(/airport/i) >= 0) {
		obj.style.display = "block";
	} else {
		obj.style.display = "none";
	}
}

function showFlightEdit(sel,tixt,divA,divB) {

	obj = document.getElementById(divA);
	selobj = document.getElementById(sel).selectedIndex;
	var aid = document.getElementById(sel).options[selobj].value;
	var str = document.getElementById(sel).options[selobj].text;
	showMyAddress(divB,aid,tixt);
	if (str.search(/airport/i) >= 0) {
		obj.style.display = "block";
	} else {
		obj.style.display = "none";
	}
	//document.getElementById(sel).selectedIndex = '';
}

function fillPhoneContact() {
	selobj = document.getElementById('phonebook').selectedIndex;
	var phoneno = document.getElementById('phonebook').value;
	if (phoneno) {
		phonenoarray  = phoneno.split("#");
		document.getElementById('namepassenger').value=phonenoarray[0];
		document.getElementById('phonepassenger').value=phonenoarray[1];
	}
}
	

//NOT USED
function OLDshowFlightEdit(sel,divA,divB) {
	objA = document.getElementById(divA);
	objB = document.getElementById(divB);
	selobj = document.getElementById(sel).selectedIndex;
	var str = document.getElementById(sel).options[selobj].text;
	var aid = document.getElementById(sel).options[selobj].value;
	if (str.search(/airport/i) >= 0) {
		objA.style.display = "block";
		objB.style.display = "none";
	} else {
		showMyAddress(divB,aid);
		objA.style.display = "none";
		objB.style.display = "block";
	}
}

//NOT USED
function OLDshowFlight(sel,div) {
	obj = document.getElementById(div);
	selobj = document.getElementById(sel).selectedIndex;
	var str = document.getElementById(sel).options[selobj].text;
	if (str.search(/airport/i) >= 0) {
		obj.style.display = "block";
	} else {
		obj.style.display = "none";
	}
}

function showcCard(sel,div,divd) {
	obj = document.getElementById(div);
	objd = document.getElementById(divd);
	selobj = document.getElementById(sel).selectedIndex;
	if (document.getElementById(sel).options[selobj].value == 2) {
		obj.style.display = "block";
		objd.style.display = "none";
	} else if (document.getElementById(sel).options[selobj].value == 1) {
		objd.style.display = "block";
		obj.style.display = "none";
	} else {
		obj.style.display = "none";
		objd.style.display = "none";
	}
}


//function to print content of a given div in another popup window
function PrintThisPage() { 
	var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
	sOption+="scrollbars=yes,width=750,height=600,left=100,top=25";    
	var winprint=window.open("print.html","Print",sOption);  
	winprint.focus(); 
}

//popup scrollable start
function myPopupRelocate() {
  var scrolledX, scrolledY;
  if( self.pageYOffset ) {
    scrolledX = self.pageXOffset;
    scrolledY = self.pageYOffset;
  } else if( document.documentElement && document.documentElement.scrollTop ) {
    scrolledX = document.documentElement.scrollLeft;
    scrolledY = document.documentElement.scrollTop;
  } else if( document.body ) {
    scrolledX = document.body.scrollLeft;
    scrolledY = document.body.scrollTop;
  }

  var centerX, centerY;
  if( self.innerHeight ) {
    centerX = self.innerWidth;
    centerY = self.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    centerX = document.documentElement.clientWidth;
    centerY = document.documentElement.clientHeight;
  } else if( document.body ) {
    centerX = document.body.clientWidth;
    centerY = document.body.clientHeight;
  }

  var leftOffset = scrolledX + (centerX - 600) / 2;
  var topOffset = scrolledY + (centerY - 400) / 2;

  document.getElementById("mypopup").style.top = topOffset + "px";
  document.getElementById("mypopup").style.left = leftOffset + "px";
}

function fireMyPopup(uu) {
  myPopupRelocate();
  document.getElementById("mypopup").style.display = "block";
  document.body.onscroll = myPopupRelocate;
  window.onscroll = myPopupRelocate;
  callAHAH('js_get_billing_address.php?usid='+uu,'mypopup','<span class="ajaxmessage">Loading page - please wait</span>', 'Error');
}
//popup scrollable end