// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function toggleExperience(elem, _id)
{
	if (elem.innerHTML == '+ more') 
	{
		// show
		elem.innerHTML = '- hide';		
		$(_id+'_desc_short').hide();
		$(_id+'_desc_long').show();
		$(_id+'_dd').addClassName('show').removeClassName('hide');		
	}
	else
	{
		// hide
		elem.innerHTML = '+ more';
		$(_id+'_desc_short').show();
		$(_id+'_desc_long').hide();
		$(_id+'_dd').addClassName('hide').removeClassName('show');
	}
}
/**
 * Give focus to a form element for the form id passed into this function.
 * No return value.
 */
function give_form_focus(form_element_id)
{
  try {
      var form_element = $(form_element_id);
      if (form_element) {
        form_element.focusFirstElement();
      } else if (form_element = document.forms[0]) { // if form not found grant the first form focus
        form_element.focusFirstElement();
      }
  }
  catch (e) {} // fail silently
}

/**
 * Countdown how many characters are left in a text box.
 */

function checkMaxInput(form) {
	maxLen = 60; // max number of characters allowed in the textbox
	if (form.profile_headline.value.length > maxLen) // if too long.... trim it!
		form.profile_headline.value = form.profile_headline.value.substring(0, maxLen);
	// otherwise, update 'characters left' counter
	else length = document.getElementById("remLen");
	length.innerHTML = maxLen - form.profile_headline.value.length;
}

function checkMaxInputOverview(form) {
	maxLen = 2000; // max number of characters allowed in the textbox
	if (form.job_posting_overview.value.length > maxLen) // if too long.... trim it!
		form.job_posting_overview.value = form.job_posting_overview.value.substring(0, maxLen);
	// otherwise, update 'characters left' counter
	else length = document.getElementById("remLenO");
	length.innerHTML = maxLen - form.job_posting_overview.value.length;
}

function checkMaxInputResposibilities(form) {
	maxLen = 2000; // max number of characters allowed in the textbox
	if (form.job_posting_responsibilities.value.length > maxLen) // if too long.... trim it!
		form.job_posting_responsibilities.value = form.job_posting_responsibilities.value.substring(0, maxLen);
	// otherwise, update 'characters left' counter
	else length = document.getElementById("remLenR");
	length.innerHTML = maxLen - form.job_posting_responsibilities.value.length;
}

function checkMaxInputRequirements(form) {
	maxLen = 2000; // max number of characters allowed in the textbox
	if (form.job_posting_requirements.value.length > maxLen) // if too long.... trim it!
		form.job_posting_requirements.value = form.job_posting_requirements.value.substring(0, maxLen);
	// otherwise, update 'characters left' counter
	else length = document.getElementById("remLenReq");
	length.innerHTML = maxLen - form.job_posting_requirements.value.length;
}

function checkMaxInputPluses(form) {
	maxLen = 2000; // max number of characters allowed in the textbox
	if (form.job_posting_pluses.value.length > maxLen) // if too long.... trim it!
		form.job_posting_pluses.value = form.job_posting_pluses.value.substring(0, maxLen);
	// otherwise, update 'characters left' counter
	else length = document.getElementById("remLenP");
	length.innerHTML = maxLen - form.job_posting_pluses.value.length;
}

function hideAndShow(idToHide, prefix, value) {
	var idToShow = prefix + "_" + value;
	$(idToHide).childElements().each(
		function(e){e.hide();}
	);
	$(idToShow).show();
}

function hideAllTabs() {
  $$('div.tab').each(
    function(e) { e.hide(); }
  );
}

// function hideAllSubTabsFor( id ) {
//   $$('#'+id+' div.sub-tab').each(
//     function(e) { e.hide(); }
//   );
// }

function showOnlyTabFor( id ) {
  hideAllTabs();
  var form_el = $(id);
  form_el.show();
  var form_name = form_el.id + "_form";
  give_form_focus(form_name);
}

