﻿if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
  };
}

function getElementsByClassName(classname, node) {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

//create function, it expects 2 values.
function insertAfter(newElement,targetElement) {
    //target is what you want it to go after. Look for this elements parent.
    var parent = targetElement.parentNode;
    //if the parents lastchild is the targetElement...
    if(parent.lastchild == targetElement) {
    //add the newElement after the target element.
        parent.appendChild(newElement);
    } 
    else {
    // else the target has siblings, insert the new element between the target and it's next sibling.
    parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

function insertImageAfterElementByClass(className,image){
    elements = getElementsByClassName(className);
    for(i=0;i<elements.length;i++){
        tmpimg = document.createElement('img');
        tmpimg.src = image;
        insertAfter(tmpimg, elements[i]);
    }
}

function insertLinksIcons(){
    insertImageAfterElementByClass('NewLink','/misitio/images/newicon.jpg');
    insertImageAfterElementByClass('ExternalLink','/misitio/images/externallink.gif');
}

function FixFormAction()
{
    var action = window.location;
    
    var formActionBase = document.getElementById('FormActionBase');
    if (formActionBase != null)
    {
        action = formActionBase.value;
        
        var actionStr = action;
        if (actionStr.endsWith(document.forms[0].action))
        {
            return;
        }
    }
    
    document.forms[0].action = action;
}

/* 
 * http://www.mediacollege.com/internet/javascript/form/limit-characters.html
 */
function InputLimitText(limitField, limitNum)
{
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}
