//
// Functions library
//

function writeProductRow( i, title, NumSys, tick95, tick98, tickMe, tickNT, tick2000, tickXP, tickServer2003, tickLonghornAlpha, tickLinux, RamHyperdrive, sterling, dollar, header )
{
if (header == 1)
{
  document.write("<TR><TD COLSPAN=9>" + title + "</TD></TR>");
}
else
{
document.write("<TR ALIGN='left'>");
document.write("<TD CLASS='content' style='padding-right: 10px'>" + title + "</TD>");
document.write("<TD CLASS='content'>" + NumSys + "</TD>");
document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tick95 + ".gif'></TD>");
document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tick98 + ".gif'></TD>");
document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tickMe + ".gif'></TD>");
//document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tickNT + ".gif'></TD>");
//document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tick2000 + ".gif'></TD>");
//document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tickXP + ".gif'></TD>");
//document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tickServer2003 + ".gif'></TD>");
//document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tickLonghornAlpha + ".gif'></TD>");
//document.write("<TD><img src='https://www.hyperossystems.co.uk/07042003/img/tick_" + tickLinux + ".gif'></TD>");
//document.write("<TD CLASS='content'>" + RamHyperdrive + "</TD>");
document.write("<TD CLASS='content' ALIGN=middle>");
document.write("<DIV ID='p_" + i + "' CLASS='changetxt'></DIV>");
document.write("</TD>");
document.write("<TD CLASS='content' ALIGN=middle>");
writeQuantityDropDown( i );
document.write("<INPUT TYPE=hidden NAME=qty_" + i + ">" );
document.write("</TD>");
document.write("<TD ALIGN='middle' CLASS='content'>");
document.write("<DIV id='t_" + i + "' CLASS='changetxt'>0.00</DIV>");
document.write("<INPUT TYPE=hidden NAME=total_" + i + ">" );
document.write("</TD>");
document.write("</TR>");
}
}

function divObjInit()
  {
  for( i = 0 ; i < products.length; i ++ ) {
    if (qs != 'eval=1' && i == 0 ) { continue }
    eval ( "p" + i + " = new lib_obj('p_" + i + "')" );
    eval ( "t" + i + " = new lib_obj('t_" + i + "')" );
    }

  texvat=new lib_obj('t_exvat');
  tshipping=new lib_obj('t_shipping');
  tvat=new lib_obj('t_vat');
  tgrandtotal=new lib_obj('t_grandtotal');
  
  gttext=new lib_obj('lbl_gt');
  currencyInDollars = false;
  displayPrices();
  }
  
function displayPrices(){
  j = ( currencyInDollars ) ? 13 : 12;
  sym = ( currencyInDollars ) ? "$" : "£";
  for( i = 0 ; i < products.length; i ++ ) {
    if (qs != 'eval=1' && i == 0 ) { continue }
    eval ( "p" + i + ".writeIt('" + sym + products[i][j] + "')" );
    }
  }

