var text_value;
//beanName = input text name
//beanName_value = input text value
//imgId = img id
//appletId = 欲顯示的文字欄位 <td id="appletId" style="width:20%"><input type="text" name="user_name" id="user_name" size='30' />
//obj = img obj
function useApplet(beanName,beanName_value, imgId, appletId, obj, imagePath){
  var one_two = parseInt(jQuery(obj).attr("value"));
  document.getElementById(imgId).src=imagePath+one_two+".jpg"
  if((one_two%2) == 1){
  	text_value = document.getElementById("applet_"+beanName).getText();
    one_two = one_two + 1;
    jQuery('#'+appletId).html("<input type='text' name='"+beanName+"' id='"+beanName+"' value='"+beanName_value+"' size='30' />")
    jQuery('#'+beanName).val(text_value);
    jQuery(obj).attr("value", "2");  
  }else{
     jQuery(obj).attr("value", "1");
  		text_value = document.getElementById(beanName).value;
      one_two = one_two - 1;
      jQuery('#'+appletId)
      .html("<applet name= 'applet_"+beanName+"'"+ 
      "id= 'applet_"+beanName+"'"+  
      "codebase= 'http://61.57.42.134/CNSServlet/'"+  
      "archive= 'CNSAppletSigned.jar'"+  
      "code= 'org.cmex.cns.CNSTextFieldApplet.class'"+  
      "width= '225'"+  
      "height= '25'"+  
      "hspace= '0'"+  
      "vspace= '0'"+ 
      "align= 'absmiddle'>"+ 
      "<param name = 'initial_focus' value = 'false'>"+ 
      "<param name = 'Editable' value = 'true'>"+ 
      "<param name = 'MultiLine' value = 'false'>"+ 
      "<param name = 'MaxLength' value = '-1'>"+ 
      "<param name = 'SysFont' value = 'rome'>"+ 
      "<param name = 'CNSFont' value = 'KaiCGI'>"+ 
      "<param name = 'FontSize' value = '13'>"+ 
      "<param name = 'IME' value = 'ChangJei|Phonetic|CNS'>"+ 
      "<param name = 'Coder' value = 'Coder'>"+ 
      "<param name = 'Encoding' value = 'NATIVE:PCC'>"+ 
      "<param name = 'Text' value = '"+text_value+"'>"+ 
      "<param name = 'Timeout' value = '3000'>"+ 
      "<param name = 'LineFeed' value = '@LF@'>"+ 
      "<param name = 'FailKey' value = 'none'>"+ 
      "<param name = 'EventHook' value = '#1005:appletBlur(\"applet_"+beanName+"\",\"applet_"+beanName+"\")'>"+ 
      "</applet>"+ 
      "<input type='hidden' name='"+beanName+"' id='"+beanName+"' value='"+beanName_value+"'/>")      
     // document.getElementById("applet_"+beanName).setText(text_value);      
      jQuery('#'+beanName).val(text_value);
  }
  
}
//beanName = iue;nput text name
//beanName_value = input text value
//imgId = img id
//appletId = 欲顯示的文字欄位 <td id="appletId" style="width:20%"><input type="text" name="user_name" id="user_name" size='30' />
//imagePath = image path
function toUseApplet(beanName,beanName_value, imgId, appletId, imagePath){  
  document.getElementById(imgId).src=imagePath+"2.jpg"
  text_value = beanName_value;
  jQuery('#'+imgId).attr("value", "1");
  jQuery('#'+appletId) 
 	 .html("<applet name= 'applet_"+beanName+"'"+ 
	  "id= 'applet_"+beanName+"'"+  
	  "codebase= 'http://61.57.42.134/CNSServlet/'"+  
	  "archive= 'CNSAppletSigned.jar'"+  
	  "code= 'org.cmex.cns.CNSTextFieldApplet.class'"+  
	  "width= '225'"+  
	  "height= '25'"+  
	  "hspace= '0'"+  
	  "vspace= '0'"+ 
	  "align= 'absmiddle'>"+ 
	  "<param name = 'initial_focus' value = 'false'>"+ 
	  "<param name = 'Editable' value = 'true'>"+ 
	  "<param name = 'MultiLine' value = 'false'>"+ 
	  "<param name = 'MaxLength' value = '-1'>"+ 
	  "<param name = 'SysFont' value = 'rome'>"+ 
	  "<param name = 'CNSFont' value = 'KaiCGI'>"+ 
	  "<param name = 'FontSize' value = '13'>"+ 
	  "<param name = 'IME' value = 'ChangJei|Phonetic|CNS'>"+ 
	  "<param name = 'Coder' value = 'Coder'>"+ 
	  "<param name = 'Encoding' value = 'NATIVE:PCC'>"+ 
	  "<param name = 'Text' value = '"+text_value+"'>"+ 
	  "<param name = 'Timeout' value = '3000'>"+ 
	  "<param name = 'LineFeed' value = '@LF@'>"+ 
	  "<param name = 'FailKey' value = 'none'>"+ 
	  "<param name = 'EventHook' value = '#1005:appletBlur(\"applet_"+beanName+"\",\"applet_"+beanName+"\")'>"+ 
	  "</applet>"+ 
	  "<input type='hidden' name='"+beanName+"' id='"+beanName+"' value='"+beanName_value+"'/>")      
  //document.getElementById("applet_"+beanName).setText(text_value);      
  jQuery('#'+beanName).val(text_value);  
}
function appletBlur(id,name) {
  var bean = name;
  var appletName = bean.substring(7,bean.length);
  jQuery('#'+appletName).val(document.getElementById(bean).getText());
}

