var EMPTY_FIELDS        = -1240;
var SUBJECT_NOT_VALID = -1241;
var TITLE_IS_LONG = -1242;
var TITLE_NOT_VALID = -1243;
var INVALID_DESCRIPTION = -1244;
var ERROR_EMPTY_TITLE = 'Looks like you forgot to give a subject line.';
var ERROR_INVALID_TITLE = 'Enter valid subject. No special characters are allowed.';
var ERROR_EMPTY_DESCRIPTION = 'Looks like you forgot to give a message.'; 
var ERROR_LONG_TITLE         = 'Subject cannot be more than 200 characters.';
var ERROR_INVALID_DESCRIPTION  = 'Please enter valid description.';
var DELETE_ANNOUNCEMENT = 'Are you sure you want to delete announcement?';
var SUCCESS = 0;

var loading = "Loading...<img src='/storage/image/default/loading.gif'/>";
var AjaxObject = {
handleSuccess_pagination:function(o){
	document.getElementById('adda_announcement_loading').style.display = 'none';
	document.getElementById("adda_announcement_loading").innerHTML = '';
	document.getElementById("announcement_view").innerHTML = o.responseText;
	//document.getElementById("pagination2").innerHTML = o.responseText;    	    	
	//document.getElementById("adda_scribble_view").innerHTML = o.responseText;    
  },
 handleFailure_pagination:function(o){
    // Failure handler                                                                                                                                   
    },
 handleSuccess_announcement_delete:function(o){
    document.getElementById("announcement_view").innerHTML = o.responseText;
  },
 handleFailure_announcement_delete:function(o){
  },
handleStart_announcement:function(){
	document.getElementById("adda_announcement_loading").innerHTML = loading;
    },
handleComplete_announcement:function(){
	document.getElementById("adda_announcement_loading").innerHTML = loading;
    },
handleAbort_announcement :function(){
	// Abort handler
    } 
};

var callback_announcement_delete =
  {
  success:AjaxObject.handleSuccess_announcement_delete,
  failure:AjaxObject.handleFailure_announcement_delete,
  scope: AjaxObject
  };

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_pagination =
{
    customevents:{ 
	        onStart:AjaxObject.handleStart_announcement, 
	        onComplete:AjaxObject.handleComplete_announcement,	       
	        onAbort:AjaxObject.handleAbort_announcement 
	    }, 
    success:AjaxObject.handleSuccess_pagination,
    failure:AjaxObject.handleFailure_pagination,
    scope: AjaxObject
};

function delete_announcement(adda_id,id)
{
    if(confirm(DELETE_ANNOUNCEMENT))
	{  
	    YAHOO.util.Connect.asyncRequest('POST',system_base_url+'ajax/adda/_delete_announcement/'+adda_id+'/'+id, callback_announcement_delete);
	    if(env_value == 'Online')
		{
		    var url = system_base_url+'ajax/adda/_delete_announcement/';
		    pageTracker._trackPageview(url);
		}
    
	}
}

function announce_restw(frm,max,dispentered,counter_increment)
{
    var inp = frm.value.replace(/^\s+|\s+$/g,'');
    var l = parseInt(inp.length);
    if (l > max)
	{             
	    inp = inp.substr(0,max);
	    frm.value = inp;
	}
    document.getElementById(counter_increment).value = inp.length;
    document.getElementById(dispentered).innerHTML = inp.length;
    document.getElementById('show_count').innerHTML = '';
}

//! Pagination
function pagination(base_url,keyword,show_from)
{      
    var post_data = 'keyword='+keyword+'&pagination=y'+'&show_from='+show_from;
    if(keyword == '')
	{
	    YAHOO.util.Connect.asyncRequest('POST',base_url, callback_pagination, post_data);       
	}
    else
	{
	    YAHOO.util.Connect.asyncRequest('POST',base_url, callback_pagination, post_data);       
	}

    if(env_value == 'Online')
	{
	    pageTracker._trackPageview(base_url);
	}
    

}

function announcement_validations(frm)
{
    var  title = document.getElementById('title').value;
    if(document.getElementById('description'))
	{
	    var description = document.getElementById('description').value;
	}
    var error_code = 0;
    
    if(title || title == '')
	{
	    var error_title = announcement_validateTitle(title);
	    if(error_title)
		{
		    announcement_showError(error_title,'error_title');
		    error_code = error_title;
		}
	    else
		{
		    document.getElementById('error_title').innerHTML = '';
		}
    }
    if (description || description == '')
	{
	    var error_description = announcement_validateDescription(description);
	    if (error_description)
		{
		    announcement_showError(error_description,'error_description');
		    error_code = error_description;
		}
	    else
		{
		    document.getElementById('error_description').innerHTML = '';
		}
	}
    if(error_code < 0)
	{
	    return false;
	}
    else
	{
	    return true;
	}
}

function announcement_validateDescription(description)
{
    //    document.write(description);
    // return false;
    var filter  = /^[a-zA-Z0-9\.\-\!\s\n\@\#\$\%\^\&\*\(\)\_\{\}\[\]\;\:\"\'\,\?\/\\\`\~\ ]+$/;                               
    if (trim(description) == '')
	{
	    return EMPTY_FIELDS;
	}
    else if(!filter.test(description))                                                                                      
	{
	    return INVALID_DESCRIPTION;
	}
    else
	{
	    return SUCCESS;
	}
}

//! Method to validate First/Last name
/*!
 * param name=>consists of data entered by user in FIRST/LAST name field
 * return Boolean True on success, negative error code on failure
 */
function announcement_validateTitle(name)
{
    var filter  = /^[a-zA-Z0-9\.\-\,\.\:\'\&\(\)\-\_\ ]+$/;                                   
    if(trim(name) == '')
	{
	    return EMPTY_FIELDS;
	}
    else if(!filter.test(name))
	{
	    return TITLE_NOT_VALID;
	}
    else if(name.length > 200)
	{
	    return TITLE_IS_LONG;
	}
    else 
	{
	    return SUCCESS;
	}
}

function announcement_showError(error_code,div_id)
{
    if(div_id == 'error_title')
	{
	    if(error_code  == EMPTY_FIELDS)
		{
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_TITLE;
		}
	    else if(error_code  == TITLE_NOT_VALID)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_INVALID_TITLE;
		}
	    else if(error_code  == TITLE_IS_LONG)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_LONG_TITLE;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}
    if (div_id == 'error_description')
	{
	    if (error_code == EMPTY_FIELDS)
		{
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_DESCRIPTION;
		}
	    if (error_code == INVALID_DESCRIPTION)
		{
		    document.getElementById(div_id).innerHTML = ERROR_INVALID_DESCRIPTION;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}    
}

function trim(str, chars)
{
  return ltrim(rtrim(str, chars), chars);
} 

function ltrim(str, chars)
{
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars)
{
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+ s", "g"), "");
}
