document.write('<link rel="shortcut icon" href="../images/favicon.ico" />');

var scrollingText = "       ELECTRICITY PRICES (£/MWh): S-09 BL 47.5, S-09 PK 59, W-09 BL 55.25, W-09 PK 68.5, S-10 BL 50, W-10 BL 57,         GAS PRICES (p/th): Day Ahead 54.25, S-09 47, W-09 61.3, S-10 51.75, W-10 65.2";
var scrolling	  = true;

callTicker = function() {
  document.tickerForm.ticker.value = scrollingText;
  document.tickerForm.ticker.style.width = window.screen.availWidth * 0.70;
  document.tickerForm.fTicker.style.width = window.screen.availWidth * 0.14;
  document.tickerForm.PriceWatch.style.width = window.screen.availWidth * 0.14;
  scrollingText = scrollingText.substring(1) + scrollingText.substring(0,1);
  if (scrolling) {
    callTickerRecall();
  }
}

function openLargeImage(ImgName) {
 /*var xAvail = screen.availWidth - 1245;
    var yAvail = screen.availHeight - 491;
    var left   = (screen.availWidth - xAvail)/2;
    var top    = (screen.availHeight - yAvail)/2;*/
    var imageURL = "";
    var xAvail = screen.availWidth;
    var yAvail = screen.availHeight;
    var left   = (screen.availWidth - xAvail)/2;
    var top    = (screen.availHeight - yAvail)/2;
    if (ImgName == 'Reverse') {
      imageURL = "../images/ReverseAuctionGraphLarge.jpg"; }
    else if (ImgName == 'Graph') {
      imageURL = "GraphLarge.png"; }
      window.open(imageURL,null,"height="+yAvail+",width="+xAvail+",left="+left+",top="+top+",status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,directories=no,copyhistory=no");
}

function openESLink() {
    var xAvail = screen.availWidth / 1.1;
    var yAvail = screen.availHeight / 1.1;
    var left     = (screen.availWidth - xAvail)/2;
    var top     = 0;
    var str = "width=" + xAvail + ",height=" + yAvail + ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top + ",menubar=no,status=no,titlebar=no,toolbar=yes,location=no,scrollbars=yes,resizable=yes,directories=no,copyhistory=no";
    link = "http://www.theenergyshop.com/getHomePage.do?agentID=217";
    /* PSWindow = window.open(link,'pswin',str); */
    this.location.href = link;
}

function openPSLink(PSForm, product) {
  document.PSForm.product.value = product;
  var isTrue = false;
  var reg1 = /^[a-zA-Z]{3,20}\s{1}[a-zA-Z]{3,20}/i;
  var reg2 = /^\w{2,50}/;
  var name = PSForm.fullName.value;
  var coName = PSForm.companyName.value;
  if (!reg1.test(name)) {
    alert("Please enter your full name as firstname [space] lastname");
    document.PSForm.fullName.focus();
    return false; }    
  else if (!reg2.test(coName)) {
    alert("Please enter your company name");
    document.PSForm.companyName.focus();
    return false; }
  else {
    var xAvail = screen.availWidth / 1.3;
    var yAvail = screen.availHeight / 1.3;
    var left     = (screen.availWidth - xAvail)/2;
    var top     = ((screen.availHeight - yAvail)/2)-50;
    var str = "width=" + xAvail + ",height=" + yAvail + ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top + ",menubar=no,status=no,titlebar=no,toolbar=yes,location=no,scrollbars=yes,resizable=no,directories=no,copyhistory=no";
    link = "http://www.electricitygastele.com/powerswitch/" + product + ".htm#" + product;
    PSWindow = window.open(link,'pswin',str);
    document.PSForm.submit(); }
}

callTickerRecall = function() {
  if (scrolling) {
  setTimeout('callTicker()',150);
  }
}