function calc(){
//  if ( ! document.cust.zone.value || document.cust.zone.value  < 0 ) {
//    alert('Please first select your delivery location flag at the top of this order form.');
//    return;
//    }

  // tot up totals for each product and total of all products ordered...
  exvat = 0;
  for( i = 0 ; i < products.length; i++ ) {
    if (qs != 'eval=1' && i == 0 ) { continue }
    zc = ( currencyInDollars ) ? 13 : 12;
    price = products[i][zc];
    quantity = eval( "document.cust.selQ_" + i + ".value" );
    z = quantity * price;
    eval ( "t" + i + ".writeIt( formatCurrency ( z, currencyInDollars ) )" );
    exvat = exvat + z;
    // write to hidden form fields:
    eval( "document.cust.qty_" + i + ".value = quantity + ' x ' + products[i][0]" );
    eval( "document.cust.total_" + i + ".value = formatCurrency ( z, currencyInDollars )" );
    }
   texvat.writeIt( formatCurrency( exvat, currencyInDollars ) );
  document.cust.total_exvat.value = formatCurrency( exvat, currencyInDollars );

  // find total shipping weight for all goods ordered...
  zone = document.cust.zone.value;
  shippingweight = 0;
  shippingcost   = 0;
  insurance      = 0;

//  // select SURFACE or AIR freight if US delivery...
//  if( zone == 2 && document.cust.deliverymethod[1].checked ) zone = 5;
//
//  for (product = 0; product < products.length; product++)
//  {
//    if (qs != 'eval=1' && product == 0) { continue }
//
//    for (weight = 0; weight < shipping[0].length; weight++)
//    {
//      if (shipping[0][weight] == products[product][14]) { break }
//    }
//
//    if (weight >= shipping[0].length) weight--;
//    eval ("shippingcost += (document.cust.selQ_" + product + ".value) * shipping[zone][weight]");
//  }

  switch (zone)
  {
    case "2":
    {
      if (document.cust.usdeliverymethod[1].checked) { zone = 5 }
      break;
    }
    case "3":
    {
      if (document.cust.eudeliverymethod[1].checked) { zone = 6 }
      break;
    }
    case "4":
    {
      if (document.cust.rwdeliverymethod[1].checked) { zone = 7 }
      break;
    }
  }

  for( i = 0 ; i < products.length; i++ )
  {
    if (qs != 'eval=1' && i == 0 ) { continue }
    eval ( "shippingweight += ( document.cust.selQ_" + i + ".value ) * products[i][14] " );

    if (zone == 1)
    {
      // Add insurance for UK Royal Mail deliveries
      eval ( "insurance += ( document.cust.selQ_" + i + ".value ) * products[i][15] " );
    }
    else
    {
      // Add insurance for FedEx deliveries
      eval ( "insurance += ( document.cust.selQ_" + i + ".value ) * products[i][16] " );
    }      
  }

  // DEV - remove before release!
  //window.status = "shipping weight " + shippingweight + " kg";
  // find the shipping cost for this weight...
  if ( shippingweight > 0 ) {
    for ( i = 0 ; i < shipping[0].length; i++ ) {
      if ( shipping[0][i] >= shippingweight ) { break }
      }
    // shift i back to end of array length - this happens when weight greater than top charge bracket
    if ( i >= shipping[0].length ) i--;
    shippingcost = shipping[zone][i];
    }

  // Add on packing costs for FedEx only
  switch (zone)
  {
    case "2": // US/Canada = 30%
      shippingcost = parseFloat(shippingcost) * 1.3;
      break;
    case "3": // EU = 30%
      shippingcost = parseFloat(shippingcost) * 1.3;
      break;
    case "4": // Rest of World = 20%
      shippingcost = parseFloat(shippingcost) * 1.2;
      break;
  }

  // Add insurance
  shippingcost = parseFloat(shippingcost) + parseFloat(insurance);

  // Convert sterling to dollars for US/Canada and Rest of World
  if (zone == 2 || zone == 4 || zone == 5 || zone == 7)
  {
    shippingcost = parseFloat(shippingcost) * 1.55;
  }

  tshipping.writeIt( formatCurrency( shippingcost, currencyInDollars ) )
  document.cust.total_pnp.value = formatCurrency( shippingcost, currencyInDollars );
  
  // Determine IF and HOW MUCH VAT ...
  vat = new Number (0);
  vatrate = ( zone == 1 || zone == 3 || zone == 6 ) ? 0.175 : 0; // 17.5%
  vat = ( parseFloat(exvat) + parseFloat(shippingcost) ) * vatrate
  tvat.writeIt( formatCurrency( vat, currencyInDollars ) ) ;
  document.cust.total_vat.value = formatCurrency( vat, currencyInDollars );

  grandtotal = parseFloat(exvat) + parseFloat(shippingcost) + parseFloat(vat);
  
   tgrandtotal.writeIt( formatCurrency( grandtotal, currencyInDollars ) );
  document.cust.total_grand.value = formatCurrency( grandtotal, currencyInDollars );
  
  // dev
  //document.cust.recipient.value = "pete@stridebird.com";
  }

