/**
 * Removes the number formatting, such as thousands separators.
 * @param number
 * @return
 */
function removeNumberFormat(number) {
  return number.replace(',' , '');
}

/**
 * Adds thousand separator and decimal places to the number format.
 * @param string nStr Number to be formatted.
 * @return formatted figure.
 */
 function addNumberFormat(number)
 {
   number = number.toFixed(2);
   number += '';
   x = number.split('.');
   x1 = x[0];
   x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1)) {
     x1 = x1.replace(rgx, '$1' + ',' + '$2');
   }
   return x1 + x2;
 }


/**
 * Checks whether the element specified in the parameter exists.
 * @param elem jQuery element locator.
 * @return TRUE if exists, FALSE otherwise
 */
function elementExists(elem) { 
  return ($(elem).length > 0);
}

//Include all java script functions which are common between Managers and Contractors Modules. 
function ShowHelp(creator, title, desc, width) {
	box = document.createElement('div');

	// find and set box position
	var x=0, y=0;
	var element = creator;
	while (element!=null){
		// positioning at this moment, so not using scroll offset
		x+=element.offsetLeft-0*element.scrollLeft;
		y+=element.offsetTop-0*element.scrollTop;
		element=element.offsetParent;
	}
	if (!width) width=300;
	box.style.position = 'absolute';
	box.style.width = width+'px';
	box.style.left = (x-width+20)+'px';
	box.style.top = (y)+'px';
	box.style.zIndex = 200;	// always on top
	box.style.overflow = 'hidden';

	// box style
	box.style.border = '0px';
	box.style.padding = '0px';

	// box content
	box.innerHTML =
		box.innerHTML =
		'<div class="helpFloatingDiv">'+
			'<a href="#" style="float: right; text-decoration: none;" onclick="return false;">X</a>'+
			'<h3>' + title + '</h3>'+
			'<div>' + desc + '</div>'+
		'</div>'+
		'<iframe src="blank.htm" frameborder="0" style="position:absolute; top:0px; left:0px; width:100%; height:100%; z-index:-1;"></iframe>';

	box.onclick = function() { this.parentNode.removeChild(this); };
	document.body.appendChild(box);	// has to be appended to the BODY because of parent is floated
}

//==============================================================
// CheckDate
//==============================================================
function CheckDate(str) {
 if (str == "") {
   return "";
 } else { 
   var dateseg = str.split("/");
   var day = parseInt(dateseg[0],10); // 10 denotes using base number system - Microsoft???
   var month = parseInt(dateseg[1] - 1,10);
   var year = parseInt(dateseg[2],10);
   if ((day == "NaN") || (month=="NaN") || (year=="NaN"))
      return "NaN";
 	   var tempDate = new Date(year,month,day);
 	   if ( (tempDate.getFullYear() == year) &&
    		(month == tempDate.getMonth()) &&
    		(day == tempDate.getDate())) {
     		return new Date(str);
 	   } else {
    		return "NaN";
   }
 }

}

$(document).ready(function(){

  /** Pull down effect in Manager section - Choose client - fix for IE */  
  if (document.all) {
    $("#topmenu li").hoverClass ("sfHover");
  }
  
});

/** Pull down effect in Manager section - Choose client - fix for IE */
$.fn.hoverClass = function(c) {
  return this.each(function(){
    $(this).hover( 
      function() { $(this).addClass(c);  },
      function() { $(this).removeClass(c); }
    );
  });
};    

/** Function to change the client in manager portal */
function changeClient( client_id ) {
  $('#change_client_id').val( client_id );
  $('#change_client_form').submit();
}
/** Check if any checkbox checked and returns true otherwise false */
function checkIfAnyCheckboxChecked( Item ) {
  var n = $("input:checked").length;
  if (n == 0) {
    alert('Nothing selected! Please select any '+Item+'(s) first.');
    return false;
  } else {
    return true;
  }
}
