/*  
 *  CiA Training Web Scripts
 *  v1.61
 *  Author: Tegs - 15/10/2004
 *  Last Updated : 06/10/2008
 *  © All rights reserved CiA Training Ltd.
*/

function checkBrowser() {
  checkCookiesEnabled();
}

function bannerCall(str) {
  if (str == "equals") {
    generateLargerImageFromFile('covers/equals_unit021.jpg');
  }
  else if (str == "ecdl_icdl") {
    generateLargerImageFromFile('covers/ecdl_module1.jpg');
  }
  else if (str == "open_learning") {
    generateLargerImageFromFile('covers/ol_windows_intro.jpg');
  }
  else if (str == "new_clait") {
    generateLargerImageFromFile('covers/newclait_unit1.jpg');
  }
  else if (str == "clait_advanced") {
    generateLargerImageFromFile('covers/claitadv_unit1.jpg');
  }
  else if (str == "ecdl_icdl_advanced") {
    generateLargerImageFromFile('covers/ecdl_am3.jpg');
  }
  else if (str == "clait_plus") {
    generateLargerImageFromFile('covers/claitplus_unit1.jpg');
  }
  else if (str == "do_offer") {
    document.location = "office_2010.php";
  }
  else if (str == "do_offer1") {
    document.location = "office_2010.php";
  }
  else if (str == "do_offer2") {
    document.location = "office_2010.php";
  }
}

function openXmasOffers() {
	openOfferWindow();
}

function writeAlertStatement(alertMessage) {
  document.write("<table border='0' width='100%' id='table41' cellspacing='0' cellpadding='0'>");
  document.write("<tr>");
  document.write("<td width='778' valign='bottom'>");
  document.write("<table border='0' width='100%' id='table42' cellspacing='0' cellpadding='0'>");
  document.write("<tr>");
  document.write("<td width='35' background='images/alert_bar_left.gif'>");
  document.write("<img border='0' src='images/alert_bar_warning.gif' width='16' height='17' align='right'   alt='Alert Message Icon'></td>");
  document.write("<td width='707' background='images/alert_bar_middle.gif'>");
  document.write("<a class='WarningTextBold'>&nbsp;Alert:</a><a class='WarningText'> "+alertMessage+"</a></td>");
  document.write("<td width='36' background='images/alert_bar_right.gif'><a class='WarningText'>&nbsp;</a></td>");
  document.write("</tr>");
  document.write("</table>");
  document.write("<table border='0' width='100%' id='table43' cellspacing='0' cellpadding='0'>");
  document.write("<tr>");
  document.write("<td>");
  document.write("<img border='0' src='images/alert_bar_bottom.gif' width='778' height='1' alt=''></td>");
  document.write("</tr>");
  document.write("</table>");
  document.write("</td>");
  document.write("</tr>");
  document.write("</table>");
}

function writeFlashBanner(id, section) {
  if (section == "full") {
    document.getElementById(id).innerHTML = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='504' height='114' id='CiABanner' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='banners/office_banner4.swf?paramText="+section+" /><param name='menu' value='false' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='banners/office_banner4.swf?paramText="+section+" quality='high' bgcolor='#ffffff' width='504' height='114' name='CiA Banner' align='middle' allowscriptaccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /><noembed><table border='0' width='100%' id='no_embed' cellspacing='0' cellpadding='0'><tr><td align='center'><a class='InlineLinkBodyText'>You need the <a class='BodyLink' href='http://www.macromedia.com/go/getflashplayer/'>Macromedia Flash Player</a><a class='InlineLinkBodyText'> to view our banner.</a></td></tr></table></noembed></object>";
  }
  else if (section == "ecdl_icdl_new") {
    document.getElementById(id).innerHTML = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='504' height='114' id='CiABanner' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='banners/office_banner4.swf?paramText="+section+" /><param name='menu' value='false' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='banners/office_banner4.swf?paramText="+section+" quality='high' bgcolor='#ffffff' width='504' height='114' name='CiA Banner' align='middle' allowscriptaccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /><noembed><table border='0' width='100%' id='no_embed' cellspacing='0' cellpadding='0'><tr><td align='center'><a class='InlineLinkBodyText'>You need the <a class='BodyLink' href='http://www.macromedia.com/go/getflashplayer/'>Macromedia Flash Player</a><a class='InlineLinkBodyText'> to view our banner.</a></td></tr></table></noembed></object>";
  }
  else {
    document.getElementById(id).innerHTML = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='504' height='114' id='CiABanner' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='banners/office_banner4.swf?paramText="+section+" /><param name='menu' value='false' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='banners/office_banner4.swf?paramText="+section+" quality='high' bgcolor='#ffffff' width='504' height='114' name='CiA Banner' align='middle' allowscriptaccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /><noembed><table border='0' width='100%' id='no_embed' cellspacing='0' cellpadding='0'><tr><td align='center'><a class='InlineLinkBodyText'>You need the <a class='BodyLink' href='http://www.macromedia.com/go/getflashplayer/'>Macromedia Flash Player</a><a class='InlineLinkBodyText'> to view our banner.</a></td></tr></table></noembed></object>";
  }
} 

