$(document).ready(function(){
	/********resume upload*******/
	$('#resume-uploadbutton').click( function() {
		$('#resumeupload').show(300);
		return false;
	});
	$('#secondchance-button').click( function() {
		$('#secondchanceform').show(300);
		return false;
	});
	/********end resume upload*******/
	
	
	$('#goback').click( function() {
		$('#donate-slider').stop().animate({ "left" : "0px"}, 250);
		//$('#goback').parents("span:first").remove();
	});
	
   $("a.contributelink1").hover( function() {
		$('#donatecoins').stop().animate({ "top" : "-10px"}, 250);
		}, function() {
			$('#donatecoins').stop().animate({ "top" : "0px"}, 150);
		});
   
   $("a.contributelink2").hover( function() {
		$('#shabbatgifts').stop().animate({ "top" : "-10px"}, 250);
		}, function() {
			$('#shabbatgifts').stop().animate({ "top" : "0px"}, 150);
		});
   
   $("a.contributelink3").hover( function() {
		$('#volhands').stop().animate({ "top" : "-10px"}, 250);
		}, function() {
			$('#volhands').stop().animate({ "top" : "0px"}, 150);
		});
   
   
   $("ul.services li a[href|=#babies]").click( function() {
		$("#babies").toggle("slow");
		return false;
	
	});
   
   $("ul.services li a[href|=#clothing]").click( function() {
		$("#clothing").toggle("slow");
		return false;
	
	});
   
   $("ul.services li a[href|=#services]").click( function() {
		$("#services").toggle("slow");
		return false;
	
	});
   
  $("ul.services li a[href|=#education]").click( function() {
		$("#education").toggle("slow");
		return false;
	
	});

  $("ul.services li a[href|=#financialaid]").click( function() {
		$("#financial").toggle("slow");
		return false;
	
	});
  
  $("ul.services li a[href|=#home]").click( function() {
		$("#home").toggle("slow");
		return false;
	
	});
   
   $("ul.services li a[href|=#legal]").click( function() {
		$("#legal").toggle("slow");
		return false;
	
	});
   
   $("ul.services li a[href|=#occasions]").click( function() {
		$("#occassions").toggle("slow");
		return false;
	
	});
   
   
   
   $('#donatecc').click( function() {
		runningCheckFormVals();
		runningCheckCCVals();
		$('#donate-slider').stop().animate({ "left" : "0px"}, 250);
		$('#donation-page').slideToggle(750);
		return false;
		
	});
   
  	
   $('#next1').click( function() {
		checkForValues();
		return false;
	});

   //Add suport for enter button - Andrew E. Oct 24, 2011
   $("#next1").keypress(function(e){
       if(e.which == 13){
           checkForValues();
           e.preventDefault();
        }
    });

   $('#next2').click( function() {
       next2Action();
		return false;
	});

   //Add suport for enter button - Andrew E. Oct 24, 2011
   $("#next2").keypress(function(e){
       if(e.which == 13){
           next2Action();
           e.preventDefault();
        }
    });

   function next2Action(){
    checkForCCinfo();
    //checkForDonateNumber();
    $('#donateamount').keyup(function() {
        if($('#donateamount').val() != "") {
            $('#senddonationbutt').removeAttr("disabled");
        } else { 
            $('#senddonationbutt').attr('disabled', 'disabled');
        } });
    
    //return false;
    $('#donateamount').click(function() {
        if($('#donateamount').val() != "") {
            $('#senddonationbutt').removeAttr("disabled");
        } else { 
            $('#senddonationbutt').attr('disabled', 'disabled');
        } });
   }
	
	$('#senddonationbutt').click( function() {
        sendDonation();
		return false;									   
	});

   //Add suport for enter button - Andrew E. Oct 24, 2011
   $("#senddonationbutt").keypress(function(e){
       if(e.which == 13){
           sendDonation();
           e.preventDefault();
        }
    });

    function sendDonation(){
		//$(this).fadeOut(250);
		
		$('#paymentresponse').html('<img src="http://tomcheishabbos.org/wp-content/themes/touchofkindness/images/toc-loading-ani2.gif" alt="loading" />').show();
		$('#paymentbuttons').hide();
		
		/*$('#cart').show(300, function() {


$('#loading').fadeIn("slow", function() {
		$('#loading').delay(875).slideDown(300).fadeOut(300, function() {
			$('#cartform').slideUp(300).fadeIn(300, function() {
				$('#cardsubmit').show();
				//$('.closethis').css("display", "block");
				$('.closethis').slideDown("fast");
			});	
			
	});				 
});		
});*/
		var data = [];
		data.push({ name: 'recurring_onetime', value: ($('#onetime').attr('checked') == true) ? 1 : 0 });
		data.push({ name: 'recurring_recurring', value: ($('#recurring').attr('checked') == true) ? 1 : 0 });
		var recurwhen = '';
		if($('#recurweekly').attr('checked') == true) recurwhen = 'weekly';
			else if($('#recurmonthly').attr('checked') == true) recurwhen = 'monthly';
				else if($('#recurquarterly').attr('checked') == true) recurwhen = 'quarterly';
					else if($('#recurannually').attr('checked') == true) recurwhen = 'yearly';
		data.push({ name: 'recurring_interval', value: recurwhen });
		if(($('#donateregular').length == 1) && ($('#donateinhonor').length == 1) && ($('#donateinmemory').length == 1))
		{
			data.push({ name: 'donation_general', value: ($('#donateregular').attr('checked') == true) ? 1 : 0 });
			data.push({ name: 'donation_inhonor', value: ($('#donateinhonor').attr('checked') == true) ? 1 : 0 });
			data.push({ name: 'donation_inmemory', value: ($('#donateinmemory').attr('checked') == true) ? 1 : 0 });
		} else {
			data.push({ name: 'donation_inhonor', value: 1 });
			data.push({ name: 'donation_hospitality', value: 1 });
		}
		data.push({ name: 'PHPSESSID', value: session_id });
		
		$.ajax(
		{
			type: 'POST'
			, url: '/wp-content/themes/touchofkindness/donate.ajax.php'
			, data: $('#donateform').serialize() + '&' + $.param(data)
			, dataType: 'json'
			, success: function(result)
			{
				if(typeof(result.status) == "undefined")
				{
					$('#paymentresponse').html('<div style="color: red; font-weight: bold; margin-bottom: 20px">Error encountered while processing the payment request</div>').show();
					$('#paymentbuttons').show();
				} else {
					switch(result.status)
					{
						case 'ok':
								// $('#paymentresponse').html('<div style="color: red; font-weight: bold;">' + result.message + '</div>').show();
								window.location = '../../thank-you/';
								
								break;
						case 'error':
								$('#paymentresponse').html('<div style="color: red; font-weight: bold; margin-bottom: 20px">' + result.message + '</div>').show();
								$('#paymentbuttons').show();
								$('#goback').click( function() {
									$('#donate-slider').stop().animate({ "left" : "0px"}, 250);						 
								});
								
								break;
					}
				}
			}
			, error: function()
			{
				$('#paymentresponse').html('<div style="color: red; font-weight: bold; margin-bottom: 10px">Error encountered while processing the payment request</div>').show();
				$('#paymentbuttons').show();
			}						
		});
    }


   
   $('#recurring').click( function() {
		$('#recurringfrequency').fadeIn(250);				   
	});
   $('#onetime').click( function() {
		$('#recurringfrequency').fadeOut(250);				   
	});
   $('#donateregular').click( function() {
		$('#inhonorofbox').slideUp(250);
		$('#inmemoryofbox').slideUp(250);			   
	});
   $('#donateinhonor').click( function() {
		$('#inhonorofbox').slideDown(250);
		$('#inmemoryofbox').slideUp(250);			   
	});
   $('#donateinmemory').click( function() {
		$('#inhonorofbox').slideUp(250);
		$('#inmemoryofbox').slideDown(250);
	});
   
   $('#ZipCode').numeric();
   $('#CreditCardNumber').numeric();
   $('#CardVerificationNumber').numeric();
   $('#donateamount').numeric();
   
   //$('#donateamount')
});





