//
// global variable
var testmode=true;
var isIE = (navigator.userAgent.indexOf("MSIE") >= 0);

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

function showHide(id, show) {
        el = document.getElementById(id);
        if (!el) {return; }
        if (show) {
                el.style.display = '';
        }else {
                el.style.display = 'none';
        }
}
/*
  show login option
*/
function showSignInOption(prefix,index){
    for (var i = 1 ; i <= 3 ; i++){
    if (i == index){
      document.getElementById(prefix+i).style.display= '';
    }else{
        document.getElementById(prefix+i).style.display= 'none';
    }
  }
  }

/*
alter pax display option
*/
function alterPaxBox(paxIndex, imgElement){
	if (imgElement.src.indexOf("collapse") >= 0 ){
	    var title = valueOf("title_"+paxIndex);
	    var firstName = valueOf("first_name_"+paxIndex);
	    var lastName = valueOf("last_name_"+paxIndex);
	    // check for the minimum information availability
		if (title.length> 0 && firstName.length > 0 && lastName.length > 0){
		    document.getElementById("collapsedPaxBlock["+paxIndex+"]").innerHTML = lastName +"/"+firstName + " " + title; 
			collapseExpand("pax_box_"+paxIndex, imgElement);
		}else{
			// please enter title, first name and last name atlease before to hide
			alert("Please fill in title, first name and last name");
		}
		
	}else{
		collapseExpand("pax_box_"+paxIndex, imgElement);
	}
	
}
/*
  set the value of a particular html element
*/
function setValue(toSetEl, value){
	document.getElementById(toSetEl).value = value;
}
/*
* return the value of particular html element value
*/
function valueOf(elementId){
	return document.getElementById(elementId).value;
}
/*
 collapse and expand a section
*/
function collapseExpand(sectId, imgEl){
	if (!imgEl) {return; }
	if (imgEl.src.indexOf("collapse") >=0){
		showHide(sectId + '_fewer', true);
		showHide(sectId + '_more', false);
		imgEl.src = "rsrc/expand.gif";
		imgEl.alt = "expand";
	}else{
		showHide(sectId + '_fewer', false);
		showHide(sectId + '_more', true);
		imgEl.src = "rsrc/collapse.gif";
		imgEl.alt = "collapse";
	}
}

/*
 collapse and expand a section
*/
function showHideOption(sectId, aId){
   	var sectEl = document.getElementById(sectId);
	if (!sectEl){ return; }
	var aEl = document.getElementById(aId);
	if (!aEl) {return; }
	if (aEl.innerHTML.indexOf("show") >=0){
		sectEl.style.display = "";
		aEl.innerHTML = "hide options";
	}else{
		sectEl.style.display = "none";
		aEl.innerHTML = "show options";
	}
}
/*
Toggle an element
*/
function toggleElement(elId){
	var el = document.getElementById(elId);
	if (!el){
		return;
	}
	if (el.style.display == 'none'){
		el.style.display = '';	
	}else{
		el.style.display = 'none';
	}
}
/*
*
* Validate email address
*
*/
function isEmail(address) {
  	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  	var email = address.split(',');
	for (var i = 0; i < email.length; i++) {
		if (!regex.test(email[i])) {
			return false;
		}
	}
	return true;
}

/*
	debug messge showen
*/

function debug(msg){
	if (testmode && msg){
		alert(msg);
	}
}

function get_nodeval(node)
{
        if (node && node.hasChildNodes()) {
                return node.childNodes[0].nodeValue;
        }
        else {
                return "";
        }
}

// make params
// 030308 moved by Sunny: From booking_utility.js
function makeParam(pName, pValue){
  var param = "";
  param += "        <Param>";
  param += "          <Name>"+pName+"</Name>";
  param += "          <Value>"+pValue+"</Value>";
  param += "        </Param>";
  return param;
}

// process response message
// 030308 moved by Sunny from booking_utility.js
function onResponse(xmlhttp) {
  if (xmlhttp.readyState != XMLHTTP_COMPLETED) return;
  var response = xmlhttp.responseText;
  try {
   var xmldoc = XmlDocument.create();
    var errText = "";
    xmldoc.loadXML(response);
    var xmlerr = xmldoc.parseError;
    if (xmlerr.errorCode != 0) {
      errText = "Error (" + xmlerr.errorCode + "): " + xmlerr.reason
                + "\n"
                + "Error in line " + xmlerr.line + " character " + xmlerr.linepos
                + "\n"
                + xmlerr.srcText
                + "\n---------------------\n\n";
                alert(errText);
      return false;
    }
	var act = xmldoc.getElementsByTagName("Actions");
    if (act && act.length == 1){
      var acts = act[0].getElementsByTagName("Action");
      for (var i =0; acts && i < acts.length; i++){
      		parseRes(acts[i]);
      }
    }
  } catch (ex) {
    alert(ex);
  }
}

// return Action response "status" from the response
// 030308 moved by Sunny from booking_utility.js
function getActionStatus(actRes){
	return get_nodeval(actRes.getElementsByTagName('Status')[0]);
}

function backButtonOverride()
{
  // Work around a Safari bug
  // that sometimes produces a blank page
  setTimeout("backButtonOverrideBody()", 1);

}

function backButtonOverrideBody()
{
  // Works if we backed up to get here
  try {
    history.forward();
  } catch (e) {
    // OK to ignore
  }
  // Every quarter-second, try again. The only
  // guaranteed method for Opera, Firefox,
  // and Safari, which don't always call
  // onLoad but *do* resume any timers when
  // returning to a page
  setTimeout("backButtonOverrideBody()", 500);
}