/* Upload Files */
function onkeyPress(e)
{
    var key = window.event ? e.keyCode : e.which;
    if (key == 13)
    StartClick();
    e.cancelBubble = true;
    e.returnValue = false;
    return false;
}

function Validate(invalidFile, invalidType) 
{ 
    var objUpload=eval("document.getElementById('ctl00_MainContentPlaceHolder_filename')");
    var sUpload=objUpload.value;  
    if(sUpload!="") 
    { 
        var iExt=sUpload.indexOf("\\"); 
        var iDot=sUpload.indexOf("."); 
   
        if((iExt < 0 ) || (iDot < 0)) 
        { 
             alert(invalidFile); 
             objUpload.focus();             
             event.returnValue=false;             
            return false;
        } 
        if(iDot > 0)
        { 
           var aUpload=sUpload.split("."); 
           if(aUpload[aUpload.length-1]!="jpg" && aUpload[aUpload.length-1]!="JPG" && aUpload[aUpload.length-1]!="bmp" && aUpload[aUpload.length-1]!="png") 
           {                  
                 alert(invalidType); 
                 objUpload.focus(); 
                 event.returnValue=false; 
                 return false;
           }
       }      
    } 
}
/* end Upload Files */

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','/images/alerts/newicon.jpg');
    insertImageAfterElementByClass('ExternalLink','/images/alerts/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);
	}
}
// finds 'y' value of given object
function FindYPosition(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curtop];
    }
}

// scroll window in order to make the control visible
function ScrollTo(id) {
    var element = document.getElementById(id);
    var yPos = FindYPosition(element);
    yPos -= 10;
    window.scroll(0, yPos);
}

function EncodeHtml(decoded) {
    return decoded.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function DecodeHtml(encoded) {
    var decoded = jQuery('<textarea/>').html(encoded).val();
    return decoded;
}
