var oSoftwareUpdates = function()
{
    var isChecked = false;
    var isEmailValid = false;
    var unsubscribeNode = null;
    var recallPasswordNode = null;

    var publicObject = {

	selectAllProducts: function(categ1, categ2) {
            $("." + categ1).find('input:checkbox').attr('checked','checked');
            $("." + categ2).find('input:checkbox').attr('checked','checked');

	this.updateSubscribeButtonState();
        },

        checkAllCheckboxes: function(categ,elem) {
            if($("#" + elem.id).attr('checked')) {
                $("." + categ).attr('checked','checked');
            } else {
                $("." + categ).removeAttr('checked');
            }
            this.updateSubscribeButtonState();
        },

        checkEmail: function(emailInput) {
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (filter.test($(emailInput).val())) {
                isEmailValid = true;
            } else {
                isEmailValid = false;
            }

            this.updateSubscribeButtonState();
        },

        updateSubscribeButtonState: function() {

            if(isChecked && isEmailValid) {
                $('#subscribeButton').removeAttr('disabled');
            } else {
                $('#subscribeButton').attr('disabled', 'disabled');
            }
        },

        subscribe: function() {

            var postdata = $('#subscribeForm').formSerialize();

            ajax.post('/jcontroller/index.php', postdata + '&subscribe=true&ajaxmethod=subscribeupdates', function(responseText) {
                $('#subscribeButton').removeAttr('disabled');
                $('#subscribeButton').val('SUBSCRIBE');
				
				$.fn.colorbox({'width':'50%', 'height':'150px', 'inline':true, 'href':'#subscribeNotice', 'open':true});
			//	$.fn.colorbox({href:"thankyou.html"});
//                oOverlay.openOverlay($('#subscribeNotice').clone().css('display', 'block'));
            });

            $('#subscribeButton').attr('disabled', 'disabled');
            $('#subscribeButton').val('Please wait...');
        },

        showUnsubscribeForm: function() {
            unsubscribeNode = $('#unsubscribeForm').clone().css('display', 'block');
            oOverlay.openOverlay(null, unsubscribeNode);
        },

        showRecallPasswordForm: function() {
            recallPasswordNode = $('#recallPasswordForm').clone().css('display', 'block');
            oOverlay.openOverlay(null, recallPasswordNode);
        },

        unsubscribe: function(button) {

       		unsubscribeNode = $('#unsubscribeForm');
			var postdata = unsubscribeNode.find('form').formSerialize();

            ajax.post('/jcontroller/index.php', postdata + '&unsubscribe=true&ajaxmethod=subscribeupdates', function(responseText) {
                var form = unsubscribeNode.find('form');
                if(responseText == 1) {

                    var notice = '<div style="text-align:center;margin:55px 0;">You have been unsubscribed.</div>';
                    form.html(notice);

                } else {
                    form.find('input[class="inputText"]').css('border-color', 'red');
                }

                $(button).removeAttr('disabled');
                $(button).val('Unsubscribe');
            });

            $(button).attr('disabled', 'disabled');
            $(button).val('Please wait...');
        },

        recallPassword: function(button) {
			recallPasswordNode = $('#recallPasswordForm');
            var postdata = recallPasswordNode.find('form').formSerialize();
            ajax.post('/jcontroller/index.php', postdata + '&forgot=true&ajaxmethod=subscribeupdates', function(responseText) {
                var form = recallPasswordNode.find('form');
                if(responseText == 1) {

                    var notice = '<div style="text-align:center;margin:38px 0;">New password has sent.</div>';
                    form.html(notice);

                } else {
                    form.find('input[class="inputText"]').css('border-color', 'red');
                }

                $(button).removeAttr('disabled');
                $(button).val('Send new password');
            });

            $(button).attr('disabled', 'disabled');
            $(button).val('Please wait...');
        },
		checkCheckbox: function() {
			isChecked = false;
            $('input[name="product[]"]:checked').each(function() {
                isChecked = true;
            });
		}
    };

    $(document).ready(function() {
		$('#subscribeButton').attr('disable','disable');
        $('input[type="checkbox"]').click(function(){
			publicObject.checkCheckbox();
            publicObject.updateSubscribeButtonState();
		/*	isChecked = false;
            $('input[name="product[]"]:checked').each(function() {
                isChecked = true;
            });
		*/	
        });
	 publicObject.checkCheckbox();	
     publicObject.updateSubscribeButtonState();		
	$("#unsubscribe, #recallpassword").removeAttr('href');
	$("#unsubscribe").colorbox({width:"50%", height:"240px", inline:true, href:"#unsubscribeForm"});	
	$("#recallpassword").colorbox({width:"50%",height:"200px",inline:true, href:"#recallPasswordForm"});	
    });

    return publicObject;

}();