function showPyramidDesc(descName) {
  var element = document.getElementById("pyramidDescRow");
  var newText = "";
  element.innerHTML=newText;
  element.style.visibility  = "visible";
  if (descName == 'Reverse') {
    newText="Reverse Auction Service: Our procurement service is based on a two-phase reverse auction approach which has been tailored to deliver the most competitive energy prices available in the market.";
  }
  else if (descName == "Risk") {
    newText="Risk Management Service: Our risk management service provides facilities to assist timing your market entry to secure best prices and to help you determine the most appropriate contract to meet your specific risk requirements.";
  }
  else if (descName == "Financial") {
    newText="Financial Management Service: Our financial management service provides facilities for controlling and aggregating site usage and cost detail for budgeting and management reporting purposes.";
  }
  else if (descName == "Energy") {
    newText="Energy Efficiency Service: Our Energy Efficiency Service is delivered with our partner, EMS, best-of-breed engineer consultants for expertise and insight in delivering client savings.";
  }
  else if (descName == "Environmental") {
    newText="Environmental Services: Our environmental services provide the expertise and facilities required to minimise your regulated environmental costs and take advantage of emerging renewable energy technologies.";
  }
    element.innerHTML=newText;
}

hidePyramidDesc = function() {
  document.getElementById("pyramidDescRow").style.visibility = "hidden";
}

freezeTicker = function() {
  if (scrolling) {
    scrolling = false;
    document.tickerForm.fTicker.value = "Start Ticker";
    }
  else
  {
    scrolling = true;
    document.tickerForm.fTicker.value = "Stop Ticker";
    callTickerRecall();
  }
}

callIntro = function() {
    document.pyramidForm.intro.value = "Liquidity, transparency and speed for lower prices and less risk";
}

