define(function(require){
	var $ = require('jquery');
    require('validation')($);
    require('datepickerCN')($);
    require('dialog')($);
    require('form')($); 
	var IFA = require('util'); 
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css'); 
	require('../../../styles/cim/validation.css');
	$('#companyForm').ajaxForm({success:function(){
		$.msg.alert(i18n['Company.Profile.Save.Success'],function(){
			location.href=$("#indexUrl").val().trim();
		});
	}});
	$(function(){
		$("#link").keyup(function(event){
			if(event.keyCode == 8 || event.keyCode == 46){
				$("#link").val('');
			}								
		});
		$(":input[name='isCredibility']").bind("click",function(){
			if(this.value=="Y"){
				 $("#crLimitsTh").css("display","inline");
				 $("#crLimitsTd").css('display','inline');
			}else{
				$("#creditLimits").val("0.00");
				$("#crLimitsTh").css("display","none");
				$("#crLimitsTd").css('display','none');
			}
			
		});
		$("#saveButton").bind("click",formSave);
		$("#cancleButton").bind("click",function(){
			location.href=$("#indexUrl").val().trim();
		});
		$(":radio[name='isCredibility']").bind("click",function(){
			if(this.value=="Y"){
				 $("#crLimitsTh").css("display","inline");
				 $("#crLimitsTd").css('display','inline');
			}else if(this.value=="N"){
				$("#creditLimits").val("0.00");
				$("#crLimitsTh").css("display","none");
				$("#crLimitsTd").css('display','none');
			}
			
		});
		requiredReg("requiredReg","&nbsp;");
		formValidate();
		fileType("fileType",/gif|GIF|jpg|JPG|bmp|BMP|jpeg|JPEG|png|PNG|^$/,i18n['Company.Profile.Image.Only']);
	});
	var formSeriarlize=$("#companyForm").serialize();
	//保存数据
	function formSave(){
	 var saveFormSeriarlize=$("#companyForm").serialize();
     if(formSeriarlize!=saveFormSeriarlize){
		if($('#companyForm').valid()){
			$.msg.confirm(i18n['Page.Dialog.title'],i18n['Page.title.updateSure'],function(result){
				if(result){
						$("#companyForm").submit();
				}
			});
	}else{
		$.msg.alert(i18n['Company.Profile.Form.FillCorrect']);
	}
     }else{
    	 if($("#link").val()){
 			$.msg.confirm(i18n['Page.Dialog.title'],i18n['Page.title.updateSure'],function(result){
 				if(result){
 					$("#companyForm").submit();
 				}
 			});
 	 }
 	 else{
		  $.msg.alert(i18n['Company.Profile.Form.NotModified']);}
		  }
	}
	//验证表单
	function formValidate(){
		$("#companyForm").validate({
		rules:{
			isCredibility:{
			    required:true
			},
			accountPeriod:{
				required:true,
				digits:true
			},
			link:{
				fileType:true
			},
			creditLimits:{
				requiredReg:true,
				regex:/^(([0-9]+[\.]?[0-9]+)|[0-9])$/,
				number:true
			}},
			errorPlacement:function(error,element){
				$("<span />").html(error).insertAfter(element);
			}
		});
	}
//是否需要验证
function requiredReg(method,message){
	$.validator.addMethod(method,function(value,element,params){
		var credibility=$(":radio[name='isCredibility':checked]").val();
		if(credibility=='Y'){
			if($("#creditLimits").val()!=""){
				return true;
				}else{
					return false;
					}
			}else{
				$("#creditLimits").val("0.00");
				return true;
		}
	},message);
}

function fileType(method,regex,message){
	$.validator.addMethod(method,function(value,element,params){
		value=value.trim();
		var fileArray=value.split(".");
		var ext=fileArray[fileArray.length-1];
		if(regex.test(ext)){
			if(ext){
			 $("#picLable").css("display","none");}
			else{
			 $("#picLable").css('display','inline');
			}
			return true;
		}else{
			return false;
		}
	},message);
}
});

