/*------------ A small plugin to clear the form fields that is used on the form---*/
/*-------------Currently we clear only text and password fields -----------------*/
jQuery(function($) {
	$.fn.clearForm = function() {
		return this.each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();			
			if (tag == 'form')			
				return $(':input', this).removeClass('error').clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';												
			// else if (type == 'checkbox' || type == 'radio')
				// this.checked = false;
				// else if (tag == 'select')
				// this.selectedIndex = -1;
			//tweaking the code to remove label with error fields 
			//that get generated coz of validation plugin
			$('label').remove('.error');
			});
	}
});
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*------------------------For Submitting Detail side Forms ----------------------------*/
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
function formSubmit(formID, displayMssgID) {
	jQuery(function($) {
		var CDT = '&' + getTimeStamp();
		var qstring = $('#' + formID).serialize() + CDT;
		var val = $('#' + formID).validate( {
			rules : {
				Mobile : {
					number : true,
					minlength : 8
				}
			}
		}).form();
		if (val) {
			$('#' + displayMssgID)
					.html(
							'<div class="process_img"> Registering your Request ... Please Wait ...</div> ');
			$.ajax( {
				url : "index.php",
				data : qstring,
				success : function(data) {
					$('#' + displayMssgID).html('');
					$('#' + displayMssgID).append(data);					
					window.setTimeout(function() {
						$('#' + displayMssgID).html('');
						$(".ui-dialog-content").dialog("close");
					}, 4000);
				}
			});
		}
	});
}

/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*----------------------------For Lisitng Req Forms-------------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/

jQuery(function($) {
	$(document).ready(function() {
		$('a.sitevisit').click(function(e) {
			e.preventDefault();
			var prpID = $(this).attr("rel");
			var prpName = $(this).attr("name");
			var dialogID = 'prpReqShow';
			var formID = 'frmReqShow';
			var messageID = 'displayMessage';
			openReqShowDialog(dialogID,formID,prpID,prpName,messageID);
		});
		
		//Locality Browser site visit form.
		$('a.prpsitevisit').click(function(e) {
			e.preventDefault();
			var prpID = $(this).attr("rel");
			var prpName = $(this).attr("name");
			var dialogID = 'propertyReqShow';
			var formID = 'frmPrpReqShow';
			var messageID = 'displayPrpMessage';
			openReqShowDialog(dialogID,formID,prpID,prpName,messageID);			
		});
		
		$('a.prjsitevisit').click(function(e) {
			e.preventDefault();
			var prpID = $(this).attr("rel");
			var prpName = $(this).attr("name");
			var dialogID = 'projectReqShow';
			var formID = 'frmPrjReqShow';
			var messageID = 'displayPrjMessage';
			openReqShowDialog(dialogID,formID,prpID,prpName,messageID);			
		});
		//jquery for text-box onclick value change
		$('.default-value').each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if (this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if (this.value == '') {
					this.value = default_value;
				}
			});
		});
		
	});
});

function openReqShowDialog(dialogID,formID,prpID,prpName,messageID){
	jQuery(function($) {
		// intializing the dialog box
		$("#prpID").val(prpID);
		$("#prpName").val(prpName);
		var Title = $("#"+formID).attr("title");
		var dialogBox = $("#" + dialogID).dialog( {
			autoOpen : false,
			height : 300,
			width : 650,
			modal : true,
			title : Title,
			buttons : {
				Submit : function() {
					formSubmit(formID, messageID);
				},
				Cancel : function() {
					$(this).dialog("close");
				}
			},
			open : function() {				
				$("#"+formID).clearForm();
			}
		});

		// instatiating the dialog box;
		dialogBox.dialog('open');
	});
}
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*----------------------------Clear Form Fields on Reset--------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
function resetFormFields(formID){
	jQuery(function($) {
			$('#'+formID).clearForm();
	});
}

/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*----------------------------Get Time Stamp Function--------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/

function getTimeStamp() {
		var currentTime = new Date();
		var day = currentTime.getDate();
		var month = currentTime.getMonth() + 1;		
		var year = currentTime.getFullYear();
		var hours = currentTime.getHours() + 1;
		var miniutes = currentTime.getMinutes() + 1;
		var seconds = currentTime.getSeconds() + 1;
		return (day + "-" + month + "-" + "-" + year + " " + hours + ":" + miniutes + ":" + seconds);
}
