// @ Code by Carl Magno for Worldtourism.com.au
function launchCenter(url, name, height, width, scrolling, resize) {
	var popupNow = null;
	var winObject = popupNow;
	var str;
	if (height == 'fullscreen') {
		str = "type=fullWindow,fullscreen,scrollbars=" + scrolling + ",resizable=" + resize;
	}
	else {
		str = "height=" + height + ",innerHeight=" + height;
		str += ",width=" + width + ",innerWidth=" + width + ",scrollbars=" + scrolling + ",resizable=" + resize;	
		
		if (window.screen) {
			var ah = screen.availHeight - 30;
			var aw = screen.availWidth - 10;
			
			var xc = (aw - width) / 2;
			var yc = (ah - height) / 2;
			
			str += ",left=" + xc + ",screenX=" + xc;
			str += ",top=" + yc + ",screenY=" + yc;
		}
	}

	if (winObject == null || winObject.closed)
		winObject = window.open(url, name, str);
	else {
		winObject.location = url;
	}
	winObject.focus();
}

function setAllOptions(selectID, clickID, cCount){

	var selectOption = document.getElementById(clickID).checked;
	
	for (x = 0; x < cCount; x++) {
		if (selectOption == true)
			document.getElementById(selectID + x).checked = true;
		else if (selectOption == false)
			document.getElementById(selectID + x).checked = false;
	}
}

function promptRedirect(warnMsg, location, returnMsg){
	var returnVal;
	returnVal = window.confirm(warnMsg);
	if (returnVal){
		alert(returnMsg);
		window.location = location;
	}
}

// Auto-tab code
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autotab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}

function setValue(type, selectName, value, bDisabled)
{
	var x = eval(document.getElementById(selectName));
	// 1:select 2:text 3: blah 4: innerhtml
	if (type == 1){
	for (index = 0; index < x.options.length; index++){
		if  (x.options[index].value == value)
			x.options.selectedIndex = index;
	}
	}else if (type == 2){
		x.value = value;
	}else if (type == 3){
		for (index = 0; index < x.options.length; index++)
		if  (x.options[index].value == value)
			x.options[index].selected = true;
	}else if (type == 4){
		x.innerHTML = value;	
	}else if (type == 5){
		if (value == 'True') { val2 = 1; } else { val2 = 0; }
		for (index = 0; index < x.options.length; index++)
			if  (x.options[index].value == val2)
				x.options.selectedIndex = index;
	}
	
	if (bDisabled == 1)	x.disabled = true; else x.disabled = false;
}

function showPanel(myID){ document.getElementById(myID).style.display = "inline"; }
function hidePanel(myID){ document.getElementById(myID).style.display = "none";}
function togglePanel(id1, id2) {
	var eID1 = document.getElementById(id1);
	var eID2 = document.getElementById(id2);

	if ((eID1.style.display == "") || (eID1.style.display == "inline")) {
		eID1.style.display = "none";
		eID2.style.display = "inline";
	} else {
		eID2.style.display = "none";
		eID1.style.display = "inline";
	}
		
}


function setCaretToEnd (control) {
  if (control.createTextRange) {
    var range = control.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (control.setSelectionRange) {
    control.focus();
    var length = control.value.length;
    control.setSelectionRange(length, length);
  }
}
 
function moveCaret(cPos, cKey, cThisID){
 
	var boxLen = document.getElementById(cThisID).value.length;
	var boxIDStr = cThisID.toString();
	var boxIDLen = boxIDStr.length;
	var boxID = boxIDStr.substr(0, (boxIDLen - 1));
    



	// if len == 4, move on to the next tab
	if (boxLen == 4) {
		if (cPos != 4) { 
			var x = document.getElementById(boxID + (cPos+1));  x.focus(); }
	}
	// if backspace is used.
	if ((boxLen == 0) && (cKey == 8)) {
	   if (cPos != 1) { var x = document.getElementById(boxID + (cPos-1)); x.focus(); setCaretToEnd(x); }
	}
}


function GetXmlHttpObject(handler)
{ 
	var xmlhttp=false;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		xmlhttp.onreadystatechange = handler;
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			xmlhttp.onreadystatechange = handler;
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
		
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onload = handler;
		xmlhttp.onerror = handler;
	}
	
	return xmlhttp;
} 
  
