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

function popup (divid,action) {
    el=document.getElementById(divid);

	if (action == "open") {
		//set default actions
		if (el.style.visibility=='visible')
		{
			el.style.visibility='hidden';
		} else {
			el.style.visibility='visible';
		}
	} else {
		el.style.visibility='hidden';
    }

	return false;
	
}
	function popup_window (id,acn) {


	if (acn == "open") {
		if (document.getElementById("card_processing_details"+id).style.visibility=='visible')
		{
			document.getElementById("card_processing_details"+id).style.visibility='hidden';
		} else {
			document.getElementById("card_processing_details"+id).style.visibility='visible';
		}
	} else {
		document.getElementById("card_processing_details"+id).style.visibility='hidden';
                
    }



}
lastone='empty'; 
function showIt(lyr) 
{ 
	if (lastone!='empty') lastone.style.display='none'; 
	lastone=document.getElementById(lyr); 
	lastone.style.display='block';
}


function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}

function add_school()
{
  var length=0;
  jQuery("#schools").children(".aboutformlister").each(function(){
      if(jQuery(this).css('display')=='block')
      {
         length=length+1;
      }
  });
  var len=length;
  if(len<20){
  jQuery("#schools").children(".aboutformlister:first").clone(true).insertAfter(jQuery("#schools").children(".aboutformlister:last"));
  jQuery("#schools").children(".aboutformlister:last").show();
  jQuery("#schools").children(".aboutformlister:last").find("INPUT").each(function(){jQuery(this).val('');});
  
  }
}
function add_education()
{
  var length=0;
  jQuery("#education").children(".aboutformlister").each(function(){
      if(jQuery(this).css('display')=='block')
      {
         length=length+1;
      }
  });
  var len=length;
  if(len<6){
  jQuery("#education").children(".aboutformlister:first").clone(true).insertAfter(jQuery("#education").children(".aboutformlister:last"));
  jQuery("#education").children(".aboutformlister:last").show();
  jQuery("#education").children(".aboutformlister:last").find("INPUT").each(function(){jQuery(this).val(" ");});
  jQuery("#education").children(".aboutformlister:last").find(".textbox_education").addClass('emptyfield');
   jQuery('.emptyfield').keyup (function(e) { 
     obj=e.target
     if (jQuery(obj).attr('class')=='textbox_education emptyfield' && jQuery.trim(obj.value)!=''){
       add_education();
       jQuery(obj).removeClass('emptyfield');
       
     }
  });
 }
}
function add_teacher()
{
  var length=0;
  jQuery("#teachers").children(".aboutformlister").each(function(){
      if(jQuery(this).css('display')=='block')
      {
         length=length+1;
      }
  });
  var len=length;
  if(len<20){
  jQuery("#teachers").children(".aboutformlister:first").clone(true).insertAfter(jQuery("#teachers").children(".aboutformlister:last"));
  jQuery("#teachers").children(".aboutformlister:last").show();
  jQuery("#teachers").children(".aboutformlister:last").find("INPUT").each(function(){jQuery(this).val('');});
  
  }
}

function numbersonly(evt){
   
//var charCode = (evt.which) ? evt.which : evt.keyCode
//var keychar = String.fromCharCode(charCode);
 //if ((charCode < 48 || charCode > 57  )&& (charCode!=8)&&(charCode != 53) && (charCode != 39)&&(charCode != 9))
 //if ((charCode!= 8 && charCode!= 9) && charCode < 33 || (charCode > 40 && charCode < 48 && evt.keycode!= 46) || charCode > 57 &&(charCode!=116))
 /*{  return false;	
 }
  else if((keychar.indexOf("%") > -1)){
     return false;
     
 }
 else{
     return true;
 }*/
  var keyEntry = (evt.which) ? evt.which : evt.keyCode
  var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }
   
 if ((keyEntry > 31 && charcode!=0) && (keyEntry < 48 || keyEntry > 57))
      return false;

 return true;

 
}
function AllowAlphabet(evt)
{
    
    /*var keyEntry = (evt.which) ? evt.which : evt.keyCode
    if(((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || keyEntry=='8') 
            return true;  
    else
            return false;*/
    var keyEntry = (evt.which) ? evt.which : evt.keyCode
    var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }
   
    if(((keyEntry >= '32') && (keyEntry <= '64') && charcode!=0)|| ((keyEntry >= '91'&& keyEntry <= '96')) || ((keyEntry >= '123'&& keyEntry <= '127')))
        return false;  
    else
            return true;
    
}

function remove_school(obj)
{
  var length=0;
  jQuery("#schools").children(".aboutformlister").each(function(){
      if(jQuery(this).css('display')=='block')
      {
         length=length+1;
      }
  });
  var len=length;
  if(len>1){
    jQuery(obj).parent().find("INPUT:first").val('');
    jQuery(obj).parent().parent().hide();
    
    }
}

function remove_teacher(obj)
{
  var length=0;
  jQuery("#teachers").children(".aboutformlister").each(function(){
      if(jQuery(this).css('display')=='block')
      {
         length=length+1;
      }
  });
  var len=length;
  if(len>1){
    jQuery(obj).parent().find("INPUT:first").val('');
    jQuery(obj).parent().parent().hide();
    }
}

function remove_education(obj)
{
  var length=0;
  jQuery("#education").children(".aboutformlister").each(function(){
      if(jQuery(this).css('display')=='block')
      {
         length=length+1;
      }
  });
  
  var len=length;
  if(len>1){
    
    jQuery(obj).parent().parent().find("INPUT:first").val('');
    jQuery(obj).parent().parent().hide();
    if((len==6 && jQuery("#education").children(".aboutformlister:last").find('INPUT:first').attr('class')=='textbox_education')||(jQuery("#education").children(".aboutformlister:last").css('display','block').find('INPUT:first').attr('class')=='textbox_education')){
        add_education();
    }
    
    }
}

