// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var updating_message = '<p>Updating...<img src="/images/spinner16.gif"/></p>';
var deleting_message = '<p>Deleting...<img src="/images/spinner16.gif"/></p>';
var uploading_message = '<p>Uploading...<img src="/images/spinner16.gif"/></p>';
function check_day(){
        $('is_time_ignored').value = "1"
        if($F('day') == "0" || $F('day') == "1"){
            Element.show('set_time');
            Element.hide('time');
			//Element.hide('datetime');
			Element.hide('datetime_cancel');
            Element.hide('specific_datetime');

			if($("day").value == "0")
			{
				var d = new Date();
				$("date_hour").value = d.getHours() + 1<24?d.getHours() + 1:"00";
				$("date_minute").value = "00";
			}
			else if($("day").value == "1")
			{
				$("date_hour").value = "10"
				$("date_minute").value = "00"
			}
        }
        else{
            Element.hide('set_time');
			Element.hide('datetime');
			Element.hide('datetime_cancel');
            Element.hide('time');
            Element.hide('specific_datetime')
            if($F('day')== "5"){
                Element.show('specific_datetime')
				Element.show('datetime');
                $('is_time_ignored').value = "0"

				$("date_hour").value = "10"
				$("date_minute").value = "00"
            }
        }
    }

function edit_check_day(){
        $('edit_is_time_ignored').value = "1"
        if($F('edit_day') == "0" || $F('edit_day') == "1"){
            Element.show('edit_set_time');
            Element.hide('edit_time');
			Element.hide('edit_time_cancel');
            Element.hide('edit_specific_datetime');
        }
        else{
            Element.hide('edit_set_time');
            Element.hide('edit_time');
			Element.hide('edit_time_cancel');
            Element.hide('edit_specific_datetime')
            if($F('edit_day')== "5"){
                Element.show('edit_specific_datetime')
				Element.show('edit_time')
                $('edit_is_time_ignored').value = "0"
            }
        }
    }

cCheckAll = function (val){		
		for (var i=0; i<ckbCount; i++){
			if($("ckbNo"+i).disabled == false){
				$("ckbNo"+i).checked = val;
			}
		}
	}
	unCheck = function(){
		$("chkall").checked = false;
	}
	getCheckedValue = function(){
		var check='';
		for (var i=0; i<ckbCount;i++){
			if($("ckbNo"+i).checked){
				check += $("ckbNo"+i).value + ',';
			}
		}
		if (check != ''){
			check = check.substr(0,check.length-1);			
		}
		return check;
	}
	
	function checkCookie(){
	  if(readCookie("tag_list") != false){
            Element.hide("cloud");
            Element.show("tag_list");
            Element.hide("show_opt2");
            Element.show("show_opt1");
            return;
        }
        Element.hide("tag_list");
        Element.show("cloud");
        Element.hide("show_opt1");
        Element.show("show_opt2");
        return;
    }

    function createCookie(name,value) {
    	var date = new Date();
    	date.setTime(date.getTime()+(365*24*60*60*1000));
    	var expires = "; expires="+date.toGMTString();
    	document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return false;
    }

	/* YFT effect - courtesy 37Signals */
	var Color= new Array();
		Color[0] = "ff";
		Color[1] = "ee";
		Color[2] = "dd";
		Color[3] = "cc";
		Color[4] = "bb";
		Color[5] = "aa";
		Color[6] = "99";
		Color[7] = "88";

	function waittofade() {
		if (document.getElementById('fade')) {
	    setTimeout("fadeIn(7)", 20);
	 }
	}

	function fadeIn(where) {
    	if (where >= 1) {
        	document.getElementById('fade').style.backgroundColor = "#FFFF" + Color[where];
		  if (where > 1) {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 150);
			} else {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 150);
			  document.getElementById('fade').style.backgroundColor = "transparent";
			}
    }
	}
	
	function task_listing_call(id)
	{
							$('notice').style.display='block';
							var o = document.getElementById('assigned_tab');
							if (!o)
							{
								$('task_list').innerHTML='<div id=div_loading><span class=spinner16> Loading...</span></div>'; 
								new Ajax.Updater('task_list',
												 '/task/mini_task_listing/'+id, 
												{asynchronous:true, evalScripts:true}
		
								);
							}
							else
							{
								if($('assigned_tab').className == 'selected')
								{
									$('task_list').innerHTML='<div id=div_loading><span class=spinner16> Loading...</span></div>'; 
									new Ajax.Updater('task_list',
													 '/task/assigned_listing', 
													{asynchronous:true, evalScripts:true,
													onComplete:function(request)
														{
															$('assigned_tab').className='selected';
															$('upcoming_tab').className='';
															$('completed_tab').className=''; 
														}
													}
			
									);
								}
								else if ($('completed_tab').className == 'selected')
								{
									$('task_list').innerHTML='<div id=div_loading><span class=spinner16> Loading...</span></div>'; 
									new Ajax.Updater('task_list',
													 '/task/completed_listing', 
													{asynchronous:true, evalScripts:true,
													onComplete:function(request)
														{
															$('assigned_tab').className='';
															$('upcoming_tab').className='';
															$('completed_tab').className='selected'; 
														}
													}
			
									);
								}
								else if ($('upcoming_tab').className == 'selected')
								{
									$('task_list').innerHTML='<div id=div_loading><span class=spinner16> Loading...</span></div>'; 
									new Ajax.Updater('task_list',
													 '/task/task_listing', 
													{asynchronous:true, evalScripts:true,
													onComplete:function(request)
														{
															$('assigned_tab').className='';
															$('upcoming_tab').className='selected';
															$('completed_tab').className=''; 
														}
													}
			
									);
								}
							}
	}
	