function formatCurrency(num,dollar)
{
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
  cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  if (dollar){return (((sign)?'':'-') + '$' + num + '.' + cents);}
  else {return (((sign)?'':'-') + '£' + num + '.' + cents);}
}

function writeQuantityDropDown(release,returnstring)
{
  var strQuantityDropDown = new String();
  strQuantityDropDown = '<select name="selQ_' + release + '" onchange="calc();">' +
  '<option value=0>0</option>' +
  '<option value=1>1</option>' +
  '<option value=2>2</option>' +
  '<option value=3>3</option>' +
  '<option value=4>4</option>' +
  '<option value=5>5</option>' +
  '<option value=6>6</option>' +
  '<option value=7>7</option>' +
  '<option value=8>8</option>' +
  '<option value=9>9</option>' +
  '</select>';

  if (returnstring){return strQuantityDropDown;}
  else{document.write(strQuantityDropDown);}
}




// ***** CHECK DETAILS **********************************************

function check_details(){

missing_details="";

// ---- Choose a release ----------------------

nothingorderedflag = 0;
for( i = 0; i < products.length; i++ ) {
  if (qs != 'eval=1' && i == 0 ) { continue }
  eval ( "nothingorderedflag += parseInt(document.cust.qty_" + i + ".value)"); 
  }
if ( nothingorderedflag == 0 || isNaN(nothingorderedflag)) {missing_details+="A quantity next to the revision you wish to order:\n";}

// ---- Card Holder Details ----------------------


if(document.cust.cust_title.options[document.cust.cust_title.selectedIndex].value==0)
 {missing_details+="Your Title\n";}

if(document.cust.firstname.value==0)
{missing_details+="Your first name\n";}

if(document.cust.secondname.value==0)
{missing_details+="Your second name\n";}

if(document.cust.card_address.value==0)
{missing_details+="Card Holder's address\n";}

if(document.cust.county.value==0)
{missing_details+="Your county or state\n";}

if(document.cust.country.value==0)
{missing_details+="Your country\n";}

if(document.cust.postcode.value==0)
{missing_details+="Your postcode or zip\n";}

email_test = document.cust.email.value;

if((email_test.indexOf("@") == -1) || (email_test.indexOf(".") == -1)) {
  missing_details+="Your e-mail\n";
}


// ---- Credit Card Details ----------------------


if(document.cust.account_name.value==0)
{missing_details+="The account name on your card\n";}

if(document.cust.cardtype.options[document.cust.cardtype.selectedIndex].value==0)
{missing_details+="Your card type\n";}

// check card number

str_card_number=new String(document.cust.card_number.value);

if(str_card_number.length==0)
{missing_details+="The sixteen digit number on your card\n";}

str_allowed = new String("0123456789 ");
bln_validcard = new Boolean(true);

for (i=0;i<str_card_number.length;i++)
{
//  alert('testing: ' + str_card_number.substr(i,1) + '\nposition: ' + str_allowed.indexOf(str_card_number.substr(i,1),0))
  if (str_allowed.indexOf(str_card_number.substr(i,1),0)==-1){bln_validcard=false;}
}

if(!bln_validcard)
{missing_details+="Numbers only for your card number\n";}

// check security number

str_secu_numb=new String(document.cust.secu_numb.value);

if((str_secu_numb.length!=3) && (str_secu_numb.length!=4))
{missing_details+="The security number on reverse of card\n";}

str_allowed = new String("0123456789");
bln_validcard = new Boolean(true);

for (i=0;i<str_secu_numb.length;i++)
{
//  alert('testing: ' + str_secu_numb.substr(i,1) + '\nposition: ' + str_allowed.indexOf(str_secu_numb.substr(i,1),0))
  if (str_allowed.indexOf(str_secu_numb.substr(i,1),0)==-1){bln_validcard=false;}
}

if(!bln_validcard)
{missing_details+="Numbers only for your security number\n";}

//if(isNaN(str_card_number))
//{missing_details+="numbers only for your card number\n";}

//if(document.cust.bank_name.value==0)
//{missing_details+="The name of the bank, which issues your card\n";}

cc=document.cust.cardtype.options[document.cust.cardtype.selectedIndex].value;

if(cc=="Switch")
  {if((document.cust.start_month.options[document.cust.start_month.selectedIndex].value=="x")||(document.cust.start_year.options[document.cust.start_year.selectedIndex].value=="x"))
    {missing_details+="The start date of your switch card\n";}
   if((document.cust.exp_month.options[document.cust.exp_month.selectedIndex].value=="x")||(document.cust.exp_year.options[document.cust.exp_year.selectedIndex].value=="x"))
    {missing_details+="The expiry date of your switch card\n";}
   if((document.cust.issue_number.options[document.cust.issue_number.selectedIndex].value=="x")||(document.cust.issue_number.options[document.cust.issue_number.selectedIndex].value=="x"))
    {missing_details+="The issue number of your switch card\n";}
  }

if(cc=="American_Express")
  {if((document.cust.start_month.options[document.cust.start_month.selectedIndex].value=="x")||(document.cust.start_year.options[document.cust.start_year.selectedIndex].value=="x"))
    {missing_details+="The start date of your American Express card\n";}
   if((document.cust.exp_month.options[document.cust.exp_month.selectedIndex].value=="x")||(document.cust.exp_year.options[document.cust.exp_year.selectedIndex].value=="x"))
    {missing_details+="The expiry date of your American Express card\n";}
  }

if((cc!="American_Express") && (cc!="Switch"))
   {if((document.cust.exp_month.options[document.cust.exp_month.selectedIndex].value=="x")||(document.cust.exp_year.options[document.cust.exp_year.selectedIndex].value=="x"))
    {missing_details+="The expiry date of your card\n";}
  }

// ---- End Credit Card Details ----------------------


if(missing_details=="")
{return true;}

missing_details="Please enter:\n" + missing_details;

alert(missing_details);
return false;
}




