//JAVASCRIPT Check Text Area Limit, uses maxLength from TA element attribute
function taLimit(){
	var taObj=event.srcElement;
	if(taObj.value.length==taObj.maxLength*1) return false;
}

//JAVASCRIPT Display Text Area characters left, visCnt is the Span ID for the number left
function taCount(visCnt, maxLength){ 
	var taObj=event.srcElement;
	if(taObj.value.length>maxLength) taObj.value=taObj.value.substring(0,maxLength);
	if(visCnt) visCnt.innerText=maxLength-taObj.value.length;
}

//JAVASCRIPT Change BGColor of an element
function setColor(el, bgc){
	if(el.style) el.style.backgroundColor = bgc;
}
//JAVASCRIPT Check for required fields
function check_required(object_array){
	var count=0;
	var passwrong = 0;	
	for(i=0;i<object_array.length;i++){
		if((object_array[i].value == "") && (object_array[i].name.indexOf("url") == -1)){
			setColor(object_array[i],"#9C9ECE");
			count++;
		}else{
			setColor(object_array[i],"#FFFFFF")
			if(object_array[i].name.indexOf("url") != -1 && object_array[i].value != "") {
				object_array[i].value = check_http(object_array[i].value);
			}			
			if(object_array[i].name.indexOf("password") != -1 && object_array[i].value.length < 6) {
				setColor(object_array[i],"#9C9ECE");
				passwrong = 1;
			}			
		}
	}
	if(count>0){
		alert("Please complete the field(s) highlighted")
		return false
	}else if(passwrong == 1){
		alert("Please make sure your password is 6 to 10 characters long")	
		return false
	}else{
		return true
	}
}

function check_required1(object_array){
	var count=0;
	var passwrong = 0;
	
	if(document.update.utype[0].checked == true || document.update.utype[1].checked == true || document.update.utype[2].checked == true || document.update.utype[3].checked == true || document.update.utype[4].checked == true){
		var count=0;
		var nn =	document.getElementById('nnnn');
		setColor(nn, "#FFFFFF");
		if(document.update.utype[0].checked == true || document.update.utype[1].checked == true || document.update.utype[2].checked == true){
			var categ 	=	document.getElementById('category');
		}else{
			var categ 	=	document.getElementById('category[]');
		}
		var desc		=	document.getElementById('description');
		if(categ.value == ""){ setColor(categ, "#9C9ECE"); count++;	}else{ setColor(categ, "#FFFFFF"); }
		if(desc.value == ""){ setColor(desc, "#9C9ECE"); count++;	}else{ setColor(desc, "#FFFFFF"); }
	}else{
		count++;
		var nn =	document.getElementById('nnnn');
		setColor(nn, "#9C9ECE");
	}
	for(i=0;i<object_array.length;i++){		
		if((object_array[i].value == "") && (object_array[i].name.indexOf("url") == -1)){
			setColor(object_array[i],"#9C9ECE");
			count++;
		}else{
			setColor(object_array[i], "#FFFFFF")
			if(object_array[i].name.indexOf("url") != -1 && object_array[i].value != ""){
				object_array[i].value = check_http(object_array[i].value);
			}			
			if(object_array[i].name.indexOf("password") != -1 && object_array[i].value.length < 6) {
				setColor(object_array[i], "#9C9ECE");
				passwrong = 1;
			}			
		}
	}

		
	 if (document.update.terms.checked == false){
	//	alert('Please agree the membership terms and conditions!');
	//alert("ss");
			var ter 	=	document.getElementById('ter');
			setColor(ter, "#9C9ECE");
count++;
	}
	
 if ( (document.update.links[0].checked == false ) && (document.update.links[1].checked == false ) )
	{
	//alert ( "Please select when will you add TUKAG.com links page ?" );
		var ter1 	=	document.getElementById('ter1');
			setColor(ter1, "#9C9ECE");
count++;
	}
		if(count>0){
		alert("Please complete the required field1(s)")
		return false
	}
	else if(passwrong == 1){
		alert("Please make sure your password is 6 to 10 characters long")	
		return false
	}else{
		return true
	}
}

//JAVASCRIPT Add http:// if required
function check_http(url_string) {
	if(url_string.indexOf("http://") == 0) {
		return url_string;
	}else {
		return "http://" + url_string;
	}
}

//JAVASCRIPT create any pop-ups
function pop_up($url,$title,$h,$w) {
	if(!window.pop_win || window.pop_win.closed) { 
		pop_win=window.open($url,$title,"height="+$h+",width="+$w+",menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,left=10,top=10,screenX=10,screenY=10");
	} else if(window.pop_win.location.href.indexOf($url.substring(1)) == -1) {
		window.pop_win.close();
		pop_win=window.open($url,$title,"height="+$h+",width="+$w+",menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,left=10,top=10,screenX=10,screenY=10");
	}else{
		window.pop_win.focus();
	} 
}

//JAVASCRIPT display a messgae in the status bar ta the bottom of a browser
function displayStatusMsg($msg){
// this is the new one
	window.status = $msg;
	document.$return_value = true;
}
//JAVASCRIPT open the map pop-up
function open_map($title){
// this is the new one
	pop_up("/pop_ups/region_map_popup.html",$title,370,520);
}
//JAVASCRIPT goto the anchor #bottom
function gotobottom(){
// this is the new one
	document.location.href = document.location.href + "#bottom";
}

// word count
function CountWords (this_field) {

	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;
	}
	
	return word_count;
	
}

function wtaLimit(sender) {
	
}

function wtaCount(sender,visCnt,maxLength) {
	
	//alert('wtaCount(' + sender.id + ', ' + visCnt + ', ' + maxLength + ')');
	
	var numwords = CountWords(sender);
	var el = document.getElementById(visCnt);
	/*if(typeof(visCnt) == 'object') // fix for weird IE sending the object rather than object id
	{
		el = visCnt;
	}
	else
	{
		el = document.getElementById(visCnt);
	}	*/
	if(el)
	{
		
		//alert(maxLength - numwords);
		
		el.innerHTML=maxLength-numwords;
	}
}
