(function($){
$(document).ready(function(){

  if (!isAuth){
    $('#fdbk_tab').hide();
  }

	$('#keyword2').focus(function(){
    if (this.value == 'Example: "CSCO" or "Google"'){
      this.value = '';
    }
	});
	
	$('#invitation_email_request').focus(function(){
    if (this.value == 'Enter your email address'){
      this.value = '';
    }
	});
  
	$('#keyword22').focus(function(){
    if (this.value == 'Enter stock or ETF'){
      this.value = '';
    }
	});
  
	$('#add_signals').focus(function(){
    if (this.value == 'Search Stocks'){
      this.value = '';
    }
	});
  
	$('#login_name').focus(function(){
    if (this.value == 'username'){
      this.value = '';
    }
	});
  
	$('#login_password').focus(function(){
    if (this.value == 'password'){
      this.value = '';
    }
	});
  
	$('#feedback_link').click(function(){
    $('#fdbk_tab').click();
	});
	
	$('#btn_request_invite').click(function(){
		if (!isValidEmail($("#invitation_email_request").val()))
	    {
	      alert('Invalid email'); 
	      return false;
	    }; 
		$.ajax({
			type: "POST",
			dataType: 'json',
			async : false,
			url: urlForHomepage+"home/invitationsend",
			data: {invitation_email: $('#invitation_email_request').val()}
		});
		alert('Your email was accepted. We will consider it and you will get email from us.');
		$('#invitation_email_request').val('Enter your email address');
	});
  
  $('.signal_td').click(function(){
    var id = this.parentNode.id;
    var arr = id.split('_');
    expandRow(arr[1]);
  });

  $('#login_show').click(function(){
    $('#login').show();
    $('#header_panel').hide();
    $('#login_show').css('color', '#000');
  });
    
	$('#terms_condition').click(function(){
    showTerms();
	});
  
	$('#terms_condition_panel').click(function(){
    showTerms();
	});
  
	$('#close_mask').click(function(){
    $('#mask_screen').hide();
	});

  $('.opacity2_img').mouseover(function(){
      $(this).attr('class', "opacity_img");
	});
  
  $('.opacity2_img').mouseleave(function(){
      $(this).attr('class', "opacity2_img");
	});
  
  $('.signals_tr_even').mouseover(function(){
      var id = $(this).attr('id');
      var arr_id = id.split('_');
      if (arr_id[0] == 'row'){
        $(this).attr('class', "back_green");
      }else{
        $('#row_'+arr_id[1]).attr('class', "back_green");
      }
	});
  $('.signals_tr_even').mouseleave(function(){
      
      var id = $(this).attr('id');
      var arr_id = id.split('_');
      if (arr_id[0] == 'row'){
        $(this).attr('class', "signals_tr_even");
      }else{
        $('#row_'+arr_id[1]).attr('class', "signals_tr_even");
      }      
	});
  
  $('.signals_tr_ogg').mouseover(function(){
      
      var id = $(this).attr('id');
      var arr_id = id.split('_');
      if (arr_id[0] == 'row'){
        $(this).attr('class', "back_green");
      }else{
        $('#row_'+arr_id[1]).attr('class', "back_green");
      }       
	});
  $('.signals_tr_ogg').mouseleave(function(){
      
      var id = $(this).attr('id');
      var arr_id = id.split('_');
      if (arr_id[0] == 'row'){
        $(this).attr('class', "signals_tr_ogg");
      }else{
        $('#row_'+arr_id[1]).attr('class', "signals_tr_ogg");
      }       
	});
  
  open_block1 = true;
  $('#block_close_image').click(function(){
    if (open_block1){
      $('#block_close_image').attr('src', '/images/quotes_content_block_open.gif');
      $('#block_part1').height(70);
      $('#block_part2').height(5);
      $('#block_part3').height(70);
      open_block1 = false;
    }else{
      $('#block_close_image').attr('src', '/images/quotes_content_block_close.gif');
      $('#block_part1').height(150);
      $('#block_part2').height(85);
      $('#block_part3').height(150);      
      open_block1 = true;    
    }
	});
  open_block2 = true;
  $('#block2_close_image').click(function(){
    if (open_block2){
      $('#block2_close_image').attr('src', '/images/quotes_content_block_open.gif');
      $('#block2_part1').height(70);
      $('#block2_part2').height(5);
      $('#block2_part3').height(70);
      open_block2 = false;
    }else{
      $('#block2_close_image').attr('src', '/images/quotes_content_block_close.gif');
      $('#block2_part1').height(90);
      $('#block2_part2').height(25);
      $('#block2_part3').height(90);      
      open_block2 = true;    
    }
	});
  
 
  $('#menu_your_signals').click(function(){
  	if (isAuth){
      document.location.href = urlForHomepage+"your_signals";
    }else{
      alert('You are not authenticated.');
    }
	}); 
  
  $('#learn_more').click(function(){
    runEffect3();
	}); 
  
  
  $('#close_learn').click(function(){
    runEffect32();
	}); 
	
	$('#welcome_close').click(function(){
  			$.ajax({
			  type: "POST",
			  dataType: 'json',
			  async : false,
			  url: urlForHomepage+"home/closeloginpanel",
			  success: function (data, textStatus) {
          if (data){
            $('#loginpanel').hide();
            $('#line_div').hide();
          }
			  }
			});
	}); 
  
	$('#email_report').change(function(){
      $.ajax({
			  type: "POST",
			  dataType: 'json',
			  async : false,
			  url: urlForHomepage+"quotes/checkemail",
        data: {email: $('#email_report').val()},
			  success: function (data, textStatus) {
          if (data){
            if (data != 'Bad email'){
              $('#check_email_answer')[0].innerHTML ='<img src="/images/tick.png" width="16" height="16" style="vertical-align:text-bottom;"/>';
            }else{
              $('#check_email_answer')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> It is invalid email';
            }
          }
			  }
			});
	}); 
  
  $('#reset_password').click(function(){
  			$.ajax({
			  type: "POST",
			  dataType: 'json',
			  async : false,
			  url: urlForHomepage+"home/resetpassword",
        data: {email: $('#forgot_email').val()},
			  success: function (data, textStatus) {
          if (data){
            alert('To a specified email were sent instructions');
            runEffect22();
          }else{
            alert('This email is not found');
          }
			  }
			});
	}); 
  
	$('#panel_login').focus(function(){
    if (this.value == 'Email'){
      this.value = '';
    }
	}); 
  
	$('#panel_password').focus(function(){
    if ((this.value == 'Password')&&(this.type == 'text')){
      this.value = '';
      this.type = 'password';
    }
	});  	
//	check entered signup email;
	$('#panel_signup_email').change(function() {
    $('#email_is_good').val(0);
		if (this.value != ''){
			$.ajax({
			  type: "POST",
			  dataType: 'json',
			  async : false,
			  url: urlForHomepage+"quotes/checkemail",
			  data: {email: this.value},
			  success: function (data, textStatus) {
				if (data){
					switch (data){				
						case 'Bad email':
							$('#panel_check_signup_email')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> It is invalid email';
              $('#panel_signup_email').css("backgroundColor", "#FAA");
							$('#email_is_good').val(0);
							$('#panel_check_signup_email').show();
							break;
						case 'occup':
							$('#panel_check_signup_email')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> This email is registered';
              $('#panel_signup_email').css("backgroundColor", "#FAA");
							$('#email_is_good').val(0);
							$('#panel_check_signup_email').show();
							break;
						case 'good':
							$('#panel_check_signup_email')[0].innerHTML ='<img src="/images/tick.png" width="16" height="16" style="vertical-align:text-bottom;"/> This email is available';
              $('#panel_signup_email').css("backgroundColor", "#FFF");
							$('#email_is_good').val(1);
							$('#panel_check_signup_email').show();
							break;
						
					}
				}
			  }
			});
		}else{
			$('#panel_check_signup_email').hide();
		}
    });  
    $('#panel_signup_email').keypress(function() {
      $('#email_is_good').val(0);
    });  
   
    
	$("#signUpBtn_panel").click(function() {
		$('#panel_signup_email').change();
    if ($('#email_is_good').val() != 0){
      $('#panel_signup_email').css("backgroundColor", "#FFF");
      if ($('#createPass_panel').val() != ''){
        $('#createPass_panel').css("backgroundColor", "#FFF");
        if ($('#agree_panel').attr('checked')){
          var ticket = new Array();
          ticket[0] = $('#panel_symbol1').val();
          ticket[1] = $('#panel_symbol2').val();
          ticket[2] = $('#panel_symbol3').val();
          var str = ticket.join('-:-');
          $.ajax({
            type: "POST",
            dataType: 'json',
            async : false,
            url: urlForHomepage+"registration/addtrialuser",
            data: { email: $('#panel_signup_email').val(), 
                    password: $('#createPass_panel').val(), 
                    ticket: str
                  },
            success: function (data, textStatus) {
              if (data){
                //alert('User create successfully');
                //runEffect12();
                document.location.href = urlForHomepage+"home/login?panel_login="+$('#panel_signup_email').val()+"&panel_password="+$('#createPass_panel').val();
              }
            }
          });
        }else{
          alert('You agreed not with Investio Terms & Conditions');
        }
      }else{
        //alert('Password is empty');
        $('#createPass_panel').css("backgroundColor", "#FAA");
      }
    }else{
      //alert('Email is wrong');
      $('#panel_signup_email').css("backgroundColor", "#FAA");
      $('#panel_check_signup_email')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> Email is wrong';
      $('#panel_check_signup_email').show();
    }
	});    
    
//	check entered signup email on HOMEPAGE;
	$('#email').change(function() {
    $('#email_is_good').val(0);
		if (this.value != ''){
			$.ajax({
			  type: "POST",
			  dataType: 'json',
			  async : false,
			  url: urlForHomepage+"quotes/checkemail",
			  data: {email: this.value},
			  success: function (data, textStatus) {
				if (data){
					switch (data){				
						case 'Bad email':
							$('#check_signup_email')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> It is invalid email';
              $('#email').css("backgroundColor", "#FAA");
							$('#email_is_good').val(0);
							$('#check_signup_email').show();
							break;
						case 'occup':
							$('#check_signup_email')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> This email is registered';
              $('#email').css("backgroundColor", "#FAA");
							$('#email_is_good').val(0);
							$('#check_signup_email').show();
							break;
						case 'good':
							$('#check_signup_email')[0].innerHTML ='<img src="/images/tick.png" width="16" height="16" style="vertical-align:text-bottom;"/> This email is available';
              $('#email').css("backgroundColor", "#FFF");
							$('#email_is_good').val(1);
							$('#check_signup_email').show();
							break;
						
					}
				}
			  }
			});
		}else{
			$('#check_signup_email').hide();
		}
    }); 
    
    $('#email').keypress(function() {
      $('#email_is_good').val(0);
    });  
   
    
	$("#signUpBtn").click(function() {
		$('#email').change();
    if ($('#email_is_good').val() != 0){
      $('#email').css("backgroundColor", "#FFF");
      if ($('#createPass').val() != ''){
        $('#createPass').css("backgroundColor", "#FFF");
        if ($('#agree').attr('checked')){
          var ticket = new Array();
          ticket[0] = $('#symbol1').val();
          ticket[1] = $('#symbol2').val();
          ticket[2] = $('#symbol3').val();
          var str = ticket.join('-:-');
          $.ajax({
            type: "POST",
            dataType: 'json',
            async : false,
            url: urlForHomepage+"registration/addtrialuser",
            data: { email: $('#email').val(), 
                    password: $('#createPass').val(), 
                    ticket: str
                  },
            success: function (data, textStatus) {
              if (data){
                //alert('User create successfully');
                //runEffect12();
                document.location.href = urlForHomepage+"home/login?panel_login="+$('#email').val()+"&panel_password="+$('#createPass').val();
              }
            }
          });
        }else{
          alert('You agreed not with Investio Terms & Conditions');
        }
      }else{
        //alert('Password is empty');
        $('#createPass').css("backgroundColor", "#FAA");
      }
    }else{
      //alert('Email is wrong');
      $('#email').css("backgroundColor", "#FAA");
      $('#check_signup_email')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> Email is wrong';
      $('#check_signup_email').show();
    }
	});    
    
	//set effect from select menu value
	$("#signup_panel_btn").click(function() {
		runEffect1();
		return false;
	});
	//set effect from select menu value
	$("#sign_close").click(function() {
		runEffect12();
		return false;
	});

	//set effect from select menu value
	$("#forgot_link").click(function() {
		runEffect2();
		return false;
	});
	//set effect from select menu value
	$("#close_forgot").click(function() {
		runEffect22();
		return false;
	});
	
//	$("#effect").hide();
//	$("#effect2").hide();
});

})(jQuery);

