function changeCSS(theClass, element, value) {
  var cssRules;
  if (document.all) {
	cssRules = 'rules';
  }
  else if (document.getElementById) {
	cssRules = 'cssRules';
  }
  for (var S = 0; S < document.styleSheets.length; S++){
	for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
		 document.styleSheets[S][cssRules][R].style[element] = value;
	   }
	}
  }	
}
function stackFunction(func, func2) {
  if (typeof func != 'function') {
    return func2;
  } else {
    return function() {
      func();
      func2();
    }
  }
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function associateActions(actions) {
  for(var element in actions) {
	var e;
	e = document.getElementById(element);
	if(e != null) {
	  events = actions[element];
	  for(var event in events) {
		e["on" + events[event][0]] = events[event][1];
	  }
	}
  }
}
function addConfirmDialogsToDeleteButtons() {
  nl = document.getElementsByTagName("input");
  for(i = 0; i < nl.length; i++) {
	if(nl[i].getAttribute("value") == 'Delete') {
	  nl[i].onclick = stackFunction(nl[i].onclick, function() { return confirm('Are you sure that you want to delete this item?'); });
	}
  }
}
function addVerificationToForms() {
  nl = document.getElementsByTagName("form");
  for(i = 0; i < nl.length; i++) {
    nl[i].onsubmit = stackFunction(nl[i].onsubmit, function() { return doVerification(this); });
  }
}
function hasClass(e, clasz) {
  //if(!e) return false;
  for(var i = 0; i < e.attributes.length; i++) {
	if(e.attributes[i].nodeName == 'class') {
 	  classes = e.attributes[i].nodeValue.split(" ");
 	  for (j=0; j<classes.length; j++) {
 		  if(classes[j] == clasz) return true;
 	  }
	}
  }
  return false;
}
function doVerification(e) {
  happy = true;
  tick_limits_checked = new Array();
  mand_ticks_checked = new Array();
  for(var q = 0; q < 3; q++) {
  if(q == 0) nl = e.getElementsByTagName("input");
  if(q == 1) nl = e.getElementsByTagName("textarea");
  if(q == 2) nl = e.getElementsByTagName("select");
  for(var i = 0; i < nl.length; i++) {
	if(hasClass(nl[i], 'mand_text')) {
	  if(nl[i].value == '') {
	    alert('Please enter a value for "' + nl[i].getAttribute("title") + '"');
	    happy = false;
	    break;
	  }
	}
	if(hasClass(nl[i], 'mand_url')) {
	  if(nl[i].value.substring(0, 4) != 'http') {
	    alert('Please enter a valid HTTP URL for "' + nl[i].getAttribute("title") + '"');
	    happy = false;
	    break;
	  }
	}
	if(hasClass(nl[i], 'mand_ticked')) {
	  if(!nl[i].checked) {
	    alert(nl[i].getAttribute("title"));
	    happy = false;
	    break;
	  }
	}
	if(hasClass(nl[i], 'at_least_one_ticked')) {
	  //mand_ticked for when there are many tickboxes with the same name
	  
	  //make sure we've not already validated another tickbox with the same name
	  found = 0;
	  for(ii=0; ii<mand_ticks_checked.length; ii++) {
	  	if(mand_ticks_checked.length[ii]==nl[i].getAttribute("name")) {
			found = 1;
		}
	  }
	  if(!found) {
	    	els = document.getElementsByName(nl[i].getAttribute("name"));
		    var count = 0;
		    for(var ii = 0; ii < els.length; ii++) {
			  //alert(els[ii].getAttribute("name"));
			  if(els[ii].checked) {
				count++;
			  }
		    }
			if(count==0) {
			  alert("Please tick at least one answer for the question '"+nl[i].getAttribute("title")+"'");
	    	  happy = false;
	    	  break;
			}
			mand_ticks_checked.push(nl[i].getAttribute("name"));
	  }
	}
	if(hasClass(nl[i], 'ticked_limit')) {
	  //make sure we've not already validated another tickbox with the same name
	  found = 0;
	  for(ii=0; ii<tick_limits_checked.length; ii++) {
	  	if(tick_limits_checked.length[ii]==nl[i].getAttribute("name")) {
			found = 1;
		}
	  }
	  
	  if(!found) {
		  els = document.getElementsByName(nl[i].getAttribute("name"));
		  var count = 0;
		  for(var ii = 0; ii < els.length; ii++) {
			  //alert(els[ii].getAttribute("name"));
			  if(els[ii].checked) {
				count++;
			  }
		  }
		  no_allowed_ticks = eval("tick_limit_"+nl[i].getAttribute("name").replace(/\[/g,"").replace(/\]/g,""));
		  if(count > no_allowed_ticks) {
			  alert("You have ticked more than "+no_allowed_ticks+" answers for the question '"+nl[i].getAttribute("title")+"'");
			  happy=false;
		  	  break;
		  }
		  tick_limits_checked.push(nl[i].getAttribute("name"));
	  }
	}
	if(hasClass(nl[i], 'mand_pw')) {
	  var e2 = document.getElementById(nl[i].getAttribute("title"));
	  if(nl[i].value != e2.value) {
	    alert("The entered passwords do not match.");
	    happy = false;
	    break;
	  }
	  if(nl[i].value.length < 6 && nl[i].value.length > 0) {
	    alert("Your password is too short.");
	    happy = false;
	    break;
	  }
	}
	if(hasClass(nl[i], 'mand_email')) {
	  if(!(nl[i].value.indexOf('@') > 0)) {
	    alert('Please enter a valid email address for "' + nl[i].getAttribute("title") + '"');
	    happy = false;
            break;
	  }
	}
	if(hasClass(nl[i], 'mand_select')) {
	  //look at all the options
	  found = 0;
	  for(ii=0; ii<nl[i].options.length; ii++) {
		  if( (nl[i].options[ii].value != -1) && (nl[i].options[ii].selected) ) {
			  found=1;
		  }
	  }
	  if(!found) {
		   alert('Please select an answer for "' + nl[i].getAttribute("title") + '"');
	       happy = false;
           break;
	  }

	  //if(!(nl[i].value.indexOf('@') > 0)) {
	  //  alert('Please enter a valid email address for "' + nl[i].getAttribute("title") + '"');
	  //  happy = false;
      //      break;
	  //}
	}
 	if(hasClass(nl[i], 'mand_date')) {
 		if(!nl[i].value.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)) {
 			alert('Please enter a valid date for "' + nl[i].getAttribute("title") + '"');
 	    	happy = false;
             break;
 		} else {
 			//correct format - now need to check the actual numbers are valid
 			dateParts = nl[i].value.split('-');
 			
 			//check the month
 			month = dateParts[1];
 			if(month.length<1 || month<1 || month>12) {
 				alert('Please enter a valid month for "' + nl[i].getAttribute("title") + '"');
 				happy = false;
             	break;
 			}
 
 			daysInMonth = new Array()
 			for (ii=1; ii<=12; ii++) {
 				daysInMonth[ii] = 31;
 				if (ii==4 || ii==6 || ii==9 || ii==11) {daysInMonth[ii] = 30;}
 				if (ii==2) {daysInMonth[ii] = 29;}
    			} 
 			//check the day
 			day = dateParts[2];
 			year = dateParts[0];
 			//need to remove 0 from start of month
 			if (month.charAt(0)=="0") {month=month.substring(1);}
 			if(day.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day>daysInMonth[month]) {
 				alert('Please enter a valid day for "' + nl[i].getAttribute("title") + '"');
 				happy = false;
             	break
 			}
 		}
 	}
  }
  if(!happy) break;
  }
  return happy;
}
function addHrefForOneword() {
  nl = document.getElementById("onewordbefunge");
  elems = nl.getElementsByTagName("img");
  for(i = 0; i < elems.length; i++) {
    href = elems[i].getAttribute("src");
	elems[i].setAttribute("src", href.replace('www.oneword.co.uk', 'www.buyassociation.co.uk'));
	if(href.substring(0, 1) == '/') {
	  elems[i].setAttribute("src", "http://www.buyassociation.co.uk" + href);
	}
  }
}
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
addLoadEvent(addConfirmDialogsToDeleteButtons);
addLoadEvent(addVerificationToForms);
//addLoadEvent(addHrefForOneword);

