/* jquery js for the callback form
 */

var browserName=navigator.appName; 
var browserVer=navigator.appVersion;
// alert(browserName);
// alert(browserVer);
$(document).ready(function() {

//	$("#callbackform").dialog("destroy");

	// link to  open callback dialog
	$('a#callback').click(function() {
		  $.get('callbackform.html', function(data){
			  var dialog = $('<div>').attr('id','formDialog').attr('style', 'display:none').html($(data).find('form:last').parent('div').html());
			  $('body').append(dialog);
			  dialog.find(':submit').hide();
			  // dialog form for callback
				  dialog.dialog({
					    title: 'Callback Request',
					    bgiframe: true,
						modal: true,
						autoOpen: false,
						draggable: true,
						height: 'auto',
						width: '450px',
						buttons: {
							'Submit': function() {
								var name = $("#callbackname"),
								email = $("#email"),
								tel = $("#tel"),
								allFields = $([]).add(name).add(email).add(tel),
								tips = $(".validateTips");
								
		
								// functions
								function updateTips(t) {
									tips.text(t).addClass('ui-state-highlight');
								setTimeout(function() {
									tips.removeClass('ui-state-highlight', 1500);
									}, 500);
								}
		
								function checkLength(o,n,min,max) {
									if ( o.val().length > max || o.val().length < min ) {
										o.addClass('ui-state-error');
								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;
									}
								}					  
					  			var bValid = true;
								allFields.removeClass('ui-state-error');
								
						//		bValid = bValid && checkLength(tel,"tel",5,25);
								bValid = bValid && checkLength(name,"username",3,16);
								bValid = bValid && checkLength(email,"email",0,80);
					
						//		bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Please fill out your name.");
								// 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. info@rages.co.uk");
						//		bValid = bValid && checkRegexp(tel, /[\(\)\.\-\ ]/g,"The phone number contains illegal characters.\n" );
								
								//if valid input data, save callback info to db by sending form values to the AJAX file 
								// also send email to customer & IPC
								if (bValid) {
									var form = $(this).find('form');
									var data = form.serialize();

									// insert data into db
									$.post("AJAXinsertCallbackData.php", data, function(response) {
									});
									// send emails
									$.post("send_callback_email.php", data, function(response) {							
										alert(response);	
									});

									$(this).dialog('close');
									dialog.dialog("destroy");
								}
							},		
							'Cancel': function() {
								$(this).dialog('close');
								}
							},
							close: function() {
								$(this).remove();
						//		allFields.val('').removeClass('ui-state-error');
							}
						});

		  	}, 'html');
		  
		  $('#formDialog').dialog('open');

			if (isIE7) {
				window.scrollTo(0,0);
			//	alert('IE7');
			}
	  });


}); // end document ready function


