$(document).ready( function()
{

	$(".datepicker").datepicker( {

		changeYear: true,
		showOtherMonths: true,
		selectOtherMonths: true

	});

	initializeFormFieldText();

        $("a[rel=photogroup1]").fancybox( {

                'cyclic' : true,
                'scrolling': 'no',
                'titlePosition' : 'inside',
                'title' : true,
                'type'  : 'image',
                'titleFormat' : function( title ) {

                        if ( title.length > 0 ) {

                                return '<span class="photo-credit">Photo Credit: ' + title + '</span>';

                        }

                        return '';

                }

        });

});

function submitInformationRequest()
{

	if ( $("[name=weddingdate]").val() == "MM/DD/YYYY" ) {

		$("[name=weddingdate]").val( "" );

	}

	validForm = true;

	if ( $("[name=firstname]").val().length == 0 ) {

		validForm = false;

	}

	if ( $("[name=lastname]").val().length == 0 ) {

		validForm = false;

	}

	if ( $("[name=email]").val().length == 0 ) {

		validForm = false;

	}

	if ( $("[name=comment]").val().length > 1000 ) {

		validForm = false;

	}

	if ( validForm == true ) {

		userFields = [];

		userFields.push( "propertyid="   + escape( $("[name=propertyid]").val() ) );
		userFields.push( "firstname="    + escape( $("[name=firstname]").val() ) );
		userFields.push( "lastname="     + escape( $("[name=lastname]").val() ) );
		userFields.push( "email="        + escape( $("[name=email]").val() ) );
		userFields.push( "phone="        + escape( $("[name=phone]").val() ) );
		userFields.push( "weddingdate="  + escape( $("[name=weddingdate]").val() ) );
		userFields.push( "comment="      + escape( $("[name=comment]").val() ) );

		if ( $("[name=flagmultiple]").is(":checked") ) {

			userFields.push( "flagmultiple=true" );

		}

		$.ajax({
			  type: "GET",
			  url: "/helper.php",
			  data: "action=sendrequest&" + userFields.join( "&" ),
			  cache: false,
			  dataType: "json",
			  success: function( data ) {

				if ( data != null ) { 

					if ( data.statusid == 0 ) {

						$("#request-info-message").html( "<p>" + data.message + "</p>" );

					} else {

						$("#request-info-data").html( "<p>" + data.message + "</p>" );

					}
					
				}
			
			  },
			  error: function( request ) {
				  
			  }

		});

	} else {

		$("#request-info-message").html( "<p>Required fields are missing or incomplete.  Please check your information.</p>" );

	}

}

function submitPostcard()
{

	var validForm = true;

	if ( $("[name=sendername]").val().length == 0 ) {

		validForm = false;

	}

	if ( $("[name=senderemail]").val().length == 0 ) {

		validForm = false;

	}

	if ( $("[name=recipientemail]").val().length == 0 ) {

		validForm = false;

	}

	if ( $("[name=message]").val().length > 1000 ) {

		validForm = false;

	}

	if ( validForm == true ) {

		userFields = [];

		userFields.push( "propertyid="     + escape( $("[name=propertyid]").val() ) );
		userFields.push( "sendername="     + escape( $("[name=sendername]").val() ) );
		userFields.push( "senderemail="    + escape( $("[name=senderemail]").val() ) );
		userFields.push( "recipientemail=" + escape( $("[name=recipientemail]").val() ) );
		userFields.push( "message="        + escape( $("[name=message]").val() ) );

		$.ajax({
			  type: "GET",
			  url: "/helper.php",
			  data: "action=sendpostcard&" + userFields.join( "&" ),
			  cache: false,
			  dataType: "json",
			  success: function( data ) {

				if ( data != null ) { 

					if ( data.statusid == 0 ) {

						$("#dialog-window-message").html( "<p>Sending E-Postcard...</p>" );
						setTimeout( 'tb_remove()', 2000 ); 

					} else {

						$("#dialog-window-message").html( "<p>" + data.message + "</p>" );

					}
					
				}
			
			  },
			  error: function( request ) {
				  
			  }

		});

	} else {

		$("#dialog-window-message").html( "<p>Required fields are missing or incomplete.  Please check your information.</p>" );

	}

}

function toggleFavoriteProperty( id ) 
{

	$.ajax({
		type: "GET",
		url: "/helper.php",
		data: "action=togglefavoriteproperty&propertyid=" + id,
		cache: false,
		dataType: "json",
		success: function( data ) {

		if ( data != null ) {

			if ( data.statusid == 1 ) {

				if ( data.actiontype == 0 ) {

					$("#favorite-link").html( "Remove from Favorites" );
					$("#favorite-link").addClass( "remove" );

				} else {

					$("#favorite-link").html( "Add to Favorites" );
					$("#favorite-link").removeClass( "remove" );

				}

			} else if ( data.statusid == 2 ) {

				tb_show( "Login", "/login-warning.html?modal=true&amp;width=400&amp;height=200" );

			}

		}

		},
		error: function( request ) {

		}

	});

}
//	********************************************************************************	Rev 01BH.
//  Validate form data.
//  Return form data string to helper.php.
//	********************************************************************************	Rev 01BH.
function submitInquiry(form) {
	var dialogMsg = "";
	if (form.firstname.value.length < 1) {
		dialogMsg = "First Name is required.";
	}
	if (form.lastname.value.length < 1) {
		dialogMsg = "Last Name is required.";
	}
	if (form.senderemail.value.length < 1) {
		dialogMsg = "A valid Email Address is required.";
	}
	if (form.message.value.length > 1000) {
		dialogMsg = "Your message is limited to 1000 characters.";
	}
	if (dialogMsg.length < 1) {
		userFields = [];
		userFields.push("propertyid="   + escape(form.propertyid.value));
		userFields.push("firstname="    + escape(form.firstname.value));
		userFields.push("lastname="     + escape(form.lastname.value));
		userFields.push("senderemail="  + escape(form.senderemail.value));
		userFields.push("phone="        + escape(form.phone.value));
		userFields.push("weddingdate="  + escape(form.weddingdate.value));
		userFields.push("message="      + escape(form.message.value));
		if (form.flagmultiple.checked) {
			userFields.push("flagmultiple=true");
		}
		$.ajax({
			type: "GET",
			url: "/helper.php",
			data: "action=sendinquiry&" + userFields.join( "&" ),
			cache: false,
			dataType: "json",
			success: function( data ) {
//alert('here-s=');
//				if ( data != null ) { 
//					if ( data.statusid == 0 ) {
						$("#dialog-window-message").html( "<p>Your inquiry has been sent...thank you!</p>" );
						setTimeout( 'tb_remove()', 2000 );
						alert('Your inquiry has been sent...thank you!');
//					} else {
//						$("#dialog-window-message").html( "<p>" + data.message + "</p>" );
//					}
//				}
			},
			error: function( request ) {
//alert('here-e=');
			}
		});
	} else {
		$("#dialog-window-message").html( '<p style="color:#FF0033; font-weight:bold;">' + dialogMsg + '</p>' );
	}
}

