function Ajax () {}

document.write ("<iframe id='ajax_channel' width=0 height=0 src=''></iframe>");

Ajax.http_request		= null;
Ajax.callbackEvent		= null;
Ajax.callbackFunction	= null;
Ajax.elements			= [];
Ajax.doc	= null;
Ajax.imageArray			= [];

Ajax.createHttpRequestObj = function ()
{
	var http_request = null;
	
	if (window.XMLHttpRequest)	// Mozilla, Safari,...
	{
		http_request = new XMLHttpRequest();
		
		if (http_request.overrideMimeType)
			http_request.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject)	//IE
	{
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2)
			{
				alert ("不能使用ajax!");
			}
		}
	}
	
	return http_request;
}

Ajax.init = function ()
{
	Ajax.http_request		= Ajax.createHttpRequestObj ();
	Ajax.callbackEvent		= null;
	Ajax.callbackFunction	= null;
	Ajax.elements			= [];
}
	
Ajax.sendGetRequest = function (url, asynchronize, callbackEvent, callbackFunction)
{
	Ajax.init ();
	
	if (Ajax.http_request == null)
		return;
	
	Ajax.callbackEvent		= callbackEvent;
	Ajax.callbackFunction	= callbackFunction;
	
	Ajax.http_request.onreadystatechange = Ajax.docallback;
	
	if (url.indexOf ("?") > 0)
		url = url + "&random=" + new Date().getTime();
	else
		url = url + "?random=" + new Date().getTime();
	
	Ajax.http_request.open ('GET', url, asynchronize);
	
	Ajax.http_request.send (null);
	
	return false;
}

Ajax.sendPostRequest2 = function (url, requireString, asynchronize, callbackEvent, callbackFunction)
{
	Ajax.init ();
	
	if (Ajax.http_request == null)
		return false;
	
	Ajax.callbackEvent		= callbackEvent;
	Ajax.callbackFunction	= callbackFunction;
	Ajax.http_request.onreadystatechange = Ajax.docallback;
	
	if (url.indexOf ("?") > 0)
		url = url + "&random=" + new Date().getTime();
	else
		url = url + "?random=" + new Date().getTime();
	
	Ajax.http_request.open ('POST', url, asynchronize);
	
	Ajax.http_request.setRequestHeader ('Content-Type','application/x-www-form-urlencoded');
	Ajax.http_request.setRequestHeader ("Content-length", requireString.length);
	Ajax.http_request.setRequestHeader ("Connection", "close");
	
	Ajax.http_request.send (requireString);
	
	return false;
}

Ajax.sendPostRequest = function (url, formElement, asynchronize, callbackEvent, callbackFunction)
{
	Ajax.init ();
	
	if (Ajax.http_request == null)
		return false;
	
	var requireString = Ajax.parseParameteres (formElement);
	
	Ajax.callbackEvent		= callbackEvent;
	Ajax.callbackFunction	= callbackFunction;
	Ajax.http_request.onreadystatechange = Ajax.docallback;
	
	if (url.indexOf ("?") > 0)
		url = url + "&random=" + new Date().getTime();
	else
		url = url + "?random=" + new Date().getTime();
	
	Ajax.http_request.open ('POST', url, asynchronize);
	
	Ajax.http_request.setRequestHeader ('Content-Type','application/x-www-form-urlencoded');
	Ajax.http_request.setRequestHeader ("encoding", "utf-8");
	Ajax.http_request.setRequestHeader ("Content-length", requireString.length);
	Ajax.http_request.setRequestHeader ("Connection", "close");
	
	Ajax.http_request.send (requireString);
	
	return false;
}

Ajax.sendGetRequestByChannel = function (url, asynchronize, callbackEvent, callbackFunction)
{
	Ajax.callbackEvent		= callbackEvent;
	Ajax.callbackFunction	= callbackFunction;
	
	if (url.indexOf ("?") > 0)
		url = url + "&random=" + new Date().getTime();
	else
		url = url + "?random=" + new Date().getTime();
	
	document.all["ajax_channel"].src = url;
	
	return false;
}

