jQuery.noConflict();

cfct = {}

cfct.loading = function() {
	return '<div class="loading"><span>Loading...</span></div>';
}

cfct.ajax_post_content = function() {
	jQuery('ol.archive .excerpt .entry-title a').unbind().click(function() {
		var post_id = jQuery(this).attr('rev').replace('post-', '');
		jQuery('#post-excerpt-' + post_id).hide();
		jQuery('#post-content-' + post_id + '-target').html(cfct.loading()).load(CFCT_URL + '/index.php?cfct_action=post_content&id=' + post_id, cfct.ajax_post_comments);
		return false;
	});
}

cfct.ajax_post_comments = function() {
	jQuery('p.comments-link a').unbind().click(function() {
		var post_id = jQuery(this).attr('rev').replace('post-', '');
		jQuery(this).parent().hide();
		jQuery('#post-comments-' + post_id + '-target').html(cfct.loading()).load(CFCT_URL + '/index.php?cfct_action=post_comments&id=' + post_id);
		return false;
	});
}

jQuery(document).ready(function($) {
	// suckerfish dropdown fixes for IE
	$('#navigation li, #all-categories li').mouseover(function() {
		$(this).addClass('hover');
	});
	$('#navigation li, #all-categories li').mouseout(function() {
		$(this).removeClass('hover');
	});
	// :first-child fix for IE
	$('#navigation li li:first-child, #all-categories li li:first-child').addClass('first');
	// :hover fix for full articles in IE
	$('.full').mouseover(function() {
		$(this).addClass('hover');
	});
	$('.full').mouseout(function() {
		$(this).removeClass('hover');
	});
	if ((!$.browser.msie || $.browser.version.substr(0,1) != '6') && typeof CFCT_AJAX_LOAD != 'undefined' && CFCT_AJAX_LOAD) {
		cfct.ajax_post_content();
		cfct.ajax_post_comments();
	}
	$('#navigation li a, #all-categories li a').removeAttr('title');
});

function validateAmount(amount){
	if(amount.value.match( /^[0-9]+(\.([0-9]+))?$/)){
		return true;
	}else{
		alert('You must enter a valid donation.');
		amount.focus();
		return false;
	}
}

function newAmountOnFocus(amount) {
      if(null == amount.value.match( /^[0-9]+(\.([0-9]+))?$/)){
         return ("");
      }
      return (amount.value);
}

function datePartCheck(myDatePart, minChars, maxChars, correctString) {
      var patt = new RegExp( "[0-9]{"+ minChars + "," + maxChars + "}" );
      if (patt.test(myDatePart.value) == false || checkDateRange(myDatePart, correctString) == false) {
            alert('You must enter a valid date.');
            myDatePart.value = correctString;
            myDatePart.focus();
            return false;
      }
      return true;
}

function checkDateRange(myDatePart, correctString) {
      var datePartInt = parseInt(myDatePart.value);
      if (correctString == 'yyyy') {
           if (datePartInt > 999 && datePartInt < 10000) return true;
      }
      else if (correctString == 'mm') {
           if (datePartInt > 0 && datePartInt < 13) return true;
      }
      else if (correctString == 'dd') {
           if (datePartInt > 0 && datePartInt < 32) return true;
      }

      return false;
}

function updateDonationValues()
{
        
	document.checkout.max_charge.value = document.checkout.recurrent_unit_price.value;
document.checkout.firstDonationPrice.value = document.checkout.recurrent_unit_price.value;
}

function updateDonationDesc()
{
	document.checkout.item_description.value = document.checkout.recurrent_item_description.value;
document.checkout.firstDonationDescription.value = document.checkout.recurrent_item_description.value;
}

function updateDonationEndDate()
{
        if(document.getElementById("recurringEndDate")) {
                if (datePartCheck(document.checkout.recurringEndDateMonth, 1, 2, 'mm') && datePartCheck(document.checkout.recurringEndDateDay,1,2,'dd') && datePartCheck(document.checkout.recurringEndDateYear, 4, 4, 'yyyy')) {
                document.checkout.recurringEndDate.value = document.checkout.recurringEndDateYear.value + "-" + document.checkout.recurringEndDateMonth.value + "-" + document.checkout.recurringEndDateDay.value;
                return true;
                }
                else {
                        return false;
                }
                
        }
        else {
               return true;
        }
}

function changeRecurringDonationToEndDate()
{
	var buttonText = document.getElementById('switchEndType').value;

	if (buttonText == "Switch to Fixed End Date") {
		
		var spanElement = document.getElementById('end_date_text');
	
		var spanElementFirstText = spanElement.lastChild;
		spanElementFirstText.nodeValue = ' until ';



                var newInputBoxMonth = document.createElement('input');
		newInputBoxMonth.size = "2";
		newInputBoxMonth.value = "mm";
		newInputBoxMonth.id = "recurringEndDateMonth";
		newInputBoxMonth.setAttribute("class", "formEntry");
		newInputBoxMonth.onfocus = newDateOnFocus;
	
		spanElement.appendChild(newInputBoxMonth);

		var newInputBoxDay = document.createElement('input');
		newInputBoxDay.size = "2";
		newInputBoxDay.value = "dd";
		newInputBoxDay.id = "recurringEndDateDay";
		newInputBoxDay.setAttribute("class", "formEntry");
	
		spanElement.appendChild(newInputBoxDay);

		var newInputBoxYear = document.createElement('input');
		newInputBoxYear.size = "3";
		newInputBoxYear.value = "yyyy";
		newInputBoxYear.id = "recurringEndDateYear";
		newInputBoxYear.setAttribute("class", "formEntry");
	
		spanElement.appendChild(newInputBoxYear);

                var newInputBox = document.createElement('input');
                newInputBox.type = "hidden";
		newInputBox.id = "recurringEndDate";
		newInputBox.name = "shopping-cart.items.item-2.subscription.no-charge-after";
                //newInputBoxYear.setAttribute("class", "formEntry");
	
		spanElement.appendChild(newInputBox);

		document.getElementById('switchEndType').value = "Switch to No End Date";
	}
	else {
		var spanElement = document.getElementById('end_date_text');
	
		var inputBoxMonth = document.getElementById('recurringEndDateMonth');
		spanElement.removeChild(inputBoxMonth);

		var inputBoxDay = document.getElementById('recurringEndDateDay');
		spanElement.removeChild(inputBoxDay);

		var inputBoxYear = document.getElementById('recurringEndDateYear');
		spanElement.removeChild(inputBoxYear);

		var inputBox = document.getElementById('recurringEndDate');
		spanElement.removeChild(inputBox);

		var spanElementFirstText = spanElement.lastChild;
		spanElementFirstText.nodeValue = ' with no end date. ';
		
		document.getElementById('switchEndType').value = "Switch to Fixed End Date";
	}
	
}

function newDateOnFocus() {
	var amount = document.getElementById('recurringEndDate');
      if(null == amount.value.match( /^[0-9]+(\.([0-9]+))?$/)){
         amount.value = "";
      }
}

function recurringGiftFormValidation() {
      updateDonationDesc();
      updateDonationValues();
      if (false == validateAmount(document.checkout.recurrent_unit_price)) return false;
      return updateDonationEndDate();
      
}