/*
	getElementsByClass - algorithm by Dustin Diaz, shortened by Pawel Knapik
*/
function getElementsByClass(s,n,t) {  // class, node, tag
	var c=[], e=(n?n:document).getElementsByTagName(t?t:'*'),r=new RegExp("(^|\\s)"+s+"(\\s|$)");
	for (var i=0,j=e.length;i<j;i++) r.test(e[i].className)?c.push(e[i]):''; return c }
	
/*
	$() based on prototype.js dollar function idea, optimized by Pawel Knapik.
*/
function $(){var r=[],a=arguments;for(var i=0,j=a.length;i<j;i++){(typeof a[i]=='string')?(r.push(document.getElementById(a[i]))):(r.push(a[i]))}
return(r.length==1)?r[0]:r}


Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};


// By Simon Willison

function setCookie(name,value,expires, options) {
   if (options===undefined) { options = {}; }
   if ( expires ) {
      var expires_date = new Date();
      expires_date.setDate(expires_date.getDate() + expires)
   }
   document.cookie = name+'='+escape( value ) +
      ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + 
      ( ( options.path ) ? ';path=' + options.path : '' ) +
      ( ( options.domain ) ? ';domain=' + options.domain : '' ) +
      ( ( options.secure ) ? ';secure' : '' );
}

function getCookie( name ) {
   var start = document.cookie.indexOf( name + "=" );
   var len = start + name.length + 1;
   if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
      return null;
   }
   if ( start == -1 ) return null;
   var end = document.cookie.indexOf( ';', len );
   if ( end == -1 ) end = document.cookie.length;
   return unescape( document.cookie.substring( len, end ) );
}

function deleteCookie( name, path, domain ) {
   if ( getCookie( name ) ) document.cookie = name + '=' +
      ( ( path ) ? ';path=' + path : '') +
      ( ( domain ) ? ';domain=' + domain : '' ) +
      ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

// functions by jonathan gala: www.jongala.com

function msg(s,c) {	c?$('msgbox').innerHTML+= " -- " + s:$('msgbox').innerHTML=s;};

// requiredform requires a value for everything with class 'required' and validates and requires email addresses
function checkform(f) {
	// check for required elements, alert and focus if blank.
	reqs = getElementsByClass("required",f);
	for (i=0 ; i<reqs.length ; i++) {
		req = reqs[i];
		if (isBlank(req.value)) {
			alert("Please enter a value for " + req.name);
			req.focus();
			return false;
		}
	}
	// now check for email inputs, and validate using the regex function
	emails = getElementsByClass("email",f);
	for (j=0 ; j<emails.length ; j++) {
		email = emails[j];
		if (!checkemail(email.value) && email.value.length>0) {
			alert("Please enter a valid email for " + email.name);
			email.focus();
			return false;
		}
	}
}

// requiredform requires a value for everything without class 'optional'
function requiredform(f) {
fields = f.elements;
	for (i=0 ; i<fields.length ; i++) {
		field = fields[i];
		opt=false;
		fclass = field.className;
		fclasses = field.className.split(" ");
		if(fclasses.inArray('optional')) opt=true;
		
		if (isBlank(field.value) && !opt) {
			alert("Please enter a value for " + field.name);
			field.focus();
			return false;
		}
	}
};

function isBlank(val) {
	if(val==null){return true;}
	var b = /\S/;
	var match = b.test(val);
	return !match;
}


function checkemail(email) {
	/* This RegExp requires an address of the form xxx@xxx.xxx where xxx is one or more alphanumeric characters.  The last xxx can't have any numbers.  */
	var address= /[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}/;
	var match = address.test(email);
	return match;
}

function checkphone(num) {
	/* This functionjust strips non-digits, and checks to see that there are at least 10 numbers left over */
	x = num.replace(/[\D]*/g,"");
	if (x.length<10) {
		return false;
	} else {
		return true;
	}
}