if( !$.welflex ){
	$.weflex = {};
	$.weflex.form = {};
	$.weflex.ajax = {};
	$.weflex.widget = {};
}


(
function(){
	
	/*
	 * 数据验证正则
	 */
	var ValidateRegexEnum = {};
	ValidateRegexEnum = {
		Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
		isChinesePhone : /^[0]{0,1}(13|15|18|14){1}(\d){1}(\d){8}$/,
		Mobile : /^((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(15[389]\d{8}))$/,
		Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/,
		Ip : /^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$/,
		Currency : /^\d+(\.\d+)?$/,
		Number : /^\d+$/,
		Zip : /^[1-9]\d{5}$/,
		QQ : /^[1-9]\d{4,8}$/,
		English : /^[A-Za-z]+$/,
		Chinese :  /^[\u0391-\uFFE5]+$/,
		UserName : /^[a-z]\w{3,19}$/i,
		Integer : /^[-\+]?\d+$/,
		PasswordLength : 6
	};
	
	/**
	 * Core
	 */
	
	
	function arrayHas( arrayValues , value ){
		var rtn = false;
		$.each(arrayValues, function(){  
			if( this == value ){
				rtn = true;
			}
		});  
		return rtn;
	}
	
	function getImage( imageUrl , width , height ){
		$image_name 	= imageUrl.split( "." )[0];
		$image_pix 	= imageUrl.split( "." )[1];
		return $image_name + "_"+width+"x"+height+ "." + $image_pix;
	}
	
	function isEmail( value ){
		var flag = ValidateRegexEnum.Email.test( value );
		return flag;
	}
	
	function isEmpty( value ){
		value = trim( value );
		if( value == '' ){
			return true;
		}
		else{
			return false;
		}
	}
	
	function isSame( value_a , value_b ){
		if( value_a == value_b ){
			return true;
		}
		
		return false;
	}
	
	function trim(  str, charlist  ){
		var whitespace, l = 0, i = 0;
		str += '';

		if (!charlist) {
			// default list
			whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
		} else {
			// preg_quote custom list
			charlist += '';
			whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g,
					'\$1');
		}

		l = str.length;
		for (i = 0; i < l; i++) {
			if (whitespace.indexOf(str.charAt(i)) === -1) {
				str = str.substring(i);
				break;
			}
		}

		l = str.length;
		for (i = l - 1; i >= 0; i--) {
			if (whitespace.indexOf(str.charAt(i)) === -1) {
				str = str.substring(0, i + 1);
				break;
			}
		}

		return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
	}
	
	/**
	 * HTML 
	 */
	
	/**
	 * checkbox
	 */
	
	var CheckBox = {};
	
	CheckBox.val = function( ctlName , values ){
		
		if( values ){
			CheckBox.setVal( ctlName , values );
		}
		
	}
	
	CheckBox.setVal = function( ctlName , values ){
		$('input[name='+ctlName+']').each(function(n){
		 	if( arrayHas( values , this.value ) ){
		 		this.checked = true;
		 	}else{
		 		this.checked = false;
		 	}
		 });
	}
	
	/**
	 * radio
	 */
	
	var Radio = {};
	
	Radio.val = function( ctlName , value ){
		
		if( value ){
			Radio.setVal( ctlName , value );
		}
		
	}
	
	Radio.setVal = function( ctlName , value ){
		$('input[name='+ctlName+']').each(function(n){
		 	if( this.value == value ){
		 		this.checked = true;
		 	}else{
		 		this.checked = false;
		 	}
		 });
	}
	
	/**
	 * Form
	 */
	
	var FormClass = {}
	
	FormClass.post = function( url , data ){
        var form = document.createElement('form');
        form.action = url;
        form.method = 'POST';
        $.each(data, function(key, val) {  
        	var textfield = document.createElement('input');
			textfield.type = 'hidden';
			textfield.name  = key;
            textfield.value = val;
            form.appendChild(textfield);
        }); 
        document.body.appendChild(form);
        form.submit();
	}
	



	/**
	 * parse Jquery Ajax Response To Json
	 */
	function praseAjaxResponse( response ){
		
		var response_text = '';
		if('object'== typeof(response) && response.responseText)
		{
			response_text = response.responseText;
		}
		else
		{
			response_text = response;
		}
		
		var rst = null;
		try {
			eval('var rst=' + response_text);
		}
		catch(ex)
		{
			//do nothing
		}
		return rst;

	}
	
	
	/**
	 * date widget
	 */
	function dateSel( yearSel , monthSel , dateSel ){
		
		yearSel = $('#' + yearSel);
		monthSel = $('#' + monthSel);
		dateSel = $('#' + dateSel);
		//yearSel.html('');
		//monthSel.html('');
		//dateSel.html('');
		
		//yearOptionHtml = '<option value="0">Year</option>';
		yearOptionHtml = '';
		for( var i = 2010 ; i > 1900 ; i-- ){
			yearOptionHtml += '<option value="'+i+'">'+i+'</option>'
		}  
		yearSel.append(yearOptionHtml);
		
		//monthOptionHtml = '<option value="0">Month</option>';
		monthOptionHtml = '';
		for( var i = 1 ; i <= 12 ; i++ ){
			monthOptionHtml += '<option value="'+i+'">'+i+'</option>'
		}  
		monthSel.append(monthOptionHtml);
		
		//dateOptionHtml = '<option value="0">Date</option>';
		dateOptionHtml = '';
		for( var i = 1 ; i <= 31 ; i++ ){
			dateOptionHtml += '<option value="'+i+'">'+i+'</option>'
		}  
		dateSel.append(dateOptionHtml);
		
	}
	
	
	
	$.weflex.isEmail = isEmail;
	$.weflex.isEmpty = isEmpty;
	$.weflex.isSame  = isSame;
	
	$.weflex.getimage = getImage;
	$.weflex.checkbox = CheckBox;
	$.weflex.radio = Radio;
	
	
	
	$.weflex.form = FormClass;
	$.weflex.ajax.prase = praseAjaxResponse;
	
	
	$.weflex.widget.datesel = dateSel;
	
}		
)();
