var SAFETEXT = new RegExp("[a-zA-Z0-9\-]+");

//== start ajax
    var xmlHttp;
    
    function makeRequest(aUrlPlusParams, aReturnFunction, aPostParams){
      xmlHttp=GetXmlHttpObject();
      if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
      } 
      var url=aUrlPlusParams;
//      url=url+"?code="+aCode;
      url=url+"&sid="+Math.random();
      if(aReturnFunction==null)
        xmlHttp.onreadystatechange=updateOptions;
      else
        xmlHttp.onreadystatechange=aReturnFunction;
      xmlHttp.open("POST",url,true);
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      xmlHttp.setRequestHeader("charset", "utf-8");
      xmlHttp.send(aPostParams);
    }
    
    function GetXmlHttpObject(){ 
      var objXMLHttp=null;
      if (window.XMLHttpRequest){
        objXMLHttp=new XMLHttpRequest();
      }
      else if (window.ActiveXObject){
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      return objXMLHttp;
    } 
    
//==end ajax


function changeImages(src, image) {					
  if(src.firstChild!=null){
    src.firstChild.src=image;										
  }
}
		
function closeBrowser() {
  parent.close();
}

var BTN_SUPPORT = "form_support_btn";
var BTN_DEFAULT = "form_btn";
var BTN_CLUSTER = "form_cluster_btn";
/**
  private
**/
function generateButton(text, type, onclick, width){
  if(type==null) type = BTN_DEFAULT;
  if(width==null) width=80;
  var button='';
  button+='  <span class="form_btn_span" onmouseover="this.className=\'form_btn_span_hover\';" onmouseout="this.className=\'form_btn_span\';" onclick="'+onclick+'">';
  button+='	<table>';
  button+='		<tbody><tr>';
  button+='			<td class="'+type+'" nowarp="" valign="middle" width="'+width+'">'+text+'</td>';
  button+='		</tr>';
  button+='	</tbody></table>';
  button+='</span>		';
  return button;
}

function makeButton(text, type, onclick, width){
  makeButtonGroup(passButton(text, type, onclick, width));
}

function passButton(text, type, onclick, width){
  return generateButton(text, type, onclick, width);
}

function makeButtonGroup(){
  var buttonArray = arguments;
  document.write('<table border="0" cellpadding="0" cellspacing="0">');
  document.write('<tr>');
  
  for(var i=0; i<arguments.length;i++){
    document.write('<td>'+ arguments[i]  +'</td>');
    if(i<arguments.length-1) document.write("<td>&nbsp;</td>");
  }
  document.write('</tr>');
  document.write('</table>');
}

var POPUP_DETAILS = "height=400, width=800, left=0, top=0, status=no, scrollbars=yes, resizable=yes";
var POPUP_DETAILS_DEMO = "height=600, width=800, left=0, top=0, status=no, scrollbars=yes, resizable=yes";

function openFAQ(){
  window.open(jslabels['url.faqs'], "apps0102", POPUP_DETAILS);
}

function openFAQODLR(aSec){
  window.open("viewFaq02.do" + (aSec?"#"+aSec:""), "apps0102", POPUP_DETAILS);
}

function openSched(){
  window.open("viewSched.do", "odlr_sched", POPUP_DETAILS);
}

function openAddresses(){
  window.open("viewAddresses.do", "odlr_addresses", POPUP_DETAILS);
}

function openDemo(){
  window.open("viewDemo00.do", "apps05", POPUP_DETAILS_DEMO);
}

function openTerms(){
  window.open("viewTerms.do", "odlr_terms", POPUP_DETAILS);
}

function openNotes(){
  window.open("viewNotes.do", "odlr_notes", POPUP_DETAILS);
}

function writeFillerTable(){
  document.write('                      <table class="html-table" id="table1" border="0" height="100%">');
  document.write('                        <tbody><tr class="html-table-row-2" >');
  document.write('                          <td class="html-table-cell-noborder">');
  document.write('                          </td>');    
  document.write('                        </tr>');
  document.write('                        </tbody>');
  document.write('                      </table>');
}

function writeOnlineTutorialTable(){
  document.write('                      <table class="html-table" id="table1" border="0">');
  document.write('                        <tbody><tr class="html-table-row-2">');
  document.write('                          <td class="html-table-cell-noborder" style="padding-left: 10px"><font class="heading-3" style="cursor: pointer; cursor: hand;" onmouseover="this.className=\'heading-3-hover\'" onmouseout="this.className=\'heading-3\'" onclick="openDemo();">' + jslabels['label.online_tutorial'] + '</font>');  
  document.write('                          </td>');  
  document.write('                        </tr>');
  document.write('                        </tbody>');
  document.write('                      </table>');
}



function writeFAQTable(){
  document.write('                      <table class="html-table" id="table1" border="0">');
  document.write('                        <tbody><tr class="html-table-row-2">');                                      
  document.write('                          <td class="html-table-cell-noborder" style="padding-left: 10px"><font class="heading-3" style="cursor: pointer; cursor: hand;" onmouseover="this.className=\'heading-3-hover\'" onmouseout="this.className=\'heading-3\'" onclick="openFAQ();">' + jslabels['label.faqs'] + '</font>');  
  document.write('                          </td>');  
  document.write('                        </tr>');
  document.write('                        </tbody>');
  document.write('                      </table>');
}


function writeHelpTable(){
  document.write('                      <table class="html-table" id="table1" border="0">');
  document.write('                        <tbody><tr class="html-table-row-2">');
  document.write('                          <td class="html-table-cell-noborder" style="padding-left: 10px"><font class="heading-3">' + jslabels['label.help_desk'] + '</font></td>');
  document.write('                        </tr>');
  document.write('                        <tr class="html-table-row-3">');
  document.write('                          <td class="html-table-cell-noborder" style="padding-left: 10px"><span style="color: #000000; font-weight: bold;">' + jslabels['label.help_desk_number'] + '</span>');
  //document.write('                            or email us at <span style="color: #000000; font-weight: bold;">xxx@xxx.gov.hk</span>');  
  //document.write('                          ');  
  document.write('                          </td>');    
  document.write('                        </tr>');
  document.write('                        </tbody>');
  document.write('                      </table>');
}

function writePledgeTable(){
  document.write('                      <table class="html-table" id="table1" border="0">');
  document.write('                        <tbody><tr class="html-table-row-2">');
  document.write('                          <td class="html-table-cell-noborder" style="padding-left: 10px"><font class="heading-3">' + jslabels['label.performance_pledge'] + '</font></td>');
  document.write('                        </tr>');
  document.write('                        <tr class="html-table-row-3">');
  document.write('                          <td class="html-table-cell-noborder" style="padding-left: 10px">' + jslabels['label.performance_pledge_content']);
  document.write('                          ');
  document.write('                          </td>');
  document.write('                        </tr>');
  document.write('                        </tbody>');
  document.write('                      </table>');
}
var stepNo = 1;
function writeStepsTable(){
  document.write('                      <table class="html-table" id="table1" border="0">');
  document.write('                        <tbody>');
  document.write('                          <tr id="step1Row">');
  document.write('                            <td class="html-table-cell" style="padding-left: 10px"><strong class="step">' + jslabels['label.step1'] + '</strong><br/>');
  document.write('                            ' + jslabels['label.step1_desc']);
  document.write('                            </td>');
  document.write('                          </tr>');
  document.write('                          <tr id="step2Row">');
  document.write('                            <td class="html-table-cell" style="padding-left: 10px"><strong class="step">' + jslabels['label.step2'] + '</strong><br/>');
  document.write('                            ' + jslabels['label.step2_desc']);
  document.write('                            </td>');
  document.write('                          </tr>');
  document.write('                          <tr id="step3Row">');
  document.write('                            <td class="html-table-cell" style="padding-left: 10px"><strong class="step">' + jslabels['label.step3'] + '</strong><br/>');
  document.write('                            ' + jslabels['label.step3_desc']);
  document.write('                            </td>');
  document.write('                          </tr>');
  document.write('                          <tr id="step4Row">');
  document.write('                            <td class="html-table-cell" style="padding-left: 10px"><strong class="step">' + jslabels['label.step4'] + '</strong><br/>');
  document.write('                            ' + jslabels['label.step4_desc']);
  document.write('                            </td>');
  document.write('                          </tr>');
  document.write('                          <tr id="step5Row">');
  document.write('                            <td class="html-table-cell" style="padding-left: 10px"><strong class="step">' + jslabels['label.step5'] + '</strong><br/>');
  document.write('                            ' + jslabels['label.step5_desc']);
  document.write('                            </td>');
  document.write('                          </tr>');
  document.write('                        </tbody>');
  document.write('                      </table>');
  
  if (stepNo) {
    stepRow = document.getElementById("step" + stepNo + "Row");
    stepRow.className = "help-table-heading-cell";
  }
}

function writeDemoTable(){
  document.write('<table class="html-table" height="90">');
  document.write('	<tbody><tr class="html-demo-table-row">');
  document.write('		<td class="demo-table-cell">');
  document.write('		Find out how to make your appointment online</td>');
  document.write('	</tr>');
  document.write('	<tr class="html-demo-table-row">');
  document.write('		<td align="center">');
  document.write('		<table>');
  document.write('			<tbody>');
  document.write('      <tr>');
  document.write('				<td>');
  makeButton("View Demo", BTN_DEFAULT);
  document.write('				</td>');
  document.write('			</tr>');
  document.write('		  </tbody>');
  document.write('    </table>	');
  document.write('		</td>');
  document.write('	</tr>');
  document.write('  </tbody>');
  document.write('</table>');
}

var clickedDropDown = false;

function dropDown(anId, aLink){
  clickedDropDown = true;
  
  var field = document.getElementById(anId);
//  var link = document.getElementById(aLinkId);
  
  var pos = findPos(field);
  
  aLink.style.width = field.style.width;
  aLink.style.minWidth = field.style.width;
  //link.getElementsByTagName("iframe")[0].style.width = field.style.width;
//  alert("["+link.style.width+"]["+field.style.width+"]");
  try {
    aLink.style.left = "" + pos[0] + "px";
    aLink.style.top = "" + (pos[1]+20) + "px";
  } catch (e) {
    // don't raise
  }
  
  if(aLink.style.visibility != "visible") aLink.style.visibility = "visible";
  else aLink.style.visibility = "hidden";
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function clickAnywhere(){
  if(clickedDropDown){
    clickedDropDown = false;
    return;
  }

  //close any open lists
  var iframes = document.getElementsByTagName("iframe");
  for(var i=0; i<iframes.length;i++){
    if(iframes[i].style.visibility != "hidden") iframes[i].style.visibility = "hidden";
  }
}

function doExit(){
  window.close();
}
