//functions
//init's functions on window load

//var baseUrl =  '/intranet/'

   // uncoment this for demo site
var baseUrl = '/intranet/ '; 


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


//load events

//

//jquery functions

//system message
$(document).ready(function(){$("#message").fadeOut(6500);});

//clamshell
$(document).ready(function() {
  $('div.slide_area_wrapper:eq(0)> div.section_title_wrapper').click(function() {
	$(this).next().next().slideToggle('slow');
  });
});

//DATE PICKER
$(document).ready(function() {
	$('.date-pick').datePicker({startDate:'1910-01-01'});
	$('.date-pick-report').datePicker({startDate:'1910-01-01'});
});



// Tool tips functions
$(function() {

$("#fancy, #fancy2").tooltip({
    track: true,
    delay: 0,
    showURL: false,
    opacity: 1,
    fixPNG: true,
    showBody: " - ",
    extraClass: "pretty fancy",
    top: -15,
    left: 5
});

$('#pretty').tooltip({
    track: true,
    delay: 0,
    showURL: false,
    showBody: " - ",
    extraClass: "pretty",
    fixPNG: true,
    opacity: 0.95,
    left: -120
});


});



// fuction used

$(document).ready(function() {

	  $('#cat_assign').click(function() {
           return !$('#select1 option:selected').remove().appendTo('#select2');
      });
       $('#cat_remove').click(function() {
          return !$('#select2 option:selected').remove().appendTo('#select1');
       });


     $('#cancel_button').click( function() {
             var link = document.getElementById('form_delete').getAttribute('action');
             history.back();
     });

      // file input stles

      $("input[type=file]").filestyle({
    		image: baseUrl +"images/folder_explore.png",
    		imageheight : 22,
    		imagewidth : 22,
   			width : 162
     });



     $('#form_delete').submit( function(e) {
          e.preventDefault();
         var link = document.getElementById('form_delete').getAttribute('action');
         var name_to_delete = '<ul>';
        j=document.getElementById('form_delete').max_num_per_page.value;
          var checked_items = 0;

         if ( j == 1 ) {
             if (document.form_delete["check_value[]"].checked )  {
                name_to_delete += '<li>' + document.form_delete["check_value[]"].getAttribute('rel') + '</li>';
                checked_items = 1;
              }
         } else {

          for (i=0; i<j; i++){

                 if (document.form_delete["check_value[]"][i].checked) {

		               document.form_delete["check_value[]"][i].value;  // a id to remove
		               name_to_delete += '<li>' + document.form_delete["check_value[]"][i].getAttribute('rel') + '</li>';
                      checked_items = 1;
		         }
	      }

	      }


             name_to_delete = name_to_delete + '</ul>';

         if (checked_items == 0) {
           var message_display =  " Please check at least one item";
           dialog_box( message_display, function () {
		   });
         } else {
	       var message_display =  "Are you sure you want to delete ? <b>" + name_to_delete + "</b> ";
           confirm( message_display, function () {
		   	// window.location.href = link; // if you want to submit an individual item
		 	  document.form_delete.submit();  // this is used to submit values of a form
	 	 });

	 	 }

     });


   function confirm(message, callback) {
	 $('#confirm').modal({
		 close:false,
		 overlayId:'confirmModalOverlay',
		 containerId:'confirmModalContainer',
		 onShow: function (dialog) {
	     dialog.data.find('.message').append(message);

	     // if the user clicks "yes"
			dialog.data.find('.yes').click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					 callback.apply();
					//return true;
				}
				// close the dialog

				$.modal.close();
			});

		}
	});
 }


  function dialog_box(message, callback) {
	 $('#dalog_box').modal({
		 close:false,
		 overlayId:'confirmModalOverlay',
		 containerId:'confirmModalContainer',
		 onShow: function (dialog) {
	     dialog.data.find('.message').append(message);

	     // if the user clicks "yes"
			dialog.data.find('.close').click(function () {
				// close the dialog
				$.modal.close();
			});

		}
	});
   }


    $('#Generate').click( function(f) {

      	function randomString() {
			var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
			var string_length = 6;
			var randomstring = '';
			for (var i=0; i<string_length; i++) {
				var rnum = Math.floor(Math.random() * chars.length);
				randomstring += chars.substring(rnum,rnum+1);
		    }
	      return randomstring;
         }

      	var myRandomValue = randomString();
     	document.getElementById("pass1").value = myRandomValue;

     });


     $('#acc_member_id').change( function() {
              var selectBox =  document.getElementById("acc_member_id");
               for (i = 0; i < selectBox.length; i++) {
                      if (selectBox.options[i].selected) {
                        var selectedRefNo = selectBox.options[i].getAttribute('id');
                       }
               }
               document.getElementById("acc_username").value = selectedRefNo;

     });






     //  tooltips functions

     $('.toolTip').hover(
		function() {
		this.tip = this.title;
		$(this).append(
			'<div class="toolTipWrapper">'
				+'<div class="toolTipTop"></div>'
				+'<div class="toolTipMid">'
					+this.tip
				+'</div>'
				+'<div class="toolTipBtm"></div>'
			+'</div>'
		);
		this.title = "";
		this.width = $(this).width();
		$(this).find('.toolTipWrapper').css({left:this.width-43})
		$('.toolTipWrapper').fadeIn(300);
	},
	function() {
		$('.toolTipWrapper').fadeOut(100);
		$(this).children().remove();
			this.title = this.tip;
		}
	);


	// interractive textbox maxlength

	 function limitChars(textid, limit, infodiv)  {
         var text = $('#'+textid).val();
         var textlength = text.length;
         if(textlength > limit)  {
        	$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
         	$('#'+textid).val(text.substr(0,limit));
          	return false;
         } else {
            $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
           return true;
         }
     }


     $(function(){
         $('#pub_services').keyup(function(){
          limitChars('pub_services', 800, 'charlimit_pub_service');
        })
     });


     $(function(){
         $('#freel_services').keyup(function(){
          limitChars('freel_services', 800, 'charlimit_freel_services');
        })
     });

     $(function(){
         $('#freel_special_sub').keyup(function(){
          limitChars('freel_special_sub', 800, 'charlimit_freel_special_sub');
        })
     });



     $(function(){
         $('#commu_content').keyup(function(){
          limitChars('commu_content', 1500, 'charlimit_commu_content');
        })
     });



	 // help is here

	 $('#help').click( function(e) {
          e.preventDefault();
          help_box( function () {
		   });

     });

	 function help_box(callback) {
	 $('#help_box').modal({
		 close:false,
		 overlayId:'helpModalOverlay',
		 containerId:'helpModalContainer',
		 onShow: function (dialog) {
	     dialog.data.find('.close').click(function () {
				// close the dialog
				$.modal.close();
	  });

		}
	});
   }






});







