﻿// Templates

Templates = {
    currentUrl: window.location.href.split('?')[0],
    position: 0,
    categoryId: 0,
    occasionId: 0,
    searchString: "",
    theme: 0,
    
    processSelection: function(position, count, ecardOnly, einviteOnly) {
        
        if ($('input_ddlCategory_id'))
        {
            var categoryElement = $($('input_ddlCategory_id').value);
            Templates.categoryId = categoryElement.options[categoryElement.selectedIndex].value;
        }
        if ($('input_ddlOccasion_id'))
        {
            var occasionElement = $($('input_ddlOccasion_id').value); 
            Templates.occasionId = occasionElement.options[occasionElement.selectedIndex].value;
        }
        if ($('input_search'))
        {
            var searchElement = $($('input_search').value);
            Templates.searchString = searchElement.value;
        }
        if ($('theme_id'))
        {
            Templates.theme = $('theme_id').value;
        }
        if (position)
        {
            Templates.position = position;
        }
		
        // Create the new url
        var newLocation = Templates.currentUrl + "?position=" + Templates.position;

        if(Templates.occasionId != 0){
            newLocation += '&occasion=' + Templates.occasionId;
        }
        if(Templates.categoryId != 0){
            newLocation += '&category=' + Templates.categoryId;
        }
        if(count && count != 0) {
            newLocation += '&count=' + count;
        }
        if(Templates.searchString != "") {
            newLocation += '&search=' + Templates.searchString;
        }
        if(Templates.theme != 0) {
            newLocation += '&theme=' + Templates.theme;
        }
        if(ecardOnly === 1) {
            newLocation += '&invites=false';
        }
        if(einviteOnly === 1) {
            newLocation += '&invites=true';
        }    
        window.location = newLocation;
    },
    
    tagItemAsFavourite : function(itemId) {
        
        if ( $('input_member_logged_in') && $('input_member_logged_in').value == 1 )
        {
            Templates.favourites = $('input_member_favourites').value.split(",");
    	    var index 		= Templates.favourites.indexOf(itemId);
		    var checkBox	= $("chkbox_" + itemId);
		    var checked		= checkBox.checked;
		    var tempId		= itemId;

		    // initialize ajax request
		    new Ajax.Request(
			    "/webservice/ajax/template.asmx/AddToFavourite",
			    {
				    method: "get",
				    parameters: { template_id: itemId },
				    onSuccess: function(transport) {

					    var response = new Ajax.Response(transport);

					    // evaluate response
					    if (!response.empty) {
					    	var sStatus = response.xml.firstChild.nodeValue;

						    // modify favourite templates array
						    if (!sStatus.empty() && sStatus != 0) {
						    	if (checked && index == -1) {
							    	Templates.favourites.push(tempId);
							    } else if (!checked && index != -1) {
								    Templates.favourites.splice(index, 1);
						    	}
						    } else {
						    	alert("You cannot tag any more invites as favourites");
						    	checkBox.checked = false;
					    	}
				    	}

			    	} // end function: onSuccess
			    }
		    ) // end object: Ajax.Request
		}
	},
	
    templateSelect: function(itemId) {
    
        var query = window.location.href.split('?')[1];
        if (query)
        {
            location.href = window.location.href + "&template_id=" + itemId;
        }
        else
        {
            location.href = window.location.href + "?template_id=" + itemId;
        }                       
	}
}


