jQuery.noConflict();

function validateOrder()
{
	cookiesEnabled = _checkCookies();
	
	if( !cookiesEnabled )
	{
		var output = '<p class="red-text">Cookies are required to shop The Vermont Country Store website. It appears that your browser <br/>has cookies disabled. To learn how to enable cookies, please click <a href="http://www.vermontcountrystore.com/vcsinc/Site_Assets/static/cookiesset.html" target="_blank">here</a>.</p>';
    	
    	jQuery( '#Errors_Wrapper' ).html( output );
    	openJSError();
	}
	else
	{
	
		var i = 0;
		var errors = new Array();
	
	    jQuery( '.required-item' ).each( function()
	    {    	
	        if( this.value == 0 || this.value == '' || this.value.indexOf( 'Select' ) >= 0 )
	        {
	        	errors[i] = translateError( jQuery( this ).parent().siblings( '.order-form-label' ).filter( ':first' ).html() );
	        	
	        	if( typeof document.body.style.maxHeight != 'undefined' )
	        	{
	        		jQuery( this ).addClass( 'order-form-error' );
	        	}
	        	
	        	jQuery( this ).parent().siblings( '.order-form-label' ).filter( ':first' ).addClass( 'order-form-error' );
	        	i++;
	        }
	        else
	        {
	       		if( typeof document.body.style.maxHeight != 'undefined' )
	        	{
	        		jQuery( this ).removeClass( 'order-form-error' );
	        	}
	        	
	        	jQuery( this ).parent().siblings( '.order-form-label' ).filter( ':first' ).removeClass( 'order-form-error' );
	        }
	    });
	    
	    if( i == 0 )
	    {
	    	submitForm( 'Product_Order_Form' );
	    }
	    else
	    {
	    	var output = '<ul id="Errors_List">';
	    	
	    	for( var j = 0; j < i; j++ )
	    	{
	    		output += '<li><span class="red-text">' + errors[j] + '</span></li>';
	    	}
	    	
	    	output += '</ul>';
	    	
	    	jQuery( '#Errors_Wrapper' ).html( output );
	    	openJSError();
	    }
	}
}

function validatePhone()
{
	// Validate that input is numeric
	var area = jQuery( '#phone_area' ).val();
									
	if( isNumeric( area ) == false )
	{
		jQuery( '#phone_area' ).val( area.substring( 0, area.length - 1 ) );
	}
	
	var prefix = jQuery( '#phone_prefix' ).val();
									
	if( isNumeric( prefix ) == false )
	{
		jQuery( '#phone_prefix' ).val( prefix.substring( 0, prefix.length - 1 ) );
	}
	
	var body = jQuery( '#phone_body' ).val();
									
	if( isNumeric( body ) == false )
	{
		jQuery( '#phone_body' ).val( body.substring( 0, body.length - 1 ) );
	}
	
	// Combine the phone inputs
	if( jQuery( '#phone_area' ).val() != '' && jQuery( '#phone_prefix' ).val() != '' && jQuery( '#phone_body' ).val() != '' )
	{
		var combined = jQuery( '#phone_area' ).val() + '-' + jQuery( '#phone_prefix' ).val() + '-' + jQuery( '#phone_body' ).val();
		jQuery( 'input[name="phone"]' ).val( combined );
	}
	else
	{
		jQuery( 'input[name="phone"]' ).val( '' );
	}
									
	// Auto focus phone number fields
	if( jQuery( '#phone_area' ).val().length >= 3 && jQuery( '#phone_prefix' ).val().length >= 3 )
	{
		jQuery( '#phone_body' ).focus();
	}
	else if( jQuery( '#phone_area' ).val().length >= 3 )
	{
		jQuery( '#phone_prefix' ).focus();
	}
}

function validateEmail( formID )
{
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = jQuery( '#email' ).val();
	
	if( email.match( regex ) )
	{
		submitForm( formID );
	}
	else
	{
		var output = '<ul id="Errors_List"><li><span class="red-text">You must enter a valid email address.</span></li></ul>';
    	
    	jQuery( '#Errors_Wrapper' ).html( output );
    	openJSError();
	}
}