Ajax.docallback = function ()
{
	if (Ajax.http_request.readyState == 4)
	{
		if (Ajax.http_request.status == 200)
		{
			if (Ajax.callbackFunction != null)
			{
				eval (Ajax.callbackFunction);
			}
			else if (Ajax.callbackEvent != null)
				KEvent.dispatchEvent({type:Ajax.callbackEvent, callbackDataes:Ajax.http_request.responseText});
		}
		else
		{
			if (Ajax.http_request.status == "0")
				alert ("操作失败!\r\n登录后才能操作!");
			else
				alert ("操作失败!\r\n错误代码:" + Ajax.http_request.status);
		}
	}
}

Ajax.parseElements = function (elements, formNode)
{
	for (var i = 0; i < formNode.elements.length; i++)
	{
		var elm = formNode.elements[i];
		
		if(!elm || elm.tagName.toLowerCase() == "fieldset")
			continue;
		
		var t = elm.type.toLowerCase ();
			
		if (t == "hidden" || t == "text" || t == "textarea" || t == "password" || t == "checkbox" || t == "radio" || t == "select" || t == "select-one")
			elements.push (elm);
	}
}

Ajax.encodeString = function (str)
{
	if (str == null || str == "")
		return str;
	
	var ret = "";
	var value = escape(str);
	var match, re = /%u([0-9A-F]{4})/i;
	
	while((match = value.match(re)))
	{
		ret += value.substring(0, match.index) + escape("%u") + match[1];
		value = value.substring(match.index+match[0].length);
	}
	
	ret += value.replace(/\+/g, "%2B");
	
	return ret;
}

Ajax.encodeAcute = function(str)
{
	if (str == null || str == "")
		return str;
	
	var value = str;
	var ret = "";
	var match, re = /'/;
	
	while((match = value.match(re)))
	{
		ret += value.substring(0, match.index) + "&acute;";
		value = value.substring(match.index+match[0].length);
	}
	
	ret += value;
	
	return ret;
}

Ajax.uncodeAcute = function(str)
{
	if (str == null || str == "")
		return str;
	
	var value = str;
	var ret = "";
	var match, re = /&acute;/;
	
	while((match = value.match(re)))
	{
		ret += value.substring(0, match.index) + "'";
		value = value.substring(match.index+match[0].length);
	}
	
	ret += value;
	
	return ret;
}

Ajax.parseParameteres = function (formObj)
{
	Ajax.elements = [];
	Ajax.parseElements (Ajax.elements, formObj);
	
	var s = "";
	
	for (var i = 0; i < Ajax.elements.length; i++)
	{
		var obj = Ajax.elements[i];
		
		if (obj.type == "hidden")
		{
			s += obj.name + "=" + Ajax.encodeString(obj.value) + "&";
		}
		else if (obj.type == "text")
		{
			s += obj.name + "=" + Ajax.encodeString(obj.value) + "&";
		}
		else if (obj.type == "textarea")
		{
			s += obj.name + "=" + Ajax.encodeString(obj.value) + "&";
		}
		else if (obj.type == "password")
		{
			s += obj.name + "=" + Ajax.encodeString(obj.value) + "&";
		}
		else if (obj.type == "checkbox")
		{
			if (obj.checked)
			{
				s += obj.name + "=" + Ajax.encodeString(obj.value) + "&";
			}
		}
		else if (obj.type == "radio")
		{
			if (obj.checked)
			{
				s += obj.name + "=" + Ajax.encodeString(obj.value) + "&";
			}
		}
		else if (obj.type == "select")
		{
			s += obj.name + "=" + Ajax.encodeString(obj.options[obj.selectedIndex].value) + "&";
		}
		else if (obj.type == "select-one")
		{
			s += obj.name + "=" + Ajax.encodeString(obj.options[obj.selectedIndex].value) + "&";
		}
	}

	return s;
}

