/*
'--------------------------------------------------------------------------------------------------
' Title			: (modal?) Dialog "Request Callback" JavaScript
' Description	: JavaScript function for the pop up form (div) on (for example) the member profile page
'					see php file named similarly in inc folder
'--------------------------------------------------------------------------------------------------
' History
' 1/6/2009	: GF - Created Page
'					some code used from http://jqueryui.com/demos/dialog/#modal-form
'--------------------------------------------------------------------------------------------------
*/

$(document).ready(function(){

	var pricingForMyProject = $('pricing_for_my_project'),
		informationOrIdea = $('information_or_idea'),
		haveContractorsContactYou = $('have_contractors_contact_you'),
		pType = $('p_type'),
		name = $("#name"),
		phone = $("#phone"),
		message = $("#message_back_mt"),
		securityCode = $("#security_code"),
		uEmail = $('u_email'),
		allFields = $([]).add(name).add(phone).add(message).add(securityCode),
		tips = $("#validateTips");

	// Initialize the Request Callback dialog
	$('#QuickInquiryDialog').dialog({
		autoOpen:false,
		modal:true,
		buttons:{
			'Submit Message':function(){

				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"Name",3,60);
				bValid = bValid && checkRegexp(name,/^([0-9a-z ])+$/i,"Name may consist of alphabets and numbers.");

				bValid = bValid && checkLength(phone,"Phone",10,14);
				bValid = bValid && checkRegexp(phone,/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/,"Phone must be a valid US phone number.");

				bValid = bValid && checkLength(message,"Message",5,1000);

				bValid = bValid && checkLength(securityCode ,"Security Code ",5,5);

				/*
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
				bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
				*/

				if (bValid) {
					document.getElementById('request_quick_submitted').value ='Form Submitted!';
					tips.text("Processing...");

					//Submit the form for processing...
					$("form:QuickInquiryDialog").submit();

				}
				else
				{
					//tips.text("No");
				}


			},
			Cancel:function(){
				$(this).dialog('close');
			}
		}
	});
	/*$('#QuickInquiryDialogBtn').click(function() {
		$('#QuickInquiryDialog').dialog('open');
	});*/

	// tips are like instructions
	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			if (min == max)
			{
				updateTips("Length of " + n + " must be "+min+".");
			}
			else
			{
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			}
			return false;
		} else {
			return true;
		}

	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}


});


