
var pendingRequest = false;
var async = null;

function setPending(target) {
  pendingRequest = true;
	div = document.getElementById(target);
	div.innerHTML = "<br/><img src='core/images/progress.gif' alt='Please Wait...' align='center' /> Loading ... (<a href='javascript:stopRequest()'><img src='/core/images/icons/link_break.png' alt='x' /></a>)<br/><br/><br/>";
}

var handleSuccessRequest = function(o) {
  pendingRequest = false;
  var arguments = o.argument.split(":");
  var div = document.getElementById(arguments[0]); // alert("arg: " + arguments[0]);
	if (o.responseText !== undefined) {
		if (div != null) div.innerHTML = o.responseText;
		if (arguments.length > 1) {
		  div = document.getElementById(arguments[1]);
		  if ((div != null) && (div.disabled == false)) div.focus();
		}
		if (arguments.length > 2) {
		  functionCall = eval(arguments[2]);
		  functionCall();
		}
	} else {
	  div.innerHTML = "<br/>Processing Failed.  Please Reload and Try Again.<br/><br/><br/>";
	}
};

var handleFailureRequest = function(o) {
  pendingRequest = false;
  arguments = o.argument.split(":")
	div = document.getElementById(arguments[0]);
	div.innerHTML = "<br/>Processing Failed.  Please Reload and Try Again.<br/><br/><br/>";
};

var handleAbortRequest = function(o) {
  pendingRequest = false;
  arguments = o.argument.split(":")
	div = document.getElementById(arguments[0]);
	div.innerHTML = "<br/>Transaction aborted.<br/><br/><br/>";
};

var handleUpload = function(o) {
  pendingRequest = false;
  alert('upload');
}

var callbackRequest =
{
  success:handleSuccessRequest,
  upload:handleSuccessRequest,
  failure:handleFailureRequest,
  abort:handleAbortRequest,
  timeout: 2000000
};

function stopRequest() {
  result = YAHOO.util.Connect.abort(async, callbackRequest);
}

function coreGet(target, url, focus) {
  if (!pendingRequest) {
		setPending(target);
		callbackRequest['argument'] = target + ':' + focus;
	  async = YAHOO.util.Connect.asyncRequest('GET', url, callbackRequest);
	}
}

function coreGetLoop(target, url, focus, functionCall) {
  if (!pendingRequest) {
		setPending(target);
		callbackRequest['argument'] = target + ':' + focus + ':' + functionCall;
	  async = YAHOO.util.Connect.asyncRequest('GET', url, callbackRequest);
	}
}

function coreGetAdvLoop(pendingTarget, target, url, focus, functionCall) {
  if (!pendingRequest) {
		setPending(pendingTarget);
		callbackRequest['argument'] = target + ':' + focus + ':' + functionCall;
	  async = YAHOO.util.Connect.asyncRequest('GET', url, callbackRequest);
	}
}

function corePost(target, url, targetform, multipart) {
  if (!pendingRequest) {
		setPending(target);
		callbackRequest['argument'] = target;
		YAHOO.util.Connect.setForm(targetform, multipart);
	  async = YAHOO.util.Connect.asyncRequest('POST', url, callbackRequest);
	}
}

function corePostLoop(target, url, targetform, multipart, focus, functionCall) {
  if (!pendingRequest) {
		setPending(target);
		callbackRequest['argument'] = target + ':' + focus + ':' + functionCall;
		YAHOO.util.Connect.setForm(targetform, multipart);
	  async = YAHOO.util.Connect.asyncRequest('POST', url, callbackRequest);
	}
}

function coreGetToken(target, url, focus, token, func) {
  if (!pendingRequest) {
    coreGet(target, url, focus);
    YAHOO.util.Event.onAvailable(token, eval(func));
  }
}

var testingfunc = function() {
  alert('test');
}

function corePostToken(target, url, targetform, multipart, token, func) {
  if (!pendingRequest) {
    corePost(target, url, targetform, multipart);
    YAHOO.util.Event.onAvailable(token, eval(func));
  }
}

