jQuery(document).ready(function(){
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		jQuery("#dialog-register").dialog("destroy");
		
		jQuery( "#datePicker" ).datepicker();
		
        // Inquiry
		var fName = jQuery("#fName"),
		    lName = jQuery("#lName"),
			emailAdd = jQuery("#emailAdd"),
			confirmedEmailAdd = jQuery("#confirmedEmailAdd"),
			userPassword = jQuery("#userPassword"),
			confirmedUserPassword = jQuery("#confirmedUserPassword"),
			phoneNo = jQuery("#phoneNo"),
			addStreet = jQuery("#addStreet"),
			addCity = jQuery("#addCity"),
			addZip = jQuery("#addZip"),
			addCountry = jQuery("#addCountry"),
			propertyType = jQuery("#propertyType"),
			prefLocation = jQuery("#prefLocation"),
			prefRoom = jQuery("#prefRoom"),
			prefBath = jQuery("#prefBath"),
			prefRentalPrice = jQuery("#prefRentalPrice"),
			dateArrival = jQuery("#dateArrival"),
			numberNights = jQuery("#numberNights"),
			numberAdult = jQuery("#numberAdult"),
			numberChildren = jQuery("#numberChildren"),
			purchasePrice = jQuery("#purchasePrice"),
			message = jQuery("#message"),
			tips = jQuery(".validateTips");
					
			allFields = jQuery([]).add(fName).add(lName).add(emailAdd).add(confirmedEmailAdd).add(userPassword).add(confirmedUserPassword).add(phoneNo).add(addStreet).add(addCity).add(addCity).add(addCountry).add(propertyType).add(prefRoom).add(prefBath).add(prefRentalPrice).add(prefLocation).add(
			).add(numberNights).add(numberAdult).add(numberChildren).add(purchasePrice).add(message);
		
		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 checkLength2(o,n,min,max) {
            if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("" + n + " is required. Please provide.");
				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;
			}

		}
		
		
		function checkEquality(x,y,a,b) {
            if (x.val()!=a.val()) {
			   x.addClass('ui-state-error');
			   a.addClass('ui-state-error');
			   updateTips("The " + y + " and " + b + " should be equal.");
			   return false;
			} else {
			   return true;
			}
        }
		   
		
		jQuery("#dialog-register").dialog({
			autoOpen: false,
			height: 750,
			width: 700,
			modal: true,
			buttons: {
				'Send': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength2(fName,"First Name",1,80);
					bValid = bValid && checkLength2(lName,"Last Name",1,80);
					bValid = bValid && checkLength2(emailAdd,"Email address",6,80);
					bValid = bValid && checkLength2(confirmedEmailAdd,"Confired Email address",6,80);
					bValid = bValid && checkRegexp(emailAdd,/^((([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,"Invalid Format! eg. info@gstaadbusiness.com");
					bValid = bValid && checkEquality(emailAdd,"Email address", confirmedEmailAdd,"Confired Email address");
					bValid = bValid && checkLength(userPassword,"Password",6,80);
					bValid = bValid && checkLength(confirmedUserPassword,"Confirmed Password",6,80);
					bValid = bValid && checkEquality(userPassword,"Password", confirmedUserPassword,"Confired Password");
					bValid = bValid && checkLength2(phoneNo,"Phone Number",6,80);
					bValid = bValid && checkLength2(addStreet,"Address",1,80);
					bValid = bValid && checkLength2(addCity,"City",1,80);
					bValid = bValid && checkLength(addZip,"Zip Code",4,10);
					bValid = bValid && checkLength2(addCountry,"Country",1,80);
					bValid = bValid && checkLength2(propertyType,"Property Type",1,80);
					bValid = bValid && checkLength2(prefLocation,"Property Location",1,80);
					bValid = bValid && checkLength2(prefRoom,"Preferred Number of Rooms",1,80);
					bValid = bValid && checkLength2(prefBath,"Preferred Number of Bathroom",1,80);
					bValid = bValid && checkLength2(prefRentalPrice,"Preferred Rental Price",1,80);
					bValid = bValid && checkLength2(dateArrival,"Date of Arrival",1,80);
					bValid = bValid && checkLength2(numberNights,"Number of Nights",1,80);
					bValid = bValid && checkLength2(numberAdult,"Number of Adults",1,80);
					bValid = bValid && checkLength2(numberChildren,"Number of Children",1,80);
					bValid = bValid && checkLength2(numberNights,"Number of Nights",1,80);
					bValid = bValid && checkLength2(purchasePrice,"Preferred Purchase Price",1,80);
					bValid = bValid && checkLength(message,"Message or Comments",4,500);
					
					if (bValid) {
//newsmailer hook
newsmailer_hook(emailAdd.val(),addStreet.val()+", "+addCity.val()+", "+addCountry.val(),"",fName.val()+" "+lName.val(),phoneNo.val());



						
						http.open("GET", "_codebehind/process/processRegistration.php?fName=" + fName.val() + "&lName=" + lName.val() + "&emailAdd=" + emailAdd.val() + "&confirmedEmailAdd=" + confirmedEmailAdd.val() + "&userPassword=" + userPassword.val() + "&confirmedUserPassword=" + confirmedUserPassword.val() + "&phoneNo=" + phoneNo.val() + "&addStreet=" + addStreet.val() + "&addCity=" + addCity.val() + "&addZip=" + addZip.val() + "&addCountry=" + addCountry.val() + "&propertyType=" + propertyType.val() + "&prefRoom=" + prefRoom.val() + "&prefBath=" + prefBath.val() + "&prefRentalPrice=" + prefRentalPrice.val() + "&dateArrival=" + dateArrival.val() + "&numberNights=" + numberNights.val() + "&numberAdult=" + numberAdult.val() + "&numberChildren=" + numberChildren.val() + "&purchasePrice=" + purchasePrice.val() + "&message=" + message.val(), true);
						http.onreadystatechange = handleHttpResponseRegister;
						http.send(null); 
					}
					
					
				},
				Cancel: function() {
					jQuery(this).dialog('close');
				}
			},
			close: function() {
				emailAdd.val('').removeClass('ui-state-error');
			}
		});
		
	
    // Notification	
	jQuery('#dialog-RegisterSuccessNotify').dialog({
		    autoOpen: false,
		    width: 400,
			buttons: {
			    "OK": function() { 
				    jQuery(this).dialog("close"); 
			    } 
			}
	});	
	
	jQuery('#dialog-RegisterErrorNotify').dialog({
		    autoOpen: false,
		    width: 400,
			buttons: {
			    "OK": function() { 
				    jQuery(this).dialog("close"); 
			    } 
			}
	});	
	
	
});
