// AJAX script

var http = false;
var loadstatustext = "<img src='static/_loopylove/i/ajaxtabs/ajaxtabs/loading.gif' /> Requesting content...";

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
} 

function highlightMsg() {
	new Effect.Highlight('topMsgBox', {startcolor:'#ffffcc', endcolor:'#ffffff', duration: 5000.0}); 
}

function getAJAXContent(thisURL,thisDiv) {
// display wait message
document.getElementById(thisDiv).innerHTML = loadstatustext;

  http.open("GET", thisURL+'&body_only=yes', true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      document.getElementById(thisDiv).innerHTML = http.responseText;
      tb_init('a.thickbox');
    }
  }
  http.send(null);
}

function clearDIVContent(thisDiv) {
	document.getElementById(thisDiv).innerHTML = '';
}

// used for the hometabbed box primarily, this function switches the display styles of the given elements

function showHide(show,hide) {
	var arrID = hide.split(",");
	var hideLen = arrID.length;
	document.getElementById(show).style.display = 'block';
	
	for (i=0;i<hideLen;i++)
	{
	document.getElementById(arrID[i]).style.display = 'none';
	}
}

// function deals with changing the class of list items, used for tabbed content boxes

function changeClass(sel,desel) {
	var arrID = desel.split(",");
	var deselLen = arrID.length;
	
	// set the new class for the active item
	document.getElementById(sel).className="on"
	classString="on"
				
	// deselect the inactive items
	for (i=0;i<deselLen;i++)
	{
	document.getElementById(arrID[i]).className="off"
	classString="off"
	}
}

// change tabs function pass the id of the div or ul containing the list elements that need to be changed and the id of the tab to be selected.
// function will loop through the rest of the list elements and swap them to off status.

function changeTabs(containerID,tabID){
	//if (document.getElementById(containerID)){
		var tmpVar = document.getElementById(containerID).getElementsByTagName('li');
		for (i=0;i<tmpVar.length;i++){
			if (tabID == tmpVar[i].id){
				tmpVar[i].className = 'on';
				//alert(tmpVar[i].id);
				//alert(tmpVar[i].style.background);
			}
			else
			{
				tmpVar[i].className = 'off';
			}
		}
	//}
}


// image form submission used for the photo uploads

function submitimageform()
{
	if (document.myImageForm.imagefile.value == '')
		{
		alert('Please select a GIF or JPG image!')
		document.myImageForm.imagefile.focus();
		return false;
		}
	else
	{
		// 09.01.2008 Jane.Tan
		// extension validation
		var extension = new Array();
		var fieldvalue = document.myImageForm.imagefile.value;
			
			// allowed extensions
			extension[0] = ".jpg";
			extension[1] = ".jpeg";
			extension[2] = ".gif";
			extension[3] = ".wmf";
			extension[4] = ".png";
			extension[5] = ".emf";
			extension[6] = ".exi";
			extension[7] = ".bmp";
			extension[8] = ".JPG";
			extension[9] = ".JPEG";
			extension[10] = ".GIF";
			extension[11] = ".WMF";
			extension[12] = ".PNG";
			extension[13] = ".EMF";
			extension[14] = ".EXI";
			extension[15] = ".BMP";
			
			document.myImageForm.imagefile.focus();
			
			var thisext = fieldvalue.substr(fieldvalue.lastIndexOf('.'));
			for(var i = 0; i < extension.length; i++) {
				if(thisext == extension[i]) { 
					alert("The image(s) you just uploaded will be viewed by a moderator within 48 hours\nplease be patient and refrain from uploading the same image again");
					document.myImageForm.submit();
					return true; 
				}
			}
		
			alert("Oops! Your picture was in the wrong format. Please try again.");
			return false; 
	}
}
	
	
// textcounter for wall messages

function textCounter(field,cntfield,maxlimit) 
	{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
		cntfield.value = maxlimit - field.value.length;
	}
	
	
// registration form validation

function ValidateBasics() {
	
	// tagline
	if (document.basicForm.tagline.value.length < 10)
			{
			alert ('tagline must be at least 10 characters')
			document.basicForm.tagline.focus()
			return false
			}
			
	// description
	if (document.basicForm.description.value.length < 10)
			{
			alert ('description must be at least 10 characters')
			document.basicForm.description.focus()
			return false
			}
			
	// interests
	var total = 0;
	var max = document.basicForm.catid.length;
	for (var idx = 0; idx < max; idx++) {
	if (eval("document.basicForm.catid[" + idx + "].checked") == true) {
	    total += 1;
	   }
	}
	if (total >= 1) {
		// alert("You selected " + total + " boxes.");
	}
	else {
		alert("Please select at least one answer");
		var errors = 1;
		return false
	}	

}

/* counts characters remaining in a text field*/ 
function countVar(fld,display){
	
	tmp = document.getElementById(fld);
	cnt = tmp.maxLength;
	document.getElementById(display).innerHTML = cnt - tmp.value.length;

}

function textCounter2(field,cntfield,maxlimit) {
	var tmpvar = document.getElementById(field);
	var tmpCntField = document.getElementById(cntfield);
	
	if (tmpvar.value.length > maxlimit) // if too long...trim it!
		{	
			tmpvar.value = tmpvar.value.substring(0, maxlimit);
		}
	// otherwise, update 'characters left' counter
		
	else
		{
			tmpCntField.innerHTML = maxlimit - tmpvar.value.length;
		}
	}

function updateWord(){
			alert(document.getElementById('loopyword').value);
		}
