﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus1 = 0;

//loading popup with jQuery magic!
function loadPopup1(){
	//loads popup only if it is disabled
	if(popupStatus1==0){
		$("#backgroundPopup1").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup1").fadeIn("slow");
		$("#popupContact1").fadeIn("slow");
		popupStatus1 = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup1(){
	//disables popup only if it is enabled
	if(popupStatus1==1){
		$("#backgroundPopup1").fadeOut("slow");
		$("#popupContact1").fadeOut("slow");
		popupStatus1 = 0;
	}
}

//centering popup
function centerPopup1(){
	//request data for centering
	var windowWidth1 = document.documentElement.clientWidth;
	var windowHeight1 = document.documentElement.clientHeight;
	var popupHeight1 = $("#popupContact1").height();
	var popupWidth1 = $("#popupContact1").width();
	//centering
	$("#popupContact1").css({
		"position": "absolute",
		"top": windowHeight1/2-popupHeight1/2,
		"left": windowWidth1/2-popupWidth1/2
	});
	//only need force for IE6
	
	$("#backgroundPopup1").css({
		"height": windowHeight1
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button1").click(function(){
		//centering with css
		centerPopup1();
		//load popup
		loadPopup1();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose1").click(function(){
		disablePopup1();
	});
	//Click out event!
	$("#backgroundPopup1").click(function(){
		disablePopup1();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup1();
		}
	});

});
$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#tellafriend1").validate({
		rules: {
		    tname:"required",
		    tmessage:"required",
			tfrom:
			{
				required: true,
				email: true
			},
			tto:
			{
				required: true,
				email: true
			}
			
		},
		
		messages: {
		    tname:" ",
		    tmessage:" ",
			tfrom:
			{
				required: " ",
				email: " "
			},
			tto:
			{
				required: " ",
				email: " "
			}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().next() );
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
		        var message = $('#tname').val()+ " has invited you to view \n"+ location.href +"\n\n"+$('#tmessage').val();
		        $('#tcmessage').val(message);
		        disablePopup1();
		        send_mail1('tellafriend1');
				//document.getElementById('contactagent_form').submit();
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});

});
function send_mail1(elm)
{
var action = root + "property/send_mail";
				var dt = $("#tellafriend1").serializeArray();
				$.ajax({
				   type: 'POST',
				   url: action,
				   data: dt,
				   dataType: 'json',
				   success: function(data){
				   disablePopup();
				   },
				   error: function () { 
				   disablePopup1();
				   }
				 });
}
function tellafriend()
{
	$("#tellafriend1").trigger('submit');
}