jQuery(document).ready(function(){
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		jQuery("#dialog").dialog("destroy");
		
        // Inquiry
		var newPropertyName;
		
		var propertyName = jQuery("#propertyName"),
			propertySummary = jQuery("#propertySummary"),
			propertyLocation = jQuery("#propertyLocation"),
			propertyReception = jQuery("#propertyReception"),
			propertyBedroom = jQuery("#propertyBedroom"),
			propertyBathroom = jQuery("#propertyBathroom"),
			propertyType = jQuery("#propertyType"),
			propertyGarden = jQuery("#propertyGarden"),
			propertyStudio = jQuery("#propertyStudio"),
			propertyParking = jQuery("#propertyParking"),
			propertySize = jQuery("#propertySize"),
			propertySale = jQuery("#propertySale"),
			propertyRental = jQuery("#propertyRental"),
            fName = jQuery("#fName"),
		    lName = jQuery("#lName"),
			phoneNo = jQuery("#phoneNo"),
			emailAdd = jQuery("#emailAdd"),
			addStreet = jQuery("#addStreet"),
			addCity = jQuery("#addCity"),
			addZip = jQuery("#addZip"),
			addCountry = jQuery("#addCountry"),
			tips = jQuery(".validateTips");
					
			allFields = jQuery([]).add(fName).add(lName).add(phoneNo).add(emailAdd).add(addStreet).add(addCity).add(addZip).add(addCountry).add(propertyName).add(propertySummary).add(propertyLocation).add(propertyReception).add(propertyBedroom).add(propertyBathroom).add(propertyType).add(propertyGarden).add(propertyStudio).add(propertyParking).add(propertySize).add(propertySale).add(propertyRental);
		
		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-upload").dialog({
			autoOpen: false,
			height: 750,
			width: 700,
			modal: true,
			buttons: {
				'Send': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength2(propertyName,"Property Type",1,80);
					bValid = bValid && checkLength2(propertySummary,"Summary",1,80);
					bValid = bValid && checkLength2(propertyLocation,"Location",1,80);
					bValid = bValid && checkLength2(propertyReception,"No. of Reception Room",1,80);
					bValid = bValid && checkLength2(propertyBedroom,"No. of Bedrooms",1,80);
					bValid = bValid && checkLength2(propertyBathroom,"No. of Bathroom",1,80);
					bValid = bValid && checkLength2(propertyType,"Property Type",1,80);
					bValid = bValid && checkLength2(propertyGarden,"Property Garden",1,80);
					bValid = bValid && checkLength2(propertyStudio,"Studio",1,80);
					bValid = bValid && checkLength2(propertyParking,"Parking",1,80);
					bValid = bValid && checkLength2(propertySize,"Size",1,80);
					bValid = bValid && checkLength2(propertySale,"Purchase Price",1,80);
					bValid = bValid && checkLength2(propertyRental,"Rental Price",1,80);
					
					bValid = bValid && checkLength2(fName,"First Name",1,80);
					bValid = bValid && checkLength2(lName,"Last Name",1,80);
					bValid = bValid && checkLength2(phoneNo,"Phone Number",6,80);
					bValid = bValid && checkLength(emailAdd,"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@righteyedetective.com");
					bValid = bValid && checkLength2(addStreet,"Street",1,80);
					bValid = bValid && checkLength2(addCity,"City",1,80);
					bValid = bValid && checkLength(addZip,"Zip Code",4,10);
					bValid = bValid && checkLength2(addCountry,"Country",1,80);
					
					newPropertyName = fName.val() + '-' + lName.val() + '-' + propertyName.val() + '-' + propertyLocation.val();
					
					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/processUpload.php?fName=" + fName.val() + "&lName=" + lName.val() + "&phoneNo=" + phoneNo.val() + "&emailAdd=" + emailAdd.val() + "&addStreet=" + addStreet.val()+ "&addCity=" + addCity.val()+ "&addZip=" + addZip.val() + "&addCountry=" + addCountry.val() + "&propertyName=" + propertyName.val() + "&propertySummary=" + propertySummary.val() + "&propertyLocation=" + propertyLocation.val() + "&propertyReception=" + propertyReception.val() + "&propertyBedroom=" + propertyBedroom.val() + "&propertyBathroom=" + propertyBathroom.val() + "&propertyType=" + propertyType.val() + "&propertyGarden=" + propertyGarden.val() + "&propertyStudio=" + propertyStudio.val() + "&propertyParking=" + propertyParking.val() + "&propertySize=" + propertySize.val() + "&propertySale=" + propertySale.val() + "&propertyRental=" + propertyRental.val(), true);
						http.onreadystatechange = handleHttpResponseUpload;
						http.send(null);
						//jQuery('#uploadify').uploadifySettings('scriptData' : {'moduleId': jQuery('fName').val() });
						//jQuery('#uploadify').uploadifySettings('scriptData' , : { 'moduleId': jQuery('#fName').val()  });
						//jQuery('#uploadify').uploadifySettings('scriptData', { 'moduleId': jQuery('fName').val() });
						//jQuery('#uploadify').uploadifySettings('scriptData', { 'moduleId': fName.val() });
						//jQuery('#uploadify').uploadifySettings('scriptData', { 'moduleId': newPropertyName });
					}
					
					
				},
				Cancel: function() {
					jQuery(this).dialog('close');
				}
			},
			close: function() {
				emailAdd.val('').removeClass('ui-state-error');
			}
		});
		
	
    // Notification	
	jQuery('#dialog-UploadSuccessNotify').dialog({
		    autoOpen: false,
		    width: 400,
			buttons: {
			    "OK": function() { 
				    jQuery(this).dialog("close"); 
			    } 
			}
	});	
	
	jQuery('#dialog-UploadErrorNotify').dialog({
		    autoOpen: false,
		    width: 400,
			buttons: {
			    "OK": function() { 
				    jQuery(this).dialog("close"); 
			    } 
			}
	});	
	
	
});
