var $j = jQuery.noConflict(); 

// prepare the form when the DOM is ready 
$j(document).ready(function() {
	//Options array					   
	var options = { 
		target: '#output',
		beforeSubmit:  validate,  // pre-submit callback 
		success: showResponse,    // post-submit callback 
		resetForm: true,         // reset the form after successful submit 
		//-- other available options: 
		//url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		dataType:  'script',        // 'xml', 'script', or 'json' (expected server response type) 
		//clearForm: true        // clear all form fields after successful submit 
		//resetForm: true        // reset the form after successful submit
	
		// $j.ajax options can be used here too, for example: 
        timeout:   3000
	};
	
	
	
    // bind form using ajaxForm 
    $j('#wholesaleForm').ajaxForm(options);
});
 
//success function				
function showResponse(responseText, statusText){
	$j("#content #stylized form ul li #contentLoading").animate({ opacity: "hide" }, "slow");
	$j('.success, #confirm').slideToggle();
	$j('.success, #confirm').animate({ opacity: "show" }, "slow");
	//$j('html, body').animate({scrollTop: $j("#formEnd").offset().top}, 500);
	//alert('showResponse() Was Called!');
	
}

// pre-submit callback
function validate(formData, jqForm, options) {
	$j("p.error").animate({ opacity: "hide" }, "slow");
	$j("p#confirm").animate({ opacity: "hide" }, "slow");
	$j("#content #stylized form ul li #contentLoading").animate({ opacity: "show" }, "fast");

	//alert('validation just ran');
	//Required Fields
	var wrong_ID = $j('input[name=login_id]').fieldValue(); 
	var coNameValue = $j('input[name=company]').fieldValue();
	var ownerValue = $j('input[name=owner_name]').fieldValue();
	var taxValue = $j('input[name=tax_number]').fieldValue();
	
	//compare email address to this regex
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var correct = true;
	
	// formData is an array; here we use $j.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 = $j.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); 
	
	//validate fields
	if (!wrong_ID[0]) {
		$j("p.error.wrong_id").animate({ opacity: "show" }, "slow");
		$j("#stylized input#login_id").css({border:"1px dashed red"});
		correct = false;
	}else{
		$j("#stylized  input#login_id").css({border:"1px solid #AACFE4"});
	}
	//
	if (!coNameValue[0]) {
		$j("p.error.wrong_co_name").animate({ opacity: "show" }, "slow");
		$j("#stylized input#company").css({border:"1px dashed red"});
		correct = false;
	}else{
		$j("#stylized  input#company").css({border:"1px solid #AACFE4"});
	}
	//
	if (!ownerValue[0]) {
		$j("p.error.wrong_owner_name").animate({ opacity: "show" }, "slow");
		$j("#stylized input#owner_name").css({border:"1px dashed red"});
		correct = false;
	}else{
		$j("#stylized  input#owner_name").css({border:"1px solid #AACFE4"});
	}
	//
	if (!taxValue[0]) {
		$j("p.error.wrong_tax_num").animate({ opacity: "show" }, "slow");
		$j("#stylized input#tax_number").css({border:"1px dashed red"});
		correct = false;
	}else{
		$j("#stylized  input#tax_number").css({border:"1px solid #AACFE4"});
	}
	//alert('Correct Value: ' + correct);
	//
	if (!correct) {
		//if there are errors
		$j('html, body').animate({scrollTop: $j("#content").offset().top}, 1000);
		$j("#content #stylized form ul li #contentLoading").animate({ opacity: "hide" }, "fast");
		//alert('Correct Value: ' + correct);
		return false;
	}else{
		return true;
			
	}
	//alert('Correct Value: ' + correct);
} 	

//Hide the success message when you click it		
$j("p#success,p.notice").click( function () { 
	$j(this).animate({ opacity: "hide" }, "slow"); 
});	
