var owc_validation = {
	is_valid	: true,
	exclude_ids	: [
	'reg_pnr',
	'reg_coaddress',
	'newsletter',
	'ship_to_same_address',
	'reg_shi_coaddress'
	],
	email_ids : [
		'reg_email'
	],
	invalid_class	: 'invalid-field',
	invalid_el_class	: 'invalid-msg',
	init: function() {
		
		$('#normal_proceed').click(function(e) {
			
			owc_validation.reset();
			
			$('#ShippingInformation tr').filter(':visible').find('input:not(.no_validation)').each(function() {
				
				if ( $(this).attr('type') != 'hidden' ) { // Exclude hidden inputs
					if ( $(this).attr('id') in owc_helpers.object_converter( owc_validation.exclude_ids ) == false ) { // Exclude excluded inputs
						
						var is_email = $(this).attr('id') in owc_helpers.object_converter( owc_validation.email_ids );
						var val = $(this).val();
						
						if ( $(this).is(':checkbox') ) {
							if ( $(this).is(':checked') ) {
								val = 'on';
							} else {
								val = '';
							}
						}
						
						owc_validation.validate( val, is_email, $(this) );
						
					}
				}
				
			}).focus(function() {
				owc_validation.reset_input( $(this) );
			}).click(function() {
				owc_validation.reset_input( $(this) );
			}).end().find('select').filter(':visible').each(function() {
				
				var val = $(this).val();
				if ( val == '0' ) {
					val = '';
				}
				
				owc_validation.validate( val, false, $(this) );
				
			}).change(function() {
				owc_validation.reset_input( $(this) );
			});
			
			
			
			if ( owc_validation.is_valid == false ) {
				owc_validation.fail(e);
			}
			
		});
		
		$('#step2 .payment').click(function() {
			owc_validation.reset();
		});
		
	},
	validate: function( val, is_email, _this ) {
		
		if ( val == '' ) {
			this.is_valid = false;
			
			var label_txt = 'You have to enter your ' + _this.parent().siblings('th').text().replace(' *:', '').replace(' 1', '').toLowerCase();
			
			if ( _this.attr('id') == 'agree' ) {
				label_txt = 'You have to accept the terms and conditions';
			}
			
			_this.addClass( this.invalid_class ).parent().append('<div class="' + this.invalid_el_class + '">' + label_txt + '</div>');
			
		} else if ( is_email ) {
			if ( owc_helpers.validate_email( val ) == false ) {
				this.is_valid = false;
				_this.addClass( this.invalid_class ).parent().append('<div class="' + this.invalid_el_class + '">Your e-mail address is not filled in correctly</div>');
			}
		}
		
	},
	fail: function(e) {
		e.preventDefault();
	},
	reset: function() {
		owc_validation.is_valid = true;
		$('#submit_form input').removeClass( this.invalid_class );
		$('.' + this.invalid_el_class ).remove();
	},
	reset_input: function(_this) {
		_this.removeClass(owc_validation.invalid_class).parent().children('.' + this.invalid_el_class).remove();
	}
};

var owc_helpers = {
	validate_email: function(input){  
		var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		return pattern.test(input);  
	},
	is_touch: function(){
		return (
			(navigator.platform.indexOf("iPhone") != -1) ||
			(navigator.platform.indexOf("iPod") != -1) ||
			(navigator.platform.indexOf("iPad") != -1)
		);
	},
	create_cookie: function(name,value,days) {
		var expires;
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		} else {
			expires = "";
		}
		document.cookie = name+"="+value+expires+"; path=/";
	},
	read_cookie: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for ( var i=0;i < ca.length;i++ ) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	erase_cookie: function(name) {
		this.create_cookie(name,"",-1);
	},
	object_converter: function(arr) {
		var obj = {};
		for( var i = 0; i < arr.length; i++ ) {
			obj[arr[i]]='';
		}
		return obj;
	}
};

$(function() {
	owc_validation.init();
});