function clearField(field, def) {
  if (trim(field.value) == def) field.value = "";
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function removeVATnumber()
{
  document.form.vtn.value = 0;
  document.form.zone.value = document.form.D1.value;
  document.form.submit();   
}

function getVATnumber(zone) {
  vatNumber=prompt('Please enter your registered VAT Number:','');

  vatNumber = vatNumber.toUpperCase();

  if (vatNumber != null)
  {
    //check validity - 2 letters then 12 digits
    var valid = true;
    var cc1 = vatNumber.substring(0,1); //1st char
    var cc2 = vatNumber.substring(1,2); //2nd char
    var rest = vatNumber.substr(2);
    if(rest.length > 12 || rest.length < 8 || !isNaN(cc1) || !isNaN(cc2))
    {
      valid = false;
    }
    if(valid)
    {
      document.form.vtn.value=vatNumber;
      document.form.zone.value=zone;     
      document.form.submit();
    }
    else
    {
      alert("You have entered an invalid VAT number. Please try again.");
    }
  }
  
}

function addToMailingList(name, email) {
  if (name == "Enter name") {
    alert("Please provide your name to join our e-Mailing list");
    return;
  }
  
  if (email == "Enter e-mail address" || !isValidEmail(email)) {
    alert("Please enter a valid e-mail address to join our e-Mailing list");
    return;
  }

  document.location = "contact_update_form.php?name="+name+"&email="+email;
}

function isValidEmail(str) {
   return (str.indexOf(".") > 0) && (str.indexOf("@") > 0);
}

function resetField(field, def) {
  if (trim(field.value) == "") field.value = def;
}

function openWindow(path) {
  window.open(path,"myWindow","width=400,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes");
}

function openDemoWindow() {
  window.open("http://www.chapmanlatimer.co.uk/demos/bank_safe/main.html","myWindow");
}

function openOfferWindow() {
  window.open("winter_offer.htm","myWindow","width=824,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes");
}

function openFinanceDemoWindow() {
  window.open("http://www.chapmanlatimer.co.uk/demos/finance/main.html","myWindow","width=970,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes");
}

function openStaffRosterDemoWindow() {
  window.open("http://www.chapmanlatimer.co.uk/demos/staffroster/main.html","myWindow","width=970,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes");
}

function closeWindow() {
  window.close();
}

function refreshWindow() {
  window.location.reload();
}

function getCookie ()
{
  var dcookie = document.cookie; 
  var cname = "CiACart=";
  var clen = dcookie.length;
  var cbegin = 0;
      while (cbegin < clen)
      {
        var vbegin = cbegin + cname.length;
        if (dcookie.substring(cbegin, vbegin) == cname)
        { 
          var vend = dcookie.indexOf (";", vbegin);
          if (vend == -1)
          {
            vend = clen;
          }
          var temp = unescape(dcookie.substring(vbegin, vend));
          var arr = temp.split("@");
          return arr;
        }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
        if (cbegin == 0) break;
      }
    return new Array();
}


function setCookie (arr)
{
  var expires = new Date();
  var value = arr.join("@");
  expires.setTime (expires.getTime() + (1000 * 60 * 60 * 24));
  document.cookie = "CiACart" + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function getQty(arr)
{
  var total = 0;
  for (k = 0; k < arr.length; k++) 
  {
    var x=document.getElementById("T"+k);
    var row = arr[k];
    var cart = row.split("|");
    total = total + (parseInt(x.value) * parseInt(cart[4]));
  }

  return total;
}

function update(zone)
{
  var arr = getCookie();
  var setRemove = false;
  var isbn = new Array(0);
  var size = new Array(0);
  for (i = 0; i < arr.length; i++) 
  {
    var row = arr[i];
    var cart = row.split("|");
    
    var x=document.getElementById("T"+i);
    
  	if (cart[2] != x.value)
  	{
      if(isNaN(parseInt(x.value))) 
      {
        alert("Please enter a valid quantity.");
      }
      else if(x.value.indexOf(".") != -1)
      {
        alert("Please enter a valid quantity.")
      }
      else
      {
        var newVal = parseInt(x.value, 10);
    	  if(newVal < 101 && getQty(arr) < 101)
    	  {
    	    if(newVal > 0)
    	    {
              cart[2] = newVal;
        	    row = cart.join("|");
        	    arr[i] = row;
    	    }
    	    else if(newVal == 0)
    	    {
    	      var temp = cart[1].split("#");
    	      isbn[isbn.length] = temp[1];
    	      size[size.length] = cart[3];
    	      setRemove = true;
          }
    	    else
    	    {
            alert("Please enter a valid quantity.");
          }
    	  }
    	  else
    	  {
    	    alert("Sorry, you can't order more than 100 items online.\nPlease contact a member of our sales team to fulfil this request.");    
    	  }
      } 
  	} 		   
  }
  
  setCookie(arr);
  if(setRemove)
  {
    for (k = 0; k < isbn.length; k++) 
    {
      remove2(isbn[k],size[k]);
    }
  }

  document.form.zone.value=zone;
  document.form.submit();
}

function setShipping()
{
  document.form.zone.value = document.form.D1.value;
  updatePrices(document.form.D1.value);
}

function remove(id, size)
{
  var arr = getCookie();
  for (i = 0; i < arr.length; i++) 
  {
    var row = arr[i];
    var cart = row.split("|");    
    var isbn = cart[1].split("#");

	if (isbn[1] == id && cart[3] == size)
	{
	  if(arr.length == 1)
	  {
	    arr = new Array();
	  }
	  else
	  {
	    var arr1 = arr.slice(0,i);
	    var arr2 = arr.slice(i+1,arr.length);
	    arr = arr1.concat(arr2);
	    //arr.splice(i,1);
	    i = arr.length;
	  }
	} 		   
  }
  setCookie(arr);
  document.form.submit();
}

function remove2(id, size)
{
  //different to remove as does not submit form at end - use for removing a series of items
  var arr = getCookie();
  for (i = 0; i < arr.length; i++) 
  {
    var row = arr[i];
    var cart = row.split("|");
    
    var isbn = cart[1].split("#");

	if (isbn[1] == id && cart[3] == size)
	{
	  if(arr.length == 1)
	  {
	    arr = new Array();
	  }
	  else
	  {
	    var arr1 = arr.slice(0,i);
	    var arr2 = arr.slice(i+1,arr.length);
	    arr = arr1.concat(arr2);
	    //arr.splice(i,1);
	    i = arr.length;
	  }
	} 		   
  }

  setCookie(arr);

}

function doCheckout(zone)
{
  var dest;
  var exclusions;

  if (zone == "Zone2")
  {
    dest = "UK Islands and Southern Ireland";
    exclusions = "Please note that we are unable to fulfil ECDL orders for clients in Southern Ireleand - please place your ECDL orders direct with ICS at www.ics-skills.ie";
  }
  else if (zone == "Zone1")
  {
    dest = "Northern Scotland/Ireland";
    exclusions = "Please note that this excludes UK Islands and Southern Ireland";
  }
  else
  {
    dest = "United Kingdom";
    exclusions = "Please note that this excludes Northern Scotland, Ireland and UK Islands";
  }

  if(confirm("You have selected \""+dest+"\" as your shipping destination. "+exclusions+". If you are happy to continue, click OK..."))
  {
    document.form.checkout.value = zone;
    update(zone);  
  }
}

function doPOCheckout(zone)
{
  var dest;
  var exclusions;

  if (zone == "Zone2")
  {
    dest = "UK Islands and Southern Ireland";
    exclusions = "Please note that we are unable to fulfil ECDL orders for clients in Southern Ireleand - please place your ECDL orders direct with ICS at www.ics-skills.ie";
  }
  else if (zone == "Zone1")
  {
    dest = "Northern Scotland/Ireland";
    exclusions = "Please note that this excludes UK Islands and Southern Ireland";
  }
  else
  {
    dest = "United Kingdom";
    exclusions = "Please note that this excludes Northern Scotland, Ireland and UK Islands";
  }

  if(confirm("You have selected \""+dest+"\" as your shipping destination. "+exclusions+". If you are happy with this, click OK to continue..."))
  {
    document.location = "store_purchase_order.php?dest="+zone;
  }
}

function openLearnersWorldTour() {
  window.open("learners_world_1.htm","myWindow","width=368,height=353,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,resizable=yes");
}

function generateLargerImageFromFile(imagePath) {
  window.open("generic_preview_large_image.htm?"+imagePath,"myWindow","width=292,height=452,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,resizable=yes");
}

function generateImage() {
  document.write("<img border='0' src='images/"+getPathExtension()+"' width=252, height=356 alt='Preview of Cover'>");
}

function getPathExtension() {
  str = location.search;
  return str.substring(1, str.length);
}

function openScreenShot(path) {
  window.open(path,"myWindow","width=620,height=543,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,resizable=yes");
}

function openBankSafeScreenShot(path) {
  window.open(path,"myWindow","width=680,height=543,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,resizable=yes");
}

function openApprenticeScreenShot(path) {
  window.open(path,"myWindow","width=680,height=543,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,resizable=yes");
}

function openDownloadManager() {
  window.open("data_files/data_file_manager_026.htm","myWindow","width=525,height=460,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,resizable=yes");
}

function openCompleteDataFileList() {
  window.open("data_files_all.php","myWindow","width=525,height=460,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes");
}

function triggerDownloadManagerHelp() {
  document.DownloadManager.SetVariable("help", "doHelp");
}

function triggerDownloadManagerInitialisation() {
  document.DownloadManager.SetVariable("refresh", "doRefresh");
}

function errorReadingFromDatabase() {
  alert("An error has occured reading from the database");
  refreshWindow();
}

function isbnSearch(isbn) {
  var rExp = /&/gi;
  isbn = isbn.replace(rExp, "%26");
  isbn = isbn.toLowerCase();
  var newISBN = reformatAsISBN(isbn);
  isbn = newISBN;

  if(isbn.length == 10 || isbn.length == 13)
  {
    document.location="store_product_list.php?isbn="+isbn;
  }
  else
  { 
    alert('Please enter a valid ISBN number');
  }
}

function contactUpdate(title, fName, sName, address, country, postcode, telphone, femail) {
  if(title.length == 0)
  {
    alert('Please enter your Title');
    return;
  }
  if(fName.length == 0)
  {
    alert('Please enter your First Name');
    return;
  }
  if(sName.length == 0)
  {
    alert('Please enter your Surname');
    return;
  }
  if(address.length == 0)
  {
    alert('Please enter your Address');
    return;
  }
  if(country.length == 0)
  {
    alert('Please enter your Country');
    return;
  }
  if(postcode.length == 0)
  {
    alert('Please enter your Postcode');
    return;
  }
  if(telphone.length == 0)
  {
    alert('Please enter your Telephone Number');
    return;
  }
  if(femail.length == 0)
  {
    alert('Please enter your Email Address');
    return;
  }
  document.form.submit();
}

function search(key, advVal) {
  var rExp = /&/gi;
  key = key.replace(rExp, "%26");
  document.location = "store_product_list.php?keyword="+key+"&advSearch="+advVal;
}

function add2Basket(bookId, title, weight, size, isbn)
{

  var arr = getCookie();
  var found = 'false';
  var total = 0;
  var val = 0;
  for (i = 0; i < arr.length; i++)
  {
    if (arr[i] == "@" || arr[i]=="")
    {
      if(i == 0)
      {
        arr = new Array(0);
        break;
      }
      else if(i < arr.length)
      {
        arr.splice(i, i+1);
      }
      else
      {
        arr.pop();
      }
    }
  
    var row = arr[i];
    var cart = row.split("|");  

    total = parseInt(total) + (parseInt(cart[2])* parseInt(cart[4]) );
    var isb = cart[1].split("#");
    if(cart[0] == bookId && cart[3] == size && isbn == isb[1])
    {
      found = 'true';
      val = i;
    }
  }


  if(found == 'true' && (parseInt(total) + parseInt(weight)) < 101)
  {
    row = arr[val];
    var cart = row.split("|");
    cart[2] = parseInt(cart[2])+1;
    row = cart.join("|");
    arr[val]= row; 
  }
  else if(arr.length < 1 || found == 'false' && (parseInt(total) + parseInt(weight)) < 101)
  {
    var entry = bookId+"|"+title+"#"+isbn+"|1|"+size+"|"+weight;
    arr[arr.length] = entry;
    //arr.push(entry);
  }

  if (total >= 100 || (parseInt(total) + parseInt(weight)) > 100)
  {
    alert("Sorry, you can't order more than 100 items online.\nPlease contact a member of our sales team to fulfil this request.");
  }
  setCookie(arr);
  //location.reload();
  document.location="store_shopping_cart.php";
}

function searchOnEnter(field, event, type) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13)
  {
    if (type == "KEYWORD") 
    {
      search(field, document.form.C1.checked);
      return true;
    }
    else if (type == "ISBN") 
    {
      isbnSearch(field);
      return true;
    }
  }
  return false;
}

