function hideAllAccounts() {
    $('#editLoyaltyAccountForm').hide();
    $('#outer3').hide();
    $('#emailBox').hide();
    $('#mobilePhoneBox').hide();
    $('#homePhoneBox').hide();
    $('#loyaltyBox').hide();
    $('#memberidBox').hide();
}

function resetDropDown() {
    $('#accountType').val('');
    hideAllAccounts();
}

function loadLoyaltyAccountForm(loyaltyId) {
    hideAllAccounts();
    $('#editLoyaltyAccountForm').show();
    $.get("editLoyaltyAccount/", {
        loyaltyAccountId: loyaltyId
    },
    function(data) {
        $('#loyaltyId').val(data.id);
        $('#loyaltyStore').val(data.storename);
        $('#loyaltyUsername').val(data.username);
        $('#loyaltyPassword').val(data.password);

    },
    "json");
}

function validateManageAccounts() {
    var accountText = $('#accountType').attr('value');
    if (accountText == 'Mobile') {
        if ($('#consumerMobilePhone').val() === '') {
            alert('Mobile cannot be empty');
            return false;
        }
        return true;
    }
}

function accountExists() {
    var username = $('#existingUsername').attr('value');
    var password = $('#existingPassword').attr('value');
    var exists = true;
    $.post("authenticateUser/", {
        existingUsername: username,
        existingPassword: password,
        existingAccount: "true"
    },
    function(data) {
        if (data.status == 'true') {
            $('#errorDescription').html('');
            return true;
        }

        else {
            $('#errorDescription').html(data.description);
            return false;
        }

    },
    "json");
}

/*$(document).ready(function(){	
	
	$("select#retlist").change(	  
		 function() {	  
			 /*if($("select#retlist").val() == 2) 
			 {		   
				 alert($("select#retlist option:selected").text()); 
			 }*/
			 /*var url = $("select#retlist option:selected").text();
			 url = url.toLowerCase();
			  url = url.replace(/[^a-z0-9]+/g, ""); 
			 *///alert(url);
			 /*var id = $("select#retlist").val();
			 if(url == "newleaf" || url == "newleafcommunitymarkets")
				 window.location = "newleaf.do";
			 else
				window.location = "partner.do?retId="+url;
		 }	 
	);	 
});*/