//run the currently selected effect
function runEffect1(){	
	//$("#effect").show('slide',null,1000);
	$("#effect").show();
};
//run the currently selected effect
function runEffect12(){	
	//$("#effect").hide('slide',null,1000);
	$("#effect").hide();
};
//run the currently selected effect
function runEffect2(){	
	//$("#effect2").show('blind',null,1000);
	$("#effect2").show();
};
//run the currently selected effect
function runEffect22(){	
	//$("#effect2").hide('blind',null,1000);
	$("#effect2").hide();
};
//run the currently selected effect
function runEffect3(){	
	//$("#effect3").show('explode',null,500);
	$("#effect3").show();
};
//run the currently selected effect
function runEffect32(){	
	//$("#effect3").hide('explode',null,1000);
	$("#effect3").hide();
};

function deleteSignalsRow(ticker){
  $.ajax({
    type: "POST",
    dataType: 'json',
    async : false,
    url: urlForHomepage+"your_signals/deletesignal",
    data: { ticker: ticker},
    success: function (data, textStatus) {
      if (data){
        tr = document.getElementById('row_'+ticker);
        parent = tr.parentNode;
        parent.removeChild(tr);
        tr = document.getElementById('rowinfo_'+ticker);
        parent = tr.parentNode;
        parent.removeChild(tr);
        
      }
    }
  });
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function saveReportData()
{
  var radioObj = document.forms['report_form'].elements['frequency'];
	var radioLength = radioObj.length;
  var selectFreq = false;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
		var value = radioObj[i].value;
    selectFreq = true;
		}
	}
  if (selectFreq){
  $.ajax({
    type: "POST",
    dataType: 'json',
    async : false,
    url: urlForHomepage+"quotes/checkemail",
    data: {email: $('#email_report').val(), freq: value},
    success: function (data, textStatus) {
      if (data){
        if (data != 'Bad email'){
          $('#check_email_answer')[0].innerHTML ='<img src="/images/tick.png" width="16" height="16" style="vertical-align:text-bottom;"/>';
          $.ajax({
            type: "POST",
            dataType: 'json',
            async : false,
            url: urlForHomepage+"your_signals/savereportdata",
            data: {email: $('#email_report').val(), value: value},
            success: function (data, textStatus) {
              if (data){
                $('#check_email_answer')[0].innerHTML ='<img src="/images/tick.png" width="16" height="16" style="vertical-align:text-bottom;"/> Saved';
                document.location.href = urlForHomepage+"your_signals";
              }
            }
          });
        }else{
          $('#check_email_answer')[0].innerHTML ='<img src="/images/delete.png" width="16" height="16" style="vertical-align:text-bottom;"/> It is invalid email';
        }
      }
    }
  });
  }else{
    alert('Not select a frequency');
  }
}