function downloadDataOnEnter(event,val) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13) {
    findDataFileByReference(val);
    return false;
  }
  return false ;
}

function findISBN(isbns, versions)
{
  var win = document.form.D1.value;
  var off = document.form.D2.value;
  var ie = document.form.D3.value;
  if(win == "none" || off == "none" || ie == "none")
  {
    alert("Please select a valid Windows, Office and Internet Explorer version.");
    return "unset";
  }
  else
  {
    var key = win+", "+off+" and "+ie;
    var tempISBN = isbns.split("|");
    var tempVer = versions.split("|");
    for (i = 0; i < tempVer.length; i++)
    {
      if(tempVer[i] == key)
      {
        return tempISBN[i];
      }
    }
  }
}

function verify(bID, title, weight, size, isbns, versions, selectionType)
{
  if (selectionType == "SELECT") {
	alert("Please select a valid product version.");
	return;
  }

  var isbn = "unset";
  if (document.getElementById('D1') != null) 
  {
    isbn = findISBN(isbns, versions);
  }
  else
  {
    isbn = document.form.D6.value;
  } 
  
  if(isbn != "unset")
  {
    add2Basket(bID, title, weight, size, isbn)
  }
}

function checkCookiesEnabled() {
  SetTempCookie('varCook','True','','/','','');
  if (GetTempCookie() != "True") {
    writeAlertStatement("This site requires Cookies to be enabled. Please consult your browser's documentation to find out how to do this...");
  }		
}

