// Form Validation
// Add classname 'requiredField' or 'requiredEmail' to any form element. Use the title attributes as the required alert messages and class 'hightlight' to adjust the style.
// Required: jQuery

$(document).ready(function() {
    if(document.getElementById('support_left') || document.getElementById('send_friend_form')){
	$('.requiredField').blur(function() {
		customVal_checkRequired(this);
	});
	$('.requiredEmail').blur(function() {
		customVal_checkEmail(this);
	});
	
	/* Donation Amount */
	$('input[name="donationother"]').attr('disabled','disabled');
	$('input[name="donationother"]').css('background-color','#ddd');
	$('.donate').click(function() {
		$('input[name="donationother"]').attr('disabled','disabled');
		$('input[name="donationother"]').css('background-color','#ddd');
		$('input[name="donationother"]').removeClass('highlight');
		$('.donationAlert').html("");
	});
	if(document.getElementById('support_left')){
    	$('input[name="donationother"]')[0].onkeydown = function(event){
    	    var e = event;
		    if(!e) {e = window.event;}
		    if(!e.which) {e = e.keyCode;}
		    else if(e.which){e = e.which};
		    return checkMoneyValue(e);
	    }
	}
	$('.other').click(function() {
		$('input[name="donationother"]').removeAttr('disabled','disabled');
		$('input[name="donationother"]').css('background-color','#fff');
		$('input[name="donationother"]').focus();
	});	
	/*----End Donation */
	
	/* Gift */
	$('input[name="per_month"]').attr('disabled','disabled');
	$('input[name="per_month"]').css('background-color','#ddd');
	$('input[value="recurring_gift"]').click(function() {
		$('input[name="per_month"]').removeAttr('disabled','disabled');
		$('input[name="per_month"]').css('background-color','#fff');
		$('.giftAlert').html("");
		$('input[name="per_month"]').removeClass('highlight');
		$('input[name="per_month"]').focus();
	});
	$('input[value="one_time_gift"]').click(function() {
		$('input[name="per_month"]').attr('disabled','disabled');
		$('input[name="per_month"]').css('background-color','#ddd');
		$('.giftAlert').html("");
		$('input[name="per_month"]').removeClass('highlight');
	});
	if(document.getElementById('support_left')){
    	$('input[name="per_month"]')[0].onkeydown = function(event){
	    	var e = event;
		    if(!e) {e = window.event;}
		    if(!e.which) {e = e.keyCode;}
		    else if(e.which){e = e.which};
		    return checkNumeric(e);
	    }
	    /*----End Gift */
	
	    /*----Zip Code */
	    $('input[name="zip"]')[0].onkeydown = function(event){
	        var e = event;
		    if(!e) {e = window.event;}
		    if(!e.which) {e = e.keyCode;}
		    else if(e.which){e = e.which};
		    //return checkZip(e);
		    return true;
	       }
	    /*----End Zip Code */
	
	    /*----CC Number */
	    $('input[name="cc_number"]')[0].onkeydown = function(event){
	        var e = event;
		    if(!e) {e = window.event;}
		    if(!e.which) {e = e.keyCode;}
		    else if(e.which){e = e.which};
		    return checkCCnum(e);
	    }
	    /*----End CC Number */
	
	    /*----CC Security Code */
	    $('input[name="cc_securitycode"]')[0].onkeydown = function(event){
	        var e = event;
		    if(!e) {e = window.event;}
		    if(!e.which) {e = e.keyCode;}
		    else if(e.which){e = e.which};
		    return checkZip(e);
	    }
	    /*----End CC Security Code */
	}
	}
});

var customVal = { suffix: "_validate" };
var RadioError = true;

function checkCCnum(e){
	var code = e;
	if(code >= 48 && code <= 57 || code == 32 || code == 109 || code == 8 || code == 9) {
		RadioError = false;
		return true;
	}
	else {
		RadioError = true;
		return false;
	}
}
function checkZip(e){
	var code = e;
	if(code >= 48 && code <= 57 || code == 8 || code == 9) {
		RadioError = false;
		return true;
	}
	else {
		RadioError = true;
		return false;
	}
}
function checkNumeric(e){
	var code = e;
	if(code >= 48 && code <= 57 || code == 8 || code == 9) {
		RadioError = false;
		return true;
	}
	else {
		RadioError = true;
		return false;
	}
}
function checkMoneyValue(e) {
	var code = e;
	if(code >= 48 && code <= 57 || code == 190 || code == 8 || code == 9) {
		RadioError = false;
		return true;
	}
	else {
		RadioError = true;
		return false;
	}
}

