
document.getElementsByClassName = function (needle)
{
  var         my_array = document.getElementsByTagName("*");
  var         retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}

function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, true);
		return true;
	} 
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} 
	else 
	{
		return false;
	}
}

addEvent(window, 'load', function()
{
	var counts = document.getElementsByClassName('maxlength');
	var i, count, matches, countHolder;
	
	for (i=0; i<counts.length; i++)
	{
		count = counts[i];	
		matches = count.className.match(/max_([0-9]+)/);
		count.maxVal = RegExp.$1;
		count.holder = document.getElementById(count.id + 'Count');
		if (count.holder)
		{
			count.holder.innerHTML = count.maxVal - count.value.length;
			count.onkeyup = function()
			{
				if (this.value.length > this.maxVal)
					this.value = this.value.substring(0, this.maxVal);
	
				this.holder.innerHTML = this.maxVal - this.value.length;		
			}
		}
	}	
});

function fMKBg(){
   document.getElementById('comment').style.height = "320px";
   document.getElementById('NoBigger').style.display = "none";
}

function validateComment()
{
    setcookie(document.getElementById("remember").checked);

    var errors = "";

    var nameVal = document.getElementById("name").value;
    var locationVal = document.getElementById("location").value;
    var commentVal = document.getElementById("comment").value;
    var agree = document.getElementById("agree").checked;

    if (isEmpty(nameVal)) errors = errors + ", Name";
    if (isEmpty(locationVal)) errors = errors + ", Town and country";
    if (isEmpty(commentVal)) errors = errors + ", Comment";

    if (errors!="") {
        errors = errors.substring(2) + " may not be blank\n";
        alert ("You must correct the following errors before submitting your comment:\n\n" + errors);
        return false;
    }

   	if (nameVal.indexOf('@') != -1) {
   		alert("The name may not contain the @ (at) character!");
   		return false;
   	}

   	if (locationVal.indexOf('@') != -1) {
   		alert("The location may not contain the @ (at) character!");
   		return false;
   	}

    if (agree==false) {
    	alert ("Please read and agree to our House Rules");
    	return false;
    }

    return true;
}

function validateSubmit() {
    var challenge = document.getElementById("challenge").value;
    var agree = document.getElementById("agree").checked;
    
    if (isEmpty(challenge)) {
        alert ("Please enter the verification code");
    } else if (agree==false) {
		alert ("Please read and agree to our terms and conditions");
    } else {
    	return true;
    }
    return false;
}

function validateAbuseReport() {

	var email = jQuery("#reporterEmail");
	var comment = jQuery("#reporterComment");
	var challenge = jQuery("#challenge");
	
	if(isEmpty(email.val()) || !isValidEmail(email.val())) {
		alert("Please enter a valid email address!");
		email.focus();
		return false;
	}
	if (isEmpty(comment.val())) {
		alert ("Please enter details of your complaint");
		comment.focus();
		return false;
	}
	if (isEmpty(challenge.val())) {
		alert ("Please enter the verification code!");
		challenge.focus();
		return false;
	}

	return true;
}

function isEmpty(val)
{
    if ((val==null) || (val==''))
    {
        return true;
    }

    for (var i=0;i<val.length;i++)
    {
        var c = val.charAt(i);
        if ((c!='') && (c!=' ') && (c!='\n') && (c!='\r'))
        {
            return false;
        }
    }
    return true;
}

var cookieName = "rcremember=";

function popFrmCk(){
   var allcookies = document.cookie;
   var loc = allcookies.indexOf(cookieName);
   if (loc!=-1){

    var start = loc + cookieName.length;
    var end = allcookies.indexOf(";",start);
    if (end==-1)
    {
        end = allcookies.length;
    }
    var fieldVals = unescape(allcookies.substring(start, end));
    var sep = fieldVals.indexOf("&");
    document.getElementById("name").value = fieldVals.substring(0, sep);
    document.getElementById("location").value = fieldVals.substring(sep+1, fieldVals.length);
    document.getElementById("remember").checked = true;
   }
}

function setcookie(add)
{
    if (add)
    {
        var expyear = new Date();
        var fieldVals = escape(document.getElementById("name").value +
                        "&" + document.getElementById("location").value);
        expyear.setFullYear(expyear.getFullYear()+10);
        document.cookie = cookieName+fieldVals+
                "; path=/"+
                "; expires="+expyear.toGMTString();
    } else
    {
        document.cookie = cookieName+
                          "; path=/"+
                          "; expires=Fri, 02-Jan-1970 00:00:00 GMT";
    }
}