(function($){
	
    // hide msgPanel
    $(".message").click(
    	function() {
	        $(this).remove();
	    }
    );


    //  pop-up
    //$('div.popUp fieldset').wrap('<fieldset class="outer"></fieldset>');


    // add new item in receipt
    var i = 1;
    $('table thead .add').parent().click(
    function() {
        var whereTo = $(this).parents('table').find('tbody');
        var newRow = $('table:has(thead .add)').find('tbody tr:last').clone();
        newRow.prependTo(whereTo);
        $(this).parents('table')
        .find('tbody tr:first').find('input')
        .attr('name',
        function() {
            return this.name + i;
        }
        ).val('');
        i++;
        return false;
    }
    );

    // select all functionality
    $("#selectAll").click(
    function() {
        $(this).parents('form')
        .find('input:checkbox').attr('checked', 'checked');
        return false;
    }
    );

    $("#unselectAll").click(
    function() {
        $(this).parents('form')
        .find('input:checkbox').removeAttr('checked');
        return false;
    }
    );
    
    // select all functionality
    $("#addSelectedToCategory").click(
	    function() {	    	
	    	return verifySelectedReceiptAndCategory();
	    }
    );    
   
    $("#removeSelectedToCategory").click(
    	    function() {	    	
    	    	return verifySelectedReceipt();
    	    }
     );
    
    // select all functionality
    $("#acceptReceipt").click(
	    function() {	 
	    	var anyReceiptSelected = false;
	    	var checks = document.getElementsByName('saveData');
			var i = 0;
			while (i < checks.length) {
				if (checks.item(i).checked) {
					anyReceiptSelected = true;
					break;
				}
				i++;
			}
			if(!anyReceiptSelected){
	        	alert('Please select at least one receipt.');
	        	return false;
	        }
			else{
				return true;
			}
	    }
    );
    
    $("#rejectReceipt").click(
    	    function() {	 
    	    	var anyReceiptSelected = false;
    	    	var checks = document.getElementsByName('saveData');
    			var i = 0;
    			while (i < checks.length) {
    				if (checks.item(i).checked) {
    					anyReceiptSelected = true;
    					break;
    				}
    				i++;
    			}
    			if(!anyReceiptSelected){
    	        	alert('Please select the receipts that do not belong to you.');
    	        	return false;
    	        }
    			else{
    				return confirm('Are you sure these are not your receipts?');
    			}
    	    }   	    
    	    
        );
    
    function verifySelectedReceipt(){

        var anyReceiptSelected = false;
		var checks = document.getElementsByName('selectedReceipts');
		var i = 0;
		while (i < checks.length) {
			if (checks.item(i).checked) {
				anyReceiptSelected = true;
				break;
			}
			i++;
		}
        if(!anyReceiptSelected){
        	alert('Please select a receipt.');
        	return false;
        }	
        else{
        	return true;
        }
    }
    
    function verifySelectedReceiptAndCategory(){

        var category = $("#fldr").val();        
      
        var anyReceiptSelected = false;
		var checks = document.getElementsByName('selectedReceipts');
		var i = 0;
		while (i < checks.length) {
			if (checks.item(i).checked) {
				anyReceiptSelected = true;
				break;
			}
			i++;
		}
        if(!anyReceiptSelected && category == ''){
        	alert('Please select a receipt and choose a category.');
        	return false;
        }
        else if(!anyReceiptSelected){
        	alert('Please select a receipt.');
        	return false;
        }
        else if(category == ''){
        	alert('Please choose a category.');
        	return false;
        }	
        else{
        	return true;
        }
    }
    
    /*$("#selectAllCheck").click(
    	    function() {	    	
    	    	return selectCheckbox('selectAllCheck', 'selectedReceipts');
    	    }
     );
    
    $("#selectedReceipts").click(
    	    function() {	    	
    	    	return updateMainCheckbox(this, 'selectAllCheck', 'selectedReceipts');
    	    }
     );*/
    
    /*$("#unAcceptedReceipts").click(
    	    function() {	    	
    	    	return updateMainCheckbox(this, 'selectAllCheck1', 'saveData');
    	    }
     );
    
    $("#selectAllCheck1").click(
    	    function() {	    	
    	    	return selectCheckbox('selectAllCheck1', 'saveData');
    	    }
     );*/
    
  
    
    
    

    $("#myAcct").change(function() {
        var str = "";
        $("#myAcct option:selected").each(function() {
            str += $(this).text();
            $('#myAcctName').val(str);
            if (str != 'Select From List'){
                $('#loyaltyInfo').show();
            }else{
                $('#loyaltyInfo').hide();
            }
        });
    });


    $("#myMerchant").parents('fieldset').find('input[name^="my"]').val('');
	// $("#myMerchant").autocomplete({
	// 	source:merchants,
	//         minChars: 0,
	//         width: 310,
	//         matchContains: true,
	//         mustMatch: false,
	//         autoFill: true,
	//         formatItem: function(row, i, max) {
	//             return row.merchant;
	//         }
	//     })
	// .focus(function() {
	//         if ($(this).val() == 'Please select...') {
	//             $(this).val('');
	//         }
	//     })
	//     .result(function(event, data, formatted) {
	//         if (data) {
	//             $(this).parents('fieldset').find('#myID').val(data.id);
	//             $(this).parents('fieldset').find('#myAddr').val(data.address);
	//             $(this).parents('fieldset').find('#myAddr2').val(data.address2);
	//             $(this).parents('fieldset').find('#myCity').val(data.city);
	//             $(this).parents('fieldset').find('#myState').val(data.state);
	//             $(this).parents('fieldset').find('#myZip').val(data.zip);
	//             $(this).parents('fieldset').find('#myPhone').val(data.phone);
	//         }
	//     });


    hideAllAccounts();
    $('#accountType').change(function() {

        var accountText = $('#accountType').attr('value');
        hideAllAccounts();
        if (accountText === '') {
            // $('#outer3').hide();
        }
        else
        {
            // $('#outer3').show();

            if (accountText == 'Email') {
                $('#emailBox').show();
            }
            else if (accountText == 'Mobile') {
                $('#mobilePhoneBox').show();
            }
            else if (accountText == 'HomePhone') {
                $('#homePhoneBox').show();
            }
            else if (accountText == 'Loyalty') {
                $('#loyaltyBox').show();
            }
            else if (accountText == 'Memberid') {
                $('#memberidBox').show();
            }
        }

    });

    $('#editLoyaltyAccountForm').hide();

	$(".validateMe").each(function(){
		$(this).validate();
	});
	
	
	/*
		finish flexbox illusion for < IE9
	*/
	if(!Modernizr.flexbox){

		$(".col2").each(function(){
			$(this).children().equalHeights();
		});
		$(".col2A:not(ul[class*='noResize'])").each(function(){
			$(this).children().equalHeights();
		});
		$(".col2R").each(function(){
			$(this).children().equalHeights();
		});
		$(".col2B").each(function(){
			$(this).children().equalHeights();
		});
		$(".col3").each(function(){
			$(this).children().equalHeights();
		});
	}
	
	// $('.price').priceFormat({
	// 	prefix: '',
	// 	centsSeparator: '.',
	// 	thousandsSeparator: ','
	// });

})(window.jQuery);
