// COPYRIGHT DENNY WONG PUI CHUNG 2009

// This document requies the use of jsXML_PageValue.js. Would refuse to do anything unless the files are loaded.


if (typeof ajax_PageValue == "function")
{
// -------------------------------------------------------------------------------------------------------------------------
// START OF FUNCTION SET
// -------------------------------------------------------------------------------------------------------------------------
	var ajax_PageFrame = function (NodeList)
						{
							this.NodeList = NodeList;
							
							this.current_level = 0;
							
							this.contents = {};
							
							//Build a Node having these properties:
							//  .Node = xml_DOM object.
							//  .NodeValue = innerXML
							//  .child
							//  .empty
							//  .repeat
							//  .eval_contents
							this.MemberNode = function (Node)
												{
													this.Node = Node;
													
													this.NodeValue = xml_DOM_Node_get_innerXML(this.Node);
													
													for (var i in attlist = {0:"child", 1:"empty", 2:"repeat", 3:"eval_contents"})
													{
														this[attlist[i]] = xml_DOM_NodeList_get_attribute(this.Node, attlist[i]);
													}
												}
														
							
							//Generate populated PageFrame from the given PageValue. Can be run more than once with the same PageFrame but different set of PageValue.
							this.populate_frame = function (PageValue, tag_name)
							{
								//------------------------------------------------------------------------------------
								//THIS IS A LOOPING FUNCTION THAT CALLS ITSELF IN ITS OWN PROCEDURE.
								//PLEASE TAKE THIS INTO CONSIDERATION WHEN DOING MODIFICATIONS.
								//------------------------------------------------------------------------------------
								
								//Make sure that the node is there. Otherwise warns user. Designed only for debugging purpose.
								
								if (xml_DOM_NodeList_tag_exists(this.NodeList, tag_name))
								{
									var current_node = new this.MemberNode (xml_DOM_NodeList_get_tag(this.NodeList, tag_name)[0]);

									current_node.eval_contents = parseInt(current_node.eval_contents, 10);
									
									if (current_node.repeat == null)
									{
										current_node.repeat = 1;
									} else {
										current_node.repeat = eval(current_node.repeat);
									}
									if (current_node.eval_contents == null)
									{
										current_node.eval_contents = 0;
									}
									if (current_node.empty != null)
									{
										current_node.EmptyNode = new this.MemberNode (xml_DOM_NodeList_get_tag(this.NodeList, current_node.empty)[0]);
									}
									
									var current_contents = "";
									this.contents[this.current_level] = "";
									
									for (var i=1; i<=current_node.repeat; i++)
									{
										current_contents = !PageValue.end_of_members() ? current_node.NodeValue : current_node.EmptyNode.NodeValue;
										
										if (current_node.eval_contents == 1)
										{
											current_contents = current_contents.replace(/{\?id\?}/, PageValue.current_member());
											current_contents = PageValue.replaceAllByPageValue(current_contents, /{\?(.+?)\?}/);
										}
										
										if (current_node.child != null)
										{
											this.current_level++;
											
												//Call itself to resolve child.
												//If empty, will be dealt with in child resolution.
												this.populate_frame.call(this, PageValue, current_node.child);
												current_contents = current_contents.replace("{?"+current_node.child+"?}", this.contents[this.current_level]);
												delete(this.contents[this.current_level]);
											
											this.current_level--;

										} else {
											PageValue.next_member();
										}
										
										this.contents[this.current_level] += current_contents;
									}
									
									if (this.current_level == 0)
									{
										return this.contents[0];
									}
								} else {
									alert("FATAL ERROR: Node \""+tag_name+"\" does not exists in NodeList.");
								}
								
								//------------------------------------------------------------------------------------
							}
						}

// -------------------------------------------------------------------------------------------------------------------------
// END OF FUNCTION SET
// -------------------------------------------------------------------------------------------------------------------------
} else { alert("FATAL ERROR: AJAX_XML_CONNECT or XML_DOM_NodeList_tag_manager or String.replaceAllByArray not loaded."); }// Ends the IF condition for the inclusion of jsXML_PageValue.js
