var invalid = [];
var BAR_WIDTH = 274;

$(document).ready(function () {
	//$(":button").bind('click', validate_page);
});

function validate_page() {

    //Find correct element to check properties within. Form will be submitting to this
    //now instead of an individual container.
    var num = get_current_screen_nbr();
    //debugger;
    var elNextBtn = get_current_next_button(num);
    //if ($(elNextBtn.target).hasClass('disabled')) {
    if ($(elNextBtn).hasClass('disabled')) {
	    var text = "";
	    $.each(invalid, function(i, el) {
		    text += validate({target:el});
		    if(invalid.indexOf(el) >= 0) {
			    $(el).siblings("label").addClass('invalid');
			    $(el).addClass('invalid');
		    }
	    });
	    alert(text);
	    return false;
    }
    var form = $(elNextBtn.target).parents('div.form-div:first');
    var data = new Object;
    $.each( $(form).find(':input:not(:button):not(:radio)') , function(i, el) {
	    data[el.name] = $(el).val();
    });
	//alert('returning true');
	return true;
}

/*
SCREEN ANIMAINTION
*/

function handler( data ) {
	return;
	// Set the name
	var name = false;
	if (name = $('#first_name').val()) {
		$('#welcome_name').html("Welcome to the club " + escape(name).replace(/%20/g, ' ') + "!");
	}
	
	// Show product selection
	if (data['products']) {
		var recs = "";
		$.each(data['products'], function(key, value) {
			recs += "<li>" + value + "</li>\n";
		});
		$("#product_recommendations").html(recs);
	}
}

function initialize( form ) {
	if (!$(form).find('.btn.nxt')) return;
	$($(form).find('.btn.nxt')).addClass('disabled');

	invalid = [];
	var validators = []; 
	$.each( $(form).find(':input:not(:button):not(:hidden):not(:radio)') , function(i, el) {
		$(el).bind('keyup', validate);
		$(el).bind('blur', validate);
		validate({target: el});
	});

	if (invalid.length == 0) {
		$($(form).find('.btn.nxt')).removeClass('disabled');
	}
}

function validate (el) {
	el = el.target;
	if ($(el).hasClass('required')) {
		return validate_required(el);
	}
	if ($(el).hasClass('zip')) {
		return validate_zip(el);
	}
	if ($(el).hasClass('email')) {
		return validate_email(el);
	}
	if ($(el).hasClass('creditcard')) {
		return validate_creditcard(el);
	}	
}

function validate_email( e ) {
	if (e.value && e.value.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)) {
		pass( e )
	}
	else {
		invalidate( e );
		return "Please provide a valid email address.\n";
	}
}

function validate_creditcard(e) {


    var elPmtOptCC = document.getElementById('rdoPmtOptCC');
	if ((!elPmtOptCC) || (elPmtOptCC.checked == true))
	{
	    if (e.value && e.value.match(/\d{4}-?\d{4}-?\d{4}-?\d{4}/))
	    {
		    pass( e )
	    }
	    else {
		    invalidate( e );
		    return "Please provide a valid credit card number.\n";
	    }
	}
	else
	{
	    pass( e )
	}
}

function validate_required( e ) {
	if (e.value && !e.value.match(/^[ ]*$/)) 
	{
		pass( e)
	}
	else {
		invalidate( e);
		var name = $(e).siblings("label").html().replace(/\*/, '');
		return name + " is a required field.\n";
	}
}

function validate_zip( e ) {
	if (e.value && e.value.match(/^\d{5}$|^\d{5}-\d{4}$/)) {
		pass( e )
	}
	else {
		invalidate( e );
		return "Please provide a valid zip code.\n";
	}
}

function invalidate(el) {
	var index = invalid.indexOf(el);
	if (index < 0) invalid.push(el);
	if (invalid.length > 0) {
		$(el).parents('div.form-div:first').find('.btn.nxt').addClass('disabled');
	}
}

function pass(el) {
	var index = invalid.indexOf(el);
	if (index >= 0) invalid.splice(invalid.indexOf(el), 1);
	if (invalid.length == 0) {
		$(el).parents('div.form-div:first').find('.btn.nxt').removeClass('disabled');
	}
	$(el).removeClass('invalid');
}

Array.prototype.indexOf = function( el ) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == el) {
			return i;
		}
	}
	return -1;
}
