//Show and Hide

function showDiv(sDivName) {
  var div = document.getElementById(sDivName);
  div.style.display = 'block';
//  $('divMap').show();
}
function hideDiv(sDivName) {
  var div = document.getElementById(sDivName);
  div.style.display = 'none';
}

function writeEmail(contact, email, emailHost) {
  document.write("<a href=" + "&#109a&#105l" + "&#116&#111:" + email + "@" + emailHost+ ">" + contact + "@" + emailHost+"</a>");
}

// We are using this on the Company.php page for the staff sections. It allows us to
// show or hide a div when the li is clicked. It also toggles the +/- images that are
// defined in the CSS as .collapse and .expand
function expandCollapse(liID, divID, color) {
  //If the DIV is visible (currently it's expanded) then we need to hide (collapse) it and then show the Expand (+) image
  if ($(divID).visible()) {
    $(liID).removeClassName(color+'_expanded').addClassName(color+'_collapsed');
  } else {
    $(liID).removeClassName(color+'_collapsed').addClassName(color+'_expanded');
  }
  Effect.toggle(divID, 'blind', { duration: .3 });
}

//For this to work you have to use an <h3> for the "list item" that's clickable. You also need to apply
//a fake style like "orange_div_expanded" in the HTML to the first DIV that starts off expanded.
var li_stat_color = new Object;
li_stat_color["orange"] = "01";
li_stat_color["blue"] = "01";
li_stat_color["red"] = "01";
li_stat_color["green"] = "01";
function expandOneCollapseRest(index, color) {
  //If the DIV is visible (currently it's expanded) then we need to hide (collapse) it and then show the Expand (+) image
  if ($('div_'+color+'_'+index).visible()) {
    //Do nothing - this DIV is already the visible one
  } else {
    //Get the DIV that's currently expanded (it's not this one) and collapse it
//alert("color...");
//alert("color [" + color + "]");
//alert("index...");
//alert("index [" + index + "]");
//alert("item [" + 'h3_'+color+'_'+index + "]");
    var el_id = 'h3_'+color+'_'+li_stat_color[color];
    var el = $(el_id);
    el = document.getElementById(el_id);
//alert("element:");
//alert(el);
    el.removeClassName(color+'_expanded').addClassName(color+'_collapsed');
    Effect.toggle($('div_'+color+'_'+li_stat_color[color]), 'blind', { duration: .3, queue:'end' });

    //Expand this DIV now
    $('h3_'+color+'_'+index).removeClassName(color+'_collapsed').addClassName(color+'_expanded');
    Effect.toggle('div_'+color+'_'+index, 'blind', { duration: .3, queue:'end' });

    li_stat_color[color] = index;
  }
}

//This is used on the home page to show/hide divs that are associated with list items. The divs
//contain the content when the page is loaded so we simply need to show the div for the tab that
//was clicked and hide the div that was visible and toggle the "active_tab" class on the selected
//and deselected tabs.
var visibleTabContent = 'divGeneral';
function switchTabs(ulId, clickedListItem, clickedListItemsDivId) {
  if (!$(clickedListItemsDivId).visible()) {
    $(visibleTabContent).hide();                                             //hide the DIV contents for the old tab
    $(ulId).select('li.active_tab').invoke('removeClassName', 'active_tab'); //remove the "active_tab" class from the old tab
    $(clickedListItem.id).addClassName('active_tab');                        //add the "active_tab" class to the new tab
    $(clickedListItemsDivId).show();                                         //show the DIV contents for the new tab
    visibleTabContent = clickedListItemsDivId;                               //save the name of the active tab for future reference
  }
}

//Portfolio POP UP
function pop(url, name, props) {
	window.open(url, name, props);
}


//Extend the String object so that we can use the trim() function like; sVar.trim();
String.prototype.trim = function() {
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};


//
// This is used by all of the ajaxPostABC functions below.
// It re-enables the submit button and removes the spinner class and puts back the normal btn_submit class.
//
// NOTE: This function requires the Prototype.js library
//
function enableSubmitButton(button_id, bEnabled) {
  if (bEnabled == true) {
    $(button_id).removeClassName('btn_submit_spinner').addClassName('btn_submit').enable();
  } else {
    $(button_id).removeClassName('btn_submit').addClassName('btn_submit_spinner').disable();
  }
  return true;
}

//
// This function is used to create a timer for a form that was submitted.
// If the timer fires then we know we've exceeded the maximum allowed time for the
// server to process the request. IE, we never got a response back from the server
// so we need to re-enable the submit button and tell the user so they can submit
// the request again if they want to.
//
function startTimeout(sSubmitButtonID) {
  var timer = new PeriodicalExecuter(function(peObject) {
    enableSubmitButton(sSubmitButtonID, true);
    peObject.stop();
  }, 30);
  return timer;
}

