
 function check_status_checked()
 {
	 
	 var flag=0;
	 $("input[type=checkbox]").each(   
    	     function() {
        	        	   if($(this).val()!=0)
        	        	   {
            	        	    if($(this).attr('checked')==false) 
    								flag=1;
        	        	   }
    	                }   
    	      );
   	return flag;
 } 

 function calculate_value()
 {
	 var pattern=/^[0-9][0-9]*$/;
	 var sum=0;
	 $("#single > input[type='text'][id='all_points']").each(function(){
		 var val=$(this).val();
		 if(pattern.test(val))
		 {	
			 sum+=parseInt(val);
		 }
		 else
			 $(this).val('');
				 
	 });
//	 alert(sum);
	 if(sum>$("#total > input[type='text'][id='all_points']").val())
	 {
		 alert('Points given to the choices are invalied');
		 return 1;
	 }
	 return 0;
 }
 
 function calculate_value_single()
 {
	 var pattern=/^[0-9][0-9]*$/;
	 var sum=0;
	 $("#single > input[type='text'][id='all_points']").each(function(){
		 var val=$(this).val();
		 if(pattern.test(val))
		 {	
			 if(parseInt(val)>$("#total > input[type='text'][id='all_points']").val())
			 {
				 alert('Point given to the choice is invalied');
				 sum=1;
			 };
		 }
		 else
			 $(this).val('');
				 
	 });
//	 alert(sum);
	 
	 return sum;
 }