writeMenu = function() {
    document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" height=\"83\">");
    document.write("<tr>");
    document.write("<td valign=\"top\" halign=\"left\"  width=\"100%\">");
    document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" cols=\"2\" align=\"left\">");
    document.write("<tr>");
    document.write("<td valign=\"top\" halign=\"left\" rowspan=\"2\">");
    document.write("<a href=\"index.htm\"><image src=\"../images/beo.png\" width=\"158\" height=\"83\" align=\"left\" border=\"0\"></a>"); 
    document.write("</td>");
    document.write("<td valign=\"top\" halign=\"left\" width=\"100%\">");
    document.write("<span class=\"frontPageHeader\" align=\"left\">The online energy market for buyers of electricity and gas. Guaranteed improved prices. Typically 5-15% better than renewal quotes and 2-3% discount to wholesale prices.</span>");
    document.write("</td>");
    document.write("</tr>");
    document.write("<tr>");
    document.write("<td valign=\"bottom\" halign=\"left\" colspan=\"2\" width=\"100%\">");
    /*document.write("<div id=\"pageMenu\">");*/
    document.write("<ul id=\"Menu\">");
    document.write("<li><a href=\"index.htm\">Home</a></li>");
    document.write("<li><a href=\"index.htm\">Services</a>");
    document.write("<ul>");
    document.write("<li><a href=\"energyefficiency.htm\">Energy Efficiency</a></li>");
    document.write("<li><a href=\"http://www.theenergyshop.com/getHomePage.do?agentID=217\">Domestic Quote</a></li>");
    document.write("<li><a href=\"testimonials.htm\">Testimonials</a></li>");
    document.write("<li><a href=\"/attachments/BEO service.pdf\">Service&nbsp;Offering&nbsp;Summary</a></li>");
    document.write("<li><a href=\"/attachments/BEO service - print friendly.pdf\">Summary&nbsp;Print&nbsp;Friendly</a></li>");
    document.write("</ul>");
    document.write("</li>");
    document.write("<li><a href=\"index.htm\">About Us</a>");
    document.write("<ul>");
    document.write("<li><a href=\"background.htm\">Background</a></li>");
    document.write("<li><a href=\"recruitment.htm\">Recruitment</a></li>");
    document.write("<li><a href=\"partnering.htm\">Partnering&nbsp;Programme</a></li>");
    document.write("</ul>");
    document.write("</li>");
    document.write("<li><a href=\"contact.htm\">Contact Us</a></li>");
    document.write("<li><a href=\"runAuction.htm\">Run Auction</a></li>");
    document.write("<li><a href=\"energyefficiency.htm\">Energy Efficiency</a></li>");
    document.write("<li><a href=\"http://www.theenergyshop.com/getHomePage.do?agentID=217\">Domestic</a></li>");
    document.write("<li><a href=\"register.htm\" style=\"background:CC0000\">Quote Estimate</a></li>");
    document.write("</ul>");
    /*document.write("</div>");*/
    document.write("</td>");
    document.write("</tr>");
    document.write("</table>");
    document.write("</td>");
    document.write("<td valign=\"top\" cols=\"2\" halign=\"right\">");
    document.write("<form action=\"http://www.buyenergyonline.net/EnergyProcurement/j_acegi_security_check\" method=\"POST\" name=\"j_acegi_security_check\">");
    document.write("<table border=\"0\" cellpadding=\"1\" cellspacing=\"0\" bgcolor=\"#f9cd85\" align=\"right\" cols=\"3\" height=\"83\">");
    document.write("<tr>");
    document.write("<td colspan=\"3\">");
    document.write("<span class=\"smallText\">Access Energy Exchange</span>");
    document.write("</td>");
    document.write("</tr>");
    document.write("<tr>");
    document.write("<td halign=\"left\" valign=\"middle\">");
    document.write("<span class=\"smallText\">UserID:</span>");
    document.write("</td>");
    document.write("<td align=\"left\" colspan=\"2\">");
    document.write("<input type=\"text\" name=\"j_username\" maxlength=\"150\" value=\"\" tabindex=\"1\" id=\"username01\" style=background-color:white;border:none;width:100%>");
    document.write("</td>");
    document.write("</tr>");
    document.write("<tr>");
    document.write("<td halign=\"left\" valign=\"middle\">");
    document.write("<span class=\"smallText\">Password:</span>");
    document.write("</td>");
    document.write("<td align=\"left\" colspan=\"2\">");
    document.write("<input type=\"password\" name=\"j_password\" maxlength=\"150\" value=\"\" tabindex=\"2\" id=\"password01\" style=background-color:white;border:none;width:100%>");
    document.write("</td>");
    document.write("</tr>");
    document.write("<tr>");
    document.write("<td align=\"left\">");
    document.write("<input type=\"button\" name=\"register\" value=\"Register\" class=\"loginButtons2\" onclick=javascript:window.location=\"register.htm\" onmousedown=javascript:window.location=\"register.htm\">");
    document.write("</td>");
    document.write("<td align=\"center\">");
    document.write("<input type=\"button\" name=\"passwordreminder\" value=\"Password?\" class=\"loginButtons2\" onclick=parent.location=\"mailto:stephen.shilling@buyenergyonline.com?subject=Please&nbsp;remind&nbsp;me&nbsp;of&nbsp;my&nbsp;login&nbsp;details\">");
    document.write("</td>");
    document.write("<td align=\"right\">");
    document.write("<input type=\"submit\" name=\"login\" value=\"Login\" class=\"loginButtons2\" onclick=\"javascript:document.j_acegi_security_check.submit();\" onmousedown=\"javascript:document.j_acegi_security_check.submit();\">");
    document.write("</td>");
    document.write("</tr>");
    document.write("</table>");
    document.write("</form>");
    document.write("</td>");
    document.write("</tr>");
    document.write("<tr>");
    document.write("<td>");
    document.write("&nbsp;");
    document.write("</td>");
    document.write("</tr>");
    document.write("</table>");
}

menuHover = function() {
  var sfEls = document.getElementById("Menu").getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className += " sfhover";
    }
    sfEls[i].onmouseout=function() {
      this.className = this.className.replace(new RegExp("\\s?sfhover\\b"), "");
    }
  }
}

