// JavaScript Document

function initPage(){
	
	//Hijack form submit
	$("#loginForm").submit(sendForm);
	
	/* sliding panel js */
	// Expand Panel
	$("#open").click(function(){
		$("div#panel").slideDown("slow");	
	});	
	// Collapse Panel
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});			
}


function GetProducts(){
$("#loading").html("loading...");
	$.getJSON('/user.cfc?wsdl',{ method : 'getUsers', returnformat : 'json', queryformat : 'column' },ShowProducts);
}


function GetProducts2(numby){
$("#loading").html("loading...");
	$.getJSON('/user.cfc?method=getfew&num='+numby+'&returnformat=json&queryformat=column', {},ShowProducts);
}


function ShowProducts(qProducts){

 var str = '<h1>People</h1>'
  if (qProducts.ROWCOUNT==0) {
    alert('Sorry, no matches found');
  }
  else {
    for (var i=0; i<qProducts.ROWCOUNT; i++) {
      
	    if (qProducts.ROWCOUNT==1) {
		str = '<h1>'+qProducts.DATA.USERNAME[i]+'</h1>'

         str += '<b>'+qProducts.DATA.USERNAME[i]+'</b>'
         str += ' Password: '+qProducts.DATA.PASSWORD[i]+' <input type="button" onclick="GetProducts()" value="Reload Usesrs" /><br/>'
		 
		}else{
			
         str += '<b>'+qProducts.DATA.USERNAME[i]+'</b>'
         str += ' Password: '+qProducts.DATA.PASSWORD[i]+' <input type="button" onclick="GetProducts2('+qProducts.DATA.ID[i]+')" value="get Just  me!" /><br/>'
		}
      }
   
    }		
	      $("#test").html(str);
		  $("#loading").html("");

}


function sendForm() {
	
	$("#status").html('Logging in...');
	
	$.getJSON('/user.cfc?method=processLogin&returnFormat=json&queryformat=column',$("#loginForm").serialize(), updateLogin);																						  
	return false
}
																		
																		
function updateLogin(qUsers){

	 if (qUsers.ROWCOUNT==0) {
		 
			$("#status").html('<b>Login failed...<br>Please try again</b>');
			
  		} else {
			
			//set our login message and collapse the panel      
			str = 'Welcome '+qUsers.DATA.FIRSTNAME[0]+'!';
		
			//good login
			$("#status").html('You are logged in!');
		
			$("#logform").load("/menutest.cfm #nav1");
			
			$("#helloMsg").html(str);

			$("#open").html('Menu | Logout');
	
		}

   }


