var ZUI = ZUI || {};
(function($) {
	var smsChar = function(char) {
		this.char = char;
		this.codeType = 'ASCII';
		this.count = {'ASCII' : 1, 'UNICODE' : 1};
		this.code = $.GSMEncode(char);
		this.encode();
	};	
	
//用16进制数组表示的基本GSM字符集
	smsChar.standCode = ["000A", "0020", "0021", "0022", "0023", "0024", "0025", "0026", "0027", "0028", "0029", "002A", "002B", "002C", "002D", "002E", "002F", "0030", "0031", "0032", "0033", "0034", "0035", "0036", "0037", "0038", "0039", "003A", "003B", "003C", "003D", "003E", "003F", "0040", "0041", "0042", "0043", "0044", "0045", "0046", "0047", "0048", "0049", "004A", "004B", "004C", "004D", "004E", "004F", "0050", "0051", "0052", "0053", "0054", "0055", "0056", "0057", "0058", "0059", "005A", "005F", "0061", "0062", "0063", "0064", "0065", "0066", "0067", "0068", "0069", "006A", "006B", "006C", "006D", "006E", "006F", "0070", "0071", "0072", "0073", "0074", "0075", "0076", "0077", "0078", "0079", "007A", "00A1", "00A3", "00A4", "00A5", "00A7", "00C4", "00C5", "00C6", "00C7", "00C9", "00D1", "00D6", "00D8", "00DC", "00E0", "00E4", "00E5", "00E6", "00E8", "00E9", "00EC", "00F1", "00F2", "00F6", "00F8", "00F9", "00FC", "0393", "0394", "0398", "039B", "039E", "03A0", "03A3", "03A6", "03A8", "03A9"];
//用16进制数组表示的扩展GSM字符集
	smsChar.extendCode = ["005B", "005C", "005D", "005E", "007B", "007C", "007D", "007E", "20AC"];
	smsChar.exceed = false;
	
	smsChar.prototype = {
		encode : function() {
			if((-1 == $.inArray(this.code, smsChar.standCode)) && 
				(-1 == $.inArray(this.code, smsChar.extendCode))) {
				this.count['ASCII'] = 2;
			}
			if (-1 < $.inArray(this.code.toString(16), smsChar.extendCode)) {
				this.count['ASCII'] = 2;
			}
		}
	};
	
	smsChar.sum = function(strs) {
		var smsCharArr = []
		var codeType = 'ASCII';
		var count = {'ASCII' : 0, 'UNICODE' : 0};
		for(var i=strs.length; i--; ) {
			smsCharArr[i] = new smsChar(strs.charAt(i));
			count['ASCII'] += smsCharArr[i].count['ASCII'];
		}
		return  count[codeType];
	}
	//合法性验证
	var vaild = false;
	$('#optionContainer :input[type = text]').live('keyup',function(){
		var str = $(this).val();
		var count = smsChar.sum(str) * 1;
		var input = $(this).parents('ul').find('li[class = errorMsg]');
        var restriction_length = 320;

        if (m_StorageLocation != '0')
        {
            restriction_length = restriction['ContactItemNameLen'];
            if ( $(this).attr('rel') == 'PhoneNumber' || $(this).attr('rel') == 'PhoneNumber1' || $(this).attr('rel') == 'PhoneNumber2' )
            {         
                restriction_length = restriction['ContactItemPhoneNumberLen'];
            }
            if ( $(this).attr('rel') == 'Email' )
            {
                restriction_length = restriction['ContactItemEmailLen'];
            }   
        }
		if(count > restriction_length){
			input.html(WORDS_TOO_LONG).show();
			recaculate(input);
			vaild = false;
			return ;
		}else{
			input.html('').hide().parents('ul').css('height','30px');
			vaild = true;
		}
		if( ($(this).attr('rel') == 'PhoneNumber') || ($(this).attr('rel') == 'PhoneNumber1') || ($(this).attr('rel') == 'PhoneNumber2')){
			var reg = /^[\d\-\s*#+\;\,\.\(\)]*$/;
			if(!reg.test(str)){
				input.html(WORDS_ILLEGAL).show();
				recaculate(input);
				return ;
			}else{
				input.html('').hide().parents('ul').css('height','30px');
			}
		}
	})
	$('input[rel = Email]').live('blur',function(){
			if(!vaild) return ;
			var str = $(this).val();
            var count = smsChar.sum(str) * 1;
			var input = $(this).parents('ul').find('li[class = errorMsg]');
			if(str == '') return ;
            
			var reg = /^[\w\-\.]{1,}@[\w\-]{1,}(\.[\da-zA-Z]{1,}){1,}$/;
			if(!reg.test(str)){
				input.html(MAILBOX_ILLEGAL).show();
				recaculate(input);
				return ;
			}else{
				input.html('').hide().parents('ul').css('height','30px');
			}
	})
    
	var recaculate = function(input){
		var height = input.css('height');
		height = height.substring(0,height.length-2) * 1;
		input.parents('ul').css('height',35+height + 'px');
	}
})(jQuery)