window.onload = FormatLoginBox;


function FormatLoginBox () {
	if (document.getElementById("user_login")){ // for the login page // for the forgot pass page
	  document.getElementById("user_login").focus();
	}
}


function wordcount(string, maxlength) {
  var a = string.split(/\s+/g); // split the sentence into an array of words
  if (a.length >= maxlength) {
	alert ("Your custom paragraph is currently " + a.length + " words long. Please revise your text to be no more than " + maxlength + " words.");
	return false;
  } else {
	return true
  }
}
	
	
function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}
function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

function go_now (fname) { window.location.href = fname }




function checkparishform ( form )
{
  
  // ** START **
  if (form.parish_address1.value != "" && (form.parish_city.value == "" || form.parish_zip.value == "")) {
    alert( "Please complete the parish street address." );
    return false ;
  }
  if (form.parish_po_box1.value != "" && (form.parish_po_city.value == "" || form.parish_po_zip.value == "")) {
    alert( "Please complete the parish po box address." );
    return false ;
  }
  // ** END **
  return true ;
}



/*
	The following function alerts the end user to make sure to include their collection date in the Special Instructions area.
*/
function checkparticipationlevel (el) {
	if(el.value.toLowerCase()=='4') {/* Donation */
		document.getElementById("diocollection_date_nonenone").checked = true;
		alert('\'If you do not take a collection, but will still be receiving materials, please remember to enter your preferred materials delivery date into the Special Instructions section of the site.\'');
	}
	if(el.value.toLowerCase()=='1') {/* Non-Participating */
		document.getElementById("diocollection_date_nonenone").checked = true;
		alert('\'If you do not take a collection, but will still be receiving materials, please remember to enter your preferred materials delivery date into the Special Instructions section of the site.\'');
	}
}
function checkcollection_none () {
		document.getElementById("participation_level").options[3].selected = 1;
		alert('\'If you do not take a collection, but will still be receiving materials, please remember to enter your preferred materials delivery date into the Special Instructions section of the site.\'');
}




/*
	The following function alerts the end user if their choice of inventory is too great (higher than maximum) or less than minimum.
*/
function checkMinMax(temp, tempMin, tempMax)
{	
	if (temp > 0 && temp < tempMin) {/* Too Low */
		alert('You have not chosen enough inventory of this item  (' + temp + ').\n\nIn order make shipping cost effective, please enter a quantity more than ' + tempMin + '.');
	}
	
	if (temp > tempMax) {/* Too High */
		alert('You have chosen too many of this inventory item(' + temp + ').\n\nPlease enter a quantity less than ' + tempMax + '.');
	}
}



/*
	The following function fills in the inventory item text box if a check box is CHECKED.
*/
function filltextbox (id, text) {
	document.getElementById(id).value = text;
}