function getDetails(thisCheck) {
	var cValue = thisCheck.checked;
	
	if (cValue == true)
		getPage();
	else {
		setValue(2, "bName", ""); setValue(2, "bAddress1", "");	setValue(2, "bAddress2", "");
		setValue(2, "bSuburb", ""); setValue(2, "bState", ""); setValue(2, "bPostcode", "");
		setValue(2, "bPhoneHome", ""); setValue(2, "bPhoneWork", ""); setValue(2, "bMobile", "");
		setValue(2, "bFax", ""); setValue(2, "bEmail", "");
	}
		
}

function showHide(z, sorh) {
	
	if (sorh == 1)
		document.getElementById(z).style.display='inline';
	else
		document.getElementById(z).style.display='none';
		
}

function showHideMe(z)
{
	if ((document.getElementById(z).style.display=='inline') || (document.getElementById(z).style.display=='block')) {
		document.getElementById(z).style.display = 'none';
	}
	else {
		document.getElementById(z).style.display = 'inline';
	}
}


function goto(url)
{
	document.location.href=url;	
}

function buildChildrenAges(cCount, ChildAgeCount, divChildren, ChildAge,ChildAges) {      
	
//	var xChildren = document.getElementById(cChildren);
//	var xChildIdx = xChildren.selectedIndex;	
//	var cCount = xChildren[xChildIdx].value;	

	var txt = itxt = "";	
	if (cCount == 0) {	
		  document.getElementById(ChildAgeCount).value = 0;	
		  document.getElementById(divChildren).innerHTML = "<select class=\"bSelA\" name=\"" + ChildAge + "0\" id=\"" + ChildAge + "0\" style=\"margin-right: 5px\" disabled=\"disabled\"><option value=\"0\">No Children</option></select>";	
		  document.getElementById(ChildAges).value = "";
	} else {      	
		  document.getElementById(ChildAgeCount).value = cCount;	
		  var y = 0;	
		  itxt = itxt + "<option value=\"0\">&nbsp;</option>";	
		  for (y = 2; y < 19; y++) itxt = itxt + "<option value='" + y + "'>" + y + "</option>";
		  var childString;
		  var childC=0;
		  if(document.getElementById(ChildAges).value != "")
		  {
			  childString = document.getElementById(ChildAges).value.split("-");
			  childC = childString.length;
		  }
		 var ExistingCount=0	

		  for (var x = 0; x < cCount; x++) {	
				 txt = txt + "<select class=\"bSelA\" onchange='SetHiddenChildAges(\"" + ChildAges + "\", " + cCount + ", \"" + ChildAge + "\");' name=\"" + ChildAge  + x + "\" id=\"" + ChildAge + x + "\" style=\"margin-right: 5px\">";
				if(childC-ExistingCount>0)
				{
					txt = txt + itxt.replace("value='" + childString[ExistingCount] + "'", "value='" + childString[ExistingCount] + "' selected='selected'") + "</select>";	
					ExistingCount +=1;
					
				}
				else
				{
					txt = txt + itxt + "</select>";	
				}
				if(cCount<childC)//If they are now choosing less rooms. repopulate the hidden ages with correct amount of kids.
				{
					var NewString = "";
					for(i=0;i<cCount;i++)
					{
						if(NewString!="")
						{
							NewString = NewString + "-";
						}
						NewString = NewString+childString[i];
					}
					document.getElementById(ChildAges).value = NewString;					
				}
				document.getElementById(divChildren).innerHTML = txt;					 
		  }  	
	}
}
function SetHiddenChildAges(ChildAges, cCount, ChildAge)
{


	var xChild = document.getElementById(ChildAges);
	xChild.value = "";
	for(var i=0;i<cCount;i++)
	{
		var xCh = document.getElementById(ChildAge + i);
		var xChIdx = xCh.selectedIndex;

		if (xChIdx>0)
		{
			xChild.value += "-" + xCh[xChIdx].value;	
		}
		
	}
	if(cCount>0)
	{
		xChild.value = xChild.value.substring(1);		
	}
	else
	{
		xChild.value = "";
	}
}
function GetLocation(Params)
{
	//alert(Params + "&cChildAges=" + document.getElementById("cChildAges").value);
	if(document.getElementById("cChildAges").value !="")
	{
		document.location = Params + "&cChildAges=" + document.getElementById("cChildAges").value;
	}
	else
	{
		document.location = Params;
	}
	
}
function OpenBeforeCenter(url, name, height, width, scrolling, resize, ChildAges) {
	var x=url;
	if(document.getElementById(ChildAges).value !="")
	{
		x = x + "&cChildAges=" + document.getElementById(ChildAges).value+ "&ChildAges=" + document.getElementById(ChildAges).value;
	}
	else
	{
		x = url;
	}
	launchCenter(x, name, height, width, scrolling, resize);
}
