var dvToload = "";
var error = "";
var dispWhat= "";

function ajRequest(cmd,divId){
	var success	= function(t)
			{				
				var xmlValues = new readXML(t.responseXML);
			};
	var failure	= function(t)
			{
				alert(t.responseText);
			}
		
	var val = "?base=ajax" + cmd;
	val = val.replace("+","%2B");					
	new Ajax.Request('http://www.rockclimbingutah.com/index.php'+val, {
		onSuccess:success,
		onFailure:failure
  	});					
}
function closeCal()
{
	document.getElementById('dispEvents').style.display='none';
}
function readXML(responseXML)
{		
	switch(dispWhat)
	{
		case 'calendar':
			var xt = responseXML.getElementsByTagName('result');
			var elId = document.getElementById('dispEvents');			
			removeChildNodes(elId);		
			
			var spanO = document.createElement('span');
			spanO.setAttribute('class','calSpan');
			var aref = document.createElement('a');
			aref.setAttribute('href','#');
			aref.setAttribute('onClick','closeCal()');
			var txt = document.createTextNode('Close(x)');
			aref.appendChild(txt);	
			spanO.appendChild(aref);
			elId.appendChild(spanO);		

			var spanO1 = document.createElement('span');
			spanO1.setAttribute('class','calSpan');
			var aref1 = document.createElement('a');
			aref1.setAttribute('href','#');
			aref1.setAttribute('onClick','Add()');
			var txt1 = document.createTextNode('Add');
			aref1.appendChild(txt1);
			spanO1.appendChild(aref1);
			elId.appendChild(spanO1);

			if(xt[0].childNodes[0].nodeValue==0)
			{
				var spanO2 = document.createElement('span');
				spanO2.setAttribute('class','calSpan2');

				var newEl = document.createElement('TABLE');
				newEl.setAttribute('cellPadding',5);
				newEl.setAttribute('id','events');
				var tmp = document.createElement('TBODY');				
				newEl.appendChild(tmp);		
			
				var row = document.createElement('TR');				
				var container1 = document.createElement('Th');
				/*var button = document.createElement('input');
				button.setAttribute('type','button');
				button.setAttribute('value','Add');
				button.setAttribute('name','Add');
				
				container1.appendChild(button);*/
				row.appendChild(container1);

				tmp.appendChild(row);
				spanO2.appendChild(newEl);
				elId.appendChild(spanO2);				
				//elId.innerHTML = "No events are/were assigned for this date.";
			}else{
				var spanO2 = document.createElement('span');
				spanO2.setAttribute('class','calSpan2');

				var newEl = document.createElement('TABLE');
				newEl.setAttribute('cellPadding',5);
				newEl.setAttribute('id','events');
				var tmp = document.createElement('TBODY');
				newEl.appendChild(tmp);		
				
				var row = document.createElement('TR');					
				
				var container1 = document.createElement('Th');
				theData1 = document.createTextNode('Event Name');
				container1.appendChild(theData1);			

				var container2 = document.createElement('Th');
				theData2 = document.createTextNode('Event Desc');
				container2.appendChild(theData2);				
				
				row.appendChild(container1);
				row.appendChild(container2);
				//row.appendChild(container3);
								
				tmp.appendChild(row);
				
				var xt1 = responseXML.getElementsByTagName('event');				
				for(j=0;j<xt1.length;j++)					
				{
					var row1 = document.createElement('TR');				
					var container1 = document.createElement('TD');			
					var theData1 = document.createTextNode(xt1[j].childNodes[1].firstChild.nodeValue);
					container1.appendChild(theData1);
					var container2 = document.createElement('TD');			
					var theData2 = document.createTextNode(xt1[j].childNodes[2].firstChild.nodeValue);
					container2.appendChild(theData2);

					var container3 = document.createElement('TD');			
					var button = document.createElement('input');
					button.setAttribute('type','button');
					button.setAttribute('value','delete');
					button.setAttribute('name','Delete');
					container3.appendChild(button);

					row1.appendChild(container1);
					row1.appendChild(container2);
					row1.appendChild(container3);
										
					tmp.appendChild(row1);				
				}
				spanO2.appendChild(newEl);
				elId.appendChild(spanO2);			
				//elId.appendChild(newEl);				
			}
			
			break;	
		default:	
			var xt = responseXML.getElementsByTagName('result');
			if(xt[0].childNodes[0].nodeValue==0)
			{		
				document.getElementById(dvToload).style.background = '#99ff99';		
				document.getElementById(dvToload).innerHTML='Username Available';		
			}else{		
				document.getElementById(dvToload).style.background = '#FF6600';		
				document.getElementById(dvToload).innerHTML='Username UnAvailable';		
				error = "Username UnAvailable";
			}
			break;	
	}		
}

