// ch.js

var p_height=-1;
var active=0;
var _popup=null;
var browser = "";

if(document.getElementById) { browser = "ns6"; }
else if(document.all) { browser = "ie4"; }
else if(document.layers) { browser = "ns4"; }

// var p_width = screen.availWidth / 4;
var p_width = 155;


// ---------------- nopopup() ----------------
function nopopup() {
  active = 0;
  if(browser == "ns4") { _popup.visibility = "hide"; }
  if(browser == "ie4") { _popup.visibility = "hidden"; }
  if(browser == "ns6") { document.getElementById('popup').style.display="none" }
}


// ---------------- tt() ----------------
// Display Tool Tips
function tt(){
  var layerhtml;
  if(browser == "ns4") { _popup = document.popup; }
  if(browser == "ie4") { _popup = document.all.popup.style; }
  if(browser == "ns6") { _popup = document.getElementById("popup"); }



  p_text="Default Text";
  p_ti="";

  p_txtc="#00007A";
  p_tic="#FFFFFF";
  p_txtc="#00007A";
  p_tic="#660066";

  p_fgc="#FFFFFF";
  p_bgc="#00007A";

// Tooltip Message Box Foreground Colour
  p_fgc="#FFF4E4";

// Tooltip Title Background Colour
  p_bgc="#FFBB56";

  p_txt=arguments[0];        // Text to be displayed
  p_ti=arguments[1];        // Box Title

  p_txtfont="Arial,Helvetica";
  p_tifont="Arial,Helvetica";
  p_txtsz="2";
  p_tisz="2";
  p_height=-1;

  p_fgc="BGCOLOR=\""+p_fgc+"\"";
  p_bgc="BGCOLOR=\""+p_bgc+"\"";

  if(p_height>0){
    p_height="HEIGHT="+p_height;
  }else{
    p_height="";
  }

  if(p_ti==""){
    layerhtml="<TABLE WIDTH="+p_width+" BORDER=0 CELLPADDING=2 CELLSPACING=0 "+p_bgc+" "+p_height+"><TR><TD><TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 "+p_fgc+" "+p_height+"><TR><TD VALIGN=TOP><FONT FACE=\""+p_txtfont+"\" COLOR=\""+p_txtc+"\" SIZE=\""+p_txtsz+"\">"+p_txt+"</FONT></TD></TR></TABLE></TD></TR></TABLE>"
  }else{
    layerhtml="<TABLE WIDTH="+p_width+" BORDER=0 CELLPADDING=2 CELLSPACING=0 "+p_bgc+" "+p_height+"><TR><TD><TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD><B><FONT COLOR=\""+p_tic+"\" FACE=\""+p_tifont+"\" SIZE=\""+p_tisz+"\"><B>"+p_ti+"</B></FONT></B></TD></TR></TABLE><TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 "+p_fgc+" "+p_height+"><TR><TD VALIGN=TOP><FONT COLOR=\""+p_txtc+"\" FACE=\""+p_txtfont+"\" SIZE=\""+p_txtsz+"\">"+p_txt+"</FONT></TD></TR></TABLE></TD></TR></TABLE>";
  }

  if(browser == "ns4"){
    var lyr=document.popup.document;
    lyr.write(layerhtml);
    lyr.close()
  }

  if(browser == "ns6" || browser == "ie4") {
    document.getElementById('popup').innerHTML=layerhtml;
  }

  if(browser == "ns4") { _popup.visibility = "show"; }
  if(browser == "ie4") { _popup.visibility = "visible"; }
  if(browser == "ns6") { document.getElementById('popup').style.display="block" }

  active=1;
}

// #############################################################################

// ---------------- openwindow ----------------
function openwindow(_url){
  _picwin = window.open(_url,"ABRTWIN");
  eval(_picwin).window.focus();
}

