var _emptyTags = {"IMG": true, "BR": true, "INPUT": true, "META": true, "LINK": true, "PARAM": true, "HR": true};

(function($) {    
    $.fn.mtgcTemplate = function(_remove){
        if (_remove==undefined) _remove = true; 
		var str = '';
		this.each(function() {
		    if (this.tagName){
				str += $.mtgcOuterHTML(this);		
				if(_remove) $(this).replaceWith('');
            };
         });
		                 
        return str;
    };
    
    $.fn.outerBugFix = function(tag, str){
	    var pos = str.indexOf(tag + '=#{');
	    while(pos>-1){
	        var strAppo = str.substr(pos);
	        var subStr = str.substr(pos, strAppo.indexOf('}') + 1); 
	        var newStr = subStr.split('=')[0].toLowerCase() + '=\"' + subStr.split('=')[1] + '\"';
	        str = str.replace(subStr, newStr);
	        pos = str.indexOf(tag + '=#{');
	    };
	    return str;
	};
	
	$.mtgcOuterHTML = function(element){	   
	      var strResult;
	      if (element.outerHTML) {
            strResult = element.outerHTML;
          } else {
			var attrs = element.attributes;
            strResult = "<" + element.tagName.toLowerCase();
            for (var i = 0; i < attrs.length; i++)
                if ((attrs[i].value) && (attrs[i].value!='') && (attrs[i].value!='null')) {
                    if ((attrs[i].name!='hideFocus') && (attrs[i].name!='contentEditable') && (attrs[i].name!='disabled') && (attrs[i].name!='tabIndex')) {                   
                        strResult += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";
                    };
                };

            if (_emptyTags[element.tagName]) {
                strResult += " />";
            } else {
                strResult += ">" + element.innerHTML + "</" + element.tagName.toLowerCase() + ">";
            };
          };
                 
          strResult = strResult.replace(/%7B/g, "{");
          strResult = strResult.replace(/%7D/g,"}");
		  strResult = $.fn.outerBugFix('value', strResult);
		  strResult = $.fn.outerBugFix('onclick', strResult);
		  strResult = $.fn.outerBugFix('id', strResult);
		  strResult = $.fn.outerBugFix('title', strResult);
		
          return strResult;
    };
    
    $.getCookie = function(_name){
        var cookieValue = '';    
        var cookie = document.cookie.split(';');
        for (var i=0;i<cookie.length;i++){
            if ((cookie[i].indexOf(_name + '=') == 0) || (cookie[i].indexOf(' ' + _name + '=') == 0)){
                cookieValue = cookie[i].substring(cookie[i].indexOf('=')+1);
                return cookieValue;
            }
        };
        
        return cookieValue;
    };

    $.removeCookie = function(_name){
        document.cookie = _name +  "=''; expires = Sat, 01 Jan 2000 00:00:00 GMT; path=/";
    };
    
    $.fn.removeElement = function(_tagStop){
        var _parent = $(this).parents(_tagStop);
          
        _parent.remove();
    };
    $.fn.moveUP = function(_tagStop){
        var _parent = $(this).parents(_tagStop)[0];
        var _prev = $(_parent).prev()[0];
                        
        if(_prev){
            _parent.parentNode.insertBefore(_parent, _prev);
        };
    };
    $.fn.moveDOWN = function(_tagStop) {
        var _parent = $(this).parents(_tagStop)[0];
        var _next = $(_parent).next()[0];
        
        if(_next){
            _next.parentNode.insertBefore(_next, _parent);
        };
    };
    $.fn.jPrintArea = function(_cssPrint){
        var iframe=document.createElement('IFRAME');
        var doc=null;
        
        $(iframe).attr('style','position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
        document.body.appendChild(iframe);
        doc=iframe.contentWindow.document;
        
        doc.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
        doc.write('<html xmlns="http://www.w3.org/1999/xhtml" >');
        doc.write('<head>');
        doc.write('<title>' + this.attr("title") + '</title>');
        doc.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
        var links=window.document.getElementsByTagName('link');
        for(var i=0;i<links.length;i++)
            if(links[i].rel.toLowerCase()=='stylesheet')
                doc.write('<link type="text/css" rel="stylesheet" href="' + links[i].href + '"></link>');
        
        if(_cssPrint) doc.write('<link type="text/css" rel="stylesheet" href="' + _cssPrint + '"></link>');
        doc.write('</head>');
        doc.write('<body>');
        doc.write('<div class="' + this.attr("class") + '">' + this.html() + '</div>');
        doc.write('</body>');
        doc.write('</html>');
        doc.close();
        iframe.contentWindow.focus();
        iframe.contentWindow.print();
        setTimeout("$('iframe').remove()", 5000);
        
        return this;
    };
    $.getCookie = function getCookie(_name){
        var cookieValue = '';    
        var cookie = document.cookie.split(';');
        for (var i=0;i<cookie.length;i++){
            if ((cookie[i].indexOf(_name + '=') == 0) || (cookie[i].indexOf(' ' + _name + '=') == 0)){
                cookieValue = cookie[i].substring(cookie[i].indexOf('=')+1);
                return cookieValue;
            }
        };
        
        return cookieValue;
    }; 
    $.removeCookie = function(_name){
        document.cookie = _name +  "=''; expires = Sat, 01 Jan 2000 00:00:00 GMT; path=/";
    };
})(jQuery);


function showFlash(){
	$("object,embed").css("visibility","visible");
}

function hideFlash(){
	$("object,embed").css("visibility","hidden");
}

function showSelectBoxes(){
	$("select").css("visibility","visible");
}

function hideSelectBoxes(){
	$("select").css("visibility","hidden");
}

function sanitizeXML(_xml) {
    var sanitizeXML = _xml;
    
    sanitizeXML = sanitizeXML.replace(/\?/g,'§inter§');   
    sanitizeXML = sanitizeXML.replace(/\+/g,'§plus§');
    sanitizeXML = sanitizeXML.replace(/\%/g,'§perc§');
    sanitizeXML = sanitizeXML.replace(/&/g, '£');
    sanitizeXML = sanitizeXML.replace(/=/g, '§');
          
    return sanitizeXML;
}

function sanitizeXML_Mail(_xml) {
    var sanitizeXML = _xml;
      
    sanitizeXML = sanitizeXML.replace(/\?/g,'§inter§');   
    sanitizeXML = sanitizeXML.replace(/\+/g,'§plus§');
    sanitizeXML = sanitizeXML.replace(/\%/g,'§perc§');
    sanitizeXML = sanitizeXML.replace(/&/g, '£');
    sanitizeXML = sanitizeXML.replace(/=/g, '§');         
                 
    return sanitizeXML;
}