function checkAv()
{
	dvToload = 'usrAv';
	if(document.reg.LoginName.value!='')
	{				
		var username = document.reg.LoginName.value;
		var cmd = '&wtg=userAv&userName='+username+'&displayHeader=off';	
		ajRequest(cmd,'usrAv');
	}else
	{
		document.getElementById(dvToload).style.background = '#FF6600';		
		document.getElementById(dvToload).innerHTML='Login Name Cannot Be Empty';
	}
}
function ValidateFormInfo(theform) {	
	var sErrors = "";
	var sFocus = "";
	
	if (document.reg.LoginName.value == ""){
		sErrors += "\t* - Login Name\n";
	}
	if(document.reg.password.value == "")
	{
		sErrors += "\t* - Password\n";
	}else{
		if(document.reg.password.value != document.reg.RePassword.value)
		{
			sErrors += "\t* - Password do not match\n";
		}
	}
	if(document.reg.recaptcha_response_field.value=="")
	{
		sErrors += "\t* - Word Verification\n";
	}
	if(document.reg.TOService.checked==false)
	{
		sErrors += "\t* - User Agreement\n";
	}			
	if (document.reg.email.value == ""){
		sErrors += "\t* - Email\n";
	}
	if (/^\w+([\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.reg.email.value)){ }else{ 		
		sErrors += "\t* - VALID EMAIL ADDRESS\n";
	}
	if (sErrors != "") {		
		// turn on the alert box
		document.getElementById("alertBox").style.display = "block";
		document.getElementById("alertBox").innerHTML = "Please check the error/s below: "+sErrors;
		return false;
	}
}
function ValidateLogin(theform){
	var sErrors = "";
	if (document.reg.LoginName.value == ""){
		sErrors += "\t* - Login Name\n";
	}
	if(document.reg.password.value == "")
	{
		sErrors += "\t* - Password\n";
	}
	if (sErrors != "") {		
		// turn on the alert box
		alert('Please enter login name and/or password');
		return false;
	}
}

function getEvents(day,ev)
{
	dispWhat = 'calendar';
	var cmd = '&wtg=getEvent&day='+day;		
	ajRequest(cmd,'usrAv');
	var elId = document.getElementById('dispEvents');	
    	var posx = 0;
    	var posy = 0;
    	var e = ev || window.event;	
    	if (e.pageX || e.pageY)     {	
    	    posx = e.pageX;
    	    posy = e.pageY;
    	}else if (e.clientX || e.clientY)     {	
    	    posx = e.clientX + document.body.scrollLeft
    	        + document.documentElement.scrollLeft;
    	    posy = e.clientY + document.body.scrollTop
    	        + document.documentElement.scrollTop;
    	}	    
    	posy = posy;
    	posx = posx;			
    	elId.style.top = posy+'px';
    	elId.style.left = posx+'px';	
    	elId.style.display = 'block';
}	


function removeChildNodes(ctrl)
{
  while (ctrl.childNodes[0])
  {
    ctrl.removeChild(ctrl.childNodes[0]);
  }
}
