/////////////////////////////////////////////////////////////////
//
// Web Beget Framework & Application under
// Copyright © 2008 Robb Garrioch, robb@datamenus.com
// All rights reserved
//
/////////////////////////////////////////////////////////////////




// Original code block by Simon Willison:
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addloadevent(func)
{
    var oldonload = window.onload;
    if(typeof window.onload != 'function')
        window.onload = func;
    else
    {
        window.onload = function() {
            oldonload();
            if(document.createElement&&document.getElementById&&document.getElementsByTagName)
            {
                func();
            }
        }
    }
}

function showlayer(id)
{
    var idobj = getstyleobj(id);

    if(idobj)
    {
        idobj.visibility = 'visible';
        return true;
    }
    else
        return false;
}





function hidelayer(id)
{
    var idobj = getstyleobj(id);

    if(idobj)
    {
        idobj.visibility = 'hidden';
        return true;
    }
    else
        return false;
}



function buildquerystring(frm)
{
    frm.onsubmit = function() {
        for(var i = 0; i < this.elements.length; i++)
        {
            if((this.elements[i].value != "") && (this.elements[i].type != "submit"))
            {
                qs += this.elements[i].name;
                qs += "=";
                qs += encodeURIComponent(this.elements[i].value);
                qs += "&";
            }
        }
        return qs;
    }
}


function set_opacity(obj, value)
{
    obj.style.opacity = value/10;
    obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}


function writeresponse(elem, content)
{
    elem.innerHTML = content;
}

function getstyleobj(id)
{
    if(document.getElementById && document.getElementById(id))
    {
	return document.getElementById(id).style;
    }
    else if (document.all && document.all(id))
    {  
 	return document.all(id).style;
    } 
    else if (document.layers && document.layers[id])
    { 
 	return document.layers[id];
    }
    else
 	return false;
}


function display(id)
{
    var idobj = getstyleobj(id);

    if(idobj)
    {
        idobj.display = 'block';
        return true;
    }
    else
        return false;
}


function displaynone(id)
{
    var idobj = getstyleobj(id);

    if(idobj)
    {
        idobj.display = 'none';
        return true;
    }
    else
        return false;
}


function open_window(page, name, width, height, top, left, center)
{
    var args
    if((parseInt(navigator.appVersion) >= 4 ) && (center != "no"))
    {
        if((center == "yes") || (center == "left"))
            left = (screen.width - width) / 2;
        if((center == "yes") || (center == "top"))
            top = (screen.height - height) / 2;
    }
    args = "width=" + width + "," + 
           "height=" + height + "," +
           "top=" + top +"," +
           "left=" + left + "," +
           "toolbar=0," +
           "location=0," +
           "directories=0," +
           "status=0," +
           "menubar=0," +
           "scrollbars=0," +
           "resizable=0"

    var winobj = window.open(page, name, args);
    winobj.focus();
    return winobj;
}






function isnum(val)
{
   var validchars = "0123456789";
   var isnum = true;
   var c;

   for(i = 0; i < val.length && isnum == true; i++) 
   { 
      c = val.charAt(i); 
      if(validchars.indexOf(c) == -1) 
      {
         isnum = false;
      }
   }
   return isnum;
}


function isfloat(val, currency_format)
{
   var validchars = ".0123456789";
   var isnum = true;
   var c;

   for(i = 0; i < val.length && isnum == true; i++) 
   { 
      c = val.charAt(i); 
      if(validchars.indexOf(c) == -1) 
      {
         isnum = false;
      }
   }
   return isnum;
}


function isvalidateemail(email)
{
    if (email.length < 7 ||
    email.indexOf(" ") != -1 ||
    email.indexOf("@.") != -1 ||
    email.indexOf("-.") != -1 ||
    email.indexOf("_.") != -1 ||
    email.indexOf("..") != -1 ||
    email.indexOf("._") != -1 ||
    email.indexOf(".-") != -1 ||
    email.indexOf(".@") != -1 ||
    email.indexOf("@-") != -1 ||
    email.indexOf("@_") != -1 ||
    email.indexOf("@") != email.lastIndexOf("@") ||
    email.indexOf("@") == -1 ||
    email.indexOf(".") == -1 ||
    (email.length - (email.lastIndexOf(".") + 1)) < 2)
        return false;
    else
        return true;
}







function getElementsByTagNames(list,obj)
{
    if(!obj) var obj = document;
    var tagNames = list.split(',');
    var resultArray = new Array();
    for(var i = 0; i < tagNames.length; i++)
    {
        var tags = obj.getElementsByTagName(tagNames[i]);
        for (var j = 0; j < tags.length; j++)
        {
            if(tags[j].getAttribute('id') != null)
            {
                resultArray.push(tags[j].getAttribute('id'));
            }
        }
    }
//    var testNode = resultArray[0];
//    if(testNode.sourceIndex)
//    {
//        resultArray.sort(function (a,b) {
//                return a.sourceIndex - b.sourceIndex;
//        });
//    }
//    else if(testNode.compareDocumentPosition)
//    {
//        resultArray.sort(function (a,b) {
//                return 3 - (a.compareDocumentPosition(b) & 6);
//        });
//    }
    return resultArray;
}







