//Jquery FIX

var showhide = function(id){
	if($('#'+id).is(':hidden')){
		show(id);
	} else {
		hide(id);
	}
	return false;
}

var show = function(id){
		if($('#'+id).is(':hidden')){
			$('#'+id).show('normal');
		}
    return false;
} 


var hide = function(id){
		if($('#'+id).is(':visible')){
			$('#'+id).hide('normal');
		}
    return false;
} 

//overwrite the alert function
var alert = function(text) {
	jAlert(text, 'Alert');
}

//END


function toggledisplay(id, style) {
	var e = document.getElementById(id);
	if(e.style.display == style){
		hide(id);
	} else {
		showstyle(id, style);
	}
}

function showstyle(id, style) {
	//safe function to show an element with a specified id
	//with a specified style
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = style;
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = style;
		}
		else { // IE 4
			document.all.id.style.display = style;
		}
	}
}


//Make sure user only submits form once
$(document).ready(function() {                                                              
	$(".nicesubmit").click( function() {
			window.myValue = this;
      var rel = $(this).attr('rel');
			if(rel!='multisubmit'){
				$('body').delay(500,function(){
					$(window.myValue).attr('disabled', true);
				});
			}
	});
});
//END


function focusIt(id){
  $('#'+id).focus();
}

function enable(id) {
	$('#'+id).attr('disabled', false);
}

function disable(id) {
	$('#'+id).attr('disabled', true); 
}

 function doClear(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }

function unCheck(Form, Name){
	document.forms[Form].elements[Name].checked = false;
}

function Check(Form, Name){
	document.forms[Form].elements[Name].checked = true;
}

function fill(object,value,focus){
	document.getElementById(object).value = value;
	if(focus==undefined){
		var focus = true;
	}
	if(focus==true){
		document.getElementById(object).focus(); 
	}
} 

function jumpToAnchor(anchor) {
	window.location = String(window.location).replace(/\#.*$/, "") + "#" + anchor;
}

function redirect(url){
	window.location=url;
}

function popup(url) {
	 winName = "details";
   options = 'width=500, height=350, resizable=yes, scrollbars=yes';
   newWin = window.open(url, winName, options);
   newWin.focus();
}

function checkUncheckAll(theElement) {
  var theForm = theElement.form, z = 0;
  for(z=0; z<theForm.length;z++){
    if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
      theForm[z].checked = theElement.checked;
    }
  }
}


/* Jquery Warn if leaving page and has not saved */
window.form_dirty = false;
var warn_if_form_dirty = function(forwardto){
  if(window.form_dirty == true){
		$.alerts.okButton = '&nbsp;Continue&nbsp;';
		$.alerts.cancelButton = '&nbsp;Cancel&nbsp;';		
		jConfirm('Are you sure you want to continue? <br> Clicking "Continue" will disregard any changes you have made without saving.  <br> Click "Cancel" to save this page before proceeding.', 'You have not saved this page!', function(r) {
			if(r){
				redirect(forwardto);
			}
		});
		$.alerts.okButton = null; // reset to default
		$.alerts.cancelButton = null; // reset to default		
   } else {
      redirect(forwardto);
   }

}


/* Jquery Iframe Dialog Box */
$(document).ready(function() {                                                              
	$("a.IframeDialog").click( function() {
		$.modal.impl.close(); /* Use jquery.simplemodal.js */
		var IframeTitle = $(this).attr('title');
		var IframeTitle = jQuery.trim(IframeTitle);		
		var IframeSrc = $(this).attr('href');
		var IframeSrc = jQuery.trim(IframeSrc);	
		if(IframeSrc!='' && IframeTitle!=''){		
			var dialogContent = '<div class="dialog" style="display:block;" id="IframeDialog"><div id="dashboard_header"><table width="100%"><tr><td class="dashboard_header"><img src="/admin/images/application_double.png" align="absmiddle"/>&nbsp;'+IframeTitle+'</td><td align="right"><a style="cursor:pointer;" onclick="$.modal.impl.close();"><img src="/admin/images/close.png" /></a></td></tr></table></div><table cellspacing="0" cellpadding="5px" border="0" width="100%"><tr class="evenrow"><td class="action" align="left"><iframe frameborder="0" src="'+IframeSrc+'" width="770px" height="300px" scrolling="auto"></iframe></td></tr></table><table cellspacing="0" border="0" width="100%;"><tr id="evenrow"><td colspan="2" align="right"><input type="button" class="nicesubmit" value="Close" onclick="$.modal.impl.close(); return false;"></td></tr></table></div>';
			$.modal(dialogContent);
		}
	});
});
//END

/* Jquery Dialog Box */
var showPopUp = function(el,width){
	if(is_int(width)){
		var width = width;
	} else {
		var width = '600';
	}
	
	if(width<600){
		var width = '600';		
	}
	
	var thisoptions = { close:false, minWidth: width, maxWidth: 850};		
	$.modal($('#'+el), thisoptions); /* Use jquery.simplemodal.js */	
	$('div#blueutopiamodal-container').css("width",width+'px');		
  return false;
}

var closePopUp = function(el,refresh){
	$.modal.impl.close(); /* Use jquery.simplemodal.js */
	$('#'+el).hide();
	if(refresh=='undefined'){
		window.location.reload();
	}
	if(refresh=='yes'){
		window.location.reload();
	}
	return false;
}



function is_int(n){
	return (!isNaN(n)) && (Math.floor(n)==n);
}


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 + "]+$", "g"), "");
}


//Overwrite the alert function
var alert = function(text) {
	jAlert(text, 'Alert');
}
//END

/* Used in finance manger */
function format_amount(id,num){ 
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+
	num.substring(num.length-(4*i+3));

	var format= num + '.' + cents;
	fill(id,format);
}


/* Used on popup window */
function resizeWin(windowHeight,windowWidth){

    if (!windowHeight) {   this.windowHeight = 400;   } else { this.windowHeight = windowHeight; }
    if (!windowWidth) {   this.windowWidth = 300;    } else { this.windowWidth = windowWidth; }
		
		window.resizeTo(this.windowHeight,this.windowWidth);
		window.focus();

		var centerWidth = (window.opener.screen.width - this.windowWidth) / 2;
		var centerHeight = (window.opener.screen.height - this.windowHeight) / 2;
} 