// ---------------- bDel ----------------
function bDel(){
// Delete Selected Company

  var f_ok = 1;
  var CoName = document.ch.CoName.value;
  var Id = document.ch.C_Id.value;

// If company ID is set then get confirmation before Deleting from DBF
  if((Id != '')){
    f_ok = confirm('Are you Sure you want to DELETE this Company ?');
  }

  if(f_ok){
// Set command to Delete Selected Companyt
    document.ch.Cmd.value = 'DelCo';

    document.ch.Param.value = '' + Id;
    document.ch.submit();
  }
}

// ---------------- v_login ----------------
function v_login(){
  var len = 0;
  var rtn = true;

  txt = document.ch.Log_in.value;
  len = txt.length;

  if(len < 5){
    if(len == 0){
      alert('Please Enter Your login ID !\nNew Users: You Can Choose Your Own Login ID and Enter Your e-mail Address to Receive your Password.');
    }else{
      alert('Your Login ID must be at least 5 characters in length !');
    }
    rtn = false;
  }else{
// Plausible Login ID, check if password is entered
    txt = document.ch.Log_Pwd.value;
    len = txt.length;

    if(len == 0){
// No Password Entered, Has user entered a plausible e-mail address ?
      txt = document.ch.Log_email.value;
      len2 = txt.length;

// Even a@b.com is 7 characters in length

      if(len2 < 7){
        if(len2 > 0){
          alert('Please Enter a Valid e-mail Address to Receive your Password.');
          rtn = false;
        }else{
          alert('Please Enter Your Password !\nNew Users: Enter a Valid e-mail Address to Receive your Password.');
          rtn = false;
        }
      }
    }
  }

  return rtn;
}
// ---------------- v_register ----------------
function v_register(){
  var len = 0;
  var rtn = true;

  txt = document.ch.NUUsername.value;
  len = txt.length;

//  alert(sessionValue);

  if(len < 5){
    if(len == 0){
      alert('Please Enter Your Username !\nPlease note that once you have chosen your username and have entered your e-mail address, password will be emailed to you.');
    }else{
      alert('Your Username must be at least 5 characters in length !');
    }
    rtn = false;
  }else{
    txt = document.ch.NUEmail.value;
    len2 = txt.length;

    if(len2 < 7){
      if(len2 > 0){
        alert('Please enter a valid e-mail address to receive your password.');
        rtn = false;
      }else{
        alert('Please enter your email address !\nRemember to enter a valid e-mail address to receive your password.');
        rtn = false;
      }
     }
    }

  return rtn;
}

// ---------------- bSub ----------------
function bSub(){
// Simulate form Submission button Action & Submit Form.  Save Next Page Details
// in hidden field, Cmd

// Main Command, or Screen to be displayed
  document.ch.Cmd.value = arguments[0];

  if(arguments.length > 1){
// Optional Argument for the Command / Screen to be displayed
    document.ch.Param.value = arguments[1];
  }

// alert("JavaScript bSub(): Cmd: " + arguments[0] + ", Val: " + arguments[1]);

  document.ch.submit();
}

// ---------------- bHelp ----------------
function bHelp(){
// Simulate form Submission button Action & Submit Form.
  document.ch.Cmd.value = "Help";

  if(arguments.length > 1){
// Optional Argument for the Help / Screen to be displayed
    document.ch.Param.value = arguments[1];
  }

  document.ch.submit();
}