function customVal_submit() {
	var count = 0;
	var modalActive = $('.modal').css('display');
	if (modalActive == 'block'){
	    var requiredField = $('.modal .requiredField');
	    for(var x=0;x<requiredField.length;x++) {
	    	if(!customVal_checkRequired(requiredField[x])) {
	    		count++;
	    	}
	    }
	    var requiredEmail = $('.modal .requiredEmail');
	    for(var x=0;x<requiredEmail.length;x++) {
	    	if(!customVal_checkEmail(requiredEmail[x])) {
	    		count++;
	    	}
	    }
	}   
	if (modalActive != 'block') {
	    var requiredField = $('.requiredField');
	    for(var x=0;x<requiredField.length;x++) {
	    	if(!customVal_checkRequired(requiredField[x])) {
	    		count++;
	    	}
	    }
	    var requiredEmail = $('.requiredEmail');
	    for(var x=0;x<requiredEmail.length;x++) {
	    	if(!customVal_checkEmail(requiredEmail[x])) {
	    		count++;
	    	}
	    }
	}
	if(modalActive != 'block'){
	    var requiredRadio = $('.requiredRadio');
	    var requiredEmail = $('#support_right .requiredEmail');
	    if(requiredRadio[0].checked == true || requiredRadio[1].checked == true || requiredRadio[2].checked == true) {
		    for(var i=0; requiredRadio.length>i; i++){
			    var fieldClass = requiredRadio[i].className;
			    if(fieldClass.match("other")){
			    	if(requiredRadio[i].checked){
			    		var inputValue = $('input[name="donationother"]').val();
			    		if(inputValue == "" || inputValue == null){
			    			$('input[name="donationother"]').addClass('highlight');
			    			count++;
			    		}
			    		else {
			    			$('input[name="donationother"]').removeClass('highlight');
			    		}
			    	}		
			    }
	    	}
	    }
	    else if(modalActive != 'block'){
	    	$('.donationAlert').html("Please select an amount");
	    	count++;
	    }
	    for(var x=0;x<requiredEmail.length;x++) {
	    	if(!customVal_checkEmail(requiredEmail[x])) {
	    		count++;
	    	}
	    }
	}
    if(modalActive != 'block'){
	var requiredGiftRadio = $('.giftRadio');
	if(requiredGiftRadio[0].checked == true || requiredGiftRadio[1].checked == true) {
		var recurGift = $('input[value="recurring_gift"]')[0];
		if (recurGift.checked) {
			var perMonthInput = $('input[name="per_month"]').val();
			if(perMonthInput == null || perMonthInput == ""){
				$('input[name="per_month"]').addClass('highlight');
				count++;
			}
			else {
				$('input[name="per_month"]').removeClass('highlight');
				$('.giftAlert').html("");
			}
		}
	}
	else if(modalActive != 'block'){
		$('.giftAlert').html("Please select gift type");
		count++;
	}
	}
	if(count > 0) {
		return false;
	}
	
	else {
	    var modalActive = $('.modal').css('display');
	    if(modalActive == 'block'){
    	    runForm();
	        return false;
	    }
	    else {
	        return true;
	    }
	//	return true;
	} 
}

function customVal_checkRequired(field) {

if(!field.disabled  /*&& field.id.length > 0 */) {
	
	var reqFieldName = field.id + customVal.suffix;
	if(field.tagName == "INPUT") {
		if(field.type == "text" || field.type == "password" || field.type == "hidden") {
			if($.trim(field.value).length > 0) {
				$(field).removeClass('highlight');
				if(document.getElementById(reqFieldName)) {
					document.getElementById(reqFieldName).innerHTML = "";
				}
				return true;
			}
			else {
				$(field).addClass('highlight');
				if(field.title.length > 0) {
					if(document.getElementById(reqFieldName)) {
						document.getElementById(reqFieldName).innerHTML = field.title;
					}
					else {
						$(field).after('<span id="'+reqFieldName+'" class="required">'+field.title+'</span>');
					}
				}
				return false;
			}
		}
		if(field.type == "checkbox") {
			if(field.checked) {
				if(document.getElementById(reqFieldName)) {
					document.getElementById(reqFieldName).innerHTML = "";
				}
				return true;
			}
			else {
				if(field.title.length > 0) {
					if(document.getElementById(reqFieldName)) {
						document.getElementById(reqFieldName).innerHTML = field.title;
					}
					else {
						$(field).after('<span id="'+reqFieldName+'" class="required">'+field.title+'</span>');
					}
				}
				return false;
			}
		}
	}
	if(field.tagName == "SELECT") {	
		if(field.selectedIndex > 0) {
			$(field).removeClass('highlight');
			if(document.getElementById(reqFieldName)) {
				document.getElementById(reqFieldName).innerHTML = "";
			}
			return true;
		}
		else {
			$(field).addClass('highlight');
			if(field.title.length > 0) {
				if(document.getElementById(reqFieldName)) {
					document.getElementById(reqFieldName).innerHTML = field.title;
				}
				else {
					$(field).after('<span id="'+reqFieldName+'" class="required">'+field.title+'</span>');
				}
			}
			return false;
		}
	}
}
else {
	return true;
}
}

function customVal_checkEmail(field) {
if(!field.disabled /*&& field.id.length > 0*/) {
	var reqFieldName = field.id + customVal.suffix;
	var emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/ig;
	if(field.value.match(emailRegex)) {
		$(field).removeClass('highlight');
		if(document.getElementById(reqFieldName)) {
			document.getElementById(reqFieldName).innerHTML = "";
		}
		return true;
	}
	else {
		$(field).addClass('highlight');
		if(document.getElementById(reqFieldName)) {
			document.getElementById(reqFieldName).innerHTML = field.title;
		}
		else {
			$(field).after('<span id="'+reqFieldName+'" class="required">'+field.title+'</span>');
		}
		return false;
	}
}
else {
	return true;
}
}
