/* site specific scripts */

/* Add Trim function to String object */
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, '');
}

function siteInit(){
  // Called with the body onload event.
  // Customise according to site requirements.
  if(navigator.userAgent.indexOf('MSIE') >= 0) {
    //fixExplorerCornersBug()
  }
  if(document.body.className.indexOf('Checkout') >= 0){
    checkDeliveryField();
  }
}

function setLayout() {
  // Not required.
}

function redrawLayout() {
  // Not required.
}

function isElement(el) {
  return (typeof(el) == "object" && el != null);
}

SIZE_DOWN_MSG = "Aa Change Size";
SIZE_UP_MSG = "Aa Change Size";
function displayFontSwitch() {
  document.write("<span id=\"font-switch\" \n");
  document.write("      class=\"\"\n");
  document.write("      onclick=\"switchFontSize(this)\"\n");
  document.write("      onmouseover=\"this.className = 'over';\"\n");
  document.write("      onmouseout=\"this.className = '';\"\n");
  document.write("      title=\"Change text size\">\n");
  if(getCookie("baseFontSize") > 0) {
    document.write(SIZE_DOWN_MSG);
  }
  else {
    document.write(SIZE_UP_MSG);
  }
  document.write("</span>");
}

function printPageComponent() {
  html  = '<span class="" \n';
  html += '      id="print-page-button" \n';
  html += '      onclick="window.print();" \n';
  html += '      onmouseover="this.className = \'over\';" \n';
  html += '      onmouseout="this.className = \'\';" \n';
  html += '      title="Print the current page." \n'; 
  html += '      type="button">Print</span>\n';
  return html;
}

function fixExplorerCornersBug() {
  var divs = document.getElementById("page").getElementsByTagName("div");
  var roundedElements = new Array();
  var innerHeight = 0;
  var innerWidth = 0;
  var newHeight = 0;
  var newWidth = 0;

  for(var i in divs) {
    if(typeof divs[i] == "object" && divs[i] != null && divs[i].nodeType == 1) {
      if(divs[i].className.indexOf("roundedElement") >= 0 || divs[i].className.indexOf("nestedRoundedElement") >= 0) {
        roundedElements.push(divs[i]);
      }
    }
  }

  for(var i in roundedElements) {
    adjustCorners(roundedElements[i]);
  }
}

function adjustCorners(el) {
  var elHeight = el.offsetHeight;
  var elWidth = el.offsetWidth;
  var children = getSubElementsByClass(el, "bgElement");
  var child = null;
  var childBottom = null;
  var childRight = null;
  var newChildBottom = null;
  var newChildRight = null;

  for(var i in children) {
    child = children[i];
    if(getStyleValue(child, "display") != "none") {

      childBottom = child.offsetTop + child.offsetHeight;
      if(childBottom < elHeight) {
        newChildBottom = getStyleValue(child, "bottom").replace("px","") - (elHeight - childBottom);
      }
      childRight = elWidth - (child.offsetWidth + child.offsetLeft);
      if(childRight > 0) {
        newChildRight = getStyleValue(child, "right").replace("px","") - 1;
      }
      if(child.className.indexOf("bgOne") >= 0) {
      }
      if(child.className.indexOf("bgTwo") >= 0 && newChildRight) {
        child.style.right = newChildRight + "px";
      }
      if(child.className.indexOf("bgThree") >= 0 && newChildBottom) {
        child.style.bottom = newChildBottom + "px";
      }
      if(child.className.indexOf("bgFour") >= 0) {
        if(childRight > 0 && newChildRight) {
          child.style.right = newChildRight + "px";
        }
        if(childBottom < elHeight && newChildBottom) {
          child.style.bottom = newChildBottom + "px";
        }
      } 
    }
  }
}

function hoverHelp(el,action) {
  switch(action) {
    case 0: el.className = "hoverHelp hhClose";
            break;
    case 1: el.className = "hoverHelp hhOpen";
            break;
    default : ;
  }
}

function clearSearchBox(text) {
  text.value = '';
}

function toggleDeliveryFields() {
  prefix = 'input-delivery_';
  ['name', 'organisation', 'address1', 'address2', 'address3', 'city', 'region', 'postcode'].each(function(s) {
    var id = prefix+s;
    if($(id).disabled == false){
      $(id).disabled = true;
    }else {
      $(id).disabled = false;
    }
  });

  country = $('delivery_country');
  if(country.disabled == false){
    country.disabled = true;
  }else{
    country.disabled = false;
  }
}

function checkDeliveryField() {
  field = $('matches_billing');
  if(field){
    if($('matches_billing').checked){
      var prefix = 'input-delivery_';
      ['name', 'organisation', 'address1', 'address2', 'address3', 'city', 'region', 'postcode'].each(function(s) {
        var id = prefix+s;
        $(id).disabled = true;
      });
      $('delivery_country').disabled = true;
    }
  }
}

function observeDownloadLink(product_code){
  $$('.addLink').each(function(evt){
    Event.observe(evt, 'click', function(e){
      new Ajax.Request('/c_shop_user_allowed_file_prc', {
        method: 'get',
        parameters: {pcode: product_code},
        onSuccess: function(response){
          var text = response.responseText;
          if(text.match('Admin')){
            window.location = '/c_shop_download_prc?pcode='+product_code;
          }else{
            userAllowedFile(text, product_code);
          }
        }
      });
      Event.stop(e);
    });
  });
}

function userAllowedFile(response, product_code){
  if(response.match('False')){
    var answer = confirm('Please click OK to register and download this file');
    if(answer){
      window.location = '/c_shop_short_register_frm?pcode='+product_code;
    }
  }else{
    var answer = confirm('Click OK to add this document to your My Items list and go to My DSC');
    if(answer){
      window.location = '/c_shop_add_free_download_prc?mydsc=1&pcode='+product_code;
    }
  }
}
