
/*******************************************************************
Returns the HTML element with the given id
Alternatively if you pass multiple ids, it will return an array of
elements with those ids
*******************************************************************/
function Id(elmId) {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

//this will add the innerText of the specified element to the clipboard
function copyElementSource(sourceElm) {
    //YOU MUST USE 'Text' as the identifier...no access to others            
    copyText($get(sourceElm).innerText);
}

function copyText(strValue) {
    //YOU MUST USE 'Text' as the identifier...no access to others            
    window.clipboardData.setData('Text', strValue);
}

/*****************************************************************
Form Validation
*****************************************************************/
function isWhiteSpaceOnly(strValue) {
    if(strValue.match(/^\s+$/gi))
        return true;
        
    return false;
}

function isNullOrEmpty(strValue) {
    //true if either of these is true
    return (isWhiteSpaceOnly(strValue) || strValue == null || strValue.length == 0);
}

function isValidEmail(strValue) {
    if(isNullOrEmpty(strValue))
        return false;   
    
    //this pattern could use some work 
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/;
    
    if(strValue.toString().match(pattern))
        return true;
        
    return false;
}

function isValidPostalCode(postal) {
    //strip '-' and ' '
    postal = postal.replace(/[\-\s]/g, "")
    
    //A1A1A1?
    if(postal.match(/^([A-Z]\d){3}$/i))
        return true;
        
    return false;
}

function isValidPhone(phone) {
    //strip everything but numbers
    phone = phone.replace(/[^\d]/g, "");
    
    //ten numbers?
    if(phone.match(/^\d{10}$/))
        return true;
        
    return false;
}

/*****************************************************************
String Extensions...if you don't like them...don't use them!
*****************************************************************/
String.prototype.endsWith = function(suffix) {
    return (this.substr(this.length - suffix.length) === suffix);
}

String.prototype.startsWith = function(prefix) {
    return (this.substr(0, prefix.length) === prefix);
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.trimEnd = function() {
    return this.replace(/\s+$/, '');
}

String.prototype.trimStart = function() {
    return this.replace(/^\s+/, '');
}

String.prototype.isNullOrEmpty = function() {
    isNullOrEmpty(this);
}

String.formatCustom = function(fmtString, args) {
    if(args == null || args.length <= 0)
        return fmtString;
        
    //regex pattern replacement
    var re;    
    for(var i=0;i<args.length;i++) {
        if(fmtString.indexOf("{"+ i +"}") >= 0) {
            re = new RegExp("\\{"+ i +"\\}", "g");            
            fmtString = fmtString.replace(re, args[i].toString());
        }
    }    
    
    return fmtString;
}

/******************************************************************
Generic Position Functions
******************************************************************/
function getPosition(e) {
	var left = 0;
	var top = 0;

	while (e.offsetParent) {
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}

	left += e.offsetLeft;
	top += e.offsetTop;

	return {x:left, y:top};
}

function getMouseCoords(e) {
    e = e || window.event;
	if(e.pageX || e.pageY){
		return {x:e.pageX, y:e.pageY};
	}
	return {
		x:e.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:e.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function getMouseOffset(target, e) {
	e = e || window.event;

	var docPos = getPosition(target);
	var mousePos = getMouseCoords(e);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function containsPoint(elm, x, y) {
    var topLeft = getPosition(elm);
    var bottomRight = {x:(topLeft.x + elm.clientWidth),y:(topLeft.y + elm.clientHeight)};
    
    return (topLeft.x <= x && x <= bottomRight.x && topLeft.y <= y && y <= bottomRight.y);
}

/******************************************
This fixes a bug with the RadEditor,
height/width auto adjusts when switching 
views
******************************************/
function OnClientModeChange(editor) { 
    //reference to the HTML mode content area
    var htmlArea = document.getElementById("RadEContentTextarea" + editor.Id); 
    
    htmlArea.style.width = (editor.GetWidth()-100) + "px"; 
    htmlArea.style.height = (editor.GetHeight()-100) + "px"; 
} 

var V51_Common_Loaded = true;