/**
 * @author Gary Wong
 * 
 * Generic AJAX handlers using JSON and XML response
 */

var postJsonResponseRequest;


function getResponse (link, timeout, target, loadSuccess) {
	this.link = link;
	this.timeout = timeout;
	this.target = target;
	this.loadSuccess = loadSuccess;
	this.startGetResponse;
	
	this.callback = {
		success: function (oResponse) {
			loadSuccess(oResponse.responseText);
		},
		failure: function (oResponse) {
			//alert('An Error Has Occured');
		},
		argument: {
			"target":target,
			"loadTarget" : loadSuccess
		}, 
		timeout: timeout
		};
	YAHOO.util.Connect.asyncRequest('GET', this.link, this.callback);
}

function getJsonResponse(link, timeout, target, loadSuccess) {
	this.link = link;
	this.timeout = timeout;
	this.target = target;
	this.loadSuccess = loadSuccess;
	this.startJsonGetResponse;
	
	this.callback = {
		success: function (oResponse) {
			try {
				loadSuccess(YAHOO.lang.JSON.parse(oResponse.responseText.trim()), target);
			} catch (e) {
				$log(e);
			}
		},
		failure: function (oResponse) {
			//alert('An Error Has Occured');
		},
		argument: {
			"target":target,
			"loadTarget" : loadSuccess
		}, 
		timeout: timeout
	};
	YAHOO.util.Connect.asyncRequest('GET', this.link, this.callback);
}

function postJsonResponse (link, timeout, target, loadSuccess, values, argument) {
	this.link = link;
	this.timeout = timeout;
	this.target = target;
	this.loadSuccess = loadSuccess;
	this.startJsonPostResponse;
	this.values = values;
	
	if (postJsonResponseRequest) {
		YAHOO.util.Connect.abort(postJsonResponseRequest);
	}
	
	this.callback = {
		success: function (oResponse){
			try {
				loadSuccess(YAHOO.lang.JSON.parse(oResponse.responseText.trim()), target,oResponse.argument);
			} catch (e) {
				$log(e);
			}
		},
		failure: function (oResponse) {
		}, 
		upload: function (oResponse) {
			$log(oResponse);
			loadSuccess(YAHOO.lang.JSON.parse(oResponse.responseText.trim()), target,oResponse.argument);
		},
		argument: argument,
		time: timeout
	};
	postJsonResponseRequest = YAHOO.util.Connect.asyncRequest('POST', this.link, this.callback, this.values);
}
function postXMLResponse (link, timeout, target, loadSuccess, values, argument) {
	this.link = link;
	this.timeout = timeout;
	this.target = target;
	this.loadSuccess = loadSuccess;
	this.startJsonPostResponse;
	this.values = values;
	this.callback = {
		success: function (oResponse){
			loadSuccess(oResponse.responseText.trim(), target,oResponse.argument);
		},
		failure: function (oResponse) {
			//alert(oResponse);
		}, 
		upload: function (oResponse) {
			loadSuccess(oResponse.responseText.trim(), target,oResponse.argument);
		},
		argument: argument,
		time: timeout
	};
	YAHOO.util.Connect.asyncRequest('POST', this.link, this.callback, this.values);
}

function formData2QueryString (docForm) 
{
	var submitString = '';
	var formElement = '';
	var lastElementName = '';
	for(i = 0; i < docForm.elements.length; i++) {
		formElement = docForm.elements[i];
		switch(formElement.type) {
			case 'text' :
			case 'button' :
			case 'select-one' :
			case 'hidden' :
			case 'password' :
				submitString += formElement.name + '=' + paramEscape(formElement.value) + '&';
				break;
			case 'textarea' :
				formEditText = tinyMCE.activeEditor.getContent();
				submitString += formElement.name + '=' + paramEscape(tinyMCE.activeEditor.getContent()) + '&';
				break;
			case 'radio' :
				if(formElement.checked) {
					submitString += formElement.name + '=' + paramEscape(formElement.value) + '&';
				}
				break;
			case 'checkbox' :
				if(formElement.checked) {
					if(formElement.name == lastElementName) {
						if(submitString.lastIndexOf('&') == submitString.length - 1) {
							submitString = submitString.substring(0, submitString.length - 1);
						}
						submitString += ',' + paramEscape(formElement.value);
					} else {
						submitString += formElement.name + '=' + paramEscape(formElement.value);
					}
					submitString += '&';
					lastElementName = formElement.name;
				}
				break;
		}
	}
	submitString = submitString.substring(0, submitString.length - 1);
	return submitString;
}

