/* visibility */
function hideId(idToHide) {
	document.getElementById(idToHide).style.display='none';
}
function showId(idToShow) {
	document.getElementById(idToShow).style.display='block';
}

function getCookie(name) {
	var start = document.cookie.indexOf(name +"=");
	if(start == -1 )
		return null;
	var end = document.cookie.indexOf( ";", start);
	if(end == -1)
		end = document.cookie.length;

	var cookieStr = document.cookie.substring(start,end);
//if(true) return '<br><b>start=</b>'+ start +' <b>end=</b>'+ end +' <b>ALL COOKIES:</b> '+ document.cookie +'<br>'
//	+'<b>COOKIE '+ name +':</b> '+ cookieStr +'<br>';

	if(name+'=' != cookieStr.substring(0,name.length+1))
		return null;

	return unescape( cookieStr.substring(name.length+1) );
}
function setCookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	//if the expires variable is set, make the correct
	//expires time, the current script below will set
	//it for x number of days, to make it for hours,
	//delete * 24, for minutes, delete * 60 * 24
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
function deleteCookie(name, path, domain) {
	if ( Get_Cookie( name ) )
		document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// Convert an xml string into html format so it can be printed.
function xmlStrToHtml(xmlStr) {
	if(xmlStr==null)
		return null;
	var xr = xmlStr.replace(/<[/]/g, "ZZZELEMEND");
	xr = xr.replace(/</g, "<div style='padding-left:10px; border:1px solid #eee'>&lt;");
	xr = xr.replace(/ZZZELEMEND([^>]+)>/g, "&lt;/$1></div>");
	return xr;
}
function xmlToStr(elem) {
	if(elem==null)
		return '';
//	elem.normalize();
	return xmlToStr0(elem);
}
function xmlToStr0(elem) {
	if(elem==null)
		return '';
	var str = '';
	if(elem.nodeName == '#text') {
		str += elem.nodeValue;
	} else {
		str += '<'+ elem.nodeName;

		var attrs = elem.attributes;
		if(attrs!=null) {
//			str += ' xmlns='+ attrs.class();
//			str += ' xmlns='+ elem.getAttribute('xmlns');
			for(var attrIx=0; attrIx<attrs.length; ++attrIx) {
				var attr = attrs[attrIx];
				str += ' '+ attr.name +'="'+ attr.value.replace(/"/g, "&quot;") +'"';
			}
		}
		str += '>';
		var childNodes = elem.childNodes;
		if(childNodes!=null) {
			for(var childIx=0; childIx<childNodes.length; ++childIx) {
				str += xmlToStr0(childNodes[childIx]);
			}
		}
		str += '</'+ elem.nodeName +'>';
	}

	return str;
}


// Takes DOM compliant element object and returns the text value of that subelement.
function domElemVal(elem,subElemName) {
	if(elem==null)
		return null;
	var elems = elem.getElementsByTagName(subElemName);
	if(elems==null || elems.length==0)
		return null;
	return elems[0].firstChild.nodeValue;
}

// Pretty print a DOM client xml element in HTML format. This returns a string with html in it.
//function xmlPrettyPrintToHtml(elem) {
//	return elem
//}


// Outputs log info to the specified div (it could be a span as well). If the div doesn't exist then
// It will create one and add it to the beginning of the body of the page.
function logTo(divId,txt) {
	var logDiv = document.getElementById(divId);
	if(logDiv==null) {
		return;
		
//		logDiv = document.createElement("<div id='"+ divId +"'></div>");
//		document.body.insertBefore(logDiv);
	}
//	logDiv.innerHTML += txt;
	// log to beginning of div, not the end of it.
	logDiv.innerHTML = txt + logDiv.innerHTML;
}
// Does the same as logTo except it outputs a <br> at the end.
function logLineTo(divId,txt) {
	logTo(divId,txt +'<br>');
}


/* Rotation. This is currently IE specific. */
function rotateNone(idToRotate) {
/*	document.getElementById(idToRotate).style.filter='';*/
}
function rotateRt90(idToRotate) {
/*	document.getElementById(idToRotate).style.filter='progid:DXImageTransform.Microsoft.BasicImage(rotation=1)';*/
}
function rotate180(idToRotate) {
/*	document.getElementById(idToRotate).style.filter='progid:DXImageTransform.Microsoft.BasicImage(rotation=2)';*/
}
function rotateLt90(idToRotate) {
/*	document.getElementById(idToRotate).style.filter='progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';*/
}



// Used with the ws.js SOAP client
function getSubElems(elem,name,nsuri) {
//logLineTo('log','elem '+ elem);
	if(elem==null)
		return null;
	var subElems;
	if(elem.get_children)
		subElems = elem.get_children( new WS.QName(name,nsuri) );
	else
		subElems = elem.getElementsByTagNameNS(nsuri,name);
//logLineTo('log','subElems RESULT: '+ subElems);
	return subElems;
}
function getSubElem(elem,name,nsuri) {
	var subElems = getSubElems(elem,name,nsuri);
	if(subElems==null || subElems.length==0)
		return null;
	return subElems[0];
}
function getSubElemVal(elem,name,nsuri) {
	var subElem = getSubElem(elem,name,nsuri);
	if(subElem==null)
		return null;
	return subElem.get_value();
}
// SOAP param value. This formats the parameter values as necessary to make it work properly.
function spv(theName,theValue) {
	// For some reason the SOAP params don't like having an integer value of 0. It has to be '0'.
	if(theValue==0)
		theValue = '0';
	return {name:theName,value:theValue};
}


/** Increases the specified text area by the specified number of rows or columns.
Negative values for height or width will reduce the size.
 **/
function resizeTextArea(textAreaId, height, width) {
	var textArea = document.getElementById(textAreaId);
	if(textArea && height!=0 && textArea.rows + height > 1) {
		textArea.rows += height;
	}
	if(textArea && width!=0 && textArea.cols + width > 1) {
		textArea.cols += width;
	}
}


// If a stick exists for this page then display it.
// The box is the container that will be made visible if there is a stick. The outputDiv is the place where the image is actually displayed.
function displayStick(idOfOutputBox,idOfOutputDiv) {
	var userCookie = getCookie('user');
	if(userCookie==null)
		userCookie = '';
//logLineTo('log','displayStick idOfOutputDiv='+ idOfOutputDiv);

	var svcName = 'getStick';
	var call = new WS.Call('/know/services/Stick');

	var nsuri = 'http://service.scifidb.org';
	var qn_op = new WS.QName(svcName,nsuri);
	var qn_op_resp = new WS.QName(svcName +'Response',nsuri);
	call.invoke_rpc(
	    qn_op,
	    new Array(
	      spv('userCookie',userCookie),
	      spv('pageUrl', window.location )
	    ),null,
	    function(call,envelope,xmlStr) {
	    	var responseElem = getSubElem(envelope.get_body(),svcName +'Response',nsuri);
//logLineTo('log','RESULTXML: '+ xmlStrToHtml(xmlStr));
			var stickObject = getSubElem(responseElem,'StickObject',nsuri);
			if(stickObject==null) {
				// don't display anything
			} else {
				var stickUrl = getSubElemVal(stickObject,'url',"http://stick.knowlist.com");
				
				//TODO: Create an image in the idOfStickImage div which points at stickUrl.
				var theDivBox = document.getElementById(idOfOutputBox);
				var theDiv = document.getElementById(idOfOutputDiv);
				if(theDivBox==null || theDiv==null)
					return;
//logLineTo('log','getStick 2');
//				theDivBox.style.display = 'table';
//logLineTo('log','getStick 3');
				theDiv.innerHTML = '<a href=""><img src="'+ stickUrl +'"></a> <a href="#qmark" class="hoverPopup qmarkInstructions">?<span>Collect all the stick friends on this site to reveal a super stick friend! Juck click the stick friend image to add it to your collection.</span></a>';
//logLineTo('log','getStick 4');
			}
//logLineTo('log','getStick RESULT: '+ stickUrl);
//logLineTo('log','getStick RESULTXML: '+ xmlStrToHtml(xmlStr));
	    }
	);
}