// ---------------- CalcPr ----------------
// Perform price related calculations and update screen with results
function CalcPr() {
  var upr = 0;
  var r_vat = document.ch.r_vat.value * 1;

// Multiply values by 1 to convert them to numeric data
// Price breaks
  var pr_b1 = document.ch.pr_b1.value * 1;
  var pr_b2 = document.ch.pr_b2.value * 1;
  var pr_b3 = document.ch.pr_b3.value * 1;

// Prices
  var pra1 = document.ch.pra1.value * 1;
  var pra2 = document.ch.pra2.value * 1;
  var pra3 = document.ch.pra3.value * 1;
  var pra4 = document.ch.pra4.value * 1;

  var prb1 = document.ch.prb1.value * 1;
  var prb2 = document.ch.prb2.value * 1;
  var prb3 = document.ch.prb3.value * 1;
  var prb4 = document.ch.prb4.value * 1;

  var prc1 = document.ch.prc1.value * 1;
  var prc2 = document.ch.prc2.value * 1;
  var prc3 = document.ch.prc3.value * 1;
  var prc4 = document.ch.prc4.value * 1;

// eBanking charges, fixed amount and percentage charge
//  var eb_f = document.ch.eb_f.value * 1;
//  var eb_p = document.ch.eb_p.value * 1;

  var Qty  = document.ch.CoP_Qty.value * 1;
  var Qty2 = document.ch.CoP_Qty2.value * 1;
  var Qty3 = document.ch.CoP_Qty3.value * 1;

// Determine Unit Price for Standard PAckage
  if(Qty < 1){
    upr = 0;
  }else{                     // if(Qty < 0)
    if(Qty < pr_b1){
      upr = pra1;
    }else{                   // if(Qty < prb1)
      if(Qty < pr_b2){        // if(Qty < prb2)
        upr = pra2;
      }else{                 // if(Qty < prb2)
        if(Qty < pr_b3){      // if(Qty < prb3)
          upr = pra3;
        }else{
          upr = pra4
        }                    // if(Qty < prb3)
      }                      // if(Qty < prb2)
    }                        // if(Qty < prb1)
  }                          // if(Qty < 0)

// Determine Unit Price for Printed Documents
  if(Qty2 < 1){
    upr2 = 0;
  }else{                     // if(Qty < 0)
    if(Qty2 < pr_b1){
      upr2 = prb1;
    }else{                   // if(Qty < prb1)
      if(Qty2 < pr_b2){       // if(Qty < prb2)
        upr2 = prb2;
      }else{                 // if(Qty < prb2)
        if(Qty2 < pr_b3){     // if(Qty < prb3)
          upr2 = prb3;
        }else{
          upr2 = prb4;
        }                    // if(Qty < prb3)
      }                      // if(Qty < prb2)
    }                        // if(Qty < prb1)
  }                          // if(Qty < 0)

// Determine Unit Price for Company Seals
  if(Qty3 < 1){
    upr3 = 0;
  }else{                     // if(Qty < 0)
    if(Qty3 < pr_b1){
      upr3 = prc1;
    }else{                   // if(Qty < prb1)
      if(Qty3 < pr_b2){      // if(Qty < prb2)
        upr3 = prc2;
      }else{                 // if(Qty < prb2)
        if(Qty3 < pr_b3){    // if(Qty < prb3)
          upr3 = prc3;
        }else{
          upr3 = prc4;
        }                    // if(Qty < prb3)
      }                      // if(Qty < prb2)
    }                        // if(Qty < prb1)
  }                          // if(Qty < 0)

  var t_pr = Qty * upr;      // Total Price
  var t_pr2 = Qty2 * upr2;   // Total Price
  var t_pr3 = Qty3 * upr3;   // Total Price

  if(r_vat > 0){
    var vat = (t_pr + t_pr2 + t_pr3) * r_vat;    // VAT amount
  }else{
    var vat = 0;
  }

  var gt = t_pr + t_pr2 + t_pr3 + vat;

// Update Screen
  document.ch.P_UnitPr.value = upr.toFixed(2);
  document.ch.P_TotalPr.value = t_pr.toFixed(2);

  document.ch.P_UnitPr2.value = upr2.toFixed(2);
  document.ch.P_TotalPr2.value = t_pr2.toFixed(2);

  document.ch.P_UnitPr3.value = upr3.toFixed(2);
  document.ch.P_TotalPr3.value = t_pr3.toFixed(2);

  document.ch.P_VAT.value = vat.toFixed(2);
  document.ch.P_Total.value = gt.toFixed(2);

// Update Hidden Variables
  document.ch.CoP_UnitPr.value = upr.toFixed(2);
  document.ch.CoP_TotalPr.value = t_pr.toFixed(2);

  document.ch.CoP_UnitPr2.value = upr2.toFixed(2);
  document.ch.CoP_TotalPr2.value = t_pr2.toFixed(2);

  document.ch.CoP_UnitPr3.value = upr3.toFixed(2);
  document.ch.CoP_TotalPr3.value = t_pr3.toFixed(2);

  document.ch.CoP_VAT.value = vat.toFixed(2);
  document.ch.CoP_Total.value = gt.toFixed(2);
}