function validate_numeric_years(evt) {
    var keyEntry = (evt.which) ? evt.which : evt.keyCode
    var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }

    if (document.getElementById('year_of_graduation').value.length == 3)
        document.getElementById('connect').disabled = false;
    
    if ((keyEntry > 31 && charcode!=0) && (keyEntry < 48 || keyEntry > 57))
        return false;

     return true;
}
function numbersonly_withhiphen(evt){

  var keyEntry = (evt.which) ? evt.which : evt.keyCode
  var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }
   
 if ((keyEntry > 31 && charcode!=0) && (keyEntry < 48 || keyEntry > 57)&& (keyEntry!=45 && charcode!=0))
      return false;

 return true;

 
}
function numbersonly_with_hiphen_and_decimal(evt){

  var keyEntry = (evt.which) ? evt.which : evt.keyCode
  var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }

 if ((keyEntry > 31 && charcode!=0) && (keyEntry < 48 || keyEntry > 57)&& (keyEntry!=45 && charcode!=0) && (keyEntry!=46 && charcode!=0))
      return false;

 return true;


}
function float_numbers(evt){

  var keyEntry = (evt.which) ? evt.which : evt.keyCode
  var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }
   
 if ((keyEntry > 31 && charcode!=0) && (keyEntry < 48 || keyEntry > 57)&& (keyEntry!=46 && charcode!=0))
      return false;

 return true;

 
}

function alphanumeric(evt){
    var keyEntry = (evt.which) ? evt.which : evt.keyCode
    var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }
   
    if(((keyEntry >= '32') && (keyEntry <= '47') && charcode!=0)||((keyEntry >= '58') && (keyEntry <= '64') && charcode!=0)|| ((keyEntry >= '91'&& keyEntry <= '96')) || ((keyEntry >= '123'&& keyEntry <= '127')))
        return false;  
    else
            return true;
}
function alpha_withspaces(evt){
    var keyEntry = (evt.which) ? evt.which : evt.keyCode
    var charcode;
    if(evt.charCode=='undefined' || evt.charCode==undefined)
    {
        charcode=(evt.which) ? evt.which : evt.keyCode
    }
    else{
      charcode=evt.charCode;
    }


    if(((keyEntry > '32') && (keyEntry <= '64') && charcode!=0)|| ((keyEntry >= '91'&& keyEntry <= '96')) || ((keyEntry >= '123'&& keyEntry <= '127')))
        return false;
    else
            return true;
}
function check_availability_ein(obj){
    ein_value=jQuery("#contributorchart_contributor_ein").val();

     var _url = '/batch_funding/check_availability_ein?ein='+ein_value
     jQuery.ajax({
        type: "GET",
        url: _url,
        dataType: "html",
        success: function(msg) {
            
           if(msg>0)
           {   
               jQuery('.contributor_form').hide(); 
           }
           else{
if (ein_value!='')
    {
    msg='<div class="headingDark">This EIN does not currently appear in the system. Please add it.</div>' + msg
           }
               jQuery('.contributor_form').html(msg);
              jQuery('.contributor_form').show();
           }
         
        }
    });
}

function fill_transaction_details(obj){
     transaction_value=jQuery("#transaction_id").val();
     if (!(transaction_value != null && isNaN(transaction_value) ))
     {
  var _url = '/journalentries/transaction_details?transaction_id='+transaction_value
     jQuery.ajax({
        type: "GET",
        url: _url,
        dataType: "text",
        success: function(msg) {
           if(msg.length>0)
           
           {  
               transaction_details=msg.split(",")
             
           if (transaction_details.length > 1 )
               {
               jQuery("#contributor_text").val(transaction_details[0]);
               jQuery("#original_date_text").val(transaction_details[1]);
               jQuery("#total_amount_text").val(transaction_details[2]);
             
                jQuery("#contributor").val(transaction_details[0]);
               jQuery("#original_date").val(transaction_details[1]);
               jQuery("#total_amount").val(transaction_details[2]);
                 jQuery("#invalid_order").hide();
           }
           else{
            
               jQuery("#invalid_order").html(transaction_details[0]);
            jQuery("#invalid_order").show();
               jQuery("#contributor_text").val('');
               jQuery("#original_date_text").val('');
               jQuery("#total_amount_text").val('');
             
           }
        }
        }
    });
     }
}
function display_company_name(obj){
    ein_value=jQuery("#contributorchart_contributor_ein").val();
     var _url = '/batch_funding/company_name_for_contributor_ein?ein='+ein_value
     jQuery.ajax({
        type: "GET",
        url: _url,
        dataType: "html",
        success: function(msg) {
           if(msg>0)
           {   
               jQuery('#contributor_name').hide();
           }
           else{
           
               jQuery('#contributor_name').html(msg);
              jQuery('#contributor_name').show();
           }
        }
    });
}
function check_donation_amount(type){
    if(jQuery('#donation_amount').val()==''){
       
       jQuery('#error_donate').html('<u>Please indicate an amount before clicking Donate</u>');
       
       return false;
    }
    
    if(jQuery('#donation_amount').val()<1){
       jQuery('#error_donate').html('<u>Minimum donation is $1.</u>');
        return false;
    }
}
function valid_amount_check(){
    value=jQuery("#amount").val();
    var isCurrency    = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
    if(value.match(isCurrency))
    {
        return true;
    }
    alert("Enter valid amount")

    return false;
}

