function ord (n) {
  if ((n != 11) && (n%10 == 1)) return n+"st"; 
  if ((n != 12) && (n%10 == 2)) return n+"nd"; 
  if ((n != 13) && (n%10 == 3)) return n+"rd";
  return n+"th"; 
}

function removeChildren(node) {
  var children = node.childNodes;
  if (children.length == 0) return;
  for (var i = 0; i < children.length; i++) {
    node.removeChild(children[i]); 
  }
  node.innerHTML= "";
}

function createHidden(name, value) {
	var el = document.createElement("input");
	el.setAttribute("type", "hidden");
	el.setAttribute("name", name);
	el.setAttribute("value", value);
	return el;
}

function makeSecure(form) {
  if ((document.location.href.indexOf(":443") == -1)) {
    var formUrl = document.location;
    form.action = "https://"+document.location.hostname+"/"+document.location.pathname;
  }
}

function setBackgroundCol(el, col) {
  if (typeof el != "object") return;
  if (el == null) return;
  if ((el.nodeName) && (el.nodeName == "TD"))
    el.style.backgroundColor = col; 
  for (var e = 0; e < el.childNodes.length; e++) {
    if (el.childNodes[e].style)
      setBackgroundCol(el.childNodes[e], col);
  }
}

function formatNum (num) {
  var trailingString, intNum, formattedNum;
  // Create an integer by multiplying the number by the 
  // base to the power (number of decimal places)...
  intNum = Math.round( (parseFloat(num)+0.0049) * 100 );
  if ( (intNum/100) == (Math.round(intNum/100)) ) {
    // Then we have two decimal places
    trailingString = ".00";
  } else if ( (intNum/10) == (Math.round(intNum/10)) ) {
    // Then we have one decimal place
    trailingString = "0";
  } else  {
    // We have no decimal places
    trailingString = ""; 
  }
  formattedNum = intNum/100;
  formattedNum += trailingString;
  return formattedNum;
}

function setLabel(divId, text) {
  if (document.getElementById) {
    var label = document.getElementById(divId);
    if (label != null) label.innerHTML = text;
  }
}

function _abandon(){
  for (var i = 0; i < document.forms.length; i++) {
    if (document.forms[i].SessionId) {
      _real_abandon(document.forms[i]);
      return;
    }     
  }
}

function _real_abandon(form){
  var Xpos=(screen.availWidth - 50)/2;
  var Ypos=(screen.availHeight - 50)/2;
  var abandonWin = window.open("","_ats_abandon","width=50,height=50");//left="+Xpos+",top="+Ypos+",screenX="+Xpos+",screenY="+Ypos);
  var abandonDoc = "<html><body><form action=\"WebResServlet\" method=\"post\">";
  abandonDoc += "<input type=hidden name=baseName value="+form.baseName.value + ">";
  abandonDoc += "<input type=hidden name=SessionId value="+form.SessionId.value + ">";
  abandonDoc += "<input type=hidden name=msgNo value="+form.msgNo.value + ">";
  abandonDoc += "<input type=hidden name=step value=abandon>";
  abandonDoc +=" </form></body></html>"
  abandonWin.document.write(abandonDoc);
  abandonWin.document.forms[0].submit();
  window.focus();
}
  
function doPax(dynamicFields) {
  var form = document.FSSform;
  if (typeof form.paxCount_AD != "object") return true; // No pax fields
  var totalPax = 0;
  var totalAdults = 0;
  var kids = parseFloat(getDropDownValue(form.paxCount_DCH));

  for (var i = 0; i < form.elements.length; i++){
    if (form.elements[i].name.indexOf("paxCount_") != -1) {
      var ptcCode = form.elements[i].name.substring(9);
      var ptcQty = parseFloat(getDropDownValue(form.elements[i]));
      totalPax += ptcQty;
      if (ptcCode != "DCH") totalAdults += ptcQty;
      if (ptcQty > 0){
        dynamicFields.appendChild(createHidden("ptcCode", ptcCode));
        dynamicFields.appendChild(createHidden("ptcQty", ptcQty));
      }
    }
  }
  var msg = "";
  for (var i = 0; i < kids; i++) {
    var age = parseFloat(getDropDownValue(form._childAge[i]));
    if (age > 11) totalAdults ++; // Adults are people 12 or older
    dynamicFields.appendChild(createHidden("childAge", age));
    if (getDropDownValue(form._childAge[i]) == -1){
      msg = "You must enter the age of all child passengers.";
    }
  }

  if (totalPax > 9) msg = "Maximum passenger count exceeded.\n\nWe can only accept a maximum of 9 passengers per booking on the internet.";
  else if (totalPax < 1) msg = "Please select at least one passenger to travel.";
  else if (totalAdults <1) msg = "Unaccompanied minors.\n\n At least one passenger must be over the age of 12.";
  
  if (msg != "") {
    alert(msg); 
    return false;
  } else {
    return true; 
  }
}

function showAges(childDDB) {
  var form = document.FSSform;
  if (form.paxCount_DCH != null){
    _showAges(childDDB);
    var kids = parseFloat(getDropDownValue(form.paxCount_DCH));
    for (var i = 1; i < 10; i++) {
      var age_dd = document.getElementById("_childAge_"+i);
      if (i > kids) age_dd.style.display = "none";
      else age_dd.style.display = "inline";
    }  
  }
} 

function changeStyle(selector, fieldName, value) {
  if (document.styleSheets.changeableStyles != null) {
    if (_changeStyle(selector, fieldName, value, document.styleSheets.changeableStyles)) {
      return true;    
    }
  }
  
  for (var sheet = 0; sheet < document.styleSheets.length; sheet++){
    if (_changeStyle(selector, fieldName, value, document.styleSheets[sheet]))
      return true;
  }
  return false;
}

function _changeStyle(selector, fieldName, value, style_sheet) {
  if (typeof style_sheet.rules == "object") {
  for (var ruleItem = 0;ruleItem < style_sheet.rules.length; ruleItem++){
    if (style_sheet.rules[ruleItem].selectorText.indexOf(selector) != -1) {
      field = eval("style_sheet.rules[ruleItem].style."+fieldName + "= \"" + value+"\"");
        return true;
      }
    }
  } else if (typeof classes == "object") {
    eval ("classes"+selector+".all."+fieldName+" = \""+value+"\"");    
  }
  return false;
}
  