$(document).ready(
function(){
	
	$('#choice input[type="text"][id="point1"]').bind('change input paste keyup',function()
			{
				validate_point(this);
			});
	 $("#check_all").bind('click',function(e) {
		   
			if($(this).attr('checked')==true)
			{
				
				$('input[type=checkbox]').attr('checked',true);
					
			}
			else
			{
				
				$('input[type=checkbox]').each(
						function(){ $(this).attr('checked',false);});
			}
		}   
);
$("input[type=checkbox]").bind('click',function(e){

if($(this).attr('checked')==true)
{
	if(check_status_checked()==0) 
		$('#check_all').attr('checked',true);
}
else
{
//	if(check_status_checked()==0)    		 
		$('#check_all').attr('checked',false);
}
});
	 $("#delete").click(function() {
	 	i=0;
	 	
	 	$("input[type=checkbox]:checked").each(
		function(){
    	 i=1;	
    	
		}
		);
	    if(i==0)
	    {
	    	alert('No selection has been made');
	    	return false;
	    }
	    else
	    {
	    	$('#action_button').val('Delete');
	    	return confirm('Delete selected?');
	    }
     
    });
$("#deletezone").click(function() {
	 	i=0;
	 	
	 	$("input[type=checkbox]:checked").each(
		function(){
    	 i=1;	
    	
		}
		);
	    if(i==0)
	    {
	    	alert('No selection has been made');
	    	return false;
	    }
	    else
	    {
	    	$('#action_button').val('Delete');
	    	return confirm('Delete selected?');
	    }
     
    });
    $("#send").click(function(){
    	
    	if($('#incomplete').val()==1)
    	{
    	alert("All question sets are not complete.Complete the question sets and try again");
    	return false;
    	}
    	else
    	{
    		$('#action_button').val('send');
    	return confirm("Are you sure you want to send this list to client?");
    	}
    });
     $("#question_delete").click(function() {
	 	i=0;
	 	j=0;
	 	$("input[name='ques_chk[]']").each(
		function(){
    	 j=j+1;	
    	
		}
		);
		
	 	$("input[name='ques_chk[]']:checked").each(
		function(){
    	 i=i+1;	
    	
		}
		);
		
	    if(i==0)
	    {
	    	alert('No selection has been made');
	    	return false;
	    }
//	   
	    else if((i==j))
	    {
	    	$('#action_button').val('Delete');
	    	return confirm('You have chosen to delete all the questions in this section.This will cause the section to be deleted.Delete selected?');
	    }
	    else
     	 {
	    	$('#action_button').val('Delete');
	    	return confirm('Delete selected?');
	    }
    });

$('#search').click(function(){
	$('#action_button').val('Search');
});

$('#cancel').click(function(){
	
	$('#action_button').val('Cancel');
});
$('#search_assign').click(function(){
	$('#action_button').val('Search');
	var x=document.getElementById("client_ques");
   var txt="";
   var ids="";
  for (i=0;i<x.length;i++)
    {
    txt=txt + "," + x.options[i].text;
    ids=ids+","+x.options[i].value;
    }
   $('#client_sel_ques').val(txt);
   $('#client_sel_ids').val(ids);
});

$('#section').change(function(){
	var str="";
	var ids="";
	$("#section option:selected").each(function () {
                str = $(this).text();
               
              });
    $('#selected_section').val(str);
   
});

$('#select').click(function(){
	
	
	$('#ques_div').show(); 
});

$('#proceed').click(function(){
	
	$('#action_button').val('Proceed');
	var x=document.getElementById("client_ques");
   var txt="";
   var ids="";
  for (i=0;i<x.length;i++)
    {
    txt=txt + "," + x.options[i].text;
    ids=ids+","+x.options[i].value;
    }
   $('#client_sel_ques').val(txt);
   $('#client_sel_ids').val(ids);
});

$('#check_all').click(function(){
	i=0;
	if($("#check_all").attr('checked')==true)
	{
		$("input[type=checkbox]").each(function(){
    			$("input[type=checkbox]").attr({ 
        		  checked:"true"
      		  });
			});
	}
	else
	{
		$("input[type=checkbox]").each(function(){
    			$("input[type=checkbox]").removeAttr("checked") ;
        		 
      		  });
	}
//	$("input[type=checkbox]:checked").each(
//		function(){
//    	i=i+1;
//    	
//		});
//		alert(i);
//		if(i==1)
//		{
//			$("input[type=checkbox]").each(function(){
//    			$("input[type=checkbox]").attr({ 
//        		  checked:"true"
//      		  });
//			});
//		}
//		else
//		{
//			$("input[type=checkbox]").each(function(){
//    			$("input[type=checkbox]").removeAttr("checked") ;
//        		 
//      		  });
//			
//		}
});

$("input[name='ques_chk[]']").click(function(){
	
	 var currentId=$(this).attr('id');
	 if($('#check_all').attr('checked')==true)
	 {
	 	$("input[type='checkbox']").each(function(){
	 		id=$(this).attr('id');
	 		if(id!=currentId)
	 		{
	 			$(this).removeAttr("checked") ;
	 		}
	 	});
	 }
});

$('#add_question').click(function(){
	i=0;
	$("input[type=checkbox]:checked").each(
		function(){
    	i=i+1;
    	
		});
	if(i==0)
	{
		alert('No question has been selected');
		return false;
	}
	else
	return true;
});

 $("#add_to_db").click(function() {
	 	i=0;
	 	$("input[type=checkbox]:checked").each(
		function(){
    	 i=1;
		}
		);
	    if(i==0)
	    {
	    	alert('No selection has been made');
	    	return false;
	    }
	    else
	    {
	    	$('#action_button').val('Add');
	    	return true;
	    }

    });
    
   $('#send_list').click(function(){
   	i=0;
	 	$("input[type=checkbox]:checked").each(
		function(){
    	 i++;
		}
		);
	    
	    if(i==0)
	    {
	    	alert('Select a question set');
	    	return false;
	    }
	    else
	    {
	    	$('#action_button').val('send_list');
	    	return true;
	    }
   });

   $('#select_choice').click(function(){
       i=0;
      $("input[type=radio]:checked").each(
		function(){
    	 i=1;
		}
		);
       if(i==0)
       {
           alert('Select a choice');
           return false;
       }
       else
           {

            $('#action_button').val('selected');
            return true;
           }
   });
   
   
//   $(":input[name='selectbox']").change(funct

   $("select").change(function(){
          var currentName=$(this).attr('name');
          var currentVal=$(this).val();
          
          var currentId=$(this).attr('id');
//          alert (currentId);
//          alert(currentName);
          var cur_text='1/'+currentId;
          if(currentVal==cur_text)
          {
//        	  alert("found match");
//              hiddenFieldName=":input[name='hid"+currentName+"']";
              
//              $(hiddenFieldName).val(currentId);
//              alert(currentId);
              name=":input[name='"+currentName+"']";
              
   			 $(name).each(function () {
                if($(this).attr('id')!=currentId) 
                {
                	
                	
                	id=$(this).attr('id');
                	
                	id="#"+id+" option[value=2/"+id+"]";
                	
                	opt=$(id);
                	
                	opt.attr({selected:"true"});
                	
                	
                	
                }
              });
   		
   }
  
   });
   
   
$('#approve_save').click(function(){
    
    i=0;
    j=0;
    k=1;
    $("select").each(function (){
    	var val='1/'+$(this).attr('id');
//    	alert(val);
    	if($(this).val()==val)
    		i=1;
    	val='2/'+$(this).attr('id');
    	if($(this).val()==val)
    		j=1;
    	val='0/'+$(this).attr('id');
    	if($(this).val()==val)
    		k=0;
    })
    if(i==0&&j==0||k==0)
    {
    	alert('select a status');
    }
	else{	
		$('#action_button').val('approve');
       return confirm("If you have approved any question sets, it cannot be modified later or if you have rejected any question sets,it will not be shown here further.Are you sure you want to continue?");
	}
    return false;
});

  $('#add').click(function(){
		lastcount=$('#num_rows').val();
            
		printcount=String(parseInt(lastcount)+ 1);
		$('#num_rows').val(printcount);
		$("#choice").before("<div class='formField'><label class='displayText_s'>Choice "+printcount+"</label><input type='text' maxlength='20' class='fieldBoxStyle' name='choice["+lastcount+"]' /><label class='displayText_s'>Points</label><input type='text' value=''id='all_points' maxlength='5' name='point["+lastcount+"]' class='fieldExtStyle'/><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>");

		return false;
	});
$('#add_question_point').click(function(){
		lastcount=$('#num_rows').val();
           
		printcount=String(parseInt(lastcount)+ 1);
		$('#num_rows').val(printcount);
		$("#choice").before("<div class='formField'><label class='displayText_ss'>Choice "+printcount+"</label><input type='text' maxlength='20' class='text_expand_name' name='choice["+lastcount+"]' /><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>");

		return false;
	});

$('#add_question_point1').click(function(){
	
	lastcount=$('#num_rows').val();
	
       
	printcount=String(parseInt(lastcount)+ 1);
	
	
	$('#num_rows').val(printcount);
	if(printcount==5)
	{
		$("#english1").before("<div id='english' class='formField' style='width:300px;height:25px;margin-left:-43px;'><label class='displayText_ss'>Choice "+printcount+"</label><input type='text' maxlength='20' class='text_expand_name' name='choice["+lastcount+"]' id="+lastcount+" onkeyup='return choice_arab(this)' onchange='return choice_arab(this)' onblur='return choice_arab(this)'/><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>" +
				"<div id='arabic' class='formField_arab' style='width:300px;height:25px; padding-right:5px; margin-left:79px;margin-top:-30px;'><label class='displayText_ss_2'></label><input type='text' dir='rtl'  maxlength='20' class='text_expand_name' name='choice_arab["+lastcount+"]' id='AR_"+lastcount+"' /><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>");
		
	}
	else
	{
	$("#english1").before("<div id='english' class='formField' style='width:300px;height:25px;margin-left:0px;'><label class='displayText_ss' >Choice "+printcount+"</label><input type='text' maxlength='20' class='text_expand_name' name='choice["+lastcount+"]' id="+lastcount+" onkeyup='return choice_arab(this)' onchange='return choice_arab(this)' onblur='return choice_arab(this)'/><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>" +
			"<div id='arabic' class='formField_arab' style='width:300px;height:25px; padding-right:5px; margin-left:79px;margin-top:-30px;'><label class='displayText_ss_2' style='margin-bottom:0px;'> </label><input type='text' dir='rtl'  maxlength='20' class='text_expand_name' style='margin-bottom:0px;' name='choice_arab["+lastcount+"]' id='AR_"+lastcount+"' /><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>");
	
	}
	return false;
});


$('#edit_question_point').click(function(){
	
	lastcount=$('#num_rows').val();
	
       
	printcount=String(parseInt(lastcount)+ 1);
	
	
	$('#num_rows').val(printcount);
	if(printcount==5)
	{
		$("#english1").before("<div id='english' class='formField' style='width:300px;height:25px;margin-left:0px;'><label class='displayText_ss'>Choice "+printcount+"</label><input type='text' maxlength='20' class='text_expand_name' name='choice["+lastcount+"]' id="+lastcount+" onkeyup='return choice_arab(this)' onchange='return choice_arab(this)' onblur='return choice_arab(this)'/><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>" +
				"<div id='arabic' class='formField_arab' style='width:300px;height:25px; padding-right:5px; margin-left:79px;margin-top:-30px;'><label class='displayText_ss_2'></label><input type='text' dir='rtl'  maxlength='20' class='text_expand_name' name='choice_arab["+lastcount+"]' id='AR_"+lastcount+"' /><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>");
		
	}
	else
	{
	$("#english1").before("<div id='english' class='formField' style='width:300px;height:25px;margin-left:0px;'><label class='displayText_ss' >Choice "+printcount+"</label><input type='text' maxlength='20' class='text_expand_name' name='choice["+lastcount+"]' id="+lastcount+" onkeyup='return choice_arab(this)' onchange='return choice_arab(this)' onblur='return choice_arab(this)'/><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>" +
			"<div id='arabic' class='formField_arab' style='width:300px;height:25px; padding-right:5px; margin-left:79px;margin-top:-30px;'><label class='displayText_ss_2' style='margin-bottom:0px;'> </label><input type='text' dir='rtl'  maxlength='20' class='text_expand_name' style='margin-bottom:0px;' name='choice_arab["+lastcount+"]' id='AR_"+lastcount+"' /><input type='hidden' value='0' name='choiceid["+lastcount+"]'/><span class='redStar'>&nbsp;&nbsp;</span></div>");
	
	}
	return false;
});
   $('#question_type').change(function(){
       var currentVal=$(this).val();
       switch(parseInt(currentVal))
       {
       		case 0:calculate_value();break;
       		case 1:
       		case 2:calculate_value_single();break;
       }
       if (currentVal == 3)
           {
               
                $("input[name='scoring']:nth(1)").attr("checked","checked");
           }
   });
   $("input[name='scoring']").change(function(){
       
      var selectVal=$('#question_type').val();
     if((selectVal==3)&&($("input[name='scoring']").val()==1))
         {
             alert('Question type is nonscoring.So scoring cannot be made true');
             $("input[name='scoring']:nth(1)").attr("checked","checked");
         }
  });
$("#save").click(function()
{
   
    $('#action_button').val('save');
    return true;
});

$("#StartDateof").bind('blur ',function()
{ 
   var currentVal=$(this).val();
  
     var currentVal2=currentVal.split(' ');
     var currentVal3=String(parseInt(currentVal2[2])+ 1);
     
     $("#endDateof").val(currentVal2[0]+" "+currentVal2[1]+" "+currentVal3);
   
   return true;
});

$('#help_prep_ques_set').click(function(){
	msg="There are three functionalities possible from this page";
	msg=msg+"\n"+"1. Click on the link provide in the 'RFQ/Contract Name'. You can create, modify and view question sets for the brands in the contract in the inner pages following this link";
	msg=msg+"\n"+"2. To approve or reject a question set sent, click the link given in 'Questionnaire Status' field. Only after atleast one question is sent, you will be able to use this feature.";
	msg=msg+"\n"+"3. To select and send question sets for a contract, click on the link 'Select and send question sets to client' provided with each contract";
	alert(msg);
	return false;
});

$('#help_sel_cont_det').click(function(){
	msg="This page lists all the question sets created per brand in this contract.";
	msg=msg+"\n"+"Note that this list contains only question sets CREATED for this contract and doesnot include question sets from other contract even if they were sent for this contract.";
	msg=msg+"\n"+"'Version' gives the version and 'Wave', if any, gives the wave number for which the question set was sent.";
	msg=msg+"\n"+"'Status' gives the current status in question set. It can take values- 'Incomplete', 'Completed,Not Send','Completed,Approved','Completed,Rejected'";
	msg=msg+"\n"+"Click on the link provide in the 'Question Set' to view ,edit or create question set";
	alert(msg);
	return false;
});

$('#help_sec_wise_summary').click(function(){ 
	msg="This page gives a section wise summary as of now";
	msg=msg+"\n"+"If a question set is incomplete or if it has been completed and not sent till now, you can edit the questions in each section by clicking on the edit button provided.";
	msg=msg+"\n"+"Also you can add new sections by clicking on 'Add new section' button.";
	msg=msg+"\n"+"If a question set is sent,then you canot further edit the questions.Instead you can create a new version of the question set by clicking the 'Create new version' button on the page";
	msg=msg+"\n"+"'View Question Set' button if clicked will give the questions along with their weightage,answers along with their weightage, grouped by sections";
	alert(msg);
	return false;
});
$('#help_master_ques_list').click(function(){
	msg="This page can be used for setting section and also the questions in that section.The section names and questions come from master database of section and question.If you want completely new section which are not listed in the section combo box or add a new question to the list, you have to first add the same to master database using 'Section' and 'Question' menu items";
	msg=msg+"\n"+"Select the questions you want and press 'Add questions' button. You will be redirected to next page where you can edit these questions as well as the section you have selected.";
	alert(msg);
	return false;
});

$('#help_use_existing_ques_set').click(function(){
	msg="This page lists all the question sets that are prepared for the current brand.You can select any one of the question sets and press 'Use this set' to use the current set as your new question set.";
	alert(msg);
	return false;
});

$('#help_edit_survey_questions').click(function(){
	msg="This page lists the questions of the selected section. You can edit the section points as well as questions in the section.";
	msg=msg+"\n"+"Using 'Add question' button you can add questions to this section .";
	msg=msg+"\n"+"Using 'Delete' button you can delete questions from this section .";
	alert(msg);
	return false;
});
$('#help_modify_status_view').click(function(){
	msg="This page lists brands and the waves corresponding to each brand.";
	msg=msg+"\n"+"Click on 'More Details' button to view the details.";
	msg=msg+"\n"+"In the detailed view you can change the status of question sets that you have send. If any one question set for a wave of a brand is approved, then all other question sets are rejected by default.The approval action is irreversible.";
	alert(msg);
	return false;
});
$('#help_sel_ques_set').click(function(){
	msg="This page lists brands and the waves corresponding to each brand.";
	msg=msg+"\nclick on select and send question set to select and send question sets";

	msg=msg+"\n"+"click on select and send question set to select and send question sets";
	alert(msg);
	return false;
});
});



