var url;


/*
 Example 1: confirm_action('block','User','Tom')
   action_name: block
   object_type: User
   object_name: Tom
 
 Example 2: confirm_action('delete','Category','Cars')
   action_name: delete
   object_type: Category
   object_name: Cars
*/
function confirm_action(action_name,object_type,object_name){
  if(action_name == "") action_name = "perform the selected operation on";
  action_object = "";
  if(object_type != ""){
    action_object = "the " + object_type;
    if(object_name != "") action_object = action_object + " ";
  }
  if(object_name != "") action_object = action_object + '"' + object_name + '"';
  if(action_object == "") action_object = "the current record";
  return confirm("Are you sure \nyou want " + action_name + " " + action_object + "?");
}

function clickClear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickRecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

function LocationVisibility(Show,Hide,thediv,thearr){
  what = document.getElementById(thediv).style.display == 'none' ? 'block' : 'none' ;
  document.getElementById(thediv).style.display=what;
  if (what == 'none'){
	  document.getElementById('AdsLink').innerHTML = Show;
	  document.getElementById(thearr).src = url + 'images/arrow1.gif';
  }
  else{
    document.getElementById('AdsLink').innerHTML = Hide;
    document.getElementById(thearr).src = url + 'images/arrow2.gif';
  }
  return false;
}

var display_hours;
var display_minutes;
var display_seconds;
function count_down_timer(ticket_id, hours, minutes, seconds){
		
		if(seconds > 0){
			seconds--;
		}else if(seconds == 0) {
		
			if(minutes > 0){
				seconds = 59;
				minutes--;
			}else if(minutes == 0){
			
				if(hours > 0){
					minutes = 60;
					hours--;
				}else if(hours == 0){
					hours = 0;
				}
			}
			
		}
		
		if(hours < 10){
			display_hours = '0' + hours;
		}else{
			display_hours = hours;
		}
		if(minutes < 10){
			display_minutes = '0' + minutes;
		}else{
			display_minutes = minutes;
		}
		if(seconds < 10){
			display_seconds = '0' + seconds;
		}else{
			display_seconds = seconds;
		}
		
		document.getElementById('count_down_'+ticket_id).innerHTML = display_hours + 'h:' + display_minutes + 'm:' +  display_seconds + 's';		
		
		if(hours != 0 || minutes != 0 ||  seconds != 0){
	    	setTimeout("count_down_timer("+ticket_id+", "+hours+", "+minutes+", "+seconds+");", 1000);
	    }
	    
		
	}
	
	
var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

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


  function showHideObject(object_id, close_object_id, delete_info_message){

	if(close_object_id != null){
		if(document.getElementById(close_object_id).style.display == 'block'){
			document.getElementById(close_object_id).style.display = 'none';
		}
	}

	if(document.getElementById(object_id).style.display == 'block'){
		document.getElementById(object_id).style.display = 'none';
	}else{
	    document.getElementById(object_id).style.display = 'block';
	}

	if(delete_info_message == true){
		if(document.getElementById('info_message') != null){
			document.getElementById('info_message').innerHTML = '';
		}
	}
}
 function showObject(object_id, delete_info_message){

	if(document.getElementById(object_id) != null){
		if(document.getElementById(object_id).style.display == 'none'){
			document.getElementById(object_id).style.display = 'block';
		}
	}

	if(delete_info_message == true){
		if(document.getElementById('info_message') != null){
			document.getElementById('info_message').innerHTML = '';
		}
	}
}
 function hideObjects(objects_ids){

	if( isArray(objects_ids) )
	{
		for(var i=0; i<objects_ids.length; i++)
		{
			hideObject(objects_ids[i]);
		}
	}
}
 function hideObject(object_id, delete_info_message){

	if(document.getElementById(object_id) != null){
		if(document.getElementById(object_id).style.display == 'block'){
			document.getElementById(object_id).style.display = 'none';
		}
	}

	if(delete_info_message == true){
		if(document.getElementById('info_message') != null){
			document.getElementById('info_message').innerHTML = '';
		}
	}
}

		
 
 var readyState = true;
 var readyTimerState;
 var last_xmlhttp_status;
 
 function getValueFromUrl(url, set_object_id){
  
  var xmlhttp=false;
  try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
	  try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e) {
	    xmlhttp = false;
	  }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 xmlhttp = new XMLHttpRequest();
  }
  
  var get_url = '';
			
	get_url = url + "&sec_code="+generate_security_code();
	if(readyState){
		readyState = false;
		var response_text = '';	
		xmlhttp.open("GET", get_url, true);
			 xmlhttp.onreadystatechange = function() {	
				if (xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						response_text = xmlhttp.responseText;
						document.getElementById(set_object_id).innerHTML = response_text;
					}
				}else if(xmlhttp.readyState == 0){
					alert("Sorry there was a problem while executing your request!!\nPlease refresh your page.");
				}
				last_xmlhttp_status = xmlhttp.readyState;
				
			};
			
		xmlhttp.send(null);
		readyState = true;
	}else{
		clearTimeout(readyTimerState);
			if(!readyState){
				readyTimerState = setTimeout("getValueFromUrl('"+url+"');", 4000);
			}else if(last_xmlhttp_status == 0){
				alert("Sorry there was a problem while executing your request!!\nPlease refresh your page.");
			}
	}

 }
 

function checkForFireBug()
{
    var firebug_enabled = false;
    if( window.console )
    {
	if( window.console.firebug != 'undefined' && window.console.firebug != 0 && window.console.firebug != null )
	{
	    firebug_enabled = true;
	}
    }
    return firebug_enabled;
}

function updateCKEditors(form)
{
    for(i=0; i < form.length; i++){
	if( form[i].type == 'textarea' )
	{
	    if( CKEDITOR.instances[form[i].name] != null )
	    {
		form[i].value = CKEDITOR.instances[form[i].name].getData();

		//force empty text if firebug is enabled
		var field_value = form[i].value;


		if( field_value.length == 15 )
		{
		    if( field_value == '<p>\n'+ '	&nbsp;</p>')
		    {
			form[i].value = '';
		    }
		}
		else if( field_value.length == 102 )
		{
		    if( field_value == '<p>\n'+ '	&nbsp;</p>\n<div firebugversion="1.5.4" id="_firebugConsole" style="display: none;">\n	&nbsp;</div>')
		    {
			form[i].value = '';
		    }
		}
	    }
	}
    }
}

function reset_form(form_param, ignore_elements, is_form_name)
{
	//alert(ignore_elements);
	if(undefined == ignore_elements.length){
		ignore_elements = array('');
	}
	var form = form_param;
	if(is_form_name){

		if(document.getElementById(form_param) != null){
			form = document.getElementById(form_param);
		}else{
			form = document.getElementsByName(form_param);
		}
	}

	for(i=0; i<form.length; i++){
		   //alert(form[i].type+'---'+form[i].name);
		  //for(var j=0; j<ignore_elements.length; j++){

			   //if(ignore_elements[j] != form[i].name){alert(form[i].name);
			   if(ignore_elements.indexOf(form[i].name) == -1){
			  		if(form[i].type == 'text' || form[i].type == 'textarea'){
			   			form[i].value = "";
			   		}else if(form[i].type == 'select-one'){


				   		form[i].value = "";
				   		if(form[i].value != ""){
				   			form[i].value = 0;
				   		}

			   		}

			   }
		  //}
	}
}

function fromJScalendarParseDateInteger(date)
{
	return Date.parse(date);
}

