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

var current_link_id = 'text';

function price(users){
	total = globalPrice + price_per_users(users);
	return total;
}

function price_per_users(users){
	total = (users * userPrice);
	return total;
}

function refresh_price(){

	if(!isInteger($("invoice_company_user_size").value)){
		$("invoice_company_user_size").select();
		return false;
	}
	users = parseInt($("invoice_company_user_size").value, 10);
	start_date = $("invoice_started_at").value;
	finish_date = $("invoice_finished_at").value;
	textual_start_date = $("textual_started_at").value;
	textual_finish_date = $("textual_finished_at").value;

	total = price(users);

	$("price").update(total);
	$("invoice_amount").value = total;
	$("invoice-confirm").update(I18n.translate('views.invoices.confirmation',{'count' : users, 'start_date' : textual_start_date, 'end_date' : textual_finish_date }));


}

function refresh_price_for_network(){
	if(!isInteger($("network_company_user_size").value)){
		$("network_company_user_size").select();
		return false;
	}
	users = parseInt($("network_company_user_size").value, 10);

	total = price(users);

	$("price").update(total);
}

function refresh_price_date(days){

	users = parseInt($("invoice_company_user_size").value, 10);
	start_date = $("invoice_started_at").value;
	finish_date = $("invoice_finished_at").value;
	textual_start_date = $("textual_started_at").value;
	textual_finish_date = $("textual_finished_at").value;

	total = price_per_users(users);

	total = (total*days)/365;
	total = Math.round(total*100)/100;
	total = Math.round(total);

	$("price").update(total);
	$("invoice_amount").value = total;
	$("invoice-confirm").update(I18n.translate('views.invoices.upgrade_confirmation',{'count' : users, 'start_date' : textual_start_date, 'end_date' : textual_finish_date }));
}

function buttonInProgress(button_id, text) {
	$(button_id).value = text;
	$(button_id).className = $(button_id).className + ' progress';
	$(button_id).disabled = true;
}

validate_email = function(email) {
    var regexEmail = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
    return regexEmail.test(email);
}

$(document).observe('dom:loaded', function(){

	// Automagicaly select the first field of a form, if any, within the div#content-inner
	// (this exclude potential form within sidebar, footer, etc)
	// Source : http://stackoverflow.com/questions/644128/how-to-select-the-first-child-form-element-using-prototype

	var firstForm = $$('#content form').first();
	selectFirstFormField(firstForm);

	// Starting up the periodical checker

	$$("input[type='submit']").each(function(button){
		button.observe('click', function(){
			button.value = I18n.translate('views.defaults.please_wait');
			button.addClassName("progress");
			/*
			 * button.disabled = true;
			 * Disabling the button prevent the form to be sent with
			 * IE & Opera
			 */
		})
	});

});

function selectFirstFormField(firstForm){

	if(firstForm != null){

		if (typeof(noFocus) != "undefined") {
			/* nothing */
		} else {
			var firstElement = Form.getElements(firstForm).find(function(element){
				return element.type != 'hidden' && !element.disabled &&
				['input', 'textarea'].include(element.tagName.toLowerCase());
			});
			if (firstElement != null) {
				firstElement.activate();
			}
		}
	}
}

function focusOnFirstElement(container){

	if (typeof(noFocus) != "undefined") {

	}else{
		var firstForm = $$(container+' form').first();
		if(firstForm != null){
			var firstElement = Form.getElements(firstForm).find(function(element) {
			    return element.type != 'hidden' && !element.disabled &&
			    ['input', 'textarea'].include(element.tagName.toLowerCase());
			});
			if(firstElement != null)
			    firstElement.activate();
		}
	}
}

function flash(message, html_className){
  if(!$("notice")){
    new Element.insert("content",
      {
        top: new Element('div',
          {
            'id': 'notice'
          }
        )
      }
    );
  }else{
    $("notice").show();
  }
  $("notice").innerHTML = message;
  $("notice").className = html_className;
}

function modalFlash(message, className){
	if ($("lightview")) {
		if (!$("modal_notice")) {
			new Element.insert($$(".lv_contentBottom").first(), {
				top: new Element('div', {
					'id': 'modal_notice',
					'className': className || ""
				})
			});
		}else{
			$("modal_notice").show();
		}
		$("modal_notice").innerHTML = message;
	}

}

AutoResizer = function(textarea){

}
/*
$$(".autoresized").each(function(textarea){
	$(textarea).observe("keyup", AutoResizer(textarea));
})
*/

function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

   function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
   }

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }

// code yanked from the Yahoo media player. Thanks, Yahoo.
if (! ("console" in window) || !("firebug" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i < names.length; ++i) {
		window.console[names[i]] = function(){
		};
	}
}

