var c_participants = 0;
var g_participants = 0;
var valid;
var first;

function setGolfParticipants(newVal){
	g_participants = newVal;
	calculateCosts();
}

function setConfParticipants(newVal){
	c_participants = newVal;
	calculateCosts();
	
	//  Enable or Disable the Additional Registrants Fields
	if (newVal == 2)
		$('.additional1').attr('disabled', false);
	else if (newVal >= 3)
		$('.additional1, .additional2').attr('disabled', false);
	else {
		$('.additional1, .additional2').attr('disabled', true).val('');
		$('.additional1.checkbox, .additional2.checkbox').attr('checked', false);
	}
}

function calculateCosts(){
	var c_cost = c_participants * 395.00;
	var g_cost = g_participants * 125.00;
	var total_fees = c_cost + g_cost;
	$('#conf_fee').text('Conference: $' + c_cost.toFixed(2));
	$('#golf_fee').text('Golf: $' + g_cost.toFixed(2));
	$('#c_total').text('Total: $' + total_fees.toFixed(2));
	$('#AMOUNT').val(total_fees.toFixed(2));
	$('#DESCRIPTION').val('# Attending Conference: ' + c_participants + ' | ' + c_cost.toFixed(2) + ' # Golfing : ' + g_participants + ' | ' + g_cost.toFixed(2));
}

function verifyInfo(){
        clearWhiteSpace();
        $(".required").css('color', 'black');
        var errMsg = "Missing Required Information";
		valid = true;
		first = true;
		$('.required').each(
			function () {
		        var item = '';
		        var iVal = new String();
				iVal = $("#"+$(this).attr('for')).val();
				iVal = iVal.trim();
				switch($(this).attr('for')){
					case 'EMAIL':
						if(! validEmail(iVal)){
							valid = false;
							$(this).css('color', '#E80000');
							if(first){
								$("#"+$(this).attr('for')).focus();
								first = false;
							}
						}
						break;
					case 'PHONE':
						if(! validPhone(iVal)){
							valid = false;
							$(this).css('color', '#E80000');
							if(first){
								$("#"+$(this).attr('for')).focus();
								first = false;
							}
						}
						break;
					default:
						if(iVal.length == 0 || iVal == 0){
                            valid = false;
                            $(this).css('color', '#E80000');
							if(first){
								$("#"+$(this).attr('for')).focus();
								first = false;
							}
                    	}
                    	break;
				}
			});
			if(! valid){ alert(errMsg);}
			return valid;
}


String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function validPhone(phone){
	var p_pattern = /^(\(\d{3}\)|\d{3})(-{0,1}|\s{0,1})(\d{3})(-{0,1}|\s{0,1})(\d{4})$/;
	return phone.match(p_pattern);
}

function clearWhiteSpace(){
	var txt, i;
	txt = document.getElementsByTagName('input');
	for(i=0; i<txt.length; i++){
		if(txt[i].type == 'text' && txt[i].value.match(/^\s*$/)){
			txt[i].value = '';
		}
	}
}

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        if(charCode == 32 || (charCode >= 44 && charCode <= 46)){
            return true;
        }
        else{
            return false;
        }
    }
    return true;
}

function validEmail(email){
    var tValid = true;
    if((email.length < 8) ||
        ((email.length>0) && (! email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi)))){
        tValid = false;
        if(first){
            first = false;
            glbElem = email;
        }
    }
    return tValid;
}