function assertImageFile( str ) {
  ext = str.substring( str.length-3, str.length );
	ext = str.split('.');
	ext = ext[ ext.length-1 ]; // get last element
  ext = ext.toLowerCase();
  if (!ext.match(/jpe*g|png|gif/i)) {
    alert('You selected a '+ext+' file. Please select a JPEG, PNG, or GIF file!');
    return false;
  } else {
    $('uploading').show();
    return true;  
  }
}

function show(id) {
	$(id).className='show';
	$(id+'_hide').style.display='block';
	$(id+'_show').style.display = 'none';
}

function hide(id) {
	$(id).className='hide';
	$(id+'_hide').style.display='none';
	$(id+'_show').style.display = 'block';
}  	


function ratingCancel( question_id, rating ) {
  var image_objects = $$('div#rating-'+question_id + ' a')  
  var hitSelected = false
  if( rating == '' )
    hitSelected = true
  image_objects.each( 
    function(e) {
      if(e.id == 'star-'+question_id+'-'+rating)
        hitSelected = true;
      if((e.id != 'star-'+question_id+'-'+rating) && (hitSelected == true)) {
        e.removeClassName('starOn');
      } else {
        e.addClassName('starOn');
      }
    }
  )
  var title = $('rating-title-'+question_id);
  renderRatingText(title,rating);
  // var yourRating = $('rating-value-'+question_id);  
  // renderYourRatingText(yourRating,rating);
}

function ratingHover( obj_id, star_obj ) {
  var image_objects = $$('div#rating-'+obj_id + ' a');
  var hitSelected = false;
  var our_rating = 0;
  image_objects.each( 
    function(e) {
      if(e == star_obj){
        hitSelected = true;
        our_rating = e.id.split('-',3)[2]
      }
      if(e != star_obj && hitSelected == true) {
        e.removeClassName('starOn');
      } else {
        e.addClassName('starOn');
      }
    }
  )
  var title = $('rating-title-'+obj_id);
  renderRatingText(title,parseInt(our_rating));
  // var yourRating = $('rating-value-'+obj_id);  
  // renderYourRatingText(yourRating,our_rating);
}


function leftMessageSelect( id ) {
  // unselect all messages in block
  $('left-message-block').immediateDescendants().each(
    function(e) { e.removeClassName('select') }
  );
  $("message-"+id+"-left").addClassName('select') //.removeClassName('ie-hover');
}

function removeMessages(id) {
  var id_str = 'message-'+id
  var rightPanelMessage = $(id_str);
  var leftPanelMessage  = $(id_str+'-left');
  
  if( leftPanelMessage )
    new Effect.Fade( leftPanelMessage, { afterFinish: function(obj) { obj.element.remove(); } } );

  if( rightPanelMessage )
    new Effect.Fade( rightPanelMessage, { });
}

function toggleMessageForm() {
  [ $('message-list'), $('new-message') ].each(
    function( e ) { e.toggle() }
  );
}

function openMessage(id) {
  // make message selected
  // var msg = $( 'message-'+id );
  // [ msg, msg.getElementsByClassName( 'time-block' ) ].flatten().each(
  //   function( e ) { e.addClassName('select') }    
  // );

  // unbold message on left pane if it exists ( if it's not sent by yourself )  
  var leftPanelMessage;
  if( leftPanelMessage = $( 'message-'+id+'-left') ) {
    leftPanelMessage.getElementsBySelector( 'p.title', 'div.time-block').each(
      function( e ) { e.removeClassName('bold') }
    );
  }
}

function validatePassword(confirm) {
  if($('new_password').value == confirm.value)
  {
    $('password-confirm-success').show();
    $('password-confirm-stop').hide();
  }
  else
  {
    $('password-confirm-success').hide();
    $('password-confirm-stop').show();
  }
}

function toggleCompanyAddress() {
  //$('companyAddy').

  if ($('billing_address').style['display'] == 'none') {
    $('billing_address').style['display'] = 'block' 
  }  else {
    $('billing_address').style['display'] = 'none'
  }
}

function expandAllDescriptions() {
  var short = $$('dl[id|=experience-description-short]');
  short.each( function(e) { e.style.display = 'none'; })
  var long = $$('dl[id|=experience-description-long]');
  long.each( function(e) { e.style.display = 'block'; })
}


