function limitChar(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}
function modifyQuestionGroup(id,code,csrftoken)
{
	var desc=$("#descId_"+id).html();
	$("#id").val(id);
	$("#code").val(code);
	$("#description").val(desc);
	$("#addQtnGroupId").show();
	$("#submitDoc").hide();
	$("#updateDoc").show();
	
	$("#msgDivCode").html("");
	$("#msgDivCode").hide();
	
	$("#msgDivDesc").html("");
	$("#msgDivDesc").hide();
	
}

	
function addMoreQuestionGroup()
{
	$("#id").val(0);
	$("#code").val("");
	$("#description").val("");
	$("#addQtnGroupId").show();
	$("#submitDoc").show();
	$("#updateDoc").hide();
	
	$("#msgDivCode").html("");
	$("#msgDivCode").hide();
	
	$("#msgDivDesc").html("");
	$("#msgDivDesc").hide();
}

function checkString(csrftoken)
{
	var desc = $("#description").html();
	var code = $("#code").val();
	var  msg = "";
	if(code=="")
	{
		msg="Please enter code <br/>";
		$("#msgDivCode").html(msg);
		$("#msgDivCode").show();
	}
	else
	{
		$("#msgDivCode").html("");
		$("#msgDivCode").hide();
	}
	if(desc=="")
	{
		msg="Please enter description <br/>";
		$("#msgDivDesc").html(msg);
		$("#msgDivDesc").show();
	}
	else
	{
		$("#msgDivDesc").html("");
		$("#msgDivDesc").hide();
	}
	if(msg == "")
	{
		var len = $("#description").html().length;
		if(len <= 100)
			Controller.onSubmitWithCsrf('questionGroupForm',csrftoken,'submitDoc',null,null);
		else
		{
			msg="You have entered "+len+" characters. Please do not enter more than 100 characters in the description field <br/>";
			$("#msgDivDesc").html(msg);
			$("#msgDivDesc").show();
		}
	}
}

function updateQuestionAndDo(formName,csrfToken,next)
{
	var data=$("form[name="+formName+"]").serialize();
	data=data+"&"+csrfToken;
	var url=$("form[name="+formName+"]").attr("action");
	url= url+"?"+data+"&redirectTo="+next;
	//alert(url);
	$.ajax( {
		"url": url,
		"type": "POST",
		"beforeSend": function() {
			
		},
		"success": function( e ) {
			/*alert("success");
			alert(e);*/
			/*var id=$(e);
			console.log('${questionId}');
			var i=id.find("input [name=id]").val(); 
			
			alert(i);
			alert($("#id").val());*/
			if(next == "preview" && formName== "questionAddForm")
				TemplateController.viewQuestion($(e).find("#id").val(),csrfToken);
			/*if(next == "list")
				Controller.load('/business/search.handle?_searchName=questionSearch&methodName=search&status=ACTIVE&viewFlag=true&default=true&_documentId=109',null,null);
			alert("successssss");*/
		}
	});
}

function updateQtemplate(id,csrfToken)
{
	$("#id").val(id);
	Controller.onSubmitWithCsrf('viewConfigTemplateForm',csrfToken,'initCreateTemplate','MODIFY FORM',null)
}

function validateQuestionForm(formName, csrfToken, mname, next)
{
	var msg="";
	var cf=0;
	var rf=0;
	var cCount=0;
	var rCount=0;
	if($("#code").val() == "" )
	{
		msg=msg+"Please enter code <br/>";
	}
	if($("#description").val() == "" )
	{
		msg=msg+"Please enter description <br/>";
	}
	$(".columnAttribute").each(function( index ) { 
		if($(this).val()== "") 
		{
			cf=1;
		} 
		cCount=parseInt(cCount)+1;
	});
	if(cf==1)
	{
		msg=msg+"Please enter all column name <br/>";
	}
	
	$(".rowAttribute1").each(function( index ) { 
		if($(this).val()== "") 
		{
			rf=1;
		} 
		rCount=parseInt(rCount)+1;
	});
	
	if(rf==1)
	{
		msg=msg+"Please enter all row name <br/>";
	}
	if(cCount>1 && rCount==0)
	{
		msg=msg+"Please enter atleast one row for tabular format <br/>";
	}
	if(msg=="")
	{
		if(next=="save")
			Controller.onSubmitWithCsrf(formName,csrfToken,mname,null,null);
		if(next=="list")
			updateQuestionAndDo(formName,csrfToken, next);
		if(next=="preview")
			updateQuestionAndDo(formName,csrfToken, next);
	}
	else
	{
		$("#validationQuestion").html(msg);
		$("#validationQuestion").show();
	}
}


function checkSpecialCharacter(fieldId,fieldName,msgDivId){  	
	var illegalChars=new Array("'","!","@","#","$","%","^","*","~","`","<",">","\""); 
  	var strValue = $("#"+fieldId).val() ;
  	for(i = 0; i < illegalChars.length; i++){
  	 if(strValue.indexOf(illegalChars[i])!=-1){
  	 	var msg= fieldName+" can not contain an illegal character: " + illegalChars[i];
  	 	do {
  	 		strValue=strValue.replace(illegalChars[i],"");
  	 	}while(strValue.indexOf(illegalChars[i])!=-1);
  	 	$('#'+fieldId).val(strValue);
  	 	/*$('#'+fieldId).val(strValue.substr(0,strValue.indexOf(illegalChars[i])) + strValue.substr(strValue.indexOf(illegalChars[i])+strValue.match(illegalChars[i]).length,strValue.length));*/
  	 	/*$("#"+msgDivId).text(msg);
  	 	$("#"+msgDivId).show();*/
  	 	alert(msg);
  	 	break;
  	 }
  	 else
	 {
  		/*$( "#"+fieldId ).on( "keyup", function( event ) {
  			//alert(event.keyCode);
  			if(event.keyCode!=16)
  				{
  				$("#"+msgDivId).hide();
  				}
  		});*/
  		
	 }
  	}

}

function checkSelectionAttachQuestion()
{
	var flag=0;
	var val= document.getElementById("section.secQuesRelList[0].question.id").value;
	if(val!=0)
	{
		flag=1;
	}
	if(flag==1)
	{
		Controller.onSubmitWithCsrf('questionAddForm','<csrf:token-name/>=<csrf:token-value/>','attachQuestion','ATTACH QUESTION AND GO TO SECTION',null);
	}
	else
	{
		alert("Please select a question!");
	}
}

function checkSerialNumber(n)
{
	var slno= $("#serialNumber").val();
	//alert(slno);
	
	if(n==1){
		if($.trim(slno)!=""){
			Controller.onSubmitWithCsrf('sectionAddForm','<csrf:token-name/>=<csrf:token-value/>','attachSection','ATTACH SECTION AND GO TO TEMPLATE',null);
		}
		else{
			$("#slNoErrorMsg").html("Please enter serial number.");
			$("#slNoErrorMsg").show();
			$('html, body').animate({ scrollTop: $('#bodyContent').offset().top }, 'slow');
		}
	}
	if(n==2){
		if($.trim(slno)==""){
			$("#serialNumber").val(0);
		}
		Controller.onSubmitWithCsrf('sectionAddForm','<csrf:token-name/>=<csrf:token-value/>','attachSection','GO TO TEMPLATE',null);
	}
	
}