
function baseURL()
{
  //Firefox
  //if(document.baseURI) return document.baseURI;
  //Ie
  var baseEl=document.getElementsByTagName("base");
  if(baseEl.length>0) {
    return baseEl[0].href;
  }
  //Last resort
  return "/";
}

//Similar to the php function of the same name but lacking a quote style option.
//Similar to but less complete that htmlentities
function htmlspecialchars(str) {
  return str.replace(/\&/g, "&amp;").replace(/\"/g, "&quot;").replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
  /*
      '&' '&amp;'
      '"' '&quot;'
      '<' '&lt;'
      '>' '&gt;' 
  */
}

function CC(amt) {
  void window.open('cc?amt='+amt,'cc','width=580,height=500,scrollbars=yes,resizable=yes');
  return;
}


/*
 Event system.
 eg 
 events['onLoad'].push('changePage()');
*/

function eventsExec(eventName) {

  if(typeof(events)!='object')
    return;
  if(typeof(events[eventName])!='object')
    return;
  
  for(var i=0; i<events[eventName].length; i++) {
    eval(events[eventName][i]);
  }
}

function eventsExecOnload() {
  eventsExec('onLoad');
}

window.onload = eventsExecOnload;


DebugStr='Page Load\n';

function Debug(label) {
  DebugStr+=label;
  for(var i=1; i<arguments.length; i++) {
    DebugStr+=' '+arguments[i];
  }
  DebugStr+='\n';
}

function showDebugging() {
  Poptext(DebugStr);
  DebugStr='Debuging displayed and reset\n';
}



function Poptext(text) {
	var scr_width = 640;
	var scr_height = 480;
	var win_title="";
	var win_bg = "#FFFFFF";
	var fontColor = "#000000";

	var newWin = null;
	newWin = window.open("","","width=" + scr_width + ",height=" + scr_height + "scrollbars=yes,resizable=yes"); 
	if (newWin != null) {
		newWin.focus();
		var htmlCode = "	<HTML>\n";
		htmlCode +=    "	 <HEAD>\n";
		htmlCode +=    "	  <TITLE>" + win_title + "</TITLE>\n";
		htmlCode +=    "    <LINK rel=\"stylesheet\" type=\"text/css\" href=\"css/main.css\">\n";
		htmlCode +=    "   </HEAD>\n";
		htmlCode +=    "	 <BODY bgcolor=\"" + win_bg + "\">\n";
		htmlCode +=    "	  <PRE>\n";
		htmlCode +=    text
		htmlCode +=    "	  </PRE>\n";
		htmlCode +=    "	 </BODY>\n";
		htmlCode +=    "	</HTML>";
		newWin.document.write(htmlCode);
		newWin.document.close();
	}
}

function ByID(id) {
  if (!document.getElementById) return false;
  return document.getElementById(id);
}

function ID(id) {
  if (!document.getElementById) return false;
  return document.getElementById(id);
}

function SelectIndexByID(id) {
  s=ByID(id); if (!s) return false;
  if(s.options.length==0 || !s.options.length > s.options.selectedIndex) return false;
  return s.options[s.options.selectedIndex].value;
}

function SelectedTextByID(id) {
  s=ByID(id); if (!s) return false;
  return s.options[s.options.selectedIndex].text;
}

function safeParseInt(x) {
  var cleanNumReg = new RegExp ('[^0-9\.]+', 'g') ;
  x=x.replace(cleanNumReg, '');
  
  if(x=='' || isNaN(x))
    return 0;
  
  while (x != '0' && x.substr(0,1) == '0') x = x.substr(1); // parseInt will treat it as an octal number if there's leading zeros
  
  return parseInt(x);
}

function safeParseFloat(x) {
  x=String(x);
  var cleanNumReg = new RegExp ('[^0-9\.]+', 'g') ;
  x=x.replace(cleanNumReg, '');
  
  while (x != '0' && x.substr(0,1) == '0') x = x.substr(1); // parseFloat will treat it as an octal number if there's leading zeros
  
  if(x=='' || isNaN(x))
    return 0;
  return parseFloat(x);
}

function Delete(page,name)
{
  var extra=(Delete.arguments.length==3) ? "\n"+Delete.arguments[2] : '';
  if (confirm("Are you sure you want to permanently delete "+name+"?"+extra))
    document.location.href=page;
}

function Cancel(page,name)
{
  var extra=(Cancel.arguments.length==3) ? "\n"+Cancel.arguments[2] : '';
  if (confirm("Are you sure you want to cancel "+name+"?"+extra)) {
    document.location.href=page;
    alert("Please ensure that any applicable refunds are processed and recorded into the payment section of the booking.");
  }
}

function Show(id,first)
// Hides/shows a block and its Show link. 'first' tells whether to hide (0) or show (1) on first click.
{
  if (!document.getElementById) return false;
  rLink=document.getElementById('l'+id); if (!rLink) return false;
  ToggleId(id,first,'block');
  rLink.innerHTML=(rLink.innerHTML=='Hide') ? 'Show' : 'Hide';
}

function SimpleHide(id) {
  document.getElementById(id).style.display='none';
}

function ShowMulti(id)
// Hides/shows a series of blocks and its Show link.
{
  var type='block';
  var i=0;
  if (!document.getElementById) return false;
  var state=document.getElementById('state_'+id);
  var rLink=document.getElementById('l'+id);
  if (rLink)
	  rLink.innerHTML=(state.value=='1') ? 'Show All' : 'Hide All';

  r=document.getElementById(id+i);
  while(r) {
	(state.value=='1') ? r.style.display="none" : r.style.display=type;
	i++;
	r=document.getElementById(id+i);
  }
  (state.value=='1') ? state.value='0' : state.value='1';
}

function ToggleId(id,first,type)
// Change display to block/inline (specified in 'type'). 'first' tells whether to hide (0) or show (1) on first click.
{
  if (!document.getElementById) return false;
  r=document.getElementById(id); if (!r) return false;
  if (first=1) // Show on 1st click
    (r.style.display==type) ? r.style.display="none" : r.style.display=type;
  else // Hide on 1st click
    (r.style.display=="none") ? r.style.display=type : r.style.display="none";
}

function ToggleOnInput(input,id)
// If text is entered (not blank and not zero), makes the given tag a block (or supplied third param)
{
  DisplayType=(ToggleOnInput.arguments.length==3) ? ToggleOnInput.arguments[2] : 'block';
  if (!document.getElementById) return false;
  var rId=document.getElementById(id); if (!rId) return false;
  if (input!='' && input!=0) {
    rId.style.display=DisplayType;
    if (rInput=rId.getElementsByTagName('input'))
      rInput[0].focus();
  }
  else
    rId.style.display='none';
}

function ToggleOnSelect(s,val,id)
// If val is selected, makes the given tag inline
{
  if (!document.getElementById) return false;
  r=document.getElementById(id); if (!r) return false;
  if (s.options[s.options.selectedIndex].value==val)
    r.style.display='inline';
  else
    r.style.display='none';
}

function ToggleOnCheck(checkbox,id,displayStyle)
// If checked, makes the given tag inline
{
  if (!displayStyle)
    displayStyle='';
  if (navigator.appName.indexOf('Internet Explorer')>0 && displayStyle=='table-row')   // IE can't handle table-row
    displayStyle='block';
  if (!document.getElementById) return false;
  r=document.getElementById(id); if (!r) return false;
  if (checkbox.checked)
    r.style.display=displayStyle;
  else
    r.style.display='none';

}

function ToggleOnCheckInv(checkbox,id,displayStyle)
// If checked, makes the given tag inline
{
  if (!displayStyle)
    displayStyle='';
  if (navigator.appName.indexOf('Internet Explorer')>0 && displayStyle=='table-row')   // IE can't handle table-row
    displayStyle='block';
  if (!document.getElementById) return false;
  r=document.getElementById(id); if (!r) return false;
  if (!checkbox.checked)
    r.style.display=displayStyle;
  else
    r.style.display='none';

}

function popCal(url)
{
  window.open(url,'','width=540,height=700,scrollbars=yes,resizable=yes');
}

function Lock() {
  window.open('lock','','width=400,height=300,scrollbars=no,resizeable=yes');
}
function empty(id) {
  document.getElementById(id).innerHTML='';
}
// Used on Login Screen, e comes from Password field onKeyPress to avoid false logins from username field.
function checkEnter(e) { 
  var characterCode;  
  if(e && e.which){ 
    characterCode = e.which;      // (Firefox)
  } else {
    characterCode = e.keyCode; // (IE)
  }  
  if(characterCode == 13) {     // 13 is ASCII for enter key
    Login();
    return false;
  } else {
    return true;
  }
}

// Check All / Uncheck All - Used on Pending Rates report for tickboxes.
var checkflag = "false";
function check(field) {
  field = document.getElementsByName(field);
  if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
      field[i].checked = true;
    }
    checkflag = "true";
    return "Uncheck All"; 
   } else {
    for (i = 0; i < field.length; i++) {
      field[i].checked = false; 
    }
    checkflag = "false";
    return "Check All"; 
  }
}
function showNewAccomm(state) {     // 1 for Visible, 0 for Hidden
  if(state =='1') {
    document.getElementById('new_accommstaying').style.display='inline';
  }else {
    document.getElementById('new_accommstaying').style.display='none';
  }
}

function swap_policy(state) {   // 1 for Allow, 2 for Deny
  var options = document.getElementById('location_access_exceptions').options;
  for(var i=0;i<options.length;i++) {
    options[i].selected=false;
  }
  
  if(state=='Allow') {
    document.getElementById('policy_label').innerHTML='Deny From:';
  }else {
    document.getElementById('policy_label').innerHTML='Allow To:';
  }
}


function updateMandatoryFields()
{
  phone = ID('phone_radio');
  email = ID('email_radio');
  
  if(phone.checked == true){
    ID('phone_required').innerHTML = ''; 
    ID('state_required').innerHTML = ''; 
  }
  if(email.checked == true){
    ID('phone_required').innerHTML = ' (optional)'; 
    ID('state_required').innerHTML = ' (optional)'; 
  }
  
}

function ValidateCompulsoryFields(){
  
  var err = '';  
  var phone = document.getElementById('phone').value;
  var state = document.getElementById('state').value;
  var phone_required = ID('phone_required').innerHTML;


  if(phone == '' && phone_required == "")
    err += 'Please fill in the Phone field.\n';

  if(state == '' && phone_required == "")
    err += 'Please fill in the State field.\n';

  if (err != '')
    return err;

  return 0;
  


}
