/*報表列印與下載提至上一層*/

  jQuery.fn.outer = function() { 
	    return $($('<div></div>').html(this.clone())).html();
	} 
  function exportReportData(type) {				
				var form  = document.getElementById('data_form');
				form.action = form.action + "&save=" + type;				
				form.submit();
	}
	
	function printReport(printUrl){
		var baseUrl = window.location.href;
		if (baseUrl.indexOf("genpage") != -1){
		    baseUrl = baseUrl.substring(0,baseUrl.indexOf("genpage"));
		}else if (baseUrl.indexOf(".php") != -1){
				baseUrl = baseUrl.substring(0,baseUrl.lastIndexOf("/") +1);
		}
		var relativePath = printUrl.replace(/\.\.\//gi,"");
		var buildPath = "";
		if (relativePath.indexOf("/ch/") != -1 || relativePath.indexOf("ch/") == 0){			
			if (relativePath.indexOf("ch/") == 0){
				buildPath = "http://" + window.location.host + "/" + relativePath;
			}else{
				buildPath = "http://" + window.location.host + relativePath;
			}	
		}else{
			buildPath = baseUrl + relativePath;
		}	
		$("#printurl").val(buildPath);		
		newprintWindow = window.open('/ch/inc/autoprintreport3.php','newprintWindow');
	}
	
	function printReport2(printUrl){
		var baseUrl = window.location.href;
		baseUrl = baseUrl.substring(0,baseUrl.lastIndexOf("/") +1);

		var relativePath = printUrl.replace(/\.\.\//gi,"");
		var buildPath = "";
		var buildPath = baseUrl + relativePath;
		
		$("#printurl").val(buildPath);		
		newprintWindow = window.open('/ch/inc/autoprintreport3.php','newprintWindow');
	}
	

 $(document).ready(function() {
    /*報表列印與下載提至上一層*/
    if (typeof($("select[name=select2]").val()) != "undefined" && $("select[name=select2]").val() == "MS"){    	
		  	$("#fixheadpic").hide();
		}
    
    var isSkip = false;
    var currentPage = window.location.href;
    if (currentPage.indexOf("tai50i.php") != -1 || currentPage.indexOf("tai100i.php") != -1 || currentPage.indexOf("taititc.php") != -1 || currentPage.indexOf("taiei.php") != -1 || currentPage.indexOf("taidividi.php") != -1 || currentPage.indexOf("ABP010.php") != -1 || currentPage.indexOf("BWM50U.php") != -1 || currentPage.indexOf("BWM51U.php") != -1){
    	$("body table td a img").each(function(){
    		if ($(this).attr("src").indexOf("print_download.gif") != -1){
    		  if (typeof($(this).attr("onclick")) != 'undefined'){
    		  	var c = new $(this).attr("onclick").toString();
					  if($.browser.msie){					
							var breakChar = /\n/g;
							if (c.indexOf("anonymous()") != -1){												
								c = c.replace(breakChar,'').replace('function anonymous(){','').replace('}','');
							}else if (c.indexOf("onclick()") != -1){
								c = c.replace(breakChar,'').replace('function onclick(){','').replace('}','');
							}
						}						
						var pos1 = c.indexOf("('");
            var pos2 = c.indexOf("',");
            var actionUrl = c.substring(pos1 + 2,pos2);    				
    				var imgPath = $(this).attr("src");					  
						var printimgPath = imgPath.replace("print_download.gif","print.gif");					
    				var a = $(this).parent().get(0);
		    		var newImage = "";
		    	  newImage = "<img src='" + printimgPath + "' onclick=\"printReport2('"+actionUrl+"')\" border=0/>&nbsp;";		    		
		    		$(a).html(newImage);
		    		$(a).css("text-decoration","none");
		    	}
    	  }
		  });		  
    	return;
    }   
    if (currentPage.indexOf("TWT49U") != -1){
    	$("body table td a img").each(function(){
    		if ($(this).attr("src").indexOf("print_download.gif") != -1){
    		  if (typeof($(this).attr("onclick")) != 'undefined'){
    				var c = new $(this).attr("onclick").toString();
    				var imgPath = $(this).attr("src");
					  
					  if($.browser.msie){					
							var breakChar = /\n/g;
							if (c.indexOf("anonymous()") != -1){												
								c = c.replace(breakChar,'').replace('function anonymous(){','').replace('}','');
							}else if (c.indexOf("onclick()") != -1){
								c = c.replace(breakChar,'').replace('function onclick(){','').replace('}','');
							}
						}
						var pos1 = c.indexOf("('");
            var pos2 = c.indexOf("',");
            var actionUrl = c.substring(pos1 + 2,pos2);		
						if (c.indexOf("type=list") == -1){
							var pos = c.indexOf("',");
              c = c.substring(0,pos) + "&save=list" + c.substring(pos);
						}
						
						var printimgPath = imgPath.replace("print_download.gif","print.gif");
						var htmlimgPath = imgPath.replace("print_download.gif","save_html.gif");
						var csvimgPath = imgPath.replace("print_download.gif","save_csv.gif");
						
						var htmlClick = c.replace("type=list","type=html").replace("save=list","save=htm");
						var csvClick = c.replace("type=list","type=csv").replace("save=list","save=csv");
						
    				var a = $(this).parent().get(0);
		    		var newImage = "";
		    	  newImage = "<img src='" + printimgPath + "' onclick=\"printReport('"+actionUrl+"')\" border=0/>&nbsp;";
		    		newImage = newImage + "<img src='" + htmlimgPath + "' onclick=\"" + htmlClick + "\" border=0/>&nbsp;";
		    		newImage = newImage + "<img src='" + csvimgPath + "' onclick=\"" + csvClick + "\" border=0/>";
		    		$(a).html(newImage);
		    		$(a).css("text-decoration","none");
		    	}
    	  }
		  });
    }     
    if (typeof($("#html").val()) == "undefined" && typeof($("input:hidden[name=html]").val()) == "undefined" ){    	    	
      $("#contentblock table td a img").each(function(){      
    		if ($(this).attr("src").indexOf("print_download.gif") != -1){    			
    		  if (typeof($(this).attr("onclick")) != 'undefined'){
    				var c = new $(this).attr("onclick").toString();
    				var imgPath = $(this).attr("src");					 
					  if($.browser.msie){					
							var breakChar = /\n/g;
							if (c.indexOf("anonymous()") != -1){												
								c = c.replace(breakChar,'').replace('function anonymous(){','').replace('}','');
							}else if (c.indexOf("onclick()") != -1){
								c = c.replace(breakChar,'').replace('function onclick(){','').replace('}','');
							}
						}
						var pos1 = c.indexOf("('");
            var pos2 = c.indexOf("',");
            var actionUrl = c.substring(pos1 + 2,pos2); 		
						if (c.indexOf("type=list") == -1){
							var pos = c.indexOf("',");
              c = c.substring(0,pos) + "&save=list" + c.substring(pos);
						}
						
						var printimgPath = imgPath.replace("print_download.gif","print.gif");
						var htmlimgPath = imgPath.replace("print_download.gif","save_html.gif");
						var csvimgPath = imgPath.replace("print_download.gif","save_csv.gif");
						
						var htmlClick = c.replace("type=list","type=html").replace("save=list","save=htm");
						var csvClick = c.replace("type=list","type=csv").replace("save=list","save=csv");
						
    				var a = $(this).parent().get(0);
		    		var newImage = "";
		    	  newImage = "<img src='" + printimgPath + "' onclick=\"printReport('"+actionUrl+"')\" border=0/>&nbsp;";
		    		newImage = newImage + "<img src='" + htmlimgPath + "' onclick=\"" + htmlClick + "\" border=0/>&nbsp;";
		    		newImage = newImage + "<img src='" + csvimgPath + "' onclick=\"" + csvClick + "\" border=0/>";
		    		$(a).html(newImage);
		    		$(a).css("text-decoration","none");
		    	}
    	  }
		  });		  		  		 
		}else{
			var actionUrl = "";			
			$("#contentblock table td a img, #contentblock table td img").each(function(){
    		if ($(this).attr("src").indexOf("print_download.gif") != -1){
    		  if (typeof($(this).attr("onclick")) != 'undefined'){
    				var c = new $(this).attr("onclick").toString();
    				var imgPath = $(this).attr("src");
					  if($.browser.msie){					
							var breakChar = /\n/g;
							if (c.indexOf("anonymous()") != -1){												
								c = c.replace(breakChar,'').replace('function anonymous(){','').replace('}','');
							}else if (c.indexOf("onclick()") != -1){
								c = c.replace(breakChar,'').replace('function onclick(){','').replace('}','');
							}
						}			
						var cc = "";
    				if (c.indexOf("getData()") != -1){
    						var path = window.location.pathname;
								cc = path.substring(0,path.indexOf(".php")) + "_print.php";
								if (cc.indexOf("BFIAUU") != -1){
									cc = cc.replace("/en/","/ch/");
								}
    				}
						
						var pos1 = c.indexOf("('");
            var pos2 = c.indexOf("',");
            actionUrl = c.substring(pos1 + 2,pos2);
            if (cc != ""){
            	actionUrl = cc + "?language=ch";
            }
						var printimgPath = imgPath.replace("print_download.gif","print.gif");
						var htmlimgPath = imgPath.replace("print_download.gif","save_html.gif");
						var csvimgPath = imgPath.replace("print_download.gif","save_csv.gif");
												
    				var a = $(this).parent().get(0);
		    		var newImage = "";
		    		newImage = "<img src='" + printimgPath + "' onclick=\"window.open(\'/ch/inc/autoprintreport.php\');\" border=0/>&nbsp;";
		    		if (cc != ""){
		    			newImage = newImage + "<img src='" + htmlimgPath + "' onclick=\"exportReportData('html');\" border=0/>&nbsp;";	
		    		}else{
		    			newImage = newImage + "<img src='" + htmlimgPath + "' onclick=\"exportReportData('htm');\" border=0/>&nbsp;";
		    		}
		    		newImage = newImage + "<img src='" + csvimgPath + "' onclick=\"exportReportData('csv');\" border=0/>";
		    		$(a).html(newImage);
		    		$(a).css("text-decoration","none");
		    	}
    	  }
		  });
		  if (currentPage.indexOf("MI_QFIIS_cat.php") == -1){		  	
				$("input:hidden[id=html], input:hidden[id=dirname],input:hidden[name=html],input:hidden[name=dirname]").wrapAll("<form id='data_form' action='"+actionUrl+"' method='post'></form>");
			}else{
				$("input:hidden[name=html]").val($("#data_table").outer());				
			}
		}
		if (typeof($("select[name=select2]").val()) == "undefined" || ( $("select[name=select2]").val() != "ALL" &&  currentPage.indexOf('TWT54U.php') !=-1)){    	
	    if ($("#tbl-container").html()){
		    $("#tbl-container tbody td,#tbl-container tbody td *").each(function(){
		      if ($.trim($(this).html()) == "" || $.trim($(this).html()) == "<!-- 1 2 3 4  -->"){       
		        $(this).html("&nbsp;");
		      }
		    });
	  	}else if ($("#tbl-containerx").html()){
	  		$("#tbl-containerx tbody td,#tbl-containerx tbody td *").each(function(){
		      if ($.trim($(this).html()) == "" || $.trim($(this).html()) == "<!-- 1 2 3 4  -->"){       
		        $(this).html("&nbsp;");
		      }
		    });
	  	}
  	}		
  });
	
	