function slideSwitch() { /**** homepage slideshow ****/
    var $active = $('#slides img.activeslide');

    if ( $active.length == 0 ) $active = $('#slides img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slides img:first');

    $active.addClass('last-activeslide');

    $next.css({opacity: 0.0})
        .addClass('activeslide')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('activeslide last-activeslide');
        });
	}
	
	$(function() {
		setInterval( "slideSwitch()", 5000 );
	});
/**** end slideshow ****/

function contactForm() {
	if($('#formname').val() == "") {
		window.alert("Please make sure you enter your name");
		//$('#partrequestbox').val("Required");
		$('#formname').focus();
	
		return false;
	}
	else if($('#phone').val() == "") {
		window.alert("Please make sure you enter your phone number");
		//$('#partrequestbox').val("Required");
		$('#phone').focus();
	
		return false;
	}
	else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#email').val()))) {
		window.alert("Please make sure you entered a valid Email Address");
		$('#email').focus();
		return false;
	}
	else {
		$('#submitbutton').attr('disabled', 'disabled');
		$('#contactform p').slideUp('slow', function() {
		$('#loading-ani').fadeIn(300).css('display','block');
		
			//var submitval = $('#submitbutton').val();
			var formname = $('#formname').val();
			var phone = $('#phone').val();
			var email = $('#email').val();
			var message = $('#message').val();

			$.ajax({
				type: 'POST',
				url: 'http://tomcheishabbos.org/wp-content/themes/touchofkindness/contact-send.php',
				data: 'formname=' + formname + '&phone=' + phone + '&email=' + email + '&message=' + message,
				
				success: function(result) {
						$('#loading-ani').fadeOut(300, function() {
							$('#response').empty().append(''+ result + '');
							$('#response').fadeIn(250);									  
						});								
					}
						
					
				});

			
			
									
		});
		
		return false;
					
	
	}
	
} /******end contactForm function*****/