function addSignalAction()
{
  var ticker_str = $('#add_signals').val();
  var arr = ticker_str.split('-');
  var ticker = TrimStr(arr[0]);
  ticker = ticker.toUpperCase();
  if ($('#row_'+ticker).length){
    alert('Symbol '+ticker+' is already added');
    $('#add_signals').val('');
  }else{
    document.form_add.submit();
  }
}

function TrimStr(s) {
  s = s.replace( /^\s+/g, '');
  return s.replace( /\s+$/g, '');
}

function showTerms()
{
  $('#mask').css('height', window['screen']['availHeight']);
    $.ajax({
			  type: "POST",
			  dataType: 'json',
			  async : false,
			  url: urlForHomepage+"home/getterms",
			  success: function (data, textStatus) {
          if (data){
            $('#terms_text')[0].innerHTML = data;
            $('#mask_screen').show();
          }
			  }
			});  
  
  
}


function isValidEmail(email)
{
     re = /^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;
     return re.test(email);
}

function checkInvitation()
{
  if (!isValidEmail($("#invitation_email").val()))
    {
      alert('Invalid email'); 
      return false;
    }; 
  if ($("#reason").val() == '')
  {
    alert('Enter please your message'); 
    return false;
  }
  $("#captcha_div").show();
}

function checkComment()
{
  if (!isValidEmail($("#contact_email").val()))
    {
      alert('Invalid email'); 
      return false;
    }; 
  if ($("#contact_comment").val() == '')
  {
    alert('Enter please your message'); 
    return false;
  }
  $("#captcha_div").show();
}

function expandRow(ticker)
{
  if (document.getElementById('expand_'+ticker).innerHTML == '+'){
    document.getElementById('expand_'+ticker).innerHTML = '&ndash;';
    if (document.getElementById('signalgauge_'+ticker).innerHTML == ''){
      document.getElementById('signalgauge_'+ticker).innerHTML = '<img height="150" src="/chart/triggers/trigger_'+ticker+'.png" />';
    }
    document.getElementById('rowinfo_'+ticker).style.display = '';
  }else{
    document.getElementById('expand_'+ticker).innerHTML = '+';
    document.getElementById('rowinfo_'+ticker).style.display = 'none';
  }
}