Ajax.parseXML = function (xmlContent)
{
	if (Ajax.doc == null)
		Ajax.doc = new ActiveXObject("Microsoft.XMLDOM");
	
	Ajax.doc.async = true;
	Ajax.doc.loadXML (xmlContent);
}

Ajax.getXMLSubNodes = function (domNode, subNodeName)
{
	if (domNode == null)
		return null;
	
	var subNodes = [];
	
	var currentNode = domNode;
	var childNodes = null;
	
	var paths = subNodeName.split ("/");
	
	for (var i = 0; paths != null && i < paths.length; i++)
	{
		if (!paths[i] || paths[i] == "")
			continue;
		
		childNodes = currentNode.childNodes;
		
		for (var j = 0; childNodes != null && j < childNodes.length; j++)
		{
			if (childNodes[j].nodeName.toLowerCase() == paths[i].toLowerCase())
			{
				if (i < paths.length - 1)
					currentNode = childNodes[j];
				
				break;
			}
		}
	}
	
	if (!currentNode)
		return null;
	
	childNodes = currentNode.childNodes;
	
	for (var i = 0; childNodes != null && i < childNodes.length; i++)
	{
		if (childNodes[i].nodeName.toLowerCase() == paths[paths.length-1].toLowerCase())
			subNodes.push (childNodes[i]);
	}
	
	return subNodes;
}

Ajax.getXMLRootNode = function ()
{
	if (Ajax.doc == null)
		return null;
	
	return Ajax.doc.documentElement;
}

Ajax.getXMLNodeValue = function (nodeName)
{
	if (Ajax.doc == null)
		return null;
	
	return Ajax.getXMLSingleNodeValue (Ajax.doc.documentElement, nodeName);
}

Ajax.getXMLSingleNodeValue = function (domNode, nodeName)
{
	var currentNode = domNode;
	
	var paths = nodeName.split ("/");
	
	for (var i = 0; paths != null && i < paths.length; i++)
	{
		if (paths[i] == "")
			continue;
		
		if (!currentNode)
			break;
		
		var childNodes = currentNode.childNodes;
	
		for (var j = 0; childNodes != null && j < childNodes.length; j++)
		{
			if (childNodes[j].nodeName.toLowerCase() == paths[i].toLowerCase())
			{
				currentNode = childNodes[j];
				break;
			}
		}
	}
	
	if (currentNode == null)
		return null;
	else if (currentNode.childNodes == null || currentNode.childNodes.length == 0)
		return null;
	else
		return currentNode.childNodes(0).nodeValue;
}

Ajax.cancelLoad = function ()
{
	for (var i = 0; i < Ajax.imageArray.length; i++)
	{
		Ajax.imageArray[i].src = null;
		Ajax.imageArray.pop ();
	}
	
	Ajax.imageArray = [];
}