function checkForValues() {
	if($('#fullName').val() == "") {
		window.alert("Please make sure you enter the full name on your credit card");
		$('#fullName').focus();
	
		return false;
	}
	else if($('#Address').val() == "") {
		window.alert("Please make sure you enter the correct address associated with your credit card");
		$('#Address').focus();
	
		return false;
	}
	else if($('#City').val() == "") {
		window.alert("Please make sure you enter the correct city associated with your credit card");
		$('#City').focus();
	
		return false;
	}
	else if($('#State').val() == "0") {
		window.alert("Please select the correct state associated with your credit card");
		$('#State').focus();
	
		return false;
	}
	else if($('#ZipCode').val() == "") {
		window.alert("Please make sure you enter the correct Zip Code");
		$('#ZipCode').focus();
	
		return false;
	}
	else if($('#PhoneNumber').val() == "") {
		window.alert("Please make sure you enter the correct phone number associated with your credit card");
		$('#PhoneNumber').focus();
	
		return false;
	}
	else if($('#City').val() == "") {
		window.alert("Please make sure you enter a city");
		$('#City').focus();
	
		return false;
	}
	else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#EmailAddress').val()))) {
		window.alert("Please make sure you enter a valid e-mail address");
		$('#EmailAddress').focus();
	}
	else {
		$('#donate-slider').stop().animate({ "left" : "-=740px"}, 250, function(){$("#cardType").focus();});
	}
	
}

function runningCheckFormVals() {
	$('#billinginfo').keyup(function() {
		if(($('#fullName').val() != "") && ($('#Address').val() != "") && ($('#City').val() != "") && ($('#State').val() != "0") && ($('#ZipCode').val() != "") && ($('#PhoneNumber').val() != "") && (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#EmailAddress').val())))  {
			$('#next1 img').attr("src", "http://tomcheishabbos.org/wp-content/themes/touchofkindness/images/toc-form-next1-active.jpg");
		}
		else {
			$('#next1 img').attr("src", "http://tomcheishabbos.org/wp-content/themes/touchofkindness/images/toc-form-next1-dim.jpg");
		}
	});
}
function runningCheckCCVals() {
	$('#paymentmethod').keyup(function() {
		if(($('#cardType').val() != "") && ($('#CreditCardNumber').val() > 15) && ($('#ExpirationMonth').val() != "") && ($('#ExpirationYear').val() != "") && ($('#CardVerificationNumber').val().length > 2))  {
			$('#next2 img').attr("src", "http://tomcheishabbos.org/wp-content/themes/touchofkindness/images/toc-form-next1-active.jpg");
		}
		else {
			$('#next2 img').attr("src", "http://tomcheishabbos.org/wp-content/themes/touchofkindness/images/toc-form-next1-dim.jpg");
		}
	});
}


function checkForCCinfo() {
	if($('#cardType').val() == "") {
		window.alert("Please select a credit card type");
		$('#cardType').focus();
	
		return false;
	}
	else if($('#CreditCardNumber').val().length < 16) {
		window.alert("Please make sure you enter a correct credit card number");
		$('#CreditCardNumber').focus();
	
		return false;
	}
	else if($('#ExpirationMonth').val() == "") {
		window.alert("Please select an expiration month");
		$('#ExpirationMonth').focus();
	}
	else if($('#ExpirationYear').val() == "") {
		window.alert("Please select an expiration year");
		$('#ExpirationYear').focus();
	}
	else if($('#CardVerificationNumber').val().length < 3) {
		window.alert("Please enter a correct CVV code");
		$('#CardVerificationNumber').focus();
	} else {
		$('#donate-slider').stop().animate({ "left" : "-=740px"}, 250, function(){$("#donateamount").focus()});
	}
}
/*function checkForDonateNumber() {
	
}*/
/*****************end donate form value check**************/
jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
			return (
				(key == 8)
				||
				(key == 9)
				||
				(key == 16)
				||
				(key == 46)
				||
				((key >= 48) && (key <= 57))
			);
        })
    })
};