function GetTempCookie() {
  var strName = "varCook";
  if (document.cookie.indexOf(strName) == -1) {
    return false;
  }
  else {
    cookieStart = document.cookie.indexOf(strName);
    cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
    cookieValEnd = document.cookie.indexOf(";", cookieStart);

    if (cookieValEnd == -1) {
      cookieValEnd = document.cookie.length;
    }
    cookieValue = document.cookie.substring(cookieValStart, cookieValEnd);
  }

  if (cookieValue = "True") {
    return ("True");
  }
}

function SetTempCookie(name, value, expires, path, domain, secure) {
  var strDNS = GetDNS();
  document.cookie = name + "=" +escape(value) +
  ( (expires) ? ";expires=" + expires.toGMTString() + 1000*60*20 : "") +
  ( (path) ? ";path=" + path : "") +
  ( (domain) ? ";domain=" + strDNS : "") +
  ( (secure) ? ";secure" : "");
  return true
}

function GetDNS() {
  cookieDomain = document.location.hostname;
}

function updatePrices(zone)
{
  writeShipping(zone);
  writeAmount(zone);
  writeVAT(zone);
  writeTotal(zone);
}

function writeShipping(zone)
{ 
  if (zone == "Zone2")
  {
    zp = document.form.shipSI.value;
  }
  else if (zone == "Zone1")
  {
    zp = document.form.shipNI.value;
  }
  else
  {
    zp = document.form.shipUK.value;
  }

  document.form.TB2.value = zp;
}