Ajax.loadImage = function ()
{
	Ajax.cancelLoad ();
	
	var loadImageInfoDiv = document.all["load_image_info"];
	
	if (loadImageInfoDiv == null)
			return;
	
	var values = loadImageInfoDiv.innerHTML.split ("|");
	
	if (values.length <= 1)
		return;
	
	var photo_containerIDes = values[0].split (",");
	var photo_urls = values[1].split (",");
	
	var imageArray = Ajax.imageArray;
	
	for (var i = 0; photo_containerIDes != null && i < photo_containerIDes.length; i++)
	{
		if (photo_containerIDes[i] == null || photo_containerIDes[i] == "")
			continue;
		
		var photoDiv = document.all[photo_containerIDes[i]];
		
		if (photoDiv)
		{
			imageArray[i] 	= new Image ();
			imageArray[i].border = 0;
			imageArray[i].containerID  = photo_containerIDes[i];
			photoDiv.innerHTML = "";
			photoDiv.appendChild (imageArray[i]);
			
			imageArray[i].onload = function ()
					{
						var photoDiv = document.all[this.containerID];
						
						if (photoDiv != null)
						{
							if (photoDiv.tagName == "TD")
							{
								photoDiv.width = this.width;
								photoDiv.height= this.height;
							}
							else if (photoDiv.tagName == "DIV")
							{
								photoDiv.style.width = this.width;
								photoDiv.style.height= this.height;
							}
						}
					};
			imageArray[i].src = photo_urls[i];
		}
		/*
		imageArray[i] 	= new Image ();
		imageArray[i].containerID  = photo_containerIDes[i];
		
		
		imageArray[i].onload = function ()
					{
						var photoDiv = document.all[this.containerID];
						
						if (photoDiv != null)
						{
							if (photoDiv.tagName == "TD")
							{
								photoDiv.width = this.width;
								photoDiv.height= this.height;
							}
							else if (photoDiv.tagName == "DIV")
							{
								photoDiv.style.width = this.width;
								photoDiv.style.height= this.height;
							}
							
							photoDiv.innerHTML = "";
							photoDiv.appendChild (this);
						}
					};
		imageArray[i].src = photo_urls[i];
		*/
	}
}

function getCommentByPhotoID(img, commentDivID, photo_id)
{
	img.src='images/comment_hide.gif';
	img.onclick= function () {this.src = 'images/comment_view.gif';hideComment(this, commentDivID, photo_id);}
	return Ajax.sendGetRequest('photocomment_inner.jsp?qzphoto_seqno='+photo_id, true, null, 'showComment(\"'+commentDivID+'\")');
}

function showComment (commentDivID)
{
	document.all[commentDivID].innerHTML = Ajax.http_request.responseText;
}

function hideComment (img, commentDivID, photo_id,photo_ViewTimes,photo_CommentedTimes)
{
	img.src='images/comment_view.gif';
	img.onclick= function ()
	{
		getCommentByPhotoID(this, commentDivID, photo_id);
	}
	document.all[commentDivID].innerHTML = "<table cellspacing=0 cellpadding=0 border=0 width='100%'><td align='right'>"
										 + "<font style='color:#666666'>( " + photo_ViewTimes + "阅/ " + photo_CommentedTimes 
										 + "评<img src='images/comment_view.gif' border='0' "
										 + " onclick=\"return getCommentByPhotoID(this, 'photocomment_" + photo_id + "','" + photo_id + "')\"/>)</font></td></tr></table>";
}

function submitComment (commentDivID, commentForm, photo_id) 
{
	var submitform = document.all[commentForm];
	if (submitform.comment.value == "")
	{
		alert ("评论内容不能为空！");
		return false;
	}
	else
	{
		Ajax.sendPostRequest ('albumEditAction.jsp?action=commentPhoto&qzphoto_seqno='+photo_id, submitform, true, null, 'reloadComment(\"'+commentDivID+'\",\"'+photo_id+'\")');
		submitform.comment.value = "";
	}
}

function reloadComment (commentDivID,photo_id) 
{
	var return_value = Ajax.http_request.responseText;
	if(return_value != null && return_value != "" && return_value.Trim() == 'true')
		Ajax.sendGetRequest ('photocomment_inner.jsp?qzphoto_seqno='+photo_id, true, null, 'showComment(\"'+commentDivID+'\")');
	else
		alert("对不起，您的操作失败。");
}

function deletePhotoComment (commentDivID,comment_id,photo_id)
{
	if(confirm("是否确定删除？"))
		Ajax.sendGetRequest ('albumEditAction.jsp?action=deleteComment&type=qzcomment&ID='+comment_id, true, null, 'reloadComment(\"'+commentDivID+'\",\"'+photo_id+'\")')
		
	return false;	
}