/**
* COMMON DHTML FUNCTIONS
* These are handy functions I use all the time.
*
* By Seth Banks (webmaster at subimage dot com)
* http://www.subimage.com/
*
* Up to date code can be found at http://www.subimage.com/dhtml/
*
* This code is free for you to use anywhere, just keep this comment block.
*/

/**
* X-browser event handler attachment and detachment
* TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
*
* @argument obj - the object to attach event to
* @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
* @argument fn - function to call
*/
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}
function removeEvent(obj, evType, fn, useCapture) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.detachEvent) {
        var r = obj.detachEvent("on" + evType, fn);
        return r;
    } else {
        alert("Handler could not be removed");
    }
}

/**
* Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
*
* Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
*
* Gets the full width/height because it's different for most browsers.
*/
function getViewportHeight() {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;

    return window.undefined;
}
function getViewportWidth() {
    var offset = 17;
    var width = null;
    if (window.innerWidth != window.undefined) return window.innerWidth;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
    if (document.body) return document.clientWidth;
}

/**
* Gets the real scroll top
*/
function getScrollTop() {


    if (self.pageYOffset) // all except Explorer
    {
        return self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
        return document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        return document.body.scrollTop;
    }
}
function getScrollLeft() {
    if (self.pageXOffset) // all except Explorer
    {
        return self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollLeft)
    // Explorer 6 Strict
    {
        return document.documentElement.scrollLeft;
    }
    else if (document.body) // all other Explorers
    {
        return document.body.scrollLeft;
    }
}

//var standardbody;
//var scroll_top;
//var scroll_left;
//var docwidth;
//var docheight;
//function getviewpoint() { //get window viewpoint numbers
//    //debugger;
//    var ie = document.all && !window.opera
//    var domclientWidth = document.documentElement && parseInt(document.documentElement.clientWidth) || 100000 //Preliminary doc width in non IE browsers
//    standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body //create reference to common "body" across doctypes
//    scroll_top = (ie) ? standardbody.scrollTop : window.pageYOffset
//    scroll_left = (ie) ? standardbody.scrollLeft : window.pageXOffset
//    docwidth = (ie) ? standardbody.clientWidth : (/Safari/i.test(navigator.userAgent)) ? window.innerWidth : Math.min(domclientWidth, window.innerWidth - 16)
//    docheight = (ie) ? standardbody.clientHeight : window.innerHeight
//    docwidth = document.documentElement.clientWidth - 220;
//    docheight = document.documentElement.clientHeight;
//}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=

function decodeBase64(inp) {
    var out = ""; //This is the output
    var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
    var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
    var i = 0; //Position counter

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    var base64test = /[^A-Za-z0-9\+\/\=]/g;

    if (base64test.exec(inp)) { //Do some error checking
        alert("There were invalid base64 characters in the input text.\n" +
		"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
		"Expect errors in decoding.");
    }
    inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do { //Here.s the decode loop.

        //Grab 4 bytes of encoded content.
        enc1 = keyStr.indexOf(inp.charAt(i++));
        enc2 = keyStr.indexOf(inp.charAt(i++));
        enc3 = keyStr.indexOf(inp.charAt(i++));
        enc4 = keyStr.indexOf(inp.charAt(i++));

        //Heres the decode part. There.s really only one way to do it.
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        //Start to output decoded content
        out = out + String.fromCharCode(chr1);

        if (enc3 != 64) {
            out = out + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            out = out + String.fromCharCode(chr3);
        }

        //now clean out the variables used
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

    } while (i < inp.length); //finish off the loop

    //Now return the decoded values.
    return out;
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(plaintext) {
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" + 				// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()"; 				// RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var encoded = "";
    for (var i = 0; i < plaintext.length; i++) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+"; 			// x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert("Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for

    return encoded;

};

function URLDecode(encoded) {
    // Replace + with ' '
    // Replace %xx with equivalent character
    // Put [ERROR] in output if %xx is invalid.
    var HEXCHARS = "0123456789ABCDEFabcdef";
    var plaintext = "";
    var i = 0;
    while (i < encoded.length) {
        var ch = encoded.charAt(i);
        if (ch == "+") {
            plaintext += " ";
            i++;
        } else if (ch == "%") {
            if (i < (encoded.length - 2)
					&& HEXCHARS.indexOf(encoded.charAt(i + 1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i + 2)) != -1) {
                plaintext += unescape(encoded.substr(i, 3));
                i += 3;
            } else {
                alert('Bad escape combination near ...' + encoded.substr(i));
                plaintext += "%[ERROR]";
                i++;
            }
        } else {
            plaintext += ch;
            i++;
        }
    } // while
    return plaintext;
}


function DDSelect(controlID, value) {
    try {
        var obj = document.getElementById(controlID);

        if (!obj) return;
        //		alert(obj.id);
        for (i = obj.options.length - 1; i >= 0; i--) {
            if (obj.options[i].value == value) {
                obj.options[i].selected = true;
            }
        }

    }
    catch (er) {
        alert(er);
    }

}

function GetValues() {

    var inputIDs = '';
    var inputValues = '';
    var inputs = document.getElementsByTagName("input");
    // debugger;
    for (var i = 0; i < inputs.length; i++) {
        // debugger;
        if (inputs[i].type == "text") {
            inputIDs += inputs[i].id + "#";
            inputValues += inputs[i].value + "#";
            SetControlValue(inputs[i].id, inputs[i].value);
        }
    }
    inputIDs = inputIDs.substring(0, inputIDs.length - 1);
    inputValues = inputValues.substring(0, inputValues.length - 1);
    

     //debugger;
    var selects = document.getElementsByTagName("select");

    var selectIDs = '';
    var selectValues = '';
    
    for (var j = 0; j < selects.length; j++) {
        selectIDs += selects[j].id;
        selectIDs += "#";
        selectValues += selects[j].value;
        selectValues += "#";
        SetControlValue(selects[j].id, selects[j].value);
    }
    selectIDs = selectIDs.substring(0, selectIDs.length - 1);
    selectValues = selectValues.substring(0, selectValues.length - 1);
    alert(selectValues);
    ///runMethod('SendCtrPrms', [inputIDs, inputValues, selectIDs, selectValues])
};

//seteaza valuarea unui obiect dupa tipul sau(input, span, textarea, select) 
function SetControlValue(controlID, value) {
    try {
       // debugger;
        if (value) {
            var obj = document.getElementById(controlID);
            if (!obj) return;

            switch (obj.tagName) {
                case "TEXTAREA":
                case "INPUT":
                    obj.innerText = value;
                    break;
                case "SPAN":
                    obj.innerHTML = value;
                    break;
                case "SELECT":
                    DDSelect(controlID, value);
                    break;
            }
        }
    }
    catch (er) {
        alert(er);
    }
}

function getValue(Name, attr) {
    var config = new RegExp(Name + "=([^,]+)", "i") //get name/value config pair (ie: width=400px,)
    return (config.test(attr)) ? parseInt(RegExp.$1) : 0 //return value portion (int), or 0 (false) if none found
   }
   function setMouseOverColor(element) {
   	oldgridSelectedColor = element.style.backgroundColor;
   	//element.style.backgroundColor = '#e2df94';
   	element.style.cursor = 'pointer';
   	// element.style.borderColor = 'red'
   	// element.style.textDecoration='underline';
   }

   