if (document.all) { //MS IE
  if (window.attachEvent) {
    window.attachEvent("onload", menuHover);
  }
  else { //IE 5.2 Mac does not support attachEvent
    var old = window.onload;
    window.onload = function() { 
      if (old) old(); menuHover();
    }
  }
}

addRow = function() {
  if (screen.availHeight>738) {
    document.write("<tr>");
    document.write("<td>");
    document.write("&nbsp;");
    document.write("</td>");
    document.write("</tr>");
  }
}

var isTrue = false;
checkRegistration = function() {
  isTrue = true;
  document.registerForm.errorField.value = "";
  if (document.registerForm.companyName.value == "")
  {
    document.registerForm.errorField.value = "Please enter your Company Name";
    document.registerForm.companyName.focus();
    isTrue = false;
  }
  else if (document.registerForm.fullname.value == "")
  {
    document.registerForm.errorField.value = "Please enter your full name";
    document.registerForm.fullname.focus();
    isTrue = false;
  }
  else if (document.registerForm.phone.value == "")
  {
    document.registerForm.errorField.value = "Please enter your phone number";
    document.registerForm.phone.focus();
    isTrue = false;
  }
  else if (document.registerForm.email.value == "")
  {
    document.registerForm.errorField.value = "Please enter your email address";
    document.registerForm.email.focus();
    isTrue = false;
  }
  else if (document.registerForm.gasAnnSpend.value == "" &&
            document.registerForm.HHAnnSpend.value == "" &&
            document.registerForm.NHHAnnSpend.value == "")
  {
    document.registerForm.errorField.value = "Please tell us which product you use by entering at least one Annual Spend";
    isTrue = false;
  }
  else if (document.registerForm.gasAnnSpend.value != "" ||
            document.registerForm.HHAnnSpend.value != "" ||
            document.registerForm.NHHAnnSpend.value != "")
  {
    if (document.registerForm.NHHAnnSpend.value != "")
    {
      var nhhSites = parseInt(document.registerForm.NHHSites.value,10);
      document.registerForm.NHHSites.value = nhhSites;
      var nHH = checkAnnSpend(document.registerForm.NHHAnnSpend.value);
      nHH = parseInt(nHH,10);
      document.registerForm.NHHAnnSpend.value = nHH;
                        
      if ((nHH == "") || (isNaN(nHH)))
      {
        document.registerForm.errorField.value = "Please tell us your approximate annual spend for NHH electricity in pounds";
        document.registerForm.NHHAnnSpend.focus();
        isTrue = false;
      }
      else
      {
        if ((nhhSites == "") || (isNaN(nhhSites)))
        {
          if (document.registerForm.NHHSites.value == "NaN")
          {
            document.registerForm.NHHSites.value = "";
          }
          document.registerForm.errorField.value = "Please tell us how many NHH electricity sites you have (integer value)";
          document.registerForm.NHHSites.focus();
          isTrue = false;
        }
        else
        {
          if (document.registerForm.NHHRenDate.value == "")
          {
            document.registerForm.errorField.value = "Please tell us the renewal date of your current NHH electricity contract";
            document.registerForm.NHHRenDate.focus();
            isTrue = false;
          }
          else
          {
            if (document.registerForm.nhhCurrentSupplier.options[document.registerForm.nhhCurrentSupplier.selectedIndex].value == "")
            {
              document.registerForm.errorField.value = "Please tell us your current NHH supplier";
              document.registerForm.nhhCurrentSupplier.focus();
              isTrue = false;
            }
          }
        }
      }
    }
    if (document.registerForm.HHAnnSpend.value != "")
    {
      var hhSites = parseInt(document.registerForm.HHSites.value,10);
      document.registerForm.HHSites.value = hhSites;
      var HH = checkAnnSpend(document.registerForm.HHAnnSpend.value);
      HH = parseInt(HH,10);
      document.registerForm.HHAnnSpend.value = HH;
                        
      if ((HH == "") || (isNaN(HH)))
      {
        document.registerForm.errorField.value = "Please tell us your approximate annual spend for HH electricity in pounds";
        document.registerForm.HHAnnSpend.focus();
        isTrue = false;
      }
      else
      {
        if ((hhSites == "") || (isNaN(hhSites)))
        {
          if (document.registerForm.HHSites.value == "NaN")
          {
            document.registerForm.HHSites.value = "";
          }
          document.registerForm.errorField.value = "Please tell us how many HH electricity sites you have (integer value)";
          document.registerForm.HHSites.focus();
          isTrue = false;
        }
        else
        {
          if (document.registerForm.HHRenDate.value == "")
          {
            document.registerForm.errorField.value = "Please tell us the renewal date of your current HH electricity contract";
            document.registerForm.HHRenDate.focus();
            isTrue = false;
          }
          else
          {
            if (document.registerForm.hhCurrentSupplier.options[document.registerForm.hhCurrentSupplier.selectedIndex].value == "")
            {
              document.registerForm.errorField.value = "Please tell us your current HH supplier";
              document.registerForm.hhCurrentSupplier.focus();
              isTrue = false;
            }
          }
        }
      }
    }
    if (document.registerForm.gasAnnSpend.value != "")
    {
      var gasSites = parseInt(document.registerForm.gasSites.value,10);
      document.registerForm.gasSites.value = gasSites;
      var gAS = checkAnnSpend(document.registerForm.gasAnnSpend.value);
      gAS = parseInt(gAS,10);
      document.registerForm.gasAnnSpend.value = gAS;
                        
      if ((gAS == "") || (isNaN(gAS)))
      {
        document.registerForm.errorField.value = "Please tell us your approximate annual spend for gas in pounds";
        document.registerForm.gasAnnSpend.focus();
        isTrue = false;
      }
      else
      {
        if ((gasSites == "") || (isNaN(gasSites)))
        {
          if (document.registerForm.gasSites.value == "NaN")
          {
            document.registerForm.gasSites.value = "";
          }
          document.registerForm.errorField.value = "Please tell us how many gas sites you have (integer value)";
          document.registerForm.gasSites.focus();
          isTrue = false;
        }
        else
        {
          if (document.registerForm.gasRenDate.value == "")
          {
            document.registerForm.errorField.value = "Please tell us the renewal date of your current gas contract";
            document.registerForm.gasRenDate.focus();
            isTrue = false;
          }
          else
          {
            if (document.registerForm.gasCurrentSupplier.options[document.registerForm.gasCurrentSupplier.selectedIndex].value == "")
            {
              document.registerForm.errorField.value = "Please tell us your current gas supplier";
              document.registerForm.gasCurrentSupplier.focus();
              isTrue = false;
            }
          }
        }
      }
    }
  }
  if (isTrue)
  {
    checkPhoneNumber();
    if (isTrue)
    {
      checkEmailAddress();
    }
  }
  return isTrue;
}

