//To Unformat Currency to Number

function getValue(num)
{
    var noJunk = ""
    var withDollar = ""
    var foundDecimal = 0
    var foundAlphaChar = 0
    num += "";

    if (num == "") { return(0); }
    for (i=0; i <= num.length; i++)
    {
        var thisChar = num.substring(i, i+1);
        if (thisChar == ".")
        {
          foundDecimal = 1;
          noJunk = noJunk + thisChar;
        }
        if ((thisChar < "0") || (thisChar > "9"))
        {
          if ((thisChar != "$") && (thisChar !=".") && (thisChar != ",") && (thisChar != " ") && (thisChar !="")) foundAlphaChar = 1;
        }
        else
 {
    withDollar = withDollar + thisChar
    noJunk = noJunk + thisChar
 }

 if ((thisChar == "$") || (thisChar == ".") || (thisChar == ","))
 {
   withDollar = withDollar + thisChar
 }
  }
     if (foundDecimal) { return parseFloat(noJunk); }
     else if (noJunk.length > 0) { return parseFloat(noJunk); }
     else return 0;
}

jQuery(document).ready(function(){

	// Add an event to the radio buttons (form 2).
	jQuery("#cforms2form input[@name='contributionRadios']").bind("change", function(e){
		radioValue = jQuery(this).val();
		if (radioValue == "Other Amount") {
			jQuery("#cforms2form input#amount").val(getValue(jQuery("#cforms2form #Other_Amount").val()));
			jQuery("#cforms2form #Other_Amount").removeAttr("disabled");
		} else {
		   jQuery("#cforms2form input#amount").val(radioValue);
		   jQuery("#cforms2form #Other_Amount").attr("disabled","disabled");
		}
	});
	
	// Add an event to the Other input box (form 2).
	jQuery("#cforms2form #Other_Amount").bind("change", function(e){
		  jQuery("#cforms2form input#amount").val(getValue(jQuery("#cforms2form #Other_Amount").val()));
	});
	
	// Add an event to the radio buttons (form 5).
	jQuery("#cforms5form input[@name='contributionRadios']").bind("change", function(e){
		radioValue = jQuery(this).val();
		if (radioValue == "Other Amount") {
			jQuery("#cforms5form input#a3").val(getValue(jQuery("#cforms5form #Other_Amount").val()));
			jQuery("#cforms5form #Other_Amount").removeAttr("disabled");
		} else {
		   jQuery("#cforms5form input#a3").val(radioValue);
		   jQuery("#cforms5form #Other_Amount").attr("disabled","disabled");
		}
	});
	
	// Add an event to the Other input box (form 5).
	jQuery("#cforms5form #Other_Amount").bind("change", function(e){
		  jQuery("#cforms5form input#a3").val(getValue(jQuery("#cforms5form #Other_Amount").val()));
	});
	
	function combineEmployer (formID) {
		var employer = jQuery(formID + " #Employer").val();
		var employerLocation = jQuery(formID + " #EmployerLocation").val();
		var combinedEmployer = employer + " (" + employerLocation + ")";
		jQuery(formID + " input#os1").val(combinedEmployer);	
	}
	
	// Combine the Employer and Employer Location fields when changed (form 2)
	jQuery("#cforms2form #Employer").bind("change", function(e){
		combineEmployer("#cforms2form");
	});
	jQuery("#cforms2form #EmployerLocation").bind("change", function(e){
		combineEmployer("#cforms2form");
	});
	
	// Combine the Employer and Employer Location fields when changed (form 5)
	jQuery("#cforms5form #Employer").bind("change", function(e){
		combineEmployer("#cforms5form");
	});
	jQuery("#cforms5form #EmployerLocation").bind("change", function(e){
		combineEmployer("#cforms5form");
	});
	
	function init () {
		radioValue = jQuery("#cforms2form input[@name='contributionRadios']").val();
		jQuery("#cforms2form input#amount").val(radioValue);
		jQuery("#cforms2form #Other_Amount").attr("disabled","disabled");
		
		radioValue = jQuery("#cforms5form input[@name='contributionRadios']").val();
		jQuery("#cforms5form input#a3").val(radioValue);
		jQuery("#cforms5form #Other_Amount").attr("disabled","disabled");
		
		combineEmployer("#cforms2form");
		combineEmployer("#cforms5form");
	}
	
	init();

});