function writeAmount(zone)
{
  if (zone == "Zone2")
  {
    amount = document.form.amountSI.value;
  }
  else if (zone == "Zone1")
  {
    amount = document.form.amountNI.value;
  }
  else
  {
    amount = document.form.amountUK.value;
  }

  document.form.TB3.value = amount;
}

function writeVAT(zone)
{
  if (zone == "Zone2")
  {
    vat = document.form.vatSI.value;
  }
  else if (zone == "Zone1")
  {
    vat = document.form.vatNI.value;
  }
  else
  {
    vat = document.form.vatUK.value;
  }

  document.form.TB4.value = vat;
}

function writeTotal(zone)
{
  if (zone == "Zone2")
  {
    total = document.form.totalSI.value;
  }
  else if (zone == "Zone1")
  {
    total = document.form.totalNI.value;
  }
  else
  {
    total = document.form.totalUK.value;
  }
  
  document.form.TB5.value = total;
}

function findDataFileByReference(isbn) {
  var rExp = /&/gi;
  isbn = isbn.replace(rExp, "%26");
  isbn = isbn.toLowerCase();
  var newISBN = reformatAsISBN(isbn);
  if (newISBN.length == 13) isbn = newISBN;

  if (isbn.length > 0) {
  var totalEntries = document.form.var0.value*1;
  var counter = 0;
  for (i=1; i<totalEntries+1; i++) {
    var v = eval("document.form.var"+i+".value");
    var name = v.substr(0,v.indexOf("="));
    var path = v.substr(v.indexOf("=")+1,v.length);

    var isbnCount = name.split("|");
    for (j=0; j < isbnCount.length; j++) {
      if (isbn==isbnCount[j].toLowerCase()) {
	document.location.href=path;
	return;
      }
    }
  }
  }
  alert("Sorry, but there are no data files available online for that FastCode or ISBN number. Try searching for your data by product title below.");
}

