/**
 * @author Logesh Kumaraguru 
 * @Created on Aug 24, 2010
 * @version 1.0
 */

var alphabets = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','U','R','S','T','U','V','W','X','Y','Z'];
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }


/**
 * @methodLogic trims the leading and trailing spaces in the text.
 * 
 * @param text
 *            value passed from calling function
 * @returns text trimmed text to called function
 * 
 */
function trimAll(text) {
	while ((text.indexOf(' ', 0) == 0) && (text.length > 1)) {
		text = text.substring(1, text.length);
	}
	while ((text.lastIndexOf(' ') == (text.length - 1) && (text.length > 1))) {
		text = text.substring(0, (text.length - 1));
	}
	if ((text.indexOf(' ', 0) == 0) && (text.length == 1)) {
		text = '';
	}
	return text;
}

Array.prototype.lastIndexOf = function(elt /*, from*/)
{
  var len = this.length;

  var from = Number(arguments[1]);
  if (isNaN(from))
  {
    from = len - 1;
  }
  else
  {
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;
    else if (from >= len)
      from = len - 1;
  }

  for (; from > -1; from--)
  {
    if (from in this &&
        this[from] === elt)
      return from;
  }
  return -1;
};

/**
 * 
 * 
 */
Array.prototype.indexOf = function(elt /* , from */) {
	var len = this.length;
	var from = Number(arguments[1]) || 0;
	from = (from < 0) ? Math.ceil(from) : Math.floor(from);
	if (from < 0)
		from += len;

	for (; from < len; from++) {
		if (from in this && this[from] === elt)
			return from;
	}
	return -1;
};


/**
 * 
 * 
 */
Array.prototype.indexOfProductName = function(elt /* , from */) {
	var len = this.length;
	var from = Number(arguments[1]) || 0;
	from = (from < 0) ? Math.ceil(from) : Math.floor(from);
	if (from < 0)
		from += len;

	for (; from < len; from++) {
		if (from in this && this[from].productName === elt)
			return from;
	}
	return -1;
};

function SortByOID(a,b) {
	var A = a.oid
	var B = b.oid
	if (A < B) { // sort string ascending
		return -1
	}
	if (A > B) {
		return 1
	}
	return 0 // default return value (no sorting)	
}

function SortByMedian(a, b) {
	var A = a.median
	var B = b.median
	if (A < B) { // sort string descending
		return 1
	}
	if (A > B) {
		return -1
	}
	return 0 // default return value (no sorting)
};

/**
 * @param str
 *            Holds the string which is to be concatenated.
 * 
 * @validationLogic Checks whether a user first name and last name together
 *                  exceeds more than the value in strSize parameter, if so the
 *                  name will be truncated to the value denoted in strSize
 *                  parameter and appends '..' with the name and returns the
 *                  param str.
 * 
 * @param str
 *            Holds the concatenated value.
 * 
 */
function truncate(str, strSize) {
	if (str.length > strSize) {
		return str.substring(0, (strSize)) + "...";
	} else {
		return str;
	}
};


function GetWindowWidth()

{
        var x = 0;
        if (self.innerHeight)
        {
             x = self.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
             x = document.documentElement.clientWidth;
        }
        else if (document.body)
        {
             x = document.body.clientWidth;
        }
        return x;
}
 
function scrollPosition() {

	  var scrollHeight = 0;
        //Window Height
    /*    if (self.innerHeight)
        {
        	alert(self.scrollTop);
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
        	alert(document.documentElement.scrollTop);
        }
        else if (document.body)
        {
        	alert(document.body.scrollTop);
        }*/
        //Window Scroll bar height
        if (self.scrollHeight)
        {
        	scrollHeight = self.scrollTop;
        }else if (document.body.scrollHeight)
        {
        	scrollHeight = document.body.scrollTop;
        }else if (document.documentElement && document.documentElement.scrollHeight)
        {
        	scrollHeight = document.documentElement.scrollTop;
        }
        return scrollHeight;
}

function GetWindowHeight()
{
		//scrollPosition();
        var windowScrollBarHeight = 0;
        var windowHeight = 0;
        //Window Height
        if (self.innerHeight)
        {
        	windowHeight = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
        	windowHeight = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
        	windowHeight = document.body.clientHeight;
        }
        //Window Scroll bar height
        if (self.scrollHeight)
        {
        	windowScrollBarHeight = self.scrollHeight;
        }else if (document.body.scrollHeight)
        {
        	windowScrollBarHeight = document.body.scrollHeight;
        }else if (document.documentElement && document.documentElement.scrollHeight)
        {
        	windowScrollBarHeight = document.documentElement.scrollHeight;
        }
        if(windowHeight > windowScrollBarHeight) {
        	return windowHeight;
        }else {
        	return windowScrollBarHeight;
        }
}