function AddItem() {
  var UPr = 0;
  var UPr2 = 0;
  var TPr = 0;
  var TPr2 = 0;
  var r_vat = document.ch.r_vat.value * 1;

// Multiply values by 1 to convert them to numeric data
// Price breaks
  var pr_b1 = document.ch.pr_b1.value * 1;
  var pr_b2 = document.ch.pr_b2.value * 1;
  var pr_b3 = document.ch.pr_b3.value * 1;

// Prices
  var pra1 = document.ch.pra1.value * 1;
  var pra2 = document.ch.pra2.value * 1;
  var pra3 = document.ch.pra3.value * 1;
  var pra4 = document.ch.pra4.value * 1;

  var prb1 = document.ch.prb1.value * 1;
  var prb2 = document.ch.prb2.value * 1;
  var prb3 = document.ch.prb3.value * 1;
  var prb4 = document.ch.prb4.value * 1;

  var prc1 = document.ch.prc1.value * 1;
  var prc2 = document.ch.prc2.value * 1;
  var prc3 = document.ch.prc3.value * 1;
  var prc4 = document.ch.prc4.value * 1;

// eBanking charges, fixed amount and percentage charge
//  var eb_f = document.ch.eb_f.value * 1;
//  var eb_p = document.ch.eb_p.value * 1;

  var sc = document.ch.CoP_ScT.checked;
  var pd = document.ch.CoP_PrD.checked;

  var Qty  = 0;
  var Qty2 = 0;
  var Qty3 = 0;

  if (sc == false) {
    Qty = 1;
    UPr = pra1;
    TPr = Qty * UPr;
  }else{
    Qty = "";
    UPr = "";
    TPr = "";
  }

  if (pd == false) {
    Qty2 = 1;
    UPr2 = prb1;
    TPr2 = Qty2 * UPr2;
  }else{
    Qty2 = "";
    UPr2 = "";
    TPr2 = "";
  }

// Update Screen
  document.ch.CoP_Qty.value = Qty;
  document.ch.P_UnitPr.value = UPr;
  document.ch.P_TotalPr.value = TPr;

  document.ch.CoP_Qty2.value = Qty2;
  document.ch.P_UnitPr2.value = UPr2;
  document.ch.P_TotalPr2.value = TPr2;
}

// ---------------- help ----------------
// function help(labl){
//  alert('Help: ' + labl);
// }

// ---------------- help ----------------
// Display Help Screen
// Usage help('IntTag', ['HelpFile'])
function help() {

  if(arguments.length > 1){
    var url = arguments[1];
  }else{
    var url = "help.html";
  }

  if(arguments[0].length > 0){
    url = url + "#" + arguments[0];
  }

  var newWin=window.open(url, "ch_help", "copyhistory,directories,location,menubar,resizable,scrollbars,status,titlebar,toolbar");
  newWin.focus();
  newWin.status=url;
}
// ---------------- MainWin ----------------
function MainWin(url, dest) {
// Update the URL of the Current Browser
// var newWin=window.open(url, dest, "copyhistory,directories,location,menubar,resizable,scrolbars,status,titlebar,toolbar");
  parent.document.location.href = url;
}
// ---------------- NewWin ----------------
function NewWin(url, dest) {
//  var newWin=window.open(url, dest, "copyhistory,directories,location,menubar,resizable,scrolbars,status,titlebar,toolbar");
  var newWin=window.open(url, dest, "");
  newWin.focus();
}