(function($) {
/*
 * Allows only valid characters to be entered into input boxes.
 * Note: does not validate that the final text is a valid number
 * (that could be done by another script, or server-side)
 *
 * @name     numeric
 * @param    decimal      Decimal separator (e.g. '.' or ',' - default is '.'). Pass false for integers
 * @param    callback     A function that runs if the number is not valid (fires onblur)
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $(".numeric").numeric();
 * @example  $(".numeric").numeric(",");
 * @example  $(".numeric").numeric(null, callback);
 *
 */
$.fn.numeric = function(decimal, callback)
{
	decimal = (decimal === false) ? "" : decimal || ".";
	callback = typeof callback == "function" ? callback : function(){};
	return this.data("numeric.decimal", decimal).data("numeric.callback", callback).keypress($.fn.numeric.keypress).blur($.fn.numeric.blur);
}

$.fn.numeric.keypress = function(e)
{
	var decimal = $.data(this, "numeric.decimal");
	var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
	// allow enter/return key (only when in an input box)
	if(key == 13 && this.nodeName.toLowerCase() == "input")
	{
		return true;
	}
	else if(key == 13)
	{
		return false;
	}
	var allow = false;
	// allow Ctrl+A
	if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
	// allow Ctrl+X (cut)
	if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
	// allow Ctrl+C (copy)
	if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
	// allow Ctrl+Z (undo)
	if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
	// allow or deny Ctrl+V (paste), Shift+Ins
	if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
	|| (e.shiftKey && key == 45)) return true;
	// if a number was not pressed
	if(key < 48 || key > 57)
	{
		/* '-' only allowed at start */
		if(key == 45 && this.value.length == 0) return true;
		/* only one decimal separator allowed */
		if(decimal && key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
		{
			allow = false;
		}
		// check for other keys that have special purposes
		if(
			key != 8 /* backspace */ &&
			key != 9 /* tab */ &&
			key != 13 /* enter */ &&
			key != 35 /* end */ &&
			key != 36 /* home */ &&
			key != 37 /* left */ &&
			key != 39 /* right */ &&
			key != 46 /* del */
		)
		{
			allow = false;
		}
		else
		{
			// for detecting special keys (listed above)
			// IE does not support 'charCode' and ignores them in keypress anyway
			if(typeof e.charCode != "undefined")
			{
				// special keys have 'keyCode' and 'which' the same (e.g. backspace)
				if(e.keyCode == e.which && e.which != 0)
				{
					allow = true;
					// . and delete share the same code, don't allow . (will be set to true later if it is the decimal point)
					if(e.which == 46) allow = false;
				}
				// or keyCode != 0 and 'charCode'/'which' = 0
				else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
				{
					allow = true;
				}
			}
		}
		// if key pressed is the decimal and it is not already in the field
		if(decimal && key == decimal.charCodeAt(0))
		{
			if(this.value.indexOf(decimal) == -1)
			{
				allow = true;
			}
			else
			{
				allow = false;
			}
		}
	}
	else
	{
		allow = true;
	}
	return allow;
}

$.fn.numeric.blur = function()
{
	var decimal = $.data(this, "numeric.decimal");
	var callback = $.data(this, "numeric.callback");
	var val = $(this).val();
	if(val != "")
	{
		var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
		if(!re.exec(val))
		{
			callback.apply(this);
		}
	}
}

$.fn.removeNumeric = function()
{
	return this.data("numeric.decimal", null).data("numeric.callback", null).unbind("keypress", $.fn.numeric.keypress).unbind("blur", $.fn.numeric.blur);
}

})(jQuery);

