window.onload = initForm;

function initForm() { 
  home_page_box_links();
  external_links();
  if(document.getElementById('ship_to_invoice[ship_to_invoice]')) {   
    document.getElementById('ship_to_invoice[ship_to_invoice]').onclick = enableDeliveryDetials;
    enableDeliveryDetials();
  }
  if(document.getElementById('how_find')) {
    var how_find_index = document.getElementById('how_find').selectedIndex;
    var how_find = document.getElementById('how_find')[how_find_index].value;
    if (document.getElementById('other_how_did_find').value.length > 0 && how_find == 41) {
      document.getElementById('other_how_did_find_cont').style.display = 'block';
    } else if(document.getElementById('other_how_did_find').value.length > 0 && how_find != 41) {
      document.getElementById('other_how_did_find_cont').style.display = 'none';
    } else {
      document.getElementById('other_how_did_find_cont').style.display = 'none';
    }
    document.getElementById('how_find').onchange = showAdditonalField;
  }
  if(document.getElementById('initials')) {
    if(document.getElementById('product_input_0')) {
      var ScriptBlockArray = new Array(131, 141, 151, 161, 171, 181);
      var chosenVariantIndex = document.getElementById('option_61').selectedIndex;
      var chosenVariant = document.getElementById('option_61')[chosenVariantIndex].value;
      if(document.getElementById('initials') && inArray(chosenVariant, ScriptBlockArray)) {
        document.getElementById('initials').style.display = 'block';
      } else {
        document.getElementById('product_input_0').value = '';
      }
    }
  }
  if(document.getElementById('prod_id')){
    if(document.getElementById('prod_id').value == 351){
       document.getElementById('product_input_2').style.display = 'none';
       document.getElementById('product_input_3_label').style.display = 'none';
       document.getElementById('option_31').onchange = showAdditonalInputField;     
    }
  }
  if(document.getElementById('easytags_flash')) {
    insertFlashObject('easytags_flash', 'http://www.youtube.com/v/8I7aT7pNs_4&hl=en_US&fs=1&iv_load_policy=3&rel=0&border=1', 317, 200) 
  }
  user_menu(); 
}

//Show additional field for futher info
function showAdditonalField() {
  var how_find_index = document.getElementById('how_find').selectedIndex;
  var how_find = document.getElementById('how_find')[how_find_index].value;
  if(how_find == 41) {
     document.getElementById('other_how_did_find_cont').style.display = 'block';
  } else {
     document.getElementById('other_how_did_find_cont').style.display = 'none';
  }
}


//Show additional field for futher info
function showAdditonalInputField() {
  var selected = document.getElementById('option_31').selectedIndex;
  var letterStyle = document.getElementById('option_31')[selected].value;
  if(letterStyle == 21) {
     document.getElementById('product_input_2').style.display = 'block';
     document.getElementById('product_input_3_label').style.display = 'block';
  } else {
     document.getElementById('product_input_2').style.display = 'none';
     document.getElementById('product_input_3_label').style.display = 'none';
  }
}

// toggle input field
function toggleInputField() {
  if(this.value == 1491) {
    document.getElementById('specialsewon').style.display = 'inline';
  } else {
    document.getElementById('product_input_2').value = '';
    document.getElementById('specialsewon').style.display = 'none';
  }
}

// Assign event handlers to user menu
function user_menu() {
  if(!document.getElementsByTagName) {
    return;
  } else {
    var div = document.getElementById('user_menu_content');
    var images = div.getElementsByTagName("img"); 
    for(var i=0; i<images.length; i++) { 
      var image = images[i];
      if(image.getAttribute("id").substr(0, 8) == 'usermenu') {
        imageName = image.getAttribute("id");
        document.getElementById(imageName).onmouseover = user_menu_hover;
        document.getElementById(imageName).onmouseout = user_menu_hover;
      } 
    } 
  }
}

// User menu image rollovers
function user_menu_hover(e) {
  if(window.event) {
    var e = window.event;
  }
  var source = e.srcElement ? e.srcElement : e.target;
  var objectID = source.getAttribute("id");
  var splitString = document.getElementById(objectID).src.split('/');
  var imagePath = '';
  for(i=0;i<=splitString.length - 2;i++) {
    imagePath = imagePath + splitString[i] + '/'; 
  }
  if(splitString[splitString.length - 1].substr(9, 4) == 'over') {
    document.getElementById(objectID).src = imagePath + objectID + '.jpg'; 
  } else {
    document.getElementById(objectID).src = imagePath + objectID + 'over.jpg';
  }
}

// Switch product image
function switch_product_image(image, title) {  
  var pImage;
  pImage = document.getElementById('product_image_src');
  pImage.src = 'images/products/main/' + image;
  pImage.title = title;
}

// Reset product image
function reset_product_image(image, title) {
  var pImage;
  pImage = document.getElementById('product_image_src');
  pImage.src = 'images/products/main/' + image;
  pImage.title = title;
}

// Open links in new window
function external_links() { 
  if(!document.getElementsByTagName) {
    return;
  } else { 
    var anchors = document.getElementsByTagName("a"); 
    for (var i=0; i<anchors.length; i++) { 
      var anchor = anchors[i]; 
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow") { 
        anchor.target = "_blank";
      }
    }
  } 
}

