/////////////////////////////////////////////////////////////////
//
// Web Beget Framework & Application under
// Copyright © 2010 Robb Garrioch, robb@datamenus.com
// All rights reserved
//
/////////////////////////////////////////////////////////////////




function set_opacity(obj, value)
{
    obj.style.opacity = value/10;
    // correct order to work on all IE versions
    obj.style['-ms-filter'] = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + (value*10) + ')'; // first!
    obj.style.filter = 'alpha(opacity=' + value*10 + ')';                                         // 2nd!
}


function writeresponse(elem, content)
{
    elem.innerHTML = content;
}








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=1," +
           "location=1," +
           "directories=0," +
           "status=1," +
           "menubar=1," +
           "scrollbars=1," +
           "resizable=1"

    var winobj = window.open(page, name, args);
    winobj.focus();
    return winobj;
}




function makestr(selst)
{
    var multisel = document.frm[selst];
    var len = multisel.length
    var a = new Array();

    var opt;
    var z = 0;
    for(var x = 0; x < len; x++)
    {
        opt = multisel.options[x];
        if(opt.selected)
        {
            a[z] = opt.value;
            z++;
        }
    }
    return a.join();
}


var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autotab(input,len, e)
{
    var keyCode = (isNN) ? e.which : e.keyCode; 
    if(input.value.length >= len)
    {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
    }
    function getIndex(input)
    {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
    return true;
}






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 iscurrency(val, n)
{
    //alert("val: " + val + " converted: " + num2money(val)  + " name: " + n);

    if(!isfloat(val))
        return false;
    else
    {
        // Format to currency 0.00
        eval("document.getElementById('frmid')." + n + ".value = num2money(val);");
        return true;
    }
}

function isfloat(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;
}

// Original:  Cyanide_7 (leo7278@hotmail.com)
// Web Site:  http://www7.ewebcity.com/cyanide7
function num2money(num)
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+''+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '' + num + '.' + cents);
}

function sum(x, y)
{
   x = x - 0;
   y = y - 0;
   return (x + y);
}



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;
}






// The credits below were as found.
// I tried to find the original owner but to no avail.
// Credits:
// "whole credits are devoted to orignal author"
var gototoptype = -1;
var gototopinterval = 0;

function goto_top_timer() {
    var y = (gototoptype == 1) ? document.documentElement.scrollTop : document.body.scrollTop;

    var moveby = 15; // set this to control scroll seed. minimum is fast

    y -= Math.ceil(y * moveby / 100);
    if (y < 0)
        y = 0;

    if (gototoptype == 1)
        document.documentElement.scrollTop = y;
    else
        document.body.scrollTop = y;

    if (y == 0) {
        clearInterval(gototopinterval);
        gototopinterval = 0;
    }
}

function goto_top()
{
    if (gototopinterval == 0)
    {
        if (document.documentElement && document.documentElement.scrollTop)
            gototoptype = 1;
        else if (document.body && document.body.scrollTop)
            gototoptype = 2;
        else
            gototoptype = 0;

        if (gototoptype > 0)
            gototopinterval = setInterval('goto_top_timer()', 25);
    }
}


function calcheight()
{
    //find the height of the internal page
    var the_height= document.getElementById('ifid').contentWindow.document.body.scrollHeight;

    //change the height of the iframe
    document.getElementById('ifid').height= the_height;
}