//
// this is an addition to only allow external control to edit, if provided
//

Ajax.InPlaceEditor.prototype = Object.extend(Ajax.InPlaceEditor.prototype, {
  initialize: function(element, url, options) {
    this.url = url;
    this.element = $(element);

    this.options = Object.extend({
      paramName: "value",
      okButton: true,
      okLink: false,
      okText: "ok",
      cancelButton: false,
      cancelLink: true,
      cancelText: "cancel",
      textBeforeControls: '',
      textBetweenControls: '',
      textAfterControls: '',
      savingText: "Saving...",
      clickToEditText: "Click to edit",
      okText: "ok",
      rows: 1,
      onComplete: function(transport, element) {
        new Effect.Highlight(element, {startcolor: this.options.highlightcolor});
      },
      onFailure: function(transport) {
        alert("Error communicating with the server: " + transport.responseText.stripTags());
      },
      callback: function(form) {
        return Form.serialize(form);
      },
      handleLineBreaks: true,
      loadingText: 'Loading...',
      savingClassName: 'inplaceeditor-saving',
      loadingClassName: 'inplaceeditor-loading',
      formClassName: 'inplaceeditor-form',
      highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor,
      highlightendcolor: "#FFFFFF",
      externalControl: null,
      submitOnBlur: false,
      ajaxOptions: {},
      evalScripts: false
    }, options || {});

    if(!this.options.formId && this.element.id) {
      this.options.formId = this.element.id + "-inplaceeditor";
      if ($(this.options.formId)) {
        // there's already a form with that name, don't specify an id
        this.options.formId = null;
      }
    }
  
    if (this.options.externalControl) {
      this.options.externalControl = $(this.options.externalControl);
    }
  
    this.originalBackground = Element.getStyle(this.element, 'background-color');
    if (!this.originalBackground) {
      this.originalBackground = "transparent";
    }
  
    this.element.title = this.options.clickToEditText;
  
    this.onclickListener = this.enterEditMode.bindAsEventListener(this);
    this.mouseoverListener = this.enterHover.bindAsEventListener(this);
    this.mouseoutListener = this.leaveHover.bindAsEventListener(this);
    if (this.options.externalControl) {
      Event.observe(this.options.externalControl, 'click', this.onclickListener);
      Event.observe(this.options.externalControl, 'mouseover', this.mouseoverListener);
      Event.observe(this.options.externalControl, 'mouseout', this.mouseoutListener);
    } else { // this is an addition to only allow external control to edit, if provided
      Event.observe(this.element, 'click', this.onclickListener);
      Event.observe(this.element, 'mouseover', this.mouseoverListener);
      Event.observe(this.element, 'mouseout', this.mouseoutListener);
    }
  }
});

/*
 * InPlaceEditor extension that adds a 'click to edit' text when the field is 
 * empty.
 */
Ajax.InPlaceEditor.prototype.__initialize = Ajax.InPlaceEditor.prototype.initialize;
Ajax.InPlaceEditor.prototype.__getText = Ajax.InPlaceEditor.prototype.getText;
Ajax.InPlaceEditor.prototype.__onComplete = Ajax.InPlaceEditor.prototype.onComplete;
Ajax.InPlaceEditor.prototype = Object.extend(Ajax.InPlaceEditor.prototype, {

    initialize: function(element, url, options){
        this.__initialize(element,url,options)
        this.setOptions(options);
        this._checkEmpty();
    },

    setOptions: function(options){
        this.options = Object.extend(Object.extend(this.options,{
            emptyText: 'click to edit...',
            emptyClassName: 'inplaceeditor-empty'
        }),options||{});
    },

    _checkEmpty: function(){
        if( this.element.innerHTML.length == 0 ){
            this.element.appendChild(
                Builder.node('span',{className:this.options.emptyClassName},this.options.emptyText));
        }
    },

    getText: function(){
        document.getElementsByClassName(this.options.emptyClassName,this.element).each(function(child){
            this.element.removeChild(child);
        }.bind(this));
        return this.__getText();
    },

    onComplete: function(transport){
        this._checkEmpty();
        this.__onComplete(transport);
    }
});


