// JavaScript Document

jQuery.fn.setttval = function(value){
	
	if(value!==''){
		return this.each(function() {				  
			var $this = jQuery(this);
			$this.val(value);
		});
	}
	
};

var selectArray = new Object();
var oldshow = '';
var oldclickshow = '';
var rconf = true;
if ( jQuery.browser.msie ) {
	var clicktrig = 'mousedown'
}else{
	var clicktrig = 'click'
}

selectArray["Dosbarth A - Cig"] = ".cf-fs1";
selectArray["Dosbarth B - Cynnyrch Llaeth"] = ".cf-fs2";
selectArray["Dosbarth C  - Cynnyrch wedi'i Bobi a Melysion"] = ".cf-fs3";
selectArray["Dosbarth D - Diod"] = ".cf-fs4";
selectArray["Dosbarth E - Pysgod a Bwyd Môr"] = ".cf-fs5";
selectArray["Dosbarth F - Garddwriaeth"] = ".cf-fs6";
selectArray["Dosbarth G - Bwydydd Arbenigol"] = ".cf-fs7";
selectArray["Dosbarth H - Bwydydd Iachus"] = ".cf-fs8";

jQuery(document).ready( function () {

	//A-H
	jQuery("[id=cf11_field_3-2]").click( function() {
	   jQuery(".cf-fs1").show("slow");
	});
	   jQuery("[id=cf11_field_3-1]").click( function() {
	     jQuery(".cf-fs1").hide("slow");
	   });
	   jQuery("#sendbutton47").attr("disabled", "disabled");
	   jQuery("#cf47_field_3").change(function () {
	       jQuery("#cf47_field_3 option:selected").each(function () {
	                jQuery("#sendbutton47").attr("disabled", "disabled");
	                jQuery(oldshow+' input').attr("checked", false);
	                jQuery(oldshow).hide("slow");
	                jQuery(selectArray[jQuery(this).val()]).show("slow");
	                oldshow=selectArray[jQuery(this).val()];
	      });
	
	   });
	
	   jQuery("#cforms47form .cf-fs3 input").click( function() {
	          if(oldclickshow != this){
	            jQuery(oldshow).attr("checked", false);
	         }else{
	            jQuery("#sendbutton47").attr("disabled", "disabled");
	         }
	         oldclickshow=this;
	   });
	
	   jQuery(".entryButton").click( function() {
	    window.location.replace("http://www.gwirflas.tv/index.php/gwobrau-2010-2011/ymgeisio-eleni/");
	   });
	
	   jQuery("#cforms47form input").click( function() {
		    if((jQuery(this).attr('type') == "radio" || jQuery(this).attr('type') == "checkbox") && (jQuery(this).attr("checked") == true)){
		      jQuery("#sendbutton47").removeAttr("disabled");
		    }
		   });
	   // END A-H
  
	   //I-K
	   jQuery(".entryButton").click( function() {
		    window.location.replace("http://www.gwirflas.tv/index.php/ymgeisio-eleni/");
		   });

		  jQuery("[id=cf38_field_3-2]").click( function() {
		jQuery(".cf-fs1").show("slow");
		});
		jQuery("[id=cf38_field_3-1]").click( function() {
		jQuery(".cf-fs1").hide("slow");
		});
		            jQuery("#sendbutton24").attr("disabled", "disabled");

		   jQuery("#cforms24form input").click( function() {
		     if((jQuery(this).attr('type') == "radio" || jQuery(this).attr('type') == "checkbox") && (jQuery(this).attr('id') != "cf24_field_12")){
		          var thischeck = this;
		          if(oldshow != this){
		            jQuery(oldshow).attr("checked", false);
		         }else{
		            jQuery("#sendbutton24").attr("disabled", "disabled");
		         }
		         oldshow=this;
		     }
		   });
		   jQuery("#cforms24form input").click( function() {
		    if((jQuery(this).attr('type') == "radio" || jQuery(this).attr('type') == "checkbox") && (jQuery(this).attr('id') != "cf24_field_12") && (jQuery(this).attr("checked") == true)){
		      jQuery("#sendbutton24").removeAttr("disabled");
		    }
		   });
		 //END I-K

	   //L-P
	  /* jQuery("[id=cf39_field_3-2]").click( function() {
		     jQuery(".cf-fs1").show("slow");
		  });
		  jQuery("[id=cf39_field_3-1]").click( function() {
		    jQuery(".cf-fs1").hide("slow");
		  });  
		   jQuery("#sendbutton33").attr("disabled", "disabled");
		   jQuery("#cforms33form input").click( function() {
		      if((jQuery(this).attr('type') == "radio" || jQuery(this).attr('type') == "checkbox") && (jQuery(this).attr('id') != "cf33_field_14")){
		          var thischeck = this;
		          if(oldshow != this){
		            jQuery(oldshow).attr("checked", false);
		         }else{
		            jQuery("#sendbutton33").attr("disabled", "disabled");
		         }
		         oldshow=this;
		     }
		   });
		   jQuery("#cforms33form input").click( function() {
		    if((jQuery(this).attr('type') == "radio" || jQuery(this).attr('type') == "checkbox") && (jQuery(this).attr('id') != "cf33_field_14") && (jQuery(this).attr("checked") == true)){
		      jQuery("#sendbutton33").removeAttr("disabled");
		    }
		   });*/
		 //END L-P
		   
		//SHOW J
		   
	   jQuery("#cforms24form input#cf24_field_10").click( function() {
		    if(jQuery(this).is(':checked')) {
		      jQuery("fieldset.cf-fs3").show('slow');
		    }else{
		      jQuery("fieldset.cf-fs3").hide('slow');
		    }
		  });
		   
		//END SHOW J

jQuery('form.cform').filter(function(index){
	var mid = jQuery(this).attr('id');
	return (mid == 37 || mid == 32);
}).each(
	function(){

  jQuery.get("/form_autocomplete.php", { "func": "form_autocomplete" },function(data){
    if(data) {
      
      
      if(data.nomination == '0') {
    	jQuery('#cf11_field_3-1').attr('checked', true);
    	jQuery('#cf38_field_3-1').attr('checked', true);
    	jQuery('#cf39_field_3-1').attr('checked', true);
      } else if(data.nomination == '1') {
      	jQuery('#cf11_field_3-2').attr('checked', true);
    	jQuery('form#cforms11form .cf-fs1').show();
    	jQuery('#cf38_field_3-2').attr('checked', true);
        jQuery('form#cforms38form .cf-fs1').show();
        jQuery('#cf39_field_3-2').attr('checked', true);
        jQuery('form#cforms39form .cf-fs1').show();
      }
      //A-H
      jQuery('#cf11_field_6').setttval(data.nominator_name);
      jQuery('#cf11_field_7').setttval(data.nominator_telephone);
      jQuery('#cf11_field_8').setttval(data.nominator_address1);
      jQuery('#cf11_field_9').setttval(data.nominator_address2);
      jQuery('#cf11_field_10').setttval(data.nominator_address3);
      jQuery('#cf11_field_11').setttval(data.nominator_postcode);
      jQuery('#cf11_field_12:not(.cf_error)').setttval(data.nominator_email);
      jQuery("input[value='" + data.entrant_title + "']").attr("checked", true);
      jQuery('#cf11_field_15:not(.cf_error)').setttval(data.entrant_forename);
      jQuery('#cf11_field_16:not(.cf_error)').setttval(data.entrant_surname);
      jQuery('#cf11_field_17:not(.cf_error)').setttval(data.entrant_job);
      jQuery('#cf11_field_18:not(.cf_error)').setttval(data.entrant_business);
      
      
      jQuery('#cf34_field_3:not(.cf_error)').setttval(data.business_address1);
      jQuery('#cf34_field_4:not(.cf_error)').setttval(data.business_address2);
      jQuery('#cfv_field_5:not(.cf_error)').setttval(data.business_address3);
      jQuery('#cf34_field_6:not(.cf_error)').setttval(data.business_postcode);
      jQuery('#cf34_field_7:not(.cf_error)').setttval(data.business_type);
      jQuery('#cf34_field_8:not(.cf_error)').setttval(data.business_telephone);
      jQuery('#cf34_field_9').setttval(data.business_fax);
      jQuery('#cf34_field_10:not(.cf_error)').setttval(data.business_email);
      jQuery('#cf34_field_11').setttval(data.business_website);
      jQuery("input[value='" + data.business_location + "']").attr("checked", true);
      
      jQuery("input[value='" + data.business_type_select + "']").attr("checked", true);
      jQuery('#cf35_field_5').setttval(data.business_type_text);
      jQuery('#cf35_field_6:not(.cf_error)').setttval(data.business_employees);
      
      jQuery('#cf41_field_4').setttval(data.business_food_acc);
      jQuery('#cf41_field_6').setttval(data.business_env_acc);
      //END A-H
      
      //I-K
      jQuery('#cf38_field_6').setttval(data.nominator_name);
      jQuery('#cf38_field_7').setttval(data.nominator_telephone);
      jQuery('#cf38_field_8').setttval(data.nominator_address1);
      jQuery('#cf38_field_9').setttval(data.nominator_address2);
      jQuery('#cf38_field_10').setttval(data.nominator_address3);
      jQuery('#cf38_field_11').setttval(data.nominator_postcode);
      jQuery('#cf38_field_12:not(.cf_error)').setttval(data.nominator_email);
      jQuery("input[value='" + data.entrant_title + "']").attr("checked", true);
      jQuery('#cf38_field_15:not(.cf_error)').setttval(data.entrant_forename);
      jQuery('#cf38_field_16:not(.cf_error)').setttval(data.entrant_surname);
      jQuery('#cf38_field_17:not(.cf_error)').setttval(data.entrant_job);
      jQuery('#cf38_field_18:not(.cf_error)').setttval(data.entrant_business);
      
      
      jQuery('#cf12_field_3:not(.cf_error)').setttval(data.business_address1);
      jQuery('#cf12_field_4:not(.cf_error)').setttval(data.business_address2);
      jQuery('#cf12_field_5:not(.cf_error)').setttval(data.business_address3);
      jQuery('#cf12_field_6:not(.cf_error)').setttval(data.business_postcode);
      jQuery('#cf12_field_7:not(.cf_error)').setttval(data.business_type);
      jQuery('#cf12_field_8:not(.cf_error)').setttval(data.business_telephone);
      jQuery('#cf12_field_9').setttval(data.business_fax);
      jQuery('#cf12_field_10:not(.cf_error)').setttval(data.business_email);
      jQuery('#cf12_field_11').setttval(data.business_website);
      jQuery("input[value='" + data.business_location + "']").attr("checked", true);
      
      jQuery("input[value='" + data.business_type_select + "']").attr("checked", true);
      jQuery('#cf13_field_5').setttval(data.business_type_text);
      jQuery('#cf13_field_6:not(.cf_error)').setttval(data.business_employees);
      
      jQuery('#cf45_field_4').setttval(data.business_food_acc);
      jQuery('#cf45_field_6').setttval(data.business_env_acc);
      //END I-K
      
      //L_P
      jQuery('#cf39_field_6').setttval(data.nominator_name);
      jQuery('#cf39_field_7').setttval(data.nominator_telephone);
      jQuery('#cf39_field_8').setttval(data.nominator_address1);
      jQuery('#cf39_field_9').setttval(data.nominator_address2);
      jQuery('#cf39_field_10').setttval(data.nominator_address3);
      jQuery('#cf39_field_11').setttval(data.nominator_postcode);
      jQuery('#cf39_field_12:not(.cf_error)').setttval(data.nominator_email);
      jQuery("input[value='" + data.entrant_title + "']").attr("checked", true);
      jQuery('#cf39_field_15:not(.cf_error)').setttval(data.entrant_forename);
      jQuery('#cf39_field_16:not(.cf_error)').setttval(data.entrant_surname);
      jQuery('#cf39_field_17:not(.cf_error)').setttval(data.entrant_job);
      jQuery('#cf39_field_18:not(.cf_error)').setttval(data.entrant_business);
      
      
      jQuery('#cf27_field_3:not(.cf_error)').setttval(data.business_address1);
      jQuery('#cf27_field_4:not(.cf_error)').setttval(data.business_address2);
      jQuery('#cf27_field_5:not(.cf_error)').setttval(data.business_address3);
      jQuery('#cf27_field_6:not(.cf_error)').setttval(data.business_postcode);
      jQuery('#cf27_field_7:not(.cf_error)').setttval(data.business_type);
      jQuery('#cf27_field_8:not(.cf_error)').setttval(data.business_telephone);
      jQuery('#cf27_field_9').setttval(data.business_fax);
      jQuery('#cf27_field_10:not(.cf_error)').setttval(data.business_email);
      jQuery('#cf27_field_11').setttval(data.business_website);
      jQuery("input[value='" + data.business_location + "']").attr("checked", true);
      
      jQuery("input[value='" + data.business_type_select + "']").attr("checked", true);
      jQuery('#cf28_field_5').setttval(data.business_type_text);
      jQuery('#cf28_field_6:not(.cf_error)').setttval(data.business_employees);
      
      jQuery('#cf44_field_4').setttval(data.business_food_acc);
      jQuery('#cf44_field_6').setttval(data.business_env_acc);
      //END L-P
    }
  }, "json");
}
);
  
  /*
   * FORM 33 -- Category L-P page 5.
   */
   
   var form33dependentfields = {
	   	'cf33_field_3' : {
						1 : new Array(3,7,8,9,10,11,12,14,15,16,25,26,27,29,30,31,32,34,35,39,40,42,43,45,47,48,49,50,51,53,54,55),
						2 : new Array(3,8,9,10,11,14,15,16,20,21,22,23,34,35,36,37,39,40,42,43,45,47,48,49,50,53,54,55),
						3 : new Array(3,7,8,9,10,11,12,14,15,16,25,26,27,29,30,31,32,34,35,39,40,42,43,45,47,48,49,50,51,53,54,55),
						4 : new Array(3,7,8,9,10,11,12,14,15,16,18,25,26,27,29,30,31,32,34,35,39,40,42,43,45,47,48,49,50,51,53,54,55),
						5 : new Array(3,7,8,9,10,11,12,14,15,16,25,26,27,29,30,31,32,34,35,39,40,42,43,45,47,48,49,50,51,53,54,55),
						6 : new Array(3,7,8,9,10,11,12,14,15,16,17,25,26,27,29,30,31,32,34,35,39,40,42,43,45,47,48,49,50,51,53,54,55)
						},
		'cf33_field_58':new Array(58,61,62,63,64,65,67,68,69),
		'cf33_field_72': {
						1 : new Array(72,79,80,82,84,85,86,90,91,92,93,94,95,96,97,98,100,101,102,104,105,106,108,109,110),
						2 : new Array(72,78,80,81,82,82,85,86,89,91,92,93,94,95,96,97,98,100,101,102,104,105,106,108,109,110),
						3 : new Array(72,78,80,81,82,84,85,86,89,91,92,93,94,95,96,97,98,100,101,102,104,105,106,108,109,110),
						4 : new Array(72,79,80,82,84,85,86,90,91,92,93,94,95,96,97,98,100,101,102,104,105,106,108,109,110)
						},
		'cf33_field_113':new Array(113,116,117,118,119,120,121,122,123,124,126,127,128,129,130,131,132,134,135,136)
   };
  
  // FIELDSET 1 -- Category L.
  function form33GetArray(no) {
    var hide = null;
    
    switch ( no ) {
      case '1':
        hide = new Array(
          '#li-33-17',
          '#li-33-18',
          '#li-33-19',
          '#li-33-20',
          '#li-33-21',
          '#li-33-22',
          '#li-33-23',
          '#li-33-36',
          '#li-33-37'
        );
        break;
      case '2':
        hide = new Array(
          '#li-33-7',
          '#li-33-12',
          '#li-33-17',
          '#li-33-18',
          '#li-33-24',
          '#li-33-25',
          '#li-33-26',
          '#li-33-27',
          '#li-33-28',
          '#li-33-29',
          '#li-33-30',
          '#li-33-31',
          '#li-33-32',
          '#li-33-51'
        );
        break;
      case '3':
        hide = new Array(
          '#li-33-17',
          '#li-33-18',
          '#li-33-19',
          '#li-33-20',
          '#li-33-21',
          '#li-33-22',
          '#li-33-23',
          '#li-33-36',
          '#li-33-37'
        );
        break;
      case '4':
        hide = new Array(
          '#li-33-17',
          '#li-33-19',
          '#li-33-20',
          '#li-33-21',
          '#li-33-22',
          '#li-33-23',
          '#li-33-36',
          '#li-33-37'
        );
        break;
      case '5':
        hide = new Array(
          '#li-33-17',
          '#li-33-18',
          '#li-33-19',
          '#li-33-20',
          '#li-33-21',
          '#li-33-22',
          '#li-33-23',
          '#li-33-36',
          '#li-33-37'
        );
        break;
      case '6':
        hide = new Array(
          '#li-33-18',
          '#li-33-19',
          '#li-33-20',
          '#li-33-21',
          '#li-33-22',
          '#li-33-23',
          '#li-33-36',
          '#li-33-37'
        );
        break;
    }

    return hide;
  }
  
  // show CATEGORY L
  jQuery('#cforms33form .cf-fs1 input').bind(clicktrig, function(event) {
	if(oldclickshow != this){	
		
		
		form33HideFieldsets(this);
		var trconf = rconf;
		rconf = false;
		if(trconf==true){
			jQuery(this).trigger('triggerclick')
			jQuery(this).attr('checked',true);
		}else{
			event.preventDefault();
			jQuery(this).attr('checked',false);
			return false
		}
	}
		oldclickshow = this
  });
  
  
  jQuery('#cforms33form .cf-fs1 input').bind('triggerclick', function() {
      
    jQuery('#cforms33form .cf-fs2 li').each(function() {
      jQuery(this).css('display','list-item');
    });
    
    var id = jQuery(this).attr('id');
    id = id.split('-');
    id = id[1];
    
    var toHide = form33GetArray(id);
    
    jQuery('#cforms33form .cf-fs2').show('slow');
    
    if ( toHide != null ) {
      while( toHide.length > 0 ) {
        var hide = toHide.pop();
        jQuery('#cforms33form .cf-fs2 li'+hide).css('display','none');
      }
    }
  });
  
  // show CATEGORY M
  jQuery("#cforms33form input#cf33_field_58").bind(clicktrig, function(event) {
    if(oldclickshow != this){
		
		form33HideFieldsets(this);
		var trconf = rconf;
		rconf = false;
		if(trconf==true){
			jQuery(this).trigger('triggerclick')
			jQuery(this).attr('checked',true);
		}else{
			event.preventDefault();
			jQuery(this).attr('checked',false);
			return false
		}
	}
	oldclickshow = this
  });
  
  jQuery("#cforms33form input#cf33_field_58").bind('triggerclick', function() {
    if(jQuery(this).is(':checked')) {
      jQuery("fieldset.cf-fs4").show('slow');
    }
  });
  
  //SETUP AN ARRAY OF THE N CATEGORY ELEMENTS THAT SHOW/HIDE DEPENDING ON THE NUMBER
  var cat_n_1_4 = jQuery('#li-33-79,#li-33-90');
  var cat_n_2_3 = jQuery('#li-33-78,#li-33-81,#li-33-89');

  
  // SHOW CATEGORY N
  jQuery('#cforms33form input[name=cf33_field_72]').bind(clicktrig, function(event) {
    if(oldclickshow != this){
		
		form33HideFieldsets(this);
		var trconf = rconf;
		rconf = false;
		if(trconf==true){
			jQuery(this).trigger('triggerclick')
			jQuery(this).attr('checked',true);
		}else{
			event.preventDefault();
			jQuery(this).attr('checked',false);
			return false
		}
	}
	oldclickshow = this
  });
  
   jQuery('#cforms33form input[name=cf33_field_72]').bind('triggerclick', function() {
      
    jQuery('fieldset.cf-fs6').show('slow');
    
    var eid = jQuery(this).attr('id');
    
    if ( eid == 'cf33_field_72-1' || eid == 'cf33_field_72-4' ) {
      cat_n_1_4.show();
      cat_n_2_3.hide();
    } else if (  eid == 'cf33_field_72-2'  ||  eid == 'cf33_field_72-3'  ) {
      cat_n_2_3.show();
      cat_n_1_4.hide();
    }
  });
  
  // show CATEGORY O
  jQuery('#cf33_field_113').bind(clicktrig, function(event) {
    if(oldclickshow != this){
		
		form33HideFieldsets(this);
		
		form33HideFieldsets(this);
		var trconf = rconf;
		rconf = false;
		if(trconf==true){
			jQuery(this).trigger('triggerclick')
			jQuery(this).attr('checked',true);
		}else{
			event.preventDefault();
			jQuery(this).attr('checked',false);
			return false
		}
	}
	oldclickshow = this
  });
  
  jQuery('#cf33_field_113').bind('triggerclick', function() {
    if ( jQuery(this).is(':checked') ) {
      jQuery('fieldset.cf-fs8').show('slow');
    }
  });
  
  // hide OTHERS
  function form33HideFieldsets(element) {
	  if(rconf == false){
		rconf=confirm("Only one category selection is permitted on each submission, if you change category now information entered at previous stages will remain, but some of the supplementary information you have completed on this screen may be discarded. Are you sure you wish to continue?");
	}
	if (rconf==true){
    var eid = jQuery(element).parents('li').attr('id');
	/*var ename = jQuery(element).attr('name');
	var evalue = jQuery(element).val();*/
	var noemptyarray = new Array();
	var indexid = jQuery(element).attr('id');
    indexidarray = indexid.split('-');
	indexname = indexidarray[0];
    indexid = indexidarray[indexidarray.length-1];
	if(typeof(form33dependentfields[indexname]) !== 'undefined'){
		if(jQuery.isArray(form33dependentfields[indexname])){
			nocleararray = form33dependentfields[indexname];
		}else{
			if(typeof(form33dependentfields[indexname][indexid]) !== 'undefined'){
				nocleararray = form33dependentfields[indexname][indexid];
			}
		}
	};
	var nocleararrayids = new Array();
	var len=nocleararray.length;
	for(var i=0; i<len; i++) {
		var fieldindex = nocleararray[i];
		nocleararrayids[i] = "cf33_field_"+nocleararray[i];
	}
    
    var toHide = new Array(
      'fieldset.cf-fs2',
      'fieldset.cf-fs4',
      'fieldset.cf-fs6',
      'fieldset.cf-fs8'
    );
    while ( toHide.length > 0 ) {
      hide = toHide.pop();
      jQuery(hide+' input[type=text]').filter(function(index) {
  		return jQuery.inArray(((jQuery(this).attr('id')).split('-'))[0], nocleararrayids) === -1;
	}).val('');
      jQuery(hide+' input[type=checkbox]').filter(function(index) {
  		return jQuery.inArray(((jQuery(this).attr('id')).split('-'))[0], nocleararrayids) === -1;
	}).attr('checked',false);
	  
      jQuery(hide+' textarea').filter(function(index) {
  		return jQuery.inArray(((jQuery(this).attr('id')).split('-'))[0], nocleararrayids) === -1;
	}).val('');
      jQuery(hide+' input[type=radio]').filter(function(index) {
  		return jQuery.inArray(((jQuery(this).attr('id')).split('-'))[0], nocleararrayids) === -1;
	}).attr('checked',false);
      jQuery(hide+' input[type=file]').filter(function(index) {
  		return jQuery.inArray(((jQuery(this).attr('id')).split('-'))[0], nocleararrayids) === -1;
	}).val('');
      jQuery(hide).hide('slow');
    }
    
    if ( eid != 'li-33-3items' ) {
      jQuery('#li-33-3items input').attr('checked',false);
    }
    if ( eid != 'li-33-58' ) {
      jQuery('#li-33-58 input').attr('checked',false);
    }
    if ( eid != 'li-33-72items' ) {
      jQuery('#li-33-72items input').attr('checked',false);
    }
    if ( eid != 'li-33-113' ) {
      jQuery('#li-33-113 input').attr('checked',false);
    }
  }
  }
  
  if(jQuery('body').find('#usermessage33a')){
	  var toClick = new Array(
      '#li-33-3items',
      '#li-33-58',
      '#li-33-72items',
      '#li-33-113'
    );
	 
	  while ( toClick.length > 0 ) {
		  checkclick = toClick.pop();
	 	 jQuery(checkclick+" input:checked").trigger('triggerclick').attr('checked',true);
	  }
  }
  
  // END FORM 32

//PREVIEW FUNCTIONALITY
  
  jQuery('.preview').click(function(){
	  var form_data = jQuery('form').serialize();
	  jQuery.get( '/form_serialise.php', {async: 'false', data: form_data} );
	  jQuery('a.thickbox').click();
  });
  
  jQuery('input.preview').each(function(){
	  jQuery('p.cf-sb').addClass('preview-submit');
	  jQuery(this).parent('li').addClass('preview-submit');
  });
  
  
  //END PREVIEW FUNCTIONALITY

});