function getLength(id){
	var stringWords = document.getElementById(id).value;
	var stringLength = document.getElementById(id).value.length;
	if(stringWords.indexOf("<page>") != -1){
		var count = stringWords.split("<page>");
		stringLength = stringLength - (30 * (count.length-1));
	}
	return stringLength;
}

( function( $ ) {
    var cns = function() {
        this.expCNS = /<page>[\da-f]{1,2}<\/page><code>[\da-f]{1,4}<\/code>/ig;
        this.transParams = { CNSFont: "KaiCGI", Coder: "Coder", Align: "absmiddle", Timeout: "3000" };
        this.transOptions = { size: 13, backgroundColor: 0xEFF1F1, color: 0x0035AD };
        this.cnsAttrs = { width: "155", height: "22", align: "absbottom" };
        this.cnsParams = { initial_focus: "false", Editable: "true", MultiLine: "false", MaxLength: "-1", SysFont: "新細明體", CNSFont: "KaiCGI", FontSize: "13", IME: "ChangJei|Phonetic|CNS", Coder: "Coder", Encoding: "NATIVE:PCC" }; //LineFeed, FailKey
        this.doubleWord = /[^\x00-\xff]/;
        
        this.addTransApplet = function( params ) {
            if( $("applet[name=CNSTrans]").length > 0 ) { return; }
            
            params = $.extend( {}, this.transParams, params );
            var appletHtml = "<applet name='CNSTrans' codebase='http://61.57.42.134/CNSServlet/' archive='CNSAppletSigned.jar' code='org.cmex.cns.CNSTransApplet.class' width='0' height='0' hspace='0' vspace='0' align='middle'>";
            for( var name in params ) { appletHtml += "<param name='" + name + "' value='" + params[name] + "' />"; }
            $("body:last").append( $(appletHtml + "</applet>") );
        };
    };
    
    /* 字串是否含有難字 */
    cns.prototype.hasCNS = function( value ) {
        return typeof value === "string" ? ( value.search( this.expCNS ) > -1 ) : false;
    };
 
    /* 字串是否含有難字及雙字節 */
    cns.prototype.hasCNSDoubleWd = function( value ) {
        return typeof value === "string" ? ( value.search( this.expCNS ) > -1  
        		|| value.search( this.doubleWord ) > -1 ) : false;
    };
    
    /* 字串難字字數 */
    cns.prototype.count = function( value ) {
        return typeof value === "string" ? ( value.match( this.expCNS ) || [] ).length : -1;
    };
    
    /* 字串總字數 */
    cns.prototype.countAll = function( value ) {
        return typeof value === "string" ? value.replace( this.expCNS, "1" ).length : -1;
    };
    cns.prototype.cnsLength = function( value ) {
        return typeof value === "string" ? value.replace( this.expCNS, "1" ).length : null;
    };
    
    /* 難字字串長度檢核 */
    cns.prototype.verify = function( value, max ) {
        return this.count( value ) <= 2 && this.countAll( value ) <= max;
    };
    
    /* 將難字字元轉成圖片顯示 */
    cns.prototype.convert = function( value, options, params ) {
        if( !this.hasCNS( value ) ) { return value; }
        this.addTransApplet( params );
        options = $.extend( {}, this.transOptions, options );
        return document.CNSTrans.transform( value, "NATIVE:PCC", options.size, options.backgroundColor, options.color, options.backup );
    };
    
    /* 新增難字輸入元件 */
    cns.prototype.create = function( container, attrs, params ) {
        attrs = $.extend( {}, this.cnsAttrs, attrs ); // id, name
        params = $.extend( {}, this.cnsParams, params ); // Text, EventHook
        
        var appletHtml = "<applet codebase='http://61.57.42.134/CNSServlet' archive='CNSAppletSigned.jar' code='org.cmex.cns.CNSTextFieldApplet.class' hspace='0' vspace='0'";
        for( var attr in attrs ) { appletHtml += " " + attr + "='" + attrs[attr] + "'"; }
        appletHtml += ">";
        for( var name in params ) { appletHtml += "<param name='" + name + "' value='" + params[name] + "' />"; }
        
        if( typeof container === "string" ) { container = $(container); }
        container.html( $(appletHtml + "</applet>") );
    };
    
    $.cns = new cns();
} )( jQuery );