

 jQuery(document).ready(function ($) {
 	loadPreviousValues();
    $(".CheckButton").click(function(){
 	    processYESandNO(this);
    });
 });
 

 function processYESandNO(DOMObject){
 
    var cellValue = myTrim($(DOMObject).text());   
	  if(cellValue == "NO"){
	     makeYESButton(DOMObject);
	  }else{ 
	     makeNOButton(DOMObject);
	  }	  
 }
 
	 function myTrim(newValue) {
	    return newValue.replace(/^\s*/, "").replace(/\s*$/, "");
	}
 
 function makeYESButton(DOMObject){ 
 
   $(DOMObject).text("YES");
   $(DOMObject).css("color","white");
   $(DOMObject).css("background-color","red");
   makeCheckedUnchecked(DOMObject,"YES");
 }
 
 
 function makeNOButton(DOMObject){
 
   $(DOMObject).text("NO");
   $(DOMObject).css("color","green");
   $(DOMObject).css("background-color","lightgreen");
   makeCheckedUnchecked(DOMObject,"NO");
 }
 

 function makeCheckedUnchecked(DOMObject,fieldType){
 	 var selectedBoxID = $(DOMObject).attr('id');
 	  var fieldName = null;
 	 
 	 var ckb=document.getElementsByTagName('input'); 	
	 for(ckbc=0;ckbc<ckb.length;ckbc++){
		if(ckb[ckbc].type=='checkbox'){
			fieldName = ReplaceAll(ckb[ckbc].name,'value(','');
			fieldName = ReplaceAll(fieldName,')','');
			
			if(fieldName == selectedBoxID){
				if(fieldType == "YES"){
					ckb[ckbc].checked = true; 
				}else{

					ckb[ckbc].checked = false; 
				}
				
			}
		
		}
	}
 	 
	 


 } 
 
 function makeUncheckboxChecked(DOMObject){
 	 var selectedBoxID = $(DOMObject).attr('id');
	 document.getElementById(selectedBoxID).checked = false; 
 } 
 
 function loadPreviousValues(){
 alert("Inside loadPreviousValues");
 var fieldName = null;
 	var inputs=document.getElementsByTagName('input');
	for(index=0;index<inputs.length;index++){
		if(inputs[index].type=="checkbox"){
	
			fieldName = ReplaceAll(inputs[index].name,'value(','');
			fieldName = ReplaceAll(fieldName,')','');
			
			if(fieldName !="ignoreFilters"){
				
				populateValuesOnLoad(fieldName,inputs[index].checked);
			}
		
		}
	}
 }
 
 function ReplaceAll(Source,stringToFind,stringToReplace){
  var temp = Source;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
 }
 
 function populateValuesOnLoad(fieldName,fieldChecked){
 
  var domObject=document.getElementById(fieldName);
  
  


	 if(fieldChecked == true){

		 makeYESButton(domObject);
	 }else {

		 makeNOButton(domObject);
	 }
	 return true;
 
 }
 
 