// ---------------- NewWin2 ----------------
function NewWin2(url) {
//  var url='index.php';
  var dest='W8x6';
  var w = 800;
  var h = 600;
  var newWin=window.open(url, dest, "copyhistory,directories,location,menubar,resizable,scrolbars,status,titlebar,toolbar,width="+w+",height="+h);
//  var newWin=window.open(url, dest, "copyhistory, directories, location, menubar, resizable, scrolbars, status, titlebar, toolbar");

  newWin.focus();
}

// ---------------- NewWin3 ----------------
function NewWin3(fld) {

  var url='http://ws6info.companieshouse.gov.uk/info/do_search.cgi?cname=' + eval('document.ch.' + fld + '.value');

  var dest='Win3';
  var newWin=window.open(url, dest, "copyhistory,directories,location,menubar,resizable,scrolbars,status,titlebar,toolbar");

  newWin.focus();
}

// ---------------- NewWin4 ----------------
function NewWin4(url) {
//  var url='index.php';
  var dest='W1024';
  var w = 1024;
  var h = 768;
  var newWin=window.open(url, dest, "copyhistory,directories,location,menubar,resizable,scrolbars,status,titlebar,toolbar,width="+w+",height="+h);
//  var newWin=window.open(url, dest, "copyhistory, directories, location, menubar, resizable, scrolbars, status, titlebar, toolbar");

  newWin.focus();
}
// ---------------- pg_title ----------------
function pg_title(ti){
// window title property
  top.window.document.all.tags['HEAD'][0].text = ti;
  alert('Windows Title: ' + ti);
}

// ---------------- SelAdr ----------------
function SelAdr(pfx){
// Select Address from Personal Address Book List

  var f_ok = 1;

// Company ID to be selected
  var CoId = eval("document.ch." + pfx + "AB.value") * 1;

  if((CoId > 0)){
//    f_ok = confirm('Selecting Address ID ' + CoId);
  }

  if(f_ok){
// Set command to select company from list
    document.ch.Cmd.value = 'SelAdr';

    document.ch.Param.value = '' + pfx + ";" + CoId;
    document.ch.submit();
  }
}

// ---------------- SelCo ----------------
function SelCo(){
// Select Company From Company List Box

  var f_ok = 1;
  var CoName = document.ch.CoName.value;
  var OldId = document.ch.C_Id.value;

// Company ID to be selected
  var CoId = document.ch.CoNList.value;

  if((CoName != '') |(OldId != '')){
    f_ok = confirm('Selecting Another Company will Abandon All Current Changes !\nClick Cancel & Save Changes or OK to Abandon Changes & Select Another Company!\nCo Name: ' + CoName + ', Old ID: ' + OldId);
  }

  if(f_ok){
// Set command to select company from list
    document.ch.Cmd.value = 'SelCo';

    document.ch.Param.value = '' + CoId;
    document.ch.submit();
// alert("JavaScript:SelCo() Cmd Set to: SelCo, Param Set to: " + CoId);
  }
}

// ---------------- SelOff ----------------
function SelOff(no){
// Select Company Officer from Number List Box
// Sets Cmd = SelOff and Param = <Selected Officer Number>

  var f_ok = 1;

// New Selected Officer Number
  if(no > 0){
    NewOffNo = no;
  }else{
    NewOffNo = document.ch.CoO_No.value;
  }

  if(NewOffNo < 1){
    NewOffNo = 1;
  }

//  alert("New Officer Selected: " + NewOffNo);

// Set command to select company from list
  document.ch.Cmd.value = 'SelOff';
  document.ch.OfficerNo.value = '' + NewOffNo;
  document.ch.submit();
}

