// AVG optimization for element5

//
// global variables
//

// ID list:
//  0 - table
//  1 - tr
var tag_list = new Array(2);
tag_list[0] = "table";
tag_list[1] = "tr";

//
// Functions
//

function get_current_page() {
  // 0 - unknown
  // 1 - cart page (not used)
  // 2 - customer information
  // 3 - payment information (not used because of single-page checkout)
  // 4 - review and submit
  // 5 - finished
  // 6 - edit data
  // 7 - product page (not used)
  var return_value = 0;
  var page_cart_id = "e5_cart_page";
  var page_cust_id = "e5_personal_page";
  var page_rev_id = "e5_review_page";
  var page_fin_id = "e5_final_page";
  var page_edit_id = "e5_edit_page";
  var page_prod_id = "e5_product_page";
  var page_cart;
  var page_cust;
  var page_pay;
  var page_rev;
  var page_fin;
  var page_edit;
  var page_prod;
  // try to get objects
  page_cust = document.getElementById(page_cust_id);
  page_rev = document.getElementById(page_rev_id);
  page_fin = document.getElementById(page_fin_id);
  page_edit = document.getElementById(page_edit_id);
  if (page_cust != null) { return_value = 2 };
  if (page_rev != null) { return_value = 4 };
  if (page_fin != null) { return_value = 5 };
  if (page_edit != null) { return_value = 6 };
  // try to identify the customer information page using different method
  if (return_value == 0) {
    var current_page;
    current_page = document.getElementsByName('progress');
    if (current_page) {
      if (current_page.length == 1) {
		    if (current_page[0].value == 'ADDITIONAL') {
          return_value = 2;
        }
      } else {
  		  if (return_value == 0) {
          var sPath = window.location.pathname;
          var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
          if (sPage == 'checkout.html') { 
            var processing_page;
            processing_page = document.getElementById('showLayer');
            if (!processing_page) { return_value = 2; }
          }
  		  }
	    }
    }
  }
  return return_value;
}

// update layout of the language and currency selection box
function optimize_lng_cur_box() {
  var selector;
  selector = document.getElementById('e5_lbl_languageid');
  // the page could be called without the box, check for its presence
  if (selector != null) {
    var box;
    box = selector.offsetParent.parentNode.parentNode.parentNode.parentNode;
    if (box != null && check_tag(box,0)) {
      box.className = "e5_language_currency";
    }
  }
}

// enable/disable the language and currency selection box
function switch_lng_cur_box(mode) {
  var box;
  box = document.getElementById('e5_lbl_languageid').offsetParent.parentNode.parentNode.parentNode.parentNode;
  // detect mode
  switch(mode) {
    // visible
    case 1:
      if (box !=null && check_tag(box,0)) {
        box.style.display = "block";
      }
      break;    
    // hidden
    default:
      if (box !=null && check_tag(box,0)) {
        box.style.display = "none";
      }
      break;
  }
}

// update layout of the returning customer box
function optimize_ret_cus_box() {
  var spacer;
  var box;
  spacer = document.getElementById('e5_hdl_cho_returning').offsetParent.rows[1];
  box = document.getElementById('e5_hdl_cho_box_login').offsetParent.parentNode.offsetParent;
  if (spacer !=null && check_tag(spacer,1) && box != null && check_tag(box,0)) {
    spacer.style.display = "none";
    box.className = "e5_returning_customer";
  }
}

// enable/disable the returning customer box
function switch_ret_cus_box(mode) {
  var header;
  var box;
  header = document.getElementById('e5_hdl_cho_returning').parentNode.parentNode.parentNode;
  box = document.getElementById('e5_hdl_cho_box_login').parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
  // detect mode
  switch(mode) {
    // visible
    case 1:
      if (header !=null && check_tag(box,0)) {
        header.style.display = "block";
        box.style.display = "block";
      }
      break;    
    // hidden
    default:
      if (header !=null && check_tag(box,0)) {
        header.style.display = "none";
        box.style.display = "none";
      }
      break;
  }
}