checkAnnSpend = function(Prod) {
  Prod = Prod.replace(',','');
  Prod = Prod.replace(' ','');
  Prod = Prod.replace(String.fromCharCode(163),'');
  if (Prod.indexOf('.') > -1)
  {
    Prod = Prod.substring(0,Prod.indexOf('.'));        
  }
  while (Prod.substring(0,1) == ' ') 
  {
    Prod = Prod.substring(1,Prod.length);
  }
  while (Prod.substring(Prod.length-1,Prod.length) == ' ') 
  {
    Prod = Prod.substring(0,Prod.length-1);
  }
  return Prod;
}

checkPhoneNumber = function() {
  isTrue = false;
  if (document.registerForm.phone.value != "")
  {
    var tfld = document.registerForm.phone.value;
    var telnr = /^\+?[0-9 ()-]+[0-9]$/;
    if (!telnr.test(tfld)) 
    {
      document.registerForm.errorField.value = "Not a valid telephone number. Characters permitted are digits, spaces and a leading +";
      document.registerForm.phone.focus();
      isTrue = false;
    }
    else
    {
      var numdigits = 0;
      for (var j=0; j<tfld.length; j++)
      {
        if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') 
        {
          numdigits++;
        }  
      }
      if (numdigits<10 || numdigits>12) 
      {
        document.registerForm.errorField.value = "A phone number with " + numdigits + " digits is not a correct UK landline or mobile phone number";
        document.registerForm.phone.focus();
        isTrue = false;
      }
      else
      {
        document.registerForm.errorField.value = "";
        isTrue = true;
      }
    }
  }
}

