/**************************** PROTOTYPES 1.1 *********************************/

/****** TRIM 
    *****************************************/

String.prototype.trim = function() {   
	// skip leading and trailing whitespace  
	// and return everything in between   
	var x=this;   
	x=x.replace(/^\s*(.*)/, "$1");   
	x=x.replace(/(.*?)\s*$/, "$1");   
	return x; 
}

/******* Strip First Char
	*************************************/
String.prototype.stripFirstChar = function() {   
	var newData = '';
	var x=this;   
	for(i = 0; i < x.length; i++) {
	        if(i > 0) {
	            newData = newData + data[i];
	        }
    	}
	x = newData;
	
	return x; 
}

/******* Strip Chars
	**************************************/
String.prototype.stripChars = function(amt, start) {   
	
	var newData = '';
	var x=this; 
	var endPos = ((start * 1) + (amt * 1)) * 1 - 1 ;
	for(i = 0; i < x.length; i++) {
	        if(i < start || i > endPos) {
	            newData = newData + x.charAt(i);
	        }
    	}
		
	x = newData;
	
	return x; 
}

/*******
	Finds the id in the document; 
	easy replacement for document.getElementById()
	***********************************************/

// Replaced with same function in prototype.js
//function $()
//{
//    var elements = new Array();
//    for (var i = 0; i < arguments.length; i++) {
//        var element = arguments[i];
//        if (typeof element == 'string') {
//            if (document.getElementById) {
//                element = document.getElementById(element);
//            }
//            else if (document.all) {
//                element = document.all[element];
//            }
//        }
//        if (arguments.length == 1) {
//            return element;
//        }
//        elements.push(element);
//    }
//    return elements;
//}

/*******
	Strip First Character
	********
function stripFirstChar(data) {
    var newData = '';

    for(i = 0; i < data.length; i++) {
        if(i > 0) {
            newData = newData + data[i];
        }
    }

    return newData;
}
*************/
/*******
	Strip an Amount of Characters from this String starting at position X
	*******************************
function stripChars(data, amt, start) {
    var newData = '';
    var endPos = ((start * 1) + (amt * 1)) * 1 - 1 ;

    for(i = 0; i < data.length; i++) {
        if(i < start || i > endPos) {
            newData = newData + data.charAt(i);
        }
    }

    return newData;
}
***************************************/
/*******
	Is this an Array Type
	*********************/
function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
/*******
	Is this an boolean Type
	*********************/
function isBoolean(a) {
    return typeof a == 'boolean';
}
/*******
	Is this Empty
	**************/
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
/*******
	Is this an Function Type
	*********************/
function isFunction(a) {
    return typeof a == 'function';
}
/*******
	Is this Null
	*************/
function isNull(a) {
    return typeof a == 'object' && !a;
}
/*******
	Is this a Number Type
	*********************/
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
/*******
	Is this an Object Type
	*********************/
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
/*******
	Is this an String Type
	*********************/
function isString(a) {
    return typeof a == 'string';
}
/*******
	Is this an Undefined Type
	*********************/
function isUndefined(a) {
    return typeof a == 'undefined';
} 

/***********
    ARRAY UTILS
    *****************/
    
function setFieldOnArray(tmpArray, tmpField) {
    tmpArray[getNextArrayLocation(tmpArray)] = tmpField.name;
}
function showRequiredFields(tmpArray) {
    if(isArray(tmpArray) && tmpArray.length > 0) {
        var fieldNames = '\n';
        for(var i = 0; i < tmpArray.length; i++) {
           fieldNames = fieldNames + tmpArray[i] + "\n";
        }
        alert("Required Fields: "+fieldNames);
    }
}
function getNextArrayLocation(tmpArray) {

    if(isArray(tmpArray)) {
        if(tmpArray.length == 0) {
            return 0;
        }
        return (tmpArray.length * 1);
    }
}    

function csvToArray(mycsv) {
	if(isString(mycsv)) {
		var dataSplit = mycsv.split(",");
				
		return dataSplit;
	}
}
/******
    Find Coordinates
    *****************/
function getPageOffsetLeft(el) {

    var x;
    // Return the x coordinate of an element relative to the page.
    x = el.offsetLeft;
    if (el.offsetParent != null) {
        x += getPageOffsetLeft(el.offsetParent);
    }
    return x;
}

function getPageOffsetTop(el) {

        var y;
        // Return the x coordinate of an element relative to the page.
        y = el.offsetTop;
        if (el.offsetParent != null) {
                y += getPageOffsetTop(el.offsetParent);
        }
        return y;
}

/*************** START -Extend Element Object ********************/
function CustomElement(){}

CustomElement.prototype.setAtributeOther = function(name, value) {
	if (bVer() >= 6) {
		var x = this;
		var attr = document.createAttribute(name);
		attr.value = value;
    		x.setAttributeNode(attr);
	}
	else {
    		var x = this;
    		x.setAttribute(name, value);
    	}
}

/*************
	Allows you to override Objects by adding the inter methods to the
	original object
	******************************************************************/
function applyInherit(original, inter)
{
	for (method in inter){
		original[method] = inter[method];
	}
	return original;
}

/**************
    Allows setting maxlength on textareas
    *******************************************************************/
function setMaxLength() {
    
    var x = document.getElementsByTagName('textarea');
    var counter = document.createElement('div');
    counter.className = 'counter';
    for (var i=0;i<x.length;i++) {
        if (x[i].getAttribute('maxlength')) {
            var counterClone = counter.cloneNode(true);
            counterClone.relatedElement = x[i];
            counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
            x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
            x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

            x[i].onkeyup = x[i].onchange = checkMaxLength;
            x[i].onkeyup();
        }
    }
}

function checkMaxLength() {
    var maxLength = this.getAttribute('maxlength');
    var currentLength = this.value.length;
    if (currentLength > maxLength) {
        this.relatedElement.className = 'toomuch';
        this.value = this.value.substring(0, this.value.length - 1);
        currentLength = this.value.length;
    }
    else {
        this.relatedElement.className = '';
    }
    this.relatedElement.firstChild.nodeValue = currentLength;
    // not innerHTML
}