// ---------------- SelPayTy ----------------
function SelPayTy(){
// Select Payment Type List Box

// Payment Method ID selected (0=None, 1=FastPay, 2=PayPal, 3=WorldPay
  var Pay_Id = document.ch.CoPayTy.value;

// Set command to select company from list
    document.ch.Cmd.value = 'CoPay';
    document.ch.Param.value = '' + Pay_Id;

  switch (Pay_Id){
    case 0:
      break;

    case 1:
      break;

    case 2:
      break;

    case 3:
      break;
  }

  document.ch.submit();
}

// ---------------- v_epwd ----------------
function v_epwd(){
// Check if a valid Login ID (minimum of 5 Characters) has
// been enterd at the Login Screen
  var len = 0;
  var rtn = true;

  txt = document.ch.Log_in.value;
  len = txt.length;

  if(len < 5){
// No Valid Login ID Entered, Has user entered a plausible e-mail address ?
    txt = document.ch.Log_email.value;
    len2 = txt.length;

// Even a@b.com is 7 characters in length

    if(len2 > 6){
      rtn = true;
    }else{
      if(len2 > 0){
        alert('Please Enter a Valid e-mail Address to Receive your Password.');
        rtn = false;
      }else{
        alert('Please Enter a Valid Login ID !');
        rtn = false;
      }
    }
  }

  return rtn;
}
// ---------------- v_email ----------------
function v_email(fld_name) {
// Check if a possible valid e-mail address (minimum of 7 Characters) has
// been enterd
  var len = 0;
  var rtn = true;

  txt = eval("document.ch." + fld_name + ".value");
  len = txt.length;

// Even a@b.com is 7 characters in length

  if(len < 7) {
    if(len > 0) {
      alert("Please Enter a Valid e-mail Address.");
      rtn = false;
    }
  }

  return rtn;
}

// ---------------- omu_prndocs ----------------
function omu_prndocs() {
  var cs_total = 0;                         // company registration cost
  var grand_total = 0;                      // grand total of all items
  var item_qty = 0;                         // quantity
  var item_sel = true;                      // item is selected
  var item_tpr = 0;                         // total price
  var item_upr = 0;                         // unit price

  // determine if the user has chosen to order
  // printed documents
  // item_sel = document.ch.CoP_PrD.checked;
  item_sel = document.ch.CoP_Plat.checked;

  // if 'item_sel' is false then the checkbox for
  // printed documents is checked, set quantity to
  // 1 and calulate the unit / total price
  if (item_sel == false) {
    document.ch.CoP_UnitPr.value = "34.99";
    document.ch.CoP_TotalPr.value = "34.99";

    document.ch.CoP_Plat.value = "";
  }else{
    // update the screen
    document.ch.CoP_UnitPr.value = "54.99";
    document.ch.CoP_TotalPr.value = "54.99";

    document.ch.CoP_Plat.value = "Platinum";
  }
  alert(document.ch.CoP_Plat.value);
}

// ---------------- omu_package ----------------
function omu_package() {
  var item_sel = false;                      // item is selected
  var unit_pr = 0;
  var totl_pr = 0;

  // determine if the user has chosen to order
  // printed documents
  item_sel = document.ch.CoP_Plat.checked;

alert(document.ch.CoP_Plat.checked);

  // if 'item_sel' is false then the checkbox for
  // printed documents is checked, set quantity to
  // 1 and calulate the unit / total price
  if (item_sel == true) {
  alert("platinum");
    unit_pr = 54.99;
    totl_pr = 54.99;
  } else {
  alert("express");
    unit_pr = 34.99;
    totl_pr = 34.99;
  }
  
  alert("Unit: " + unit_pr);
}
// ---------------- omu_package ----------------
function oc(pcode) {

  document.ch.NUPromo.value = pcode;
  bSub('CoPay');
}


