var script_start = 1;

jQuery(document).ready(function()
{	
    setTimeout(function()
    {
        jQuery('#msg').fadeOut(1000);
    }, 3000);
    setTimeout(function()
    {
        jQuery('#authMessage').fadeOut(1000);
    }, 3000);
       
    jQuery('#editlink').click(function() {
        jQuery('#edit').toggle(); // First click should toggle to 'show'
        jQuery('#st').toggle(); // First click should toggle to 'show'
    });
    jQuery('#menumaster').click(function() {
        jQuery('#menuchild').toggle(); // First click should toggle to 'show'
    });
    jQuery('#menumasterschoolcode').click(function() {
        jQuery('#menuchildschoolcode').toggle(); // First click should toggle to 'show'
    });
    
    initLogin();

});
function click(id)
{
    jQuery("#"+id).html('');
}
 
function close_div(id)
{
    //$("table > #"+id).slideUp('slow');
    jQuery("#"+id).html('');
}
function toggle_details_link(id,id2,tab_id){
    jQuery("#"+id2).fadeOut('slow');
    jQuery("#"+id).fadeIn('slow');
	 
    if(tab_id){
        jQuery("#"+tab_id).html('');
    }
}

function jqCheckAll( id, name,master)
{
    if (jQuery('#'+master).is(':checked'))
    {
        jQuery("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', true);
    }
    else
    {
        jQuery("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', false);
    }
}
function checkChangePasswordForm(){
    var errorMsg = "";
    if(jQuery("#UserOldPassword").val() == ""){
        errorMsg += "Old password must not be blank \n";
    }
    if(jQuery("#UserNewPassword").val() == ""){
        errorMsg += "New password must not be blank \n";
    }
    if(jQuery("#UserConfirmPassword").val() == ""){
        errorMsg += "Confirm password must not be blank and should match with new password. \n";
    }
	
    if(errorMsg != ""){
        alert(errorMsg);
        return false;
    }
    return true;
}

function execute_script(){

    jQuery("#loading_status").show();
    jQuery("#ajax_execute").html("");
    jQuery.ajax({
        type : "POST",
        url:"/CommunitySearches/ajax_execute_script",
        success: function(data){
            jQuery("#ajax_execute").append("<i><br />" + data + "</i>");
        },
        complete:function(){
            script_start = 0;
            jQuery("#loading_status").html("<img src='/img/success.png'> Script Execution completed sucessfully.");
            jQuery("#loading_status").fadeOut(1000);
            getAjaxUpdate(1);
        }
    });

    if(script_start){
        getAjaxUpdate(0);
    }
}

function getAjaxUpdate(flag){
    var TimerID = setTimeout(function(){
        if(script_start || flag == 1){
        jQuery.ajax({
            type : "POST",
            //url:"/CommunitySearches/ajax_get_script_update",
            url:"/getAjaxRequest.php",
            cache:false,
            success: function(data){
                jQuery("#ajax_execute").html("");
                jQuery("#ajax_execute").html(data);
            }
        });
        getAjaxUpdate(0);
        return;
        }else{
             clearTimeout(TimerID);
             return 0;
        }
    },30000);       // this will check the update after every 30 sec.
}
function updateLoginHeader(strData){
    var arrResponce = strData.split("~~");
    strDataAccountDetail = arrResponce[2];
    jQuery('#header_signup').html(strDataAccountDetail);
    jQuery('#loginBlock').html("<a style='color: rgb(0, 0, 0);' class='formlinks' href='/users/logout'> Logout</a>");
    initLogin();
    jQuery("#signin_menu").toggle();
    jQuery("#signin").toggleClass("menu-open");
    if(jQuery.trim(arrResponce[0]) == 'failer'){
        jQuery("input[id='UserPassword']").val('');
        jQuery("#signin_menu #msg").html(arrResponce[1]);

    }else{
        jQuery("#aLogin").html("<span>MY ACCOUNT</span>");
        jQuery("#aLogin").attr("href","/users");
    }
}
function initLogin(){
    jQuery("#signin").click(function(e) {
        e.preventDefault();
        jQuery("#signin_menu").toggle();
        jQuery("#signin").toggleClass("menu-open");
    });
    jQuery("#signin_menu").mouseup(function() {
        return false;
    });
    jQuery(document).mouseup(function(e) {
        if(jQuery(e.target).parent("a.signin").length==0) {
            jQuery("#signin").removeClass("menu-open");
            jQuery("#signin_menu").hide();
        }
    });
}
