﻿jQuery.utils = function() {	
    var trim = function(value) {
        return value.replace(/^\s+|\s+$/g,"");
    };
        
	return {
		//check input, if invalid, show span with a red "*" character for the error 
		checkStandartInput: function(value, object){
		    var filter  = /^([a-zA-Z\s\-\.])+$/;
            if (!filter.test(trim(value))){
                object.show();
                return false;
            }
            else
                object.hide();                            
                
            return true;
		},
		//check phone number
        checkPhoneNumber: function(value, object){            
            var filter  = /^([0-9\-\s])+$/;
            if (!filter.test(value) || value.length > 13){                
                object.show();
                return false;
            }
            else
                object.hide();                            
                
            return true;
        },
        //check email
        checkEmail: function(value, object){            
            var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test(value)){                
                object.show();
                return false;
            }
            else
                object.hide();                            
                
            return true;
        },
        //check city
        checkCity: function(value, object){            
            var filter  = /^([a-zA-Z\s\-])*$/;
            if (!filter.test(trim(value))){                
                object.show();
                return false;
            }
            else
                object.hide();                            
                
            return true;
        },
        //check address
        checkAddress: function(value, object){            
            var filter  = /^([a-zA-Z0-9\s\-\.\:])*$/;
            if (!filter.test(trim(value))){                
                object.show();
                return false;
            }
            else
                object.hide();                            
                
            return true;
        },
        //check zipcode
        checkZipcode: function(value, object){            
            var filter  = /^([1-9][0-9]{3}\s?[a-zA-Z]{2})*$/;
            if (!filter.test(trim(value))){                
                object.show();
                return false;
            }
            else
                object.hide();                            
                
            return true;
        },
        //alert error then go page
        showError: function(error, page){      
            alert(error);
            if(page != "")
                location.href = page;
        },
        //alert confirm then go page
        showWarning: function(warning, page){
            if(!confirm(warning)){
                if(page != "")
                    location.href = page;
            }
        },
        //alert text then go page
        showAlert: function(alertStr, page) {
            if ($('#alertcontrol').val() == 1)
                return;
            alert(alertStr);
            if (!(page == "" || page == "[path]"))
                location.href = page;
            window.onbeforeunload = function() {
                $('#alertcontrol').val(1);
            }
        },
        showConfirm: function(warning, okPage, cancelPage){
            if(confirm(warning)){
                if(okPage != "")
                    location.href = okPage;
            }else{
                if(cancelPage != "")
                    location.href = cancelPage;
            }
        },
        toggleSubMenu: function(liControl){
            if(liControl.find('ul').is(':visible')){
                liControl.removeClass('current');
                liControl.find('ul').slideUp('slow');
            }else{
                liControl.addClass('current');
                liControl.find('ul').slideDown('slow');
                //if click at out of submenu then hide submenu
                $(document).mousedown(function(event){
                    var element = event.target;
                    if(element.parentNode){
                        if(element.parentNode.id != 'submenu'){
                            liControl.removeClass('current');
                            liControl.find('ul').slideUp('slow');
                        }
                    }
                });
            }
        },
        //check session expire on ajax response
        checkSessionExpire: function(value){
            if(value == "LOGOUT"){
                window.location.href = resources.path_prefix + "/Login.aspx";
                return true;
            }
            return false;
        }               
	};
} ();

function checkAllRestaurants() {
    $('table[id*=cbRestaurants] :checkbox').attr('checked', !!$('h2 + :checkbox').attr('checked'));
}