function validateBillingForm( formID )
{
	//Validate
	var errors = new Array();
	
	//Make sure any supplied email is valid
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = jQuery( '#email' ).val();
	
	if( !( email.match( regex ) ) )
	{
		errors.push( '<li><span class="red-text">You must enter a valid email address.</span></li>' );
	}
	
	//If we don't have a full phone number, throw a fit
	var fullPhone = jQuery( '#phone_area' ).val() + jQuery( '#phone_prefix' ).val() + jQuery( '#phone_body' ).val();
	if( fullPhone.length > 0 && fullPhone.length < 10 )
	{
		errors.push( '<li><span class="red-text">Phone number must be valid.</span></li>' );
	}
	
	//write errors
	var output = '<ul id="Errors_List">'; 
	var numErrors = errors.length;
	for( var i = 0; i < numErrors; i++ )
	{
		output += errors[ i ];
	}
	output += '</ul>';
	
	//Check to see if it validated or not
	if( numErrors > 0 )
	{
		jQuery( '#Errors_Wrapper' ).html( output );
		openJSError();
	}
	else
	{
		submitForm( formID );
	}
}

function submitShippingForm()
{
	//Validate
	var errors = new Array();
	
	//Make sure any supplied email is valid
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = jQuery( '#email' ).val();
	if( email != '' && !( email.match( regex ) ) )
	{
		errors.push( '<li><span class="red-text">Email address must be valid.</span></li>' );	
	}
	
	//If we don't have a full phone number, throw a fit
	var fullPhone = jQuery( '#phone_area' ).val() + jQuery( '#phone_prefix' ).val() + jQuery( '#phone_body' ).val();
	if( fullPhone.length > 0 && fullPhone.length < 10 )
	{
		errors.push( '<li><span class="red-text">Phone number must be valid.</span></li>' );
	}
	
	//write errors
	var output = '<ul id="Errors_List">'; 
	var numErrors = errors.length;
	for( var i = 0; i < numErrors; i++ )
	{
		output += errors[ i ];
	}
	output += '</ul>';
	
	//Check to see if it validated or not
	if( numErrors > 0 )
	{
		jQuery( '#Errors_Wrapper' ).html( output );
		openJSError();
	}
	else
	{
		$('shippingAddressForm').enable();
		submitForm('shippingAddressForm');
	}
}

function translateError( name )
{
	switch( name )
	{
		case 'Colors:':
			return 'You must select colors.';
		case 'Cup:':
			return 'You must select a cup size.';
		case 'Inseam:':
			return 'You must select an inseam.';
		case 'Other:':
			return 'You must complete all required fields.';
		case 'Selection 1:':
			return 'You must make a first selection.';
		case 'Selection 2:':
			return 'You must make a second selection.';
		case 'Selection 3:':
			return 'You must make a third selection.';
		case 'Selection 4:':
			return 'You must make a fourth selection.';
		case 'Selection 5:':
			return 'You must make a fifth selection.';
		case 'Selection 6:':
			return 'You must make a sixth selection.';
		case 'Selection 7:':
			return 'You must make a seventh selection.';
		case 'Selection 8:':
			return 'You must make an eighth selection.';
		case 'Selection 9:':
			return 'You must make a ninth selection.';
		case 'Sizes:':
			return 'You must select sizes.';
		case 'Specify:':
			return 'You must complete all required fields.';
		case 'Styles:':
			return 'You must select styles.';
		default:
			var output = name.replace( ':', '' ).toLowerCase();
			return 'You must select a ' + output + '.';
	}
}

function isNumeric( x )
{
	var validChars = "0123456789";
	var result = true;
	var char;
	
	for( i = 0; i < x.length && result == true; i++ ) 
	{ 
		char = x.charAt( i );
		
		if( validChars.indexOf( char ) == -1 ) 
		{
			result = false;
		}
	}

	return result;
}

function _checkCookies()
{
	var enabled = false;
	if( typeof document.cookie == "string" )
	{
		if( document.cookie.length == 0 )
		{
			document.cookie = "cookieTest";
			enabled = ( document.cookie == "cookieTest" );
		}
		else
		{
			enabled = true;
		}
	}
	
	return enabled;
}

function submitForm( formID )
{
	jQuery( '#' + formID ).submit();
}