define(function(require){
	var $ = require('jquery');
    require('uriTemplate')($);
    require('validation')($);
    require('dialog')($);
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/validation.css');
	require('globalization');
	$(function(){
		
		$('#servicingProductsForm').validate({
			rules:{
				serviceId:{
					required:true,
					maxlength:20,
					VerificationSID:true
				},
				serviceDescription:{
					required:true,
					maxlength:30,
					VerificationSDE:true
				},
				avgTransactionActual:{
					//required:true,
					compareval:true,
					regex: /^(([0-9]*[1-9][0-9]*)|())$/ 
				},
				fullUsageHrs:{
					//required:true,
					compareval:true,
					regex:/^(([0-9]*[1-9][0-9]*)|())$/
				},
				avgTransactionProjected:{
					//required:true,
					regex: /^((([0-9]|\,)*(\.[0-9]{1,2})?)|(([0-9]|\.)*(\,[0-9]{1,2})?)|(([0-9]|\')*((\.|\,)[0-9]{1,2})?))$/
				},
				utilizationRateSingle:{
					//required:true,
					regex: /^((([0-9]|\,)*(\.[0-9]{1,2})?)|(([0-9]|\.)*(\,[0-9]{1,2})?)|(([0-9]|\')*((\.|\,)[0-9]{1,2})?))$/
				},
				isRestrictive:{
					required:true
				}
			},
			messages:{
				avgTransactionActual:{
					regex: i18n["Validation.product.mustbepositiveinteger"] 
				},
				fullUsageHrs:{
					regex: i18n["Validation.product.mustbepositiveinteger"]
				},
				avgTransactionProjected:{
					regex: i18n["Validation.product.mustbenumber"]
				},
				utilizationRateSingle:{
					regex: i18n["Validation.product.mustbenumber"]
				},
				isRestrictive:{
					required:i18n["Validation.product.required"]
				}
			},
			  errorPlacement:function (error,element){
				  if(element.attr("name")=="isRestrictive"){
					  error.insertAfter("#isResTips");
				  }else{
					  error.insertAfter(element);
				  }
			   }
		});
		
		$('input[name=isRestrictive]:radio').click(function(){
			var radioValue = $(':radio:checked').val();
			$('input[name=restrictiveDescription]').val('');
			if(radioValue=="Y"){
				$('#rest_desc').show();
				$('#restrictiveDescription').show();
			}else{
				$('#rest_desc').hide();
				$('#restrictiveDescription').hide();
			}
		});
		
        $('input[name=avgTransactionActual]').blur(function(){
        	CalculateATP();
            });
        $('input[name=fullUsageHrs]').blur(function(){
        	CalculateATP();
         });
		
		$.validator.addMethod("compareval",function(value,element,param){
			var ata = $('input[name=avgTransactionActual]').val();
			var fuh =  $('input[name=fullUsageHrs]').val();
			var c = fuh-ata;
			if(ata==0||fuh==0){
				return true;
			}else{
				 if(c<0){
						return false;
					}else{
						return true;
					}
			}
		},i18n["Validation.product.avgtranandfullusage"]);
		
		$.validator.addMethod("VerificationSID",function(value,element,param){
			var serviceIdVal = $('input[name=serviceId]').val();
			var flag = "";
				var urlStr = $("#uniqueUrl").val();
			    $.ajax({
			    	type:"POST",
			    	url:urlStr+".json",
					async:false,
			    	data:{"serviceId":serviceIdVal},
				    success: function(response,ioArgs){
			   		    flag = response.status;
				   	},error:function(response,ioArgs){
				   		alert(ioArgs);
				   	}
			   	});
			return flag;
		},i18n["Validation.product.serviceIdnotrepeat"]);
		$.validator.addMethod("VerificationSDE",function(value,element,param){
			var serviceDescriptionVal = $('input[name=serviceDescription]').val();
			var flag = "";
				var urlStr = $("#uniqueUrl").val();
			    $.ajax({
			    	type:"POST",
			    	url:urlStr+".json",
					async:false,
			    	data:{"serviceDescription":serviceDescriptionVal},
				    success: function(response,ioArgs){
			   		    flag = response.status;
				   	},error:function(response,ioArgs){
				   		alert(ioArgs);
				   	}
			   	});
			return flag;
		},i18n["Validation.product.notrepeat"]);
		
		$("#saveBt").click(function(){
			if($('#servicingProductsForm').valid()){	
				$("#servicingProductsForm").submit();
			}else{
				$.msg.alert(i18n["Page.Dialog.content.inputError"]);
			}
			 return false;
		});
		$('#cancleBt').click(function(){
			var url = $("#cancelUrl").val();
			location.href=url;
		});
	});
	
	function CalculateATP(){
		var ata = $('input[name=avgTransactionActual]').val();
		var fuh =  $('input[name=fullUsageHrs]').val();
		var atp = 24*ata/fuh;
		var urs = 100*ata/fuh;
		if(ata==""||fuh==""||ata==0||fuh==0){
			$('input[name=avgTransactionProjected]').attr('value',"");
			$('input[name=utilizationRateSingle]').attr('value',"");
		}else{
			$('input[name=avgTransactionProjected]').attr('value',Globalize.format(atp,'n2'));
			$('input[name=utilizationRateSingle]').attr('value',Globalize.format(urs,'n2'));
		}
	}
});