// update layout of the new customer box
function optimize_new_cus_box(strings) {
  var header = null;
  var login_lnk = null;
  var spacer_1 = null;
  var spacer_2 = null;
  var salutation = null;
  var address_1 = null;
  var address_2 = null;
  var email_notice = null;
  var email_notice_parent = null;
  var email_notice_new = null;
  var billing_address_lnk = null;
  header = document.getElementById('e5_lbl_cho_newcustomer');
  spacer_1 = get_previous_sibling(document.getElementById('e5_txt_cho_change_add').parentNode.offsetParent, 0, 3);
  spacer_2 = get_next_sibling(document.getElementById('e5_txt_cho_change_add').parentNode.offsetParent, 0, 3);
  salutation = document.getElementById('e5_lbl_cho_salutation').parentNode;
  address_1 = document.getElementById('e5_lbl_cho_address');
  address_2 = document.getElementById('e5_lbl_cho_address-add');
  email_notice = document.getElementById('e5_txt_cho_freemail');
  email_notice_parent = document.getElementById('e5_frm_email_confirm');
  // check for validation errors
  if (email_notice_parent) {
    email_notice_parent = email_notice_parent.parentNode;
  } else {
    email_notice_parent = document.getElementById('e5_err_email_confirm').parentNode;
  }
  billing_address_lnk = document.getElementById('billing').parentNode;
  if (spacer_1 !=null && spacer_2 !=null && salutation != null && address_1 != null && address_2 != null && email_notice != null && email_notice_parent != null && billing_address_lnk != null && header != null) {
    // add link to the login box
    login_lnk = document.createElement('span');
    login_lnk.className = "e5_login_lnk";
    login_lnk.innerHTML = "&nbsp;&nbsp;" + strings[0];
    header.appendChild(login_lnk);
    // remove the spacers around the new customer header
    spacer_1.style.display = "none";
    spacer_2.style.display = "none";
    // hide salutation
    salutation.style.display = "none";
    // rename the address labels
    address_1.innerHTML = strings[2];
    address_2.innerHTML = strings[3];
    // hide the old notice and add the new one
    email_notice.style.display = "none";
    email_notice_new = document.createElement('div');
    email_notice_new.className = "e5_email_notice";
  	email_notice_new.innerHTML = strings[1];
  	email_notice_parent.appendChild(email_notice_new);
    // update billing address link
    billing_address_lnk.className = "e5_billing_address_lnk";
  }
}

// update layout of the payment method box
function optimize_payment_box() {
  var box;
  box = document.getElementById('e5_lbl_cho_pay_option').offsetParent;
  if (box != null) {
    box.className = "e5_payment_method";
  }
}

// update layout of the footer information
function optimize_footer() {
  var button_next;
  var spacer_fax;
  var spacer_cs;
  button_next = document.getElementById('e5_frm_button_next').parentNode;
  spacer_fax = document.getElementById('e5_lnk_cho_fax').parentNode.parentNode.parentNode.parentNode.rows[1];
  spacer_cs = document.getElementById('e5_lnk_cho_fax').parentNode.parentNode.parentNode.parentNode.rows[3];
  if (button_next !=null && spacer_fax !=null && spacer_cs !=null) {
    // align the button
    button_next.className = "e5_button_next";
    // remove the spacers
    spacer_fax.style.display = "none";
    spacer_cs.style.display = "none";
  }
}

// enable/disable the coupon code box
function switch_coupon_box(mode) {
  var header;
  var box;
  header = document.getElementById('e5_hdl_cho_box_coupon').parentNode.parentNode.parentNode;
  box = document.getElementById('e5_lbl_crt_couponcode').parentNode.parentNode.parentNode;
  // detect mode
  switch(mode) {
    // visible
    case 1:
      if (header !=null && box !=null) {
        header.style.display = "block";
        box.style.display = "block";
      }
      break;    
    // hidden
    default:
      if (header !=null && box !=null) {
        header.style.display = "none";
        box.style.display = "none";
      }
      break;
  }
}

// tag checker
function check_tag(object, tag_id) {
  var return_value = false;
  if (object.nodeName.toLowerCase() == window.tag_list[tag_id].toLowerCase()) { return_value = true };
  //alert(object.tagName.toLowerCase());
  return return_value;
}

// get the nearest previous sibling of specific type
function get_previous_sibling(object, tag_id, attempts) {
  for (i = 0; i <= attempts; i++) {
    object = object.previousSibling;
    if (check_tag(object,tag_id)) {
      return object;
      break;
    }
  }  
  return object;
}

// get the nearest next sibling of specific type
function get_next_sibling(object, tag_id, attempts) {
  for (i = 0; i <= attempts; i++) {
    object = object.nextSibling;
    if (check_tag(object,tag_id)) {
      return object;
      break;
    }
  }  
  return object;
}