function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

/* Author: raza muhammad ali (mailto: raza.ali@confiz.com)
 * /app/summary add or edit file description related functions --start 
 * 
 * show_box(id) :  opens then text box for file description
 * close_edit(id) :  closes edit mode text box and update and cancel button
 * update_description(id) :  sends data for validating and updating to related file description.
*/

	function show_box(id)
	{

		Element.hide(id+'_desc')
		Element.hide(id+'_link')
		
		container = document.createElement('p');

		container.id = id + '_edit';
		container.style.fontSize='11px';
		container.style.marginLeft='60px';

		text_box = document.createElement('input');
		text_box.id = id + '_new_desc';
		text_box.type='text';

        art = removeHTMLTags($(id+'_desc').innerHTML);
		text_box.value = art;
		$(container).appendChild(text_box);

		update = document.createElement('a');
		update.innerHTML='update';
		update.style.marginLeft='8px';
		update.href='javascript:update_description(' + id + ');';

		$(container).appendChild(update);

		cancel = document.createElement('a');
		cancel.innerHTML='cancel';
		cancel.style.marginLeft='5px';
		cancel.href='javascript:close_edit(' + id + ');';

		$(container).appendChild(cancel);

		$(id+'_div').appendChild(container);
	}

	function close_edit(id)
	{
		Element.show(id+'_desc');
		Element.show(id+'_link');
		$(id + '_edit').remove();
	}

	function update_description(id)
	{
		
		new Ajax.Updater('',
						'/app/update_file_description/' + id ,
						{
							asynchronous:true,
							evalScripts:true,
							onComplete:function(request)
							{
								if(request.responseText == "Done")
								{
									if($(id + '_new_desc').value == '')
									{
										$(id+'_link').innerHTML = 'add description';
									}
									else
									{
										$(id+'_link').innerHTML = 'edit';
									}
									$(id+'_desc').innerHTML = $(id+'_new_desc').value.escapeHTML();
									Element.show(id+'_desc');
									Element.show(id+'_link');
									$(id + '_edit').remove();
								}
							},
							parameters:'desc=' + $(id + '_new_desc').value
						}
		);
	}

/* files description add/edit --end*/

/*app/message_list --start*/
    function checkAll(field)
    {
    for (i = 0; i < field.length; i++)
        field[i].checked = true ;
    }

    function uncheckAll(field)
    {
    for (i = 0; i < field.length; i++)
        field[i].checked = false ;
    }

    deleteSelectedMessages = function(field)
    {
        var list = '';
        var count = 0;
        for(i = 0;i < field.length;i++)
        {
            if(field[i].checked == true)
            {
                list = list + field[i].value + ','
                count = count + 1;
            }
        }
        if(count == 0)
        {
            alert('please select atleast one message to delete.');
        }
        else
        {
            if(!confirm('Are you sure, you want to delete selected messages'))
            {
                return;
            }
            new Ajax.Request
            (
                '/app/delete_selected_messages',
                {asynchronous:true, evalScripts:true,
                onComplete:function(request)
                {
                    var temp = request.responseText.split(",");
                    for (i = 0; i < temp.length; i++)
                    {
                        Element.hide(temp[i]);
                        $(temp[i]+'_chk').name = '';
                    }
                    Element.show('myform');
                    Element.hide('temp_loading');
                },
                onLoading:function(request)
                {
                    Element.hide('myform');
                    Element.show('temp_loading')

                },
                parameters:'list='+list}
            )
        }
    }
/*app/message_list --end*/

send_email = function(){
  vals = getCheckedValue();		
  if(vals == ''){
          alert('You must select an application to email');
  }else{			
          $('q').value=vals;
          $('email_form').action = "/app/bulk_mail" ;
          $('email_form').submit();
  }
}
/*added for application summary page highlighting feature.*/
function getElementsByClassName(classname, node) {
    if(!node) node = document.getElementsByTagName("body")[0];

      var a = [];
      var re = new RegExp('\\b' + classname + '\\b');
      var els = node.getElementsByTagName("*");

      for(var i=0,j=els.length; i<j; i++)

      if(re.test(els[i].className))a.push(els[i]);
      return a;
}