//
// This is the javascript validation for the Contact form (contact.php). If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function ajaxPostContactForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('cont_first_name') == '') { alert("Please input your first name."); $('cont_first_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_last_name') == '') { alert("Please input your last name."); $('cont_last_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('cont_email');
  if (sEmail == '') { alert("Please input your email address."); $('cont_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('cont_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('contact_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('contact_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_zip') == '') { alert("Please input your zip code."); $('cont_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_comments') == '') { alert("Please input your comments."); $('cont_comments').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  new Ajax.Request('ajax_handler.php', {
    parameters: $(frmId).serialize(true),
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (transport.responseText.trim() == '1') {
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peContact) {
          $(frmId).reset();
          Effect.Fade(sConfirmationDivID, {duration:.5});
          peContact.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Media Kit form (media_kit.php). If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function ajaxPostMediaKitForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('mk_organization') == '') { alert("Please input your organization."); $('mk_organization').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('mk_first_name') == '') { alert("Please input your first name."); $('mk_first_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('mk_last_name') == '') { alert("Please input your last name."); $('mk_last_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('mk_phone') == '') { alert("Please input your phone number."); $('mk_phone').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('mk_email');
  if (sEmail == '') { alert("Please input your email address."); $('mk_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('mk_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('mk_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('mk_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  new Ajax.Request('ajax_handler.php', {
    parameters: $(frmId).serialize(true),
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (transport.responseText.trim() == '1') {
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peMediaKit) {
          $(frmId).reset();
          Effect.Fade(sConfirmationDivID, {duration:.5});
          peMediaKit.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the eNewsletter Subscription form (includes/panels/switch_box.php).
// If all of the validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display a javascript
// alert popup with the error message returned from the PHP script.
//
function ajaxPostEnewsletterForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  var sEmail = $F('sub-email');
  if (sEmail == '') { alert("Please input your email address."); $('sub-email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('sub-email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  //if ($F('mk_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('mk_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  url_unsubscribe = "http://www.collidemagazine.com/index.cfm?event=newsletter.unsubscribe";
  url_subscribe = "http://www.collidemagazine.com/index.cfm?event=newsletter.subscribe";
  new Ajax.Request(url_subscribe, {
    parameters: $(frmId).serialize(true),

    onComplete: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (json.success) {
        $(sConfirmationDivID).removeClassName('side_panel_alert').addClassName('side_panel_confirmation');
        $('pConfirmationText').update('Thank you for subscribing.');
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peEnewsletter) {
          $(frmId).reset();
          Effect.Fade(sConfirmationDivID, {duration:.5});
          peEnewsletter.stop();
        }, 10);
      }
      else {
        $(sConfirmationDivID).removeClassName('side_panel_confirmation').addClassName('side_panel_alert').show();
        if (json.reason == 'alreadySubscribed') {
          $('pConfirmationText').update('You are already subscribed.');
        }
        else if (json.reason == 'invalidEmailAddress') {
          $('pConfirmationText').update('Please use a valid email address and try again.');
        }
        else {
          $('pConfirmationText').update(json.reason);
        }
      }
      enableSubmitButton(sSubmitButtonID, true);
    },

    onException : function (transport, exception) {
      $(sConfirmationDivID).removeClassName('side_panel_confirmation').addClassName('side_panel_alert').show();
      $('pConfirmationText').update("ERROR: " + exception.message);
      enableSubmitButton(sSubmitButtonID, true);
    },

    onFailure: function(transport) {
      $(sConfirmationDivID).removeClassName('side_panel_confirmation').addClassName('side_panel_alert').show();
      $('pConfirmationText').update("An error occurred, please contact customer service.");
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Send to Friend form. If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function ajaxPostStfForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('stf_name') == '') { alert("Please input your name."); $('stf_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('stf_email');
  if (sEmail == '') { alert("Please input your email address."); $('stf_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('stf_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('stf_friend_name_1') == '') { alert("Please input at your friend's name."); $('stf_friend_name_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sFriendEmail1 = $F('stf_friend_email_1');
  if (sFriendEmail1 == '') { alert("Please input your friend's email address."); $('stf_friend_email_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sFriendEmail1.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('stf_friend_email_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('stf_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('stf_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  //Submit the form via Prototype's form "request" ajax method
  $(frmId).request ({
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (transport.responseText.trim() == '1') {
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peEnews) {
          $(frmId).reset();
          Effect.Fade(sConfirmationDivID, {duration:.5});
          peEnews.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

