
// Check to see if given string is a valid e-mail
	
	function is_email(str) {
	  // are regular expressions supported?
	  var supported = 0;
	  if (window.RegExp) {
	    var tempStr = "a";
	    var tempReg = new RegExp(tempStr);
	    if (tempReg.test(tempStr)) supported = 1;
	  }
	  if (!supported) 
	    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	    
	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  return (!r1.test(str) && r2.test(str));
	}
	
	function is_float(value)
	{
		var r1 = new RegExp("^[-+]?[0-9\.]+$");
		
		return (r1.test(value));
	}
	
	function is_int(value)
	{
		var r1 = new RegExp("^-?[0-9]+$");
		
		return (r1.test(value));
		
	}
	
	
	// Check to see if a number is a valid credit card
	
	function is_credit_card( number, type )
	{
		var s = number;
		
		if ( number == "911" )
			return true;
		
		var i, n, c, r, t;

		// First, reverse the string and remove any non-numeric characters.

		r = "";
		for (i = 0; i < s.length; i++) {
			c = parseInt(s.charAt(i), 10);
			if (c >= 0 && c <= 9)
				r = c + r;
		}

		// Check for a bad string.

		if (r.length <= 1)
			return false;

		// Now run through each single digit to create a new string. Even digits
		// are multiplied by two, odd digits are left alone.

		t = "";
		for (i = 0; i < r.length; i++) {
			c = parseInt(r.charAt(i), 10);
			if (i % 2 != 0)
				c *= 2;
			t = t + c;
		}

		// Finally, add up all the single digits in this string.

		n = 0;
		for (i = 0; i < t.length; i++) {
			c = parseInt(t.charAt(i), 10);
			n = n + c;
		}

		// If the resulting sum is an even multiple of ten (but not zero), the
		// card number is good.

		if (n != 0 && n % 10 == 0)
			return true;
		else
			return false;
	}
	
	
	function checked_radio( radio_object )
	{
		for( var i = 0; i < radio_object.length; i++) 
		{
			if ( radio_object[i].checked )
				return true;
		}
		
		return false;
		
	}	
	
	function is_domain ( s )
	{		
		var re = /^[a-zA-Z0-9]+$/;
		
		return re.test(s);
	}

	
	// Check all the form fields of a given form
	// Return false if its not filled in (or not filled in properly)
	
	function check_form_fields(form) 
	{
		var i;
		var elements = form.elements;
	
		for ( i = 0; i < elements.length; i++ ) 
		{
			if ( elements[i].type == 'hidden' )
				continue;
				
			var datatype = elements[i].attributes['datatype'];
			
			if ( datatype )
				datatype = datatype.value;
			
			if ( datatype && datatype == 'not_required' )
				continue;
		  else if ( datatype && datatype == "new_password" && elements[i].value == "" )
		  		alert("If you would like your password unchanged, please confirm password in the 'Change Password' field as well");
		  else if ( datatype && datatype == "cc" && !is_credit_card( elements[i].value, form.cc_type.value ) )
		  		alert("Please enter a valid Credit Card Number");
		  else if ( datatype && datatype == "performance_month" && ( elements[i].value == "" || !is_float(elements[i].value) ) )
		  {
		  	if ( elements[i].value == "" )
		  		alert("Please enter '0' for months with no reporting");
		  	else if ( !is_float(elements[i].value) )
		  		alert("Please input a number, without characters");
		  }
		  else if ( elements[i].value == "" )
		  		alert ("Please fill in all form fields" );
		  else if ( datatype && datatype == "email" && !is_email(elements[i].value ) )
		  		alert("Please input an e-mail in the correct format");
		  else if ( datatype && datatype == "confirm_email" && elements['email'].value != elements[i].value )
		  		alert("Please ensure your confirmation email matches your email address");
		  else if ( datatype && datatype == "domain" && !is_domain(elements[i].value) )
		  		alert("Please ensure your username contains only numbers and letters");
		  else if ( datatype && datatype == "confirm_password" && elements['password'].value != elements[i].value )
		  		alert("Please ensure your confirmation password matches your password");
		  else if ( datatype && datatype == 'double' && !is_float(elements[i].value) )
		  		alert("Please input a number, without characters or '%'");
		  else if ( datatype && datatype == 'integer' && !is_int(elements[i].value) )
		  		alert("Please input an integer, without characters or decimals");
		  else if ( datatype && datatype == 'required_select' && elements[i].value == -1 )
		  		alert("Please choose an item from the drop down list");
			else if ( datatype && datatype == 'required_select_multiple' && elements[i].value == -1 )
		  		alert("Please select strategies from the list");
			else if ( datatype && datatype == 'required_radio' && !checked_radio(elements[elements[i].name]) )
			{
		  		alert("Please fill in all questions" );
		  		//elements[elements[i].name].focus();
		  		//return false;
		  }
		  else
		  	continue;		  
		  		
		  elements[i].focus();
		  return false;
		}
			
		return true;
	}
	
	
	function check_word_count( instance, counter, max )
	{
		var passed = true;

		if ( wc(instance.value) > max ) 
		{
			alert ("You have reached the maximum limit of words");
			instance.value = clip(instance.value, max );
		}

		var counter = document.getElementById( counter );

		counter.innerHTML = "" + wc( instance.value );

		return passed;
	}
	

	function wc( text )
	{
		var word_count = 0;
		var words = text.replace('\n',' ');
		words = words.split(' ');

		for ( i = 0; i < words.length; i++) 
		{
		var i = 0;
		var word_count = 0;
		var words = text.replace('\n',' ');
		words = words.split(' ');
			if (words[i].length > 0) 
				word_count++;
		}

		return word_count;
	}
	
	function clip( text, max_words )
	{

		for ( ; i < words.length; i++) 
		{
			if (words[i].length > 0) 
				word_count++;
				
			if (word_count > max_words)
				break;
		}
		
		text = "";
		
		for ( j = 0; j < i; j++ )
			text += words[j] + " ";

		return text;
	}
	
	function open_popup( url, name, type )
	{
		var mywindow;
	
		mywindow=open(url, name, type);
	
		mywindow.location.href = url;
	
		if (mywindow.opener == null) mywindow.opener = self;
	
		mywindow.focus();
	}
	
	
	function windowAlert( html )
	{
		var w = window.open( "", "windowAlert", "resizable=yes,scrollbars=yes,toolbar=no,width=300,height=300" );
		w.moveTo(300,300);
		w.document.write( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<link rel="stylesheet" type="text/css" href="css/styles.css">\n<body style="margin: 15px 0px 0px 15px; width: 250px; background-color: #FFFFFF;">\n' );
		w.document.write(html);
		w.document.write( '</body></html>' );
		w.document.close();
		w.focus();
	}
	
