// JavaScript Document for WeCanDo.BIZ
// limit characters entered into textarea
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length + " characters left";
	}
}


function countKeywords (this_field, countField, wordlimit) {
	
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2) {
		word_count = 0;
	}
	var words = (word_count == 1) ? " word" : " words";
	
	if(word_count == (wordlimit+1)) {
		this_field.value = this_field.value.substring(0, char_count-1);
	} else {
		countField.value = word_count + words;
	}
}



function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

// toggle all checkboxes
function toggleAll(id){
	box = document.getElementById(id);
	
	var setting = box.checked ? true : false;
	
	var cur_id = id+'1';
	
	var count = 1;
	
	while(box = document.getElementById(cur_id)) {
		box.checked = setting;
		cur_id = id+count;
		count++;
	}
}


/*
	Function to showing processing gif and submit form. Used when adding contacts
------------------------------------------------------------------------------------*/
function processForm()
{
	var button = document.inviteform.submitinvite;
	button.disabled = true;
	button.className = 'disabled';
	
	document.inviteform.submit();
	
	var elem = document.getElementById('processing');
	elem.style.visibility = 'visible';
	elem.style.display = 'inline';
}

/*
	Make sure the checkbox is checked
-------------------------------------------*/
function validateBizNeedForm()
{
	var checkbox = document.getElementById('nosales');

	if (!checkbox.checked) {
		alert('You must confirm that you have have read and adhere to the Biz Needs guidelines');
		return false;
	} else {
		return true;
	}
}

/*
	Function to display a centred popup window
-------------------------------------------------*/
function popitup(url, width, height) {
	var left = (screen.width/2)-(width/2);
	var top = (screen.height/2)-(height/2);
	
	newwindow=window.open(url,'WeCanDoBIZ','height='+height+',width='+width+',top='+top+',left='+left);
	if (window.focus) {newwindow.focus()}
	return false;
}