var CustomFunctions = {
	insertLocationItem: function(id, cell01_html, cell02_html)
	{
		var loc_table = document.getElementById("location_list")

		if($('no_location_id'))
		{
			loc_table.deleteRow(0);
		}

		var new_row = loc_table.insertRow(loc_table.rows.length);
		new_row.id = 'location_' + id;

		var y=new_row.insertCell(0);
		y.width = '30%';
		y.innerHTML = cell01_html;

		var z=new_row.insertCell(1);
		z.width = '7%';
		z.align = 'center';
		z.innerHTML = cell02_html;
	},

	showNoLocationMessage: function(message)
	{
		var loc_table = document.getElementById("location_list")

		var new_row = loc_table.insertRow(loc_table.rows.length);
		new_row.id = 'no_location_id';

		var y=new_row.insertCell(0);
		y.width = '30%';
		y.innerHTML = message;

		var z=new_row.insertCell(1);
		z.width = '7%';
		z.align = 'center';
		z.innerHTML = '';
	}

}


/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(strInputCode){
        /* 
                This line is optional, it replaces escaped brackets with real ones, 
                i.e. < is replaced with < and > is replaced with >
        */	
    strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
            return (p1 == "lt")? "<" : ">";
    });
    var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
    return strTagStrippedText;
}

var loading_msg = '<span id="div_loading"><span class=spinner16>&nbsp;loading...</span></span>'
function populate_new_files(id)
{
		$('files_list').innerHTML = loading_msg
	new Ajax.Updater(
		  'files_list',
		  '/app/new_files_list/'+id,
		  {	asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request)
			{
			  highlight();
			}
		  }
); 
}

/**/
 clear_user_guide_message_application = function()
 {
 
 	if($('search_val').value == '')
	{
		$('search_val').style.backgroundImage="none";
	}

 }

 add_user_guide_message_application = function()
 {

 	if($('search_val').value == '')
	{
		$('search_val').style.backgroundImage="url(/images/app_search_user_guide.jpg)"
	}

 }
/**/

mark_complete_assigned_task = function(task_id, user_name)
{
	if (confirm('This task will be marked as Completed and moved to '+user_name+'\'s Completed Tasks list. Are you sure?'))
	{
		$(task_id+'').innerHTML='<div id=div_loading><span class=spinner16>&nbsp;Loading...</span></div>';
		new Ajax.Updater(
		'task_list',
		'/task/mark_done/'+task_id+'?from=assigned',
		{	asynchronous:true,
			evalScripts:true,
			onComplete:function(request)
			{
				$('notice').style.display='';
				$('success_msg').innerHTML='Task moved to ' + user_name + '\&#39s Completed Tasks';
			}
		}
	);
	}
	else
	{
		$('chk_'+task_id).checked = false;
	}
}

/*user access based right -start*/

  set_access_rights_and_notify_users = function()
  {

    access_right_objects = document.getElementsByName("access_right");
    notify_email_objects = document.getElementsByName("notify_email");
    access_right_ids = [];
    notify_emails = [];
    j = 0;
    k = 0;
    temp_flag = false;
    for(i=0;i < access_right_objects.length;i++)
    {
      if(access_right_objects[i].checked == true)
      {
        access_right_ids[j] = access_right_objects[i].value;
        j = j + 1;
        if(notify_email_objects[i].checked == true)
        {
          notify_emails[k] = notify_email_objects[i].value;
          k = k + 1;
        }
      }
      else if($('access_right_users').value.include(access_right_objects[i].value))
      {
        temp_flag = true;
      }
    }

    access_right_ids_str = access_right_ids.join(",");
    notify_emails_str = notify_emails.join(",");

    if(document.location.href.include("/job/edit/"))
    {
      if(temp_flag == true)
      {
        if(!confirm("Are you sure you want to change access rights from users? All the tasks assigned to/by a user will be deleted!"))
          return false;
      }
    }

    $('access_right_users').value = access_right_ids.join(",");
    $('notify_emails').value = notify_emails.join(",");

    RedBox.close(); return false;

  }

  change_notify_user = function(index, status)
  {
    notify_email_object = document.getElementsByName("notify_email")[index];
    if(status == true)
    {
      notify_email_object.style.display = '';
      notify_email_object.checked = true;
    }
    else
    {
      notify_email_object.style.display = 'none';
      notify_email_object.checked = false;
    }
  }

  load_ubac_details = function()
  {
    $('users-access-right-header').innerHTML = ($('access_right_users').value == '' ? '' : 'Edit - ') + $('users-access-right-header').innerHTML + (($('job_job_title').value == '') ? '' : ' for ' + $('job_job_title').value)

    if($('access_right_users').value != '')
    {
      access_right_objects = document.getElementsByName("access_right");
      notify_email_objects = document.getElementsByName("notify_email");
      for(i=0;i < access_right_objects.length;i++)
      {
        if($('access_right_users').value.include(access_right_objects[i].value))
        {
          access_right_objects[i].checked = true;
          notify_email_objects[i].style.display = '';
          notify_email_objects[i].checked = ($('notify_emails').value.include(notify_email_objects[i].value) ? true : false);
        }
        else
        {
          access_right_objects[i].checked = false;
          notify_email_objects[i].checked = false;
        }
      }
    }
  }

  /*user access based right -end*/