function reformatAsISBN(strString) {
  var strValidCharacters = "1234567890x";
  var strReturn = "";
  var strBuffer = "";
  var intIndex = 0;

  for( intIndex = 0; intIndex < strString.length; intIndex++ ) {
    strBuffer = strString.substr( intIndex, 1 );
    if( strValidCharacters.indexOf( strBuffer ) > -1 ) {
      strReturn += strBuffer;
    }
  }
  return strReturn;
}

function validateDemoForm(email) {
  if (isValidEmail(email)) document.form.submit();
  else alert("Please enter a valid e-mail address.");
}

function validatePurchaseOrderForm(thisform) {
  with (thisform) {
    if (isValid(purchaseOrderNumber,"Please enter a valid Purchase Order Number")==false) {
      purchaseOrderNumber.focus();
      return;
    }
    if (isValid(invoiceName,"Please enter a valid Invoice Name")==false) {
      invoiceName.focus();
      return;
    }
    if (isValid(invoiceAddress1,"Please enter a valid Invoice Address")==false) {
      invoiceAddress1.focus();
      return;
    }
    if (isValid(invoiceTown,"Please enter a valid Invoice Address")==false) {
      invoiceTown.focus();
      return;
    }
    if (isValid(invoiceCounty,"Please enter a valid Invoice Address")==false) {
      invoiceCounty.focus();
      return;
    }
    if (isValid(invoicePostcode,"Please enter a valid Invoice Postcode")==false) {
      invoicePostcode.focus();
      return;
    }
    if (isValid(invoiceTelephone,"Please enter a valid Telephone Number")==false) {
      invoiceTelephone.focus();
      return;
    }
    if (isValid(invoiceEmail,"Please enter a valid Invoice Name")==false) {
      invoiceEmail.focus();
      return;
    }
  }
  document.form.submit();
}

function isValid(formItem, alertText) {
  with (formItem) {
    if (value==null||value=="") {
      alert(alertText);
      return false;
    }
    else {
      return true;
    }
  }
}

function downloadDemo(username, password) {
  if (username.toUpperCase() == "CIATRAINING" && password.toUpperCase() == "ICOURSEDEMO") document.location.href="downloads/iCourseProDemo.exe";
  else alert("Invalid Username or Password.");
}

