//
// Javascript Objects
//

// DO NOT NOT NOT NOT NOT add random methods here!!! Unless they directly relate to the way that THESE objects are being used!

// This is the permanent store of information that the UCMS system will keep for its use. (State)
function pStore(args, assoc) {
	for(var i = 0; i < args.length; i++)
	{
		this[assoc[i]] = args[i];
	}}

pStore.prototype = {
  uid : 0,
  ps : pageStore,
  is : [],
  ss : sessionStore,
  ns : [],
  ajax : [],
  titleHandler : []
};

// Storage of information about the page
function pageStore(args, assoc) {
	for(var i = 0; i < args.length; i++)
	{
		this[assoc[i]] = args[i];
	}
}

pageStore.prototype = {
	pid : 0,
	ptitle : "",
	attrs : "",
	piids : [],
	prights : 0
};


// Commented out for the moment because we're not using it & it's causing errors
// Add additional piids (page/item combinations) to the page, i.e., after login.
pageStore.prototype.addPiids = function(itemArray){
	//	log(itemArray.length);
//		for(var i = 0; i < itemArray.length; i++)
//		{
//			this.piids.push(itemArray[i]);
	//		log(this.piids.toString());
//		}
	}

// Storage of information about the item
function itemStore(args, assoc) {
	for(var i = 0; i < args.length; i++)
	{
		this[assoc[i]] = args[i];
	}
}

itemStore.prototype = {
	iid : 0,
	piid : 0,
	col : "",
	position : 0,
	ititle : "",
	blurb : "",
	funct : "",
	irights : 0
};

// Storage of information about the session
function sessionStore(args, assoc) {
	for(var i = 0; i < args.length; i++)
	{
		this[assoc[i]] = args[i];
	}
}

sessionStore.prototype = {
	uid : 0,
	sessionKey : 0,
	appKey : 0
};

// Storage of information about the navigation
function navStore(args, assoc) {
	for(var i = 0; i < args.length; i++)
	{
		this[assoc[i]] = args[i];
	}
}

navStore.prototype = {
	navid : 0,
	zoneid : 0,
	line : 0,
	pos : 0,
	disp : 0,
	text : "",
	link : "",
	pid : 0,
	nrights : 0
};
	
// Storage of strings
function stringStore(args, assoc) {
	for(var i = 0; i < args.length; i++)
	{
		this[assoc[i]] = args[i];
	}
}

stringStore.prototype = {
	sitestrs : []
}

// need to re-do this
// Input Box creation

function msgBox(title, id, toshow)
{
  return "<div class='toplevel' id='msgtop'>" + title + "<a class='closex' href='javascript:destroy(\"" + id + "\",\"" + toshow + "\")'>X</a></div>";
}

function endMsgBox(id, text, toshow)
{
	if(text != '')
	{
		return "<input id='cancelbutton' type='button' class='closebutton cancelbutton' onclick='destroy(\"" + id + "\",\"" + toshow + "\")' value='" + text + "'></div>";
	}
	else
	{
		return "</div>";
	}
}

function submitMsgBox(id, arg1, arg2, text, toshow)
{
	if(text != '' && text != 'OK')
	{
		return "<input id='closebutton' class='closebutton' type='submit' onclick='submit" + id + "(\"" + arg1 + "\",\"" + arg2 + "\")' value='" + text + "'>";
	}
	else if(text == 'OK')
	{
		return "<input id='closebutton' type='button' class='closebutton' onclick='destroy(\"box" + id + arg1 + "\",\"" + toshow + "\")' value='" + text + "'></div>";
	}
	else
	{
		return "";
	}
}

function makeInputBox(response)
{
  var d = document.createElement("div");
	document.body.appendChild(d);
	d.id = 'box' + response[0] + response[2];
	d.className = 'popupmessage';
	d.innerHTML = msgBox(response[5],'box' + response[0] + response[2], response[8]);
  var newbodyitemdiv = document.createElement('div');    
  newbodyitemdiv.setAttribute('class','body c');
  newbodyitemdiv.setAttribute('className','body c');
  newbodyitemdiv.setAttribute('style','background: white;');
  fields = response[4].split("||");
  fieldHTML = "";
  if(response.length > 11 && response[11] != '')
  {
  	fieldHTML = "<div class='msgboxerror'>" + response[11] + "</div><br>";
  }
  fieldHTML += response[10];
  if(fields.length > 1)
  {
   	fieldHTML += "<br><br>";
		for(var i = 0; i < fields.length; i++)
		{
			j = i + 1;
			fieldplus = fields[j].split("^^");
			fields[j] = fieldplus[0];
			if(fieldplus.length > 1)
			{
				var prefill = fieldplus[1];
			}
			else
			{
			  var prefill = "";
			}
			if(fields[i].substr(0,1) == "+")
			{
				fieldHTML += fields[j] + "<br><textarea rows=5 cols=60 id='" + fields[i] + response[2] + "' name='" + fields[i] + response[2] + "' >" + prefill + "</textarea><br><br>";
			}
			else if(fields[i].substr(0,1) == ".")
			{
				fieldHTML += fields[j] + "<br><br>";
			}
			else
			{
				fieldHTML += fields[j] + " <input id='" + fields[i] + response[2] + "' name='" + fields[i] + response[2] + "' value='" + prefill + "'/><br><br>";
			}
			i++;
		}
		//document.getElementById(fields[1] + response[2]).focus();
	}
	else
	{
		// make the box thinner, more dialog-like. (to-do)
	}
  newbodyitemdiv.innerHTML = fieldHTML;
  d.appendChild(newbodyitemdiv);
  d.innerHTML += submitMsgBox(response[0], response[2], response[9], response[7], response[8]);
  d.innerHTML += endMsgBox('box' + response[0] + response[2], response[6], response[8]);

}

function makeSimpleMsgBox (title, message)
{
	response = "000000<>SPLIT<>messagecontainer<>SPLIT<>0<>SPLIT<>0<>SPLIT<><>SPLIT<>" + title + "<>SPLIT<>OK<>SPLIT<><>SPLIT<><>SPLIT<>0<>SPLIT<>" + message;
	response = response.split('<>SPLIT<>');
	makeInputBox(response);
}



function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		log (strCssRule);
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

