// JavaScript Document
$(document).ready(function(){
	$('#addtobag').click(function(){
		var isQuantity = false;
		 $('.quantity').each(function(){
		 	var qty = this.value;
			if (!isNaN(qty) &&  parseInt(qty) > 0) {
				isQuantity =  true;
			}
		 	
		 });
		 if (!isQuantity){
             alert('Please provide quantity for at least one item');
            $('.quantity')[0].focus();
         }
		 return isQuantity;
		
	});
	
});
$(document).ready(function(){ 
$(".emailform").validate({
	rules : {
		email: {
				required: true,
				email: true
			}
	}
});
});
$(document).ready(function(){
	$('.digits').numberInput();
	// validate signup form on keyup and submit
	$(".cmxform").validate({
		rules: {
			chkterms : "required",
			firstname: "required",
			lastname: "required",
			address: "required",
			State: {
				required: function() {return $.trim($('#Country').val()) == 'US' }
				},
			city: {
				required: true,
				minlength: 2
			},
			zip: {
				required: true,
				minlength: 5
			},
			phone: {
				required: true,
				minlength: 5
			},
			email: {
				required: true,
				email: true
			},
			ccnum : {
				required:true,
				digits:true,
				//creditcard: true
				creditcard2: function(){ return $('#cctype').val(); }
			},
			cvvcode : "required",
			'.digits': {
            digits: true
			}

		},
		messages: {
			firstname: "Please enter First your Name",
			lastname: "Please enter Last your Name",
			address: "Please enter your address",
			State: "Please select State",
			city: {
				required: "Please enter a City",
				minlength: "Please enter valid city name"
			},
			phone: {
				required: "Please provide a phone",
				minlength: "Please provid 10 digit phone number starting with area code"
			},
			zip: {
				required: "Please provide a Zip code",
				minlength: "Your Zip code must be at least 5 characters long"
				
			},
			ccnum : {
				required:"Please provide a credit card number",
				creditcard:"Please enter a valid credit card number or select correct Card type"
			},
			chkterms: "Please check the terms and conditions",
			email: "Please enter a valid email address",
			vehicletype: "Please select a vehicle type"
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent() );
		}

	});					   
});
// prepare the form when the DOM is ready 
$(document).ready(function() { 
    var options = { 
        target:        '#resultdiv',   
        beforeSubmit:  showRequest,  
        success:       showResponse,  	
        
        url:       'processpaymentajax.cfm'       
  
    }; 
 
    // bind to the form's submit event 
    $('#frmc').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
}); 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' ); 
	$('#resultdiv').html('<h1 style="font-size:1.5em;padding:5em 1em 3em 2em;" class="aligncenter">Please wait while we process your request.</h1><p style="padding:1em 1em 3em 10em;"  class="aligncenter"><img src="/common/images/activity_indicator.gif" alt="" /></p><h1 style="font-size:1.5em;padding:5em 1em 3em 2em;" class="aligncenter">Processing may take 30-60 seconds.</h1>');
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
   // alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
     //   '\n\nThe output div should have already been updated with the responseText.'); 
}
function showError(data)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 $('#resultdiv').html("Error occured: " + 'status: ' + data.statusText + '<br />responseText: <br />' + data.responseText );
   alert('status: ' + data.statusText + '\n\nresponseText: \n' + data.responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
}