// Home page box links
function home_page_box_links() {
  if(document.getElementById('home_link_fundraising')) {
    document.getElementById('home_link_fundraising').onclick = fundraising_link;
    document.getElementById('home_link_labels').onclick = labels_link;
    document.getElementById('home_link_comments').onclick = comments_link;
  }
  if(document.getElementById('home_link_starter')) {
    document.getElementById('home_link_starter').onclick = starter_link;
    document.getElementById('home_link_school').onclick = school_link;
  } 
}

function fundraising_link() {
  window.location = "laundry-labels.htm"; 
}

function labels_link() {
  window.location = "what-to-label.htm"; 
}

function comments_link() {
  window.location = "comments.htm"; 
}

function starter_link() {
  window.location = "product.htm?product=starter-pack"; 
}

function school_link() {
  window.location = "product.htm?product=off-to-school-set"; 
}

function enableDeliveryDetials() {
  if(!document.getElementById('ship_to_invoice[ship_to_invoice]').checked || document.getElementById('ship_to_invoice[ship_to_invoice]').value == 0) {
    document.getElementById('delivery_details').style.display = 'block';
  } else {
    document.getElementById('delivery_details').style.display = 'none';
  }
}

function getFullProductPrice(phpFile, objID) {
  var obj = document.getElementById(objID);
  var title = new Array();
  var value = new Array();
  xmlhttp.open("GET", phpFile);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      obj.innerHTML = xmlhttp.responseText;
    }
  }  
  xmlhttp.send(null);  
}

function updatePrice(prodID, menuID, variantType) {
  if(document.getElementById('variantsIDs')) {
    var variantsIDs = document.getElementById('variantsIDs').value;
    // Get the IDs of the variants
    variantID = variantsIDs.split('|');
    var variantsValues = '';
    // Loop through the variants based on their IDs and get the selected values
    for(i=0; i<variantID.length; i++) {
      if(document.getElementById('option_' + variantID[i])) {
        variantValue = document.getElementById('option_' + variantID[i]).value;   
        variantsValues = variantsValues + variantValue + "|";   
      }
    }   
  } 
  if(document.getElementById('globalVariantsIDs')) {       
    var globalVariantsIDs = document.getElementById('globalVariantsIDs').value;
    // Get the IDs of the global variants     
    globalVariantID = globalVariantsIDs.split('|');
    var globalVariantsValues = '';
    // Loop through the global variants based on their IDs and get the selected values
    for(i=0; i<globalVariantID.length; i++) {
      if(document.getElementById('option_' + globalVariantID[i])) {
        globalVariantValue = document.getElementById('option_' + globalVariantID[i]).value;   
        globalVariantsValues = globalVariantsValues + globalVariantValue + "|";   
      }
    }           
  }       
  // Hide the initial field if not block or script design for the FAB & FUNKY Lables
  var FabFunkyArray = new Array();
  FabFunkyArray[0] = 11;
  FabFunkyArray[1] = 21;
  FabFunkyArray[2] = 71;
  FabFunkyArray[3] = 261;
  FabFunkyArray[4] = 371;
  FabFunkyArray[5] = 362;
  if(inArray(prodID, FabFunkyArray)) {
    if(document.getElementById('option_61')) {
      var ScriptBlockArray = new Array(131, 141, 151, 161, 171, 181);
      var chosenVariantIndex = document.getElementById('option_61').selectedIndex;
      var chosenVariant = document.getElementById('option_61')[chosenVariantIndex].value;
      // alert(chosenVariant + ' - ' + inArray(chosenVariant, ScriptBlockArray));
      if(document.getElementById('initials') && inArray(chosenVariant, ScriptBlockArray)) {
        document.getElementById('initials').style.display = 'block';
      } else {
        document.getElementById('initials').style.display = 'none';
      }      
    }
  }
  
  
  getFullProductPrice("logicajax/updateProductPrice.php?p=" + prodID + "&variantsIDs='" + variantsIDs + "'&globVariantsIDs='" + globalVariantsIDs
                       + "'&variantsValues='" + variantsValues + "'&globVariantsValues='" + globalVariantsValues + "'", "product_price");
}

function inArray(needle, haystack) { 
  for(var i = 0; i < haystack.length; i++) {
    if(haystack[i] == needle) {
      return true;   
    }
  }
  return false;
}

function insertFlashObject(objID, flashLocation, width, height, flashString) {
  var flashObject;
  flashObject = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='" + width + "' height='" + height + "'>";
  flashObject = flashObject + "<param name='wmode' value='transparent'>";
  flashObject = flashObject + "<param name='movie' value='" + flashLocation + "' />";
  flashObject = flashObject + "<param name='quality' value='high' />";
  if(typeof(flashString) == "undefined") {
    flashObject = flashObject + "<embed src='" + flashLocation + "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "' wmode='transparent'";
  } else {
    flashObject = flashObject + "<param name='FlashVars' value='" + flashString + "'";
    flashObject = flashObject + "<embed src='" + flashLocation + "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "' wmode='transparent' ";
    flashObject = flashObject + "FlashVars='" + flashString + "'";
  }
  flashObject = flashObject + "></embed></object>";
  if(document.getElementById(objID)) {
    document.getElementById(objID).innerHTML = flashObject;
  }
}