var responseSuccessLogin = function(o) {
  var div = document.getElementById('loginResult');
  if (o.responseText !== undefined) {
    div.innerHTML = "<br/>" + o.responseText;
    if (o.responseText.substr(0,16) == "Login Successful") {
      location.reload();
    }
  } else {
    div.innerHTML = "[ERROR] Unable to authenticate.  Try again later.";
  }
};

var responseFailure = function(o) {
  var div = document.getElementById('loginResult');
  div.innerHTML = "[ERROR] Unable to authenticate.  Try again later.";
};

var callbackLogin = 
{ 
  success:responseSuccessLogin, 
  failure:responseFailure, 
  timeout: 10000
};

function authenticate() {
  var div = document.getElementById('loginResult');
  div.innerHTML = "<br/><img src='core/images/progress.gif' alt='' /> Authorizing ...<br/>";
  var form = document.getElementById('loginForm');
  YAHOO.util.Connect.setForm(form);
  var transaction = YAHOO.util.Connect.asyncRequest('POST', '/user.login.rpc', callbackLogin);
}

var responseSuccessLogout = function(o) {
  location.reload();
};

var responseFailureLoginLoad = function(o) {
  alert("[ERROR] Unable to process.  Try again later.");
};

var callbackLoginLogout = 
{ 
  success:responseSuccessLogout,
  failure:responseFailureLoginLoad,
  timeout: 10000
};

function checkNewUser() {
  var div = document.getElementById('loginArea');
  div.innerHTML = "";
  var div = document.getElementById('result');
  if (div.innerHTML == "User Account Saved") location.reload();
};

function loginLoad() {
  coreGet('loginArea', '/user.loginform.rpc', 'loginID');
}

function newUser() {
  loginClear();
  coreGet('maincontent', '/user.start.rpc', 'loginID');
}

function saveUser() {
  if (!pendingRequest) {
    var startform = document.getElementById('startform');
		setPending('loginArea');
		callbackRequest['argument'] = 'maincontent:username:checkNewUser';
	  YAHOO.util.Connect.setForm(startform);
	  async = YAHOO.util.Connect.asyncRequest('POST', '/user.create.rpc', callbackRequest);
	}
}

function logout() {
  var transaction = YAHOO.util.Connect.asyncRequest('GET', '/user.logout.rpc', callbackLoginLogout);
}

function clearArea(target) {
  document.getElementById(target).innerHTML = "";
}

function loginClear() {
  clearArea('loginArea');
}

function show(target) {
  div = document.getElementById(target);
  div.style.visibility = "visible";
  div.style.display = "block";
}
  
function hide(target) {
  div = document.getElementById(target);
  div.style.visibility = "hidden";
  div.style.display = "none";
}

function toggle(target) {
  div = document.getElementById(target);
  if (div.style.visibility == "hidden") {
    show(target);
  } else {
    hide(target);
  }
}

function checkEnter(e) {
  e = e || event;
  return (e.keyCode || event.which || event.charCode || 0) !== 13;
}

function swap(target, imagelink) {
  div = document.getElementById(target);
  div.src = imagelink;
}

function refreshPage() {
  window.location.href=window.location.href;
}

function endsWith(phrase, str)
{
    var lastIndex = phrase.lastIndexOf(str);
    return (lastIndex != -1) && (lastIndex + str.length == phrase.length);
}

function notvalid(str) {
  value = document.getElementById(str).value;
  notvalidresult = ((value == null) || (value == "") || (value.charAt(0) == ' '));
  if (notvalidresult) document.getElementById(str).focus();
  return notvalidresult;
}

function notimagefile(str) {
  value = document.getElementById(str).value;
  notvalidresult = ((value == null) || (value == "") || (value.charAt(0) == ' '));
  if (!notvalidresult) {
		notvalidresult = (!endsWith(value.toLowerCase(), ".jpg")) && (!endsWith(value.toLowerCase(), ".gif")) && (!endsWith(value.toLowerCase(), ".png"));
  }
  if (notvalidresult) document.getElementById(str).focus();
  return notvalidresult;
}
