jQuery(document).ready( function($) {

$(".ajax-loader").ajaxStart(function(){// show the animated gif when loading ajax
   $(this).show();
 });

$(".ajax-loader").ajaxStop(function(){// hide the animated gif
   $(this).hide();
 });

$('input.required').blur(function(){
	// check each of the required fields for a value and if there is one, activate the submit button
	var dovalidate=0;// initialize the variable
	var checked=0;
	$('.required').each(function(){
		checked=checked+1;
		if($(this).val()==''){
			// this one is ready
			$(this).css({'border':'2px solid red'});
			dovalidate=dovalidate+1;
		}
		else{
			$(this).css({'border':'1px solid #CCCCCC'});
		}
	});

	if(dovalidate==0){
		// activate the submit button
		$('#submitblock').html("<input class='customsubmit submit' type='submit' value='Send' >");
//		$('#submitblock').css({'display':'block'});
		$('#diagnostics').html('form ready').css({'color':'green'});
	}


});
/*	var options = {
        target: '#output2',   // target element(s) to be updated with server response
        beforeSubmit:  showRequest,  // pre-submit callback
       // success:       showResponse  // post-submit callback
	   success: processPHPResponse   // call the function to process data
	}
 */
//	$("input:radio[@name=uploadfile]").click(function() {
//	    var checkedvalue = $(this).val();


 	var v=$("#commentForm").validate({
    	rules:{
    		csr: {
    			required:true
			},
    		name_first: "required",
			name_last: "required",
			address1: "required",
			phone1: "required",
			city: "required",
			state: "required",
			zipcode: "required",
			email: {
				required: true,
				email: true
			}

		},
		messages:{
			csr: "Select your CRM",
   			name_first: "First name required",
			name_last: "Last name required",
			address1: "Address required",
			phone1: "Phone required",
			city: "City required",
			state: "State required",
			zipcode: "Zip code required",
			email: "Email required"


		}	,
		submitHandler: function(form) {
				$('#commentForm').ajaxSubmit({
					target: "#output2",
					resetForm: true,
				 	success:processPHPResponse

				}
				);

			}

 		 });


/*
		// show a simple loading indicator
		var loader = $('<div id="loader"><img src="http://banyanprinting.com/wp-content/plugins/contact-form-7/images/ajax-loader.gif" alt="loading..." /></div>')
			.css({position: "relative", top: "1em", left: "25em"})
			.appendTo("body")
			.hide();
		$().ajaxStart(function() {
			loader.show();
		}).ajaxStop(function() {
			loader.hide();
		}).ajaxError(function(a, b, e) {
			throw e;
		});

		var v = $("#commentForm").validate({
			submitHandler: function(form) {
				$('#commentForm').ajaxSubmit({
					target: "#output2"
				});
			}
		});
*/
//	});
});
// 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' + queryString);

    // 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 processPHPResponse(responseText){
//   	document.location="http://banyanprinting.com/?page_id=10";
if (responseText == "redirect") {
if(confirm("You are about to be transferred to our file upload page. Click OK to continue to the file upload page or click Cancel to abort the file upload process."))
	{
		Set_Cookie('banyanjob','1','','/','','');
		window.location="/?page_id=10";}
                }


}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


