﻿
var tipafriend;

TipAFriendClass = Class.create({    

    _init: function() {

    },
    show: function() {
        $('#tip-a-friend-container').slideToggle(600);        
        return false;
    },
    
    hide: function() {
        $('#tip-a-friend-container').slideToggle(600);        
        return false;
    },
    validate: function() {
        var _name = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _email = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _friendname = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _friendemail = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _pageUri = new API.Web.UI.Client.Service.Forms.validationValue();
	    
        var failed = function() {alert('tipafriend.validate: Something went wrong');}
	    
	    var succeeded = function(result) {
	    
	        $('#tip-a-friend-errorlist').html('');
	        
	        if( result.length > 0 ){
		        for (var index = 0; index < result.length; ++index) 
			    {   
				    var item = result[index];	
				    switch (item.property) {
				        case 'name':
				            $('#tip-your-name label').addClass('error');
				            break;
				        case 'email':
				            $('#tip-your-email label').addClass('error');
				            break;
				        case 'friendname':
				            $('#tip-friend-name label').addClass('error');
				            break;
				        case 'friendemail':
				            $('#tip-friend-email label').addClass('error');
				            break;
				    }
				    $('#tip-a-friend-errorlist').append('<li>'+item.error+'</li>');				    
			    }
			    $('#tip-a-friend-error').slideDown(400);
		    }
		    else
		    {
		        $('#tip-a-friend-error').slideUp(400);
		        tipafriend.send();
			}
	    }    
	    
	    $('#tip-your-name label').removeClass('error');
        $('#tip-your-email label').removeClass('error');
        $('#tip-friend-name label').removeClass('error');
        $('#tip-friend-email label').removeClass('error');
            
        _name.property = 'name';
	    _name.value = $('#tip-your-name input').val();

        _email.property = 'email';
	    _email.value = $('#tip-your-email input').val();
	    
	    _friendname.property = 'friendname';
	    _friendname.value = $('#tip-friend-name input').val();

        _friendemail.property = 'friendemail';
	    _friendemail.value = $('#tip-friend-email input').val();
	    
	    _pageUri.property = 'pageuri';
	    if (popup.popIsActive) {
	        _pageUri.value = popup.pageUri;
	    } else {
	        _pageUri.value = document.location.href;
	    }

	    var _formArray = [_name, _email, _friendname, _friendemail, _pageUri];
	    
	    API.Web.UI.Client.Service.Forms.FormHandler.validateForm('tipafriend', _formArray, succeeded, failed)
    	
	    return false;
        
    },
    
    send: function() {
    
        var _name = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _email = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _friendname = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _friendemail = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _pageUri = new API.Web.UI.Client.Service.Forms.validationValue();
	    
	    var failed = function() {alert('tipafriend.send: Something went wrong');}
	    
	    var succeeded = function(result) { 
	        
    		$('#tip-a-friend-container').slideUp(600); 
		    
		    $('#tip-your-name input').val('');
            $('#tip-your-email input').val('');
	        $('#tip-friend-name input').val('');
            $('#tip-friend-email input').val('');
	    }
   
        _name.property = 'name';
	    _name.value = $('#tip-your-name input').val();

        _email.property = 'email';
	    _email.value = $('#tip-your-email input').val();
	    
	    _friendname.property = 'friendname';
	    _friendname.value = $('#tip-friend-name input').val();

        _friendemail.property = 'friendemail';
	    _friendemail.value = $('#tip-friend-email input').val();
	    
        _pageUri.property = 'pageuri';
	    if (popup.popIsActive) {
	        _pageUri.value = popup.pageUri;
	    } else {
	        _pageUri.value = document.location.href;
	    }
	    var _formArray = [_name, _email, _friendname, _friendemail, _pageUri];
	    
	    API.Web.UI.Client.Service.Forms.FormHandler.sendForm('tipafriend', _formArray, succeeded, failed)
    }   
    
});

$(document).ready(function() {
    if (!tipafriend) {
        tipafriend = new TipAFriendClass();
        tipafriend._init();
    }    
});