checkEmailAddress = function() {
  isTrue = false;
  if (document.registerForm.email.value != "")
  {
    var tfld = document.registerForm.email.value;
    var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
    var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/;
    if ((!email.test(tfld)) || (!email2.test(tfld))) 
    {
      document.registerForm.errorField.value = "Please enter a valid email address";
      document.registerForm.email.focus();
      isTrue = false;
    }
    else
    {
      document.registerForm.errorField.value = "";
      isTrue = true;
    }
  }
}

callDateFocus = function() {
  var txt = document.registerForm.errorField.value.toLowerCase();
  if (txt.indexOf("date") == -1)
  {
    document.registerForm.errorField.value = "Please enter a date in the format dd/mm/yyyy or enter 'No Contract' if you are out of contract";
  }
}

callAnnSpendFocus = function() {
  document.registerForm.errorField.value = "Please enter an amount in whole GBP's";
}

callSiteFocus = function() {
  document.registerForm.errorField.value = "Please enter the number of sites as an integer (whole number)";
}

callName = function() {
  document.registerForm.errorField.value = "Please enter your full name as firstname<space>lastname";
}

callCoName = function() {
  document.registerForm.errorField.value = "Please enter your full company name";
}

function isInteger(s) {
  var i;
  for (i = 0; i < s.length; i++)
  {   
    // Check that current character is number.
    var c = s.charAt(i);
    if (((c < "0") || (c > "9")))
    { 
      validateDate = false;
    }
  }
  // All characters are numbers.
  return true;
}

function stripCharsInBag(s, bag) {
  var i;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.
  for (i = 0; i < s.length; i++)
  {   
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1)
    {
      returnString += c;
    }
    return returnString;
  }
}

function daysInFebruary (year) {
  // February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
  for (var i = 1; i <= n; i++) 
  {
    this[i] = 31;
    if (i==4 || i==6 || i==9 || i==11) 
    {
      this[i] = 30;
    }
    if (i==2) 
    {
      this[i] = 29;
    }
  } 
  return this;
}

function isDate(dtStr)
{
  var daysInMonth = DaysArray(12);
  var pos1=dtStr.indexOf(dtCh);
  var pos2=dtStr.indexOf(dtCh,pos1+1);
  var strDay=dtStr.substring(0,pos1);
  var strMonth=dtStr.substring(pos1+1,pos2);
  var strYear=dtStr.substring(pos2+1);
  strYr=strYear;
  if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
    for (var i = 1; i <= 3; i++) 
    {
      if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
    }
    month=parseInt(strMonth);
    day=parseInt(strDay);
    year=parseInt(strYr);
    if (pos1==-1 || pos2==-1)
    {
      document.registerForm.errorField.value = "The date format should be: dd/mm/yyyy";
      return false;
    }
    if (strMonth.length<1 || month<1 || month>12)
    {
      document.registerForm.errorField.value = "Please enter a valid month";
      return false;
    }
    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
    {
      document.registerForm.errorField.value = "Please enter a valid day";
      return false;
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
    {
      document.registerForm.errorField.value = "Please enter a valid 4 digit year between " + minYear + " and " + maxYear;
      return false;
    }
    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
    {
      document.registerForm.errorField.value = "Please enter a valid date";
      return false;
    }
  return true;
}