// **** Post Form *******************************************

function post_form()
  {
  // debug / dev!!! remove next line!!

//   return true;

    if(check_details())
      {
        return true;
      }
        return false;
  }





/************************************
**        CUSTOM JAVASCRIPT        **
*************************************
**     HANDLES BROWSER DETECT,     **
** MAKING OBJECTS FROM LAYERS/DIVS **
**  ALTERING TEXT WITHING OBJECTS  **
**   FOR PRICE / QUANTITY CHANGES  **
************************************/
//Browsercheck (needed) ***************
function lib_bwcheck()
{
  this.ver=navigator.appVersion
  this.agent=navigator.userAgent
  this.dom=document.getElementById?1:0
  this.opera5=this.agent.indexOf("Opera 5")>-1
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
  this.ie8=(this.ver.indexOf("MSIE 8")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6
  this.mac=this.agent.indexOf("Mac")>-1
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie8||this.ie7||this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5)
  return this
}
bw=new lib_bwcheck() //Browsercheck object

//Debug function ******************
function lib_message(txt){alert(txt); return false}

//Lib objects  ********************
function lib_obj(obj,nest)
{
//  alert('started making object from ' + obj)
  if(!bw.bw) return lib_message('Old browser')
  nest=(!nest) ? "":'document.'+nest+'.'
  this.evnt=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+"document.layers." +obj):0;
  if(!this.evnt) return lib_message('The layer does not exist ('+obj+') \nIf you\'re using Netscape please check the nesting of your tags!')
  this.css=bw.dom||bw.ie4?this.evnt.style:this.evnt;
  this.ref=bw.dom||bw.ie4?document:this.css.document;
  this.obj = obj + "Object"; eval(this.obj + "=this")
//  alert('completed making object from ' + obj)
  return this
}

//Writing content to object ***
lib_obj.prototype.writeIt = function(text,startHTML,endHTML)
{
  if(bw.ns4)
  {
    if(!startHTML){startHTML=""; endHTML=""}
    this.ref.open("text/html");
    this.ref.write(startHTML+text+endHTML);
    this.ref.close()
  }
  else
  {
  this.evnt.innerHTML=text
  }
}


//

