﻿// ------------------------------------------------ //
/* Generic Javascript Functions */
// ------------------------------------------------ //
$C = function(/*String*/obj) {
    if( $(obj) ) {
        return obj;
    } else if ( $('ctl00_ApplicationContent_' + obj) ){
        return 'ctl00_ApplicationContent_' + obj;    
    }
};
// ------------------------------------------------ //
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
// ------------------------------------------------ //
cookie_delete = function(/*String*/name) {
	//summary: Deletes a cookie with the given name.
	dojo.io.cookie.setCookie(name, "-", 0);
};
// ------------------------------------------------ //
cookie_get = function(/*String*/name) {
	//summary: Gets a cookie with the given name.
	var idx = document.cookie.lastIndexOf(name+'=');
	if(idx == -1) { return null; }
	var value = document.cookie.substring(idx+name.length+1);
	var end = value.indexOf(';');
	if(end == -1) { end = value.length; }
	value = value.substring(0, end);
	value = unescape(value);
	return value; //String
};
// ------------------------------------------------ //
cookie_set = function(/*String*/name, /*String*/value, /*Number?*/days, /*String?*/path, /*String?*/domain, /*boolean?*/secure) {
	//summary: sets a cookie.
	var expires = -1;
	if(typeof days == "number" && days >= 0) {
		var d = new Date();
		d.setTime(d.getTime()+(days*24*60*60*1000));
		expires = d.toGMTString();
	}
	value = escape(value);
	document.cookie = name + "=" + value + ";" + (expires != -1 ? " expires=" + expires + ";" : "") + (path ? "path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "; secure" : "");
};
// ------------------------------------------------ //
event_add = function(/*Object*/elm,/*String*/evType, /*Object*/fn, /*Boolean*/useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
};
// ------------------------------------------------ //
form_submit = function( var_form, var_source ) {
    //summary: submit dynamically generated forms.
    var str_action = var_form.action;
    var str_querystring = '';
    var str_url = '';
    
    for( i = 0; i < var_form.elements.length; i++ ) {
        if( typeof var_form.elements[i].id != 'undefined' ) {
            if( var_form.elements[i].id.indexOf('SEARCH_TXT_') >= 0 ) {
                str_querystring = str_querystring + "&" + var_form.elements[i].id + "=" + var_form.elements[i].value;
            }
        }
    }
    if( arguments.length == 3 ) {
        str_querystring = "";
        for( i = 0; i < arguments[2].split(';').length; i++ ) {
            if( arguments[2].split(';')[i].indexOf('=') >= 0 ) {
                str_querystring = str_querystring + "&" + arguments[2].split(';')[i].split('=')[0] + "=" + arguments[2].split(';')[i].split('=')[1];
            }
        }
    }
    
    if( str_action.indexOf('?') >= 0 ) { str_action = str_action.split('?')[0]; }

    str_url = str_action + '?source=' + var_source + str_querystring;
    
    if( str_url.indexOf('/') >= 0 ) { str_url = str_url.split('/')[str_action.split('/').length - 1];}
    location.href = str_url;
};
// ------------------------------------------------ //
isObject = function (a){
     //return (typeof a == 'object' && !!a) || isFunction(a);
     return (typeof a == 'object' && !!a);
};
// ------------------------------------------------ //
insert_after = function(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
};
// ------------------------------------------------ //
var m_obj_CSSRules = {};
modifyCSS = function( varSelectorText, varJSKey, varValue, varCSSKey ) {
    var m_boo_found = false;
	var m_str_rules;
	
	if( m_obj_CSSRules[varSelectorText] ) {
	    m_obj_CSSRules[varSelectorText].style[varJSKey] = varValue;
	} else {
	    if( document.all ) {
		    m_str_rules = 'rules';
	    } else if( document.getElementById ) {
		    m_str_rules = 'cssRules';
	    }
	    for( var stylesheet_index = 0; stylesheet_index < document.styleSheets.length; stylesheet_index++ ){
		    for( var rule_index = 0; rule_index < document.styleSheets[stylesheet_index][m_str_rules].length; rule_index++ ) {
			    if( document.styleSheets[stylesheet_index][m_str_rules][rule_index].selectorText == varSelectorText ) {
				    document.styleSheets[stylesheet_index][m_str_rules][rule_index].style[varJSKey] = varValue;
				    m_obj_CSSRules[varSelectorText] = document.styleSheets[stylesheet_index][m_str_rules][rule_index];
				    m_boo_found = true;
			    }
		    }
	    }
	    if( !m_boo_found && varCSSKey ) {
	        var m_int_length_stylesheet = document.styleSheets.length;
	        if( m_int_length_stylesheet > 0 ) {
	            var m_int_length_rules = document.styleSheets[m_int_length_stylesheet - 1][m_str_rules].length;
	            if( document.styleSheets[m_int_length_stylesheet - 1].insertRule ) {
	                document.styleSheets[m_int_length_stylesheet - 1].insertRule( varSelectorText + ' { ' + varCSSKey + ': ' + varValue + '; }', m_int_length_rules );
	            } else if( document.styleSheets[m_int_length_stylesheet - 1].addRule ) {
	                document.styleSheets[m_int_length_stylesheet - 1].addRule( varSelectorText, varCSSKey + ': ' + varValue );
	            }
	        }
	    }
	}
};
// ------------------------------------------------ //
node_value_get = function ( var_node ) {
    //summary: cross broswer get node value function
    if( document.all ) {
        return var_node.text;
    } else {
        return var_node.childNodes[0].nodeValue;
    }
};
// ------------------------------------------------ //
rand = function( n ){
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
};
// ------------------------------------------------ //
setChildClassById = function(node,tag,className) {
	if ( node == null ){ node = document; }
	if ( tag == null ) { tag = '*'; }
	var parent = document.getElementById(tag);
	var childOne = parent.firstChild;
   	while ( childOne != parent.lastChild ){
		if ( childOne.nodeType == 1 ) {
			childOne.className = className;
		}
		childOne = childOne.nextSibling;
	}
	return true;
};
// ------------------------------------------------ //
window_onload = function(/*Object (Function Name) */func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		};
	}
};
// ------------------------------------------------ //
xml2json = function(xml){
    //summary: create json object from xml
    var output = {};
        output.row = [];
    var row = xml.getElementsByTagName("row");
        for(i=0;i<row.length;i++){
            output.row[i] = [];
            var column = row[i].getElementsByTagName("column");
            for(j=0;j<column.length;j++){
                output.row[i][column[j].getAttribute("name")] = node_value_get( column[j] );
                output.row[i][column[j].getAttribute("name")]["display_name"] = column[j].getAttribute("display_name");
            }
        }
        return output;
};
// ------------------------------------------------ //
_json2jsonstring = function( var_json ) {
    var m_int_counter = -1;
    var m_str_json = '';
    
    if( typeof var_json != 'undefined' ) {
        if( var_json.type == 'hash' ) {
            for( var i in var_json ) {
                if( i != 'type' && i != 'value' ) {
                    m_int_counter++;
                    if( m_int_counter > 0 ) {
                        m_str_json += ', ';
                    }
                    m_str_json += '"' + i + '": '+ _json2jsonstring( var_json[i] );
                }
            }
            m_str_json = '{ ' + m_str_json + ' }';
        } else if( var_json.type == 'array' ) {
            for( var i = 0; i < var_json.length; i++ ) {
                if( i > 0 ) {
                    m_str_json += ', ';
                }
                m_str_json += _json2jsonstring( var_json[i] );
            }
            m_str_json = '[ ' + m_str_json + ' ]';
        } else {
            m_str_json = '"' + var_json + '"';
        }
    }

    return m_str_json;
}
// ------------------------------------------------ //
_xml2json = function( var_node, var_is_child ) {
    var m_obj_json = {};
    m_obj_json.type = 'hash';

    m_obj_json[ 'value' ] = var_node.xml;
    if( var_node.hasChildNodes == false || ( var_node.hasChildNodes && var_node.childNodes.length == 1 && var_node.childNodes[0].nodeType == 3 ) ) {
        m_obj_json[ 'InnerText' ] = ( document.all ) ? var_node.text : var_node.textContent;
        m_obj_json[ 'InnerText' ].type = 'string';
    } else {
        if( typeof var_is_child == 'undefined' || var_is_child == false ) {
            m_obj_json[ var_node.nodeName ] = {};
            m_obj_json[ var_node.nodeName ].type = 'hash'
            for( var i = 0; i < var_node.childNodes.length; i++ ) {
                if( typeof m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ] == 'undefined' ) {
                    m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ] = {};
                    m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ].type = 'hash';
                } else {
                    m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ] = [];
                    m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ].type = 'array';
                }
            }
            for( var i = 0; i < var_node.childNodes.length; i++ ) {
                if( m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ].type == 'array' ) {
                    m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ].push( _xml2json( var_node.childNodes[i], true ) );
                } else {
                    m_obj_json[ var_node.nodeName ][ var_node.childNodes[i].nodeName ] = _xml2json( var_node.childNodes[i], true );
                }
            }
        } else {
            for( var i = 0; i < var_node.childNodes.length; i++ ) {
                if( typeof m_obj_json[ var_node.childNodes[i].nodeName ] == 'undefined' ) {
                    m_obj_json[ var_node.childNodes[i].nodeName ] = {};
                    m_obj_json[ var_node.childNodes[i].nodeName ].type = 'hash';
                } else {
                    m_obj_json[ var_node.childNodes[i].nodeName ] = [];
                    m_obj_json[ var_node.childNodes[i].nodeName ].type = 'array';
                }
            }
            for( var i = 0; i < var_node.childNodes.length; i++ ) {
                if( m_obj_json[ var_node.childNodes[i].nodeName ].type == 'array' ) {
                    m_obj_json[ var_node.childNodes[i].nodeName ].push( _xml2json( var_node.childNodes[i], true ) );
                } else {
                    m_obj_json[ var_node.childNodes[i].nodeName ] = _xml2json( var_node.childNodes[i], true );
                }
            }
        }
    }

    if( var_node.attributes.length > 0 ) {
        m_obj_json[ 'Attributes' ] = {};
        m_obj_json[ 'Attributes' ].type = 'hash';
        for( var i = 0; i < var_node.attributes.length; i++ ) {
            m_obj_json[ 'Attributes' ][ var_node.attributes[i].name ] = var_node.attributes[i].value;
        }
    }

    return m_obj_json;
}
// ------------------------------------------------ //
_xml2jsonstring = function( var_node ) {
    return _json2jsonstring( _xml2json( var_node ) );
}
// ------------------------------------------------ //