function postFormResponse (link, theForm, loadSuccess) 
{
	var result;
	var jsonTimeOut = 5000;
	this.postLink = link;
	this.postForm = theForm;
	var loadPostSuccess = loadSuccess;
	
	submitPost = function (postLink, postForm)
	{
		YAHOO.util.Connect.setForm(postForm, false);
		
		clearForm(postForm);
		lockButton(postForm);
		new postXMLResponse(postLink,jsonTimeOut, '', loadPostSuccess, "", "");
	}; 
	submitPost(this.postLink, this.postForm);
}


function deleteComment (link, deleteSuccess)
{
	var result; 
	var jsonTimeOut = 5000;
	this.commentDeleteLink = link;
	var deleteCommentSuccess = deleteSuccess;
	
	submitDeleteComment =  function (commentDeleteLink, deleteCommentSuccess) 
	{
		new postJsonResponse(commentDeleteLink, jsonTimeOut, "", deleteCommentSuccess, "", "");
	};
	submitDeleteComment (this.commentDeleteLink, deleteCommentSuccess)
}

function openModal(elementId, newContainer, elementContent, elementTitle, center)
{
	var modal = true; 
	var isNewContainer = newContainer;
	var fixedcenter = center;
	var containerId = elementId;
	
	if (isNewContainer) {
		if(document.getElementById(containerId)) {
			modalPanel.destroy();
		}
		modalPanel = document.createElement('div');
	} else {
		YAHOO.util.Dom.setStyle(document.getElementById(containerId), 'display', 'block');
	}
	modalPanel = new YAHOO.widget.Panel(elementId,
				{
					fixedcenter:true, 
					visible:true, 
					draggable:false, 
					close:true, 
					iframe:true, 
					modal: modal
				}
	);
	if (isNewContainer) {
		modalPanel.setHeader(elementTitle); 
		modalPanel.setBody(elementContent); 
	}
	if (modal) {
		modalPanel.hideEvent.subscribe(U.User.showTaskBar);
	}
	return modalPanel;
}

function submitThreadPost(theURL, theFormValues, threadElement, theForm, refresh) 
{
	var transaction;
	if (!refresh) {
		transaction = postXMLResponse (theURL, 5000, "", loadThreadPost, theFormValues, threadElement);
	}
	else {
		transaction = postXMLResponse (theURL, 5000, "", loadRefreshThreadPost, theFormValues, threadElement);
	}
	for (i=0; i<theForm.length; i++) {
		if (theForm[i].getAttribute('type').toLowerCase() == 'button') {
			buttonDisable = theForm[i];
			buttonDisable.disabled = false;
		}
	}
}

function loadRefreshThreadPost () {
	tinyMCE.activeEditor.setContent("");
	window.location.reload();
}

function loadThreadPost (response, target, threadElement) 
{
	var animTime = 0.500;
	var animOpacity = "1";
	var responseText;
	try {
		responseText = YAHOO.lang.JSON.parse(response);
		window.location = responseText.link;
	} catch (e) {
		
		
		/* Ajax Discussion removed for time being */
		 
		var threadElementClassName = threadElement;
		var threadElementObject = YAHOO.util.Dom.getElementsByClassName(threadElementClassName)[0];
		var responseElement = document.createElement('li');
		responseElement.innerHTML = response;
		responseElement = responseElement.firstChild;
		YAHOO.util.Dom.setStyle(responseElement, 'opacity', '0');
		if (threadElementObject.getElementsByTagName('li') != null) {
			threadElementObject.appendChild(responseElement);
			threadNaturalHeight = responseElement.offsetHeight;
			YAHOO.util.Dom.setStyle(responseElement, 'height', '0px');
			animThread = new YAHOO.util.Anim (responseElement, {
				opacity: { to: animOpacity}, 
				height: {  to: threadNaturalHeight}
			}, animTime);
			animThread.animate();
		}
		
	}
}