// JavaScript Document

function changeCss(theClass,element,value) {
	//Last Updated on May 21, 2008
	//documentation for this script at
	//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	 var added = false;
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
		if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
		 if(document.styleSheets[S][cssRules][R].style[element]){
		 document.styleSheets[S][cssRules][R].style[element] = value;
		 added=true;
		break;
		 }
		}
	}

  if(!added){
  if(document.styleSheets[S].insertRule){
	  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
	} else if (document.styleSheets[S].addRule) {
		document.styleSheets[S].addRule(theClass,element+': '+value+';');
	}
  }
 }
}


function getItemLinks (item_type) {
	var linkXml;
	var url = '/prayer/linksearch_genxml.php?item_type=' + item_type;
	if (window.ActiveXObject) { // for IE
		linkXml = new ActiveXObject("Microsoft.XMLDOM");
		linkXml.async = false;
		linkXml.load(url);
		processLinkXml(linkXml);
		/*
		linkXml.onreadystatechange = function () {
				if (linkXml.readyState == 4) processLinkXml(linkXml)
			}*/
	} else if (document.implementation && document.implementation.createDocument)	{ // for Mozilla
		linkXml = document.implementation.createDocument("", "", null);
		//url = '../prayer/linksearch_genxml.php?item_type=' + item_type;
		url = '../xml/linksearch.xml';
		linkXml.onload = function() { processLinkXml(linkXml); };
	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
	//linkXml.load(url);
}
	
function linkItem (refitem_id, refitem_type, link_type, linked_heading, linked_text) {
	this.refitem_id = refitem_id;
	this.refitem_type = refitem_type;
	this.link_type = link_type;
	this.linked_heading = linked_heading;
	this.linked_text = linked_text;
}

function getLinkIndxs (item_id) {
	var links = [], j=0;
	for (var i = 0; i < link_list.length; i++) {
		if (link_list[i].refitem_id == item_id) {links[j++] = i;}
	}
	return links;
}

function dispLinkPopup (indx) {
	if (link_list[indx].link_type == "item") {
		var link_type = 'Related ' + link_list[indx].refitem_type;
		var myText = '<div class="ovfl"><span style="color:#990000">' + link_list[indx].linked_heading + '</span><br>' + link_list[indx].linked_text + '</div>';
		return overlib(myText, WRAP, TEXTPADDING,2, BORDER,2, STICKY, CLOSECLICK, CAPTIONPADDING,4, CAPTION,link_type, MIDX,0, RELY,250, DRAGGABLE, DRAGCAP, STATUS,'none');
	} else {
		if (link_list[indx].linked_text.search(/http:/) != -1) {
			var win = window.open(link_list[indx].linked_text, 'extWin');
		} else {
			location.href = link_list[indx].linked_text;
		}
	}
}

function ClosePopup(eleName) {
	document.getElementById(eleName).className='hiddenState';
}

function formatPrayer (state, country, date, name, text, item_type) {
	var tab = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	var disp_date;
	if (date.length > 0) {
		var dateArray = date.split('-'); // expects date format yyyy-mm-dd (mysql default)
		disp_date = dateArray[1] + '/' + dateArray[2];
	} else {
		disp_date = '';
	}
	var from_str = '';
	if (country.length > 0) {
	country += ', ';
	} else {
		if (state.length > 0 && country.length == 0) {
			state += ', ';
		}
	}
	if (name.length > 0) {
		from_str = 'From: ' + name + tab;
	}
	var salutation = (item_type == 1) ? '-- Lord, hear our prayer.' :  '-- Praise the Lord.';
	var html = state + ' ' + country + disp_date + ' - ' + text + '<br> ' + tab + from_str + salutation;
	return html;
}

function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
}