/**
 * cX 	This variable is assigned with X coordinates of the Mouse pointer.\
 * 		X coordinates of mouse pointer relative to the top-left corner of the 
 *      browser window's client area.\Added with rX value
 * 
 * cY	This variable is assigned with Y coordinates of the Mouse pointer.\
 * 		Y coordinates of mouse pointer relative to the top-left corner of the 
 *      browser window's client area.\Added with rY value
 * 
 * rX   This variable is assigned to the amount of the page's content that has been 
 *      scrolled left 
 * 
 * rY	This variable is assigned to the amount of the page's content that has been 
 *      scrolled Upward
 * 
 */
var cX = 0;
var cY = 0; 
var rX = 0;
var rY = 0;


/**
 * @param el
 * 		  DOM object of the HTML element
 * 
 * @param Id
 * 		  	Id of the particular HTML element, this parameter is used to assign position
 * 		 	for the particular HTML element based on the HTML element location on Mouse over.
 * 
 * @methodLogic Assigning position for the User badge.
 * 
 */
function assignPosition(el, Id) {
	var position = getPos(el);
	Id.style.left = (position.lx + 25) + "px";
	Id.style.top = position.ly  + "px";
	//(position.ly  - document.getElementById(rccdm.id.body).scrollTop)
};

/**
 * @param el
 * 		  DOM object of the HTML element
 * 
 * @param Id
 * 		  	Id of the particular HTML element, this parameter is used to assign position
 * 		 	for the particular HTML element based on the HTML element location on Mouse over.
 * 
 * @methodLogic Assigning position for the User badge.
 * 
 */
function assignTopPosition(el, Id) {
	var position = getPos(el);
	Id.style.left = (position.lx - 295) + "px";
	Id.style.top = (position.ly - 100) + "px";
	//(position.ly  - document.getElementById(rccdm.id.body).scrollTop)
};


/**
 * 
 * @param Id
 * 		  	Id of the particular HTML element
 * 
 * @methodLogic Hiding the User badge
 * 
 */	
function hidePopupContent(Id) {
	if(Id.length < 1 && Id == 0 && Id == "null") { return; }
	var disp = document.getElementById(Id).style.display;
	if (disp == rccdm.style.block)
	{
		document.getElementById(Id).style.display = rccdm.style.none;
	}
};

/**
 * 
 * @param Id
 * 		  	Id of the particular HTML element
 * 
 * @methodLogic Showing the User badge
 */	
function showPopupContent(el, Id, content) {
	if(Id.length < 1) { return; }
	document.getElementById(rccdm.id.TooltipContent).innerHTML = content;
	var getId = document.getElementById(Id);
	assignPosition(el, getId);
	getId.style.display = rccdm.style.block;
};

/**
 * 
 * @param Id
 * 		  	Id of the particular HTML element
 * 
 * @methodLogic Showing the User badge
 */	
function showPopupTopContent(el, Id, content) {
	if(Id.length < 1) { return; }
	document.getElementById(rccdm.id.TooltipContent).innerHTML = content;
	var getId = document.getElementById(Id);
	assignTopPosition(el, getId);
	getId.style.display = rccdm.style.block;
};

/**
 * @param el
 * 		  DOM object of the HTML element
 * 
 * @param getId
 * 		  	getId of the particular HTML element, this parameter is used to assign position
 * 		 	for the particular HTML element based on the HTML element location on Mouse over.
 * 
 * @methodLogic Locating position of the HTML element on Mouse over.
 * 
 */
function getPos(el, getId) {
	for (var lx=0, ly=0;
		el != null;
		lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
		var position = {
						"lx" : lx,
						"ly" : ly
					   };
						
		return position;
}

function validateEmailID(elementId) {
   var reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
   var address = trimAll(document.getElementById(elementId).value);
   if(reg.test(address) == false || address.length == 0) {
      return false;
   }else {
	   return true;
   }
}
function validateNumber(elementId) {
	var reg = /(^-?\d\d*$)/;
	return reg.test(trimAll(document.getElementById(elementId).value));
}
