jQuery(document).ready(function()
{	 
    jQuery("#date_convert_visible").datepicker({
        altField: '#date_convert',
        altFormat: 'yy-mm-dd',
        dateFormat: 'dd MM yy',
        yearRange: '1998:2030',
        minDate: new Date(1998, 0, 1),
        maxDate: db_last_date,
        changeMonth: true,
        changeYear: true,
        gotoCurrent: true,
        onSelect: function(dateText, inst) {
           rapidConvert();
        }
    });

	jQuery("#value").keypress(function (e)
	{
	  //if the letter is not digit then display error and don't type anything
	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57) )
	  {
          // for coma
          if (e.which == 44 || e.which == 46) {
              var hasDot = e.target.value.indexOf('.');

              if (hasDot == -1) {
                  e.target.value = e.target.value + '.';
              }
          } 
          
          return false;
	  }
	});
	
	jQuery("#value").click(function(){
		jQuery(this).select();
	});
	
	jQuery("#result").click(function(){
		jQuery(this).select();
	});
	
	jQuery("#result").keypress(function (e)
	{
	  return false;
	});
	
	jQuery("#resultVAT").click(function(){
		jQuery(this).select();
	});
	
	jQuery("#resultVAT").keypress(function (e)
	{
	  return false;
	});
	
	jQuery("#VAT").click(function(){
		jQuery(this).select();
	});
	
	jQuery("#VAT").keypress(function (e) {
	  return false;
	});
	
    jQuery("#valuta1").selectbox({className: 'valuta-color-selectbox'}).bind('change', rapidConvert);

    jQuery("#valuta2").selectbox({className: 'valuta-color-selectbox'}).bind('change', rapidConvert);
    
    jQuery("#convert_invert").click(function(){
       var temp = jQuery("#valuta1").val();
       jQuery("#valuta1").selectbox({selected: jQuery("#valuta2").val()});
       jQuery("#valuta2").selectbox({selected: temp});
       return false; 
    });

    jQuery("#value").keyup(function(e) {
        limitChars('value', 10);
        jQuery("#to_advanced_convertor").attr("href", "/content/advancedConvertor?value=" + jQuery("#value").val());
        rapidConvert();
    });

    jQuery(".convert_quickLink").click(function(){
       var conversion = this.rel;
       var currencies = conversion.split("|");
       var valuta1 = currencies[0];
       var valuta2 = currencies[1];
       jQuery("#valuta1").selectbox({selected: valuta1});
       jQuery("#valuta2").selectbox({selected: valuta2});
       return false;
    });

    jQuery("#to_convertor").live("click", function() {
        if (jQuery("#convertor_sum")) {
            jQuery("#convertor_sum").val(jQuery("#calc_text").val());
		}

        if(jQuery("#value")) {
			jQuery("#value").val(jQuery("#calc_text").val());
			jQuery("#result").val(getResult());
    	  	jQuery("#resultVAT").val(getResultWithTVA());
          	jQuery("#VAT").val(getTVA());
		}

        return false;
    });
 
});

function rapidConvert() {
    jQuery("#result").val(getResult());
    jQuery("#resultVAT").val(getResultWithTVA());
    jQuery("#VAT").val(getTVA());
}

function getResult() {
    var suma = jQuery('#value').val();
  	var date = jQuery('#date_convert').val();
    var fromCurrency = jQuery("#valuta1").val();
    var toCurrency = jQuery("#valuta2").val();
  	
    if ((rates[fromCurrency][date] == undefined) || (rates[toCurrency][date] == undefined))
    {
      // get the values from the server
      jQuery.ajax({
        type: "GET",
        url: "/content/cursInData/from/"+fromCurrency+"/to/"+toCurrency+"/date/"+date,
        dataType: "script",
        async: false
      });
    }

    var fromValue	= rates[fromCurrency][date];
    var toValue		= rates[toCurrency][date];
    var rezultat	= suma * (fromValue / toValue);
    if (isNaN(rezultat)) return 0;
    return rezultat.toFixed(2);
}
  
 function getResultWithTVA() {
  	var suma = jQuery('#value').val();
  	var date = jQuery('#date_convert').val();
    var fromCurrency = jQuery("#valuta1").val();
    var toCurrency = jQuery("#valuta2").val();
    var fromValue	= rates[fromCurrency][date];
    var toValue		= rates[toCurrency][date];
    var rezultat	= suma * (fromValue / toValue);
  	var withTVA = rezultat * 1.19;
  	if (isNaN(withTVA)) return 0;
    return withTVA.toFixed(2)+ ' ' + jQuery('#valuta2').val();

}
  
function getTVA() {
  	var suma = jQuery('#value').val();
  	var date = jQuery('#date_convert').val();
    var fromCurrency = jQuery("#valuta1").val();
    var toCurrency = jQuery("#valuta2").val();
    var fromValue	= rates[fromCurrency][date];
    var toValue		= rates[toCurrency][date];
    var rezultat	= suma * (fromValue / toValue);
  	var TVA	= rezultat * 0.19;
  	if (isNaN(TVA)) return 0;
  	return TVA.toFixed(2)+ ' ' + jQuery('#valuta2').val();
}
  
function limitChars(textid, limit) {
    var text = jQuery('#'+textid).val();
	var textlength = text.length;
	
 	if(textlength > limit) {
		//jQuery('#' + infodiv).html('You cannot write more then '+limit+' characters!');
 		jQuery('#'+textid).val(text.substr(0,limit));
		return false;
 	}
}
