/**
 * Library containing functions required for the blogging area of the site.
 */


/*
 * Check the required fields have been filled in, and if so , submit the document.
 *
 * Based on the passed var, either preview or post.
 *
 */
function doBlogAddEntry_submit(bCreate) {

	fm = document.forms['addentry'];

	// see if this is a request to create or to preview
	if (bCreate) {
		fm['posttype'].value="";
	} else {
		fm['posttype'].value="PREVIEW";
	}

	msg="";
	focusme="";
	if (fm['title'].value=='' || fm['title'].value.replace(/^\s+|\s+$/g, '')=="") {
			msg+="\nA title for the entry must be entered.";
			fm['title'].value="";
			focusme="title";

	}

	oFckBody = FCKeditorAPI.GetInstance('body');
	if (oFckBody.GetHTML()=='' || oFckBody.GetHTML().replace(/^\s+|\s+$/g, '')=="") {
			msg+="\nYou must enter some text for your blog entry.";
			// set text to blank
			oFckBody.GetHTML()="";
			if (focusme=='') focusme="body";
	}

	// see if private and if so, that a list of private members has been passed
	if (fm['access'][2].checked==true) {
		bOK= false;
		for (i=0; i<fm['privatelistid[]'].length; i++){

			if (fm['privatelistid[]'][i].checked == true) {
				bOK=true;
			}
		}
		if (!bOK) {
			msg+="\nYou have selected to mark the blog as Private but have not chosen users to restrict to.";
		}
	}
	if (msg=="") {
		fm.submit();

	} else {

		alert("Cannot submit the blog entry for the following reasons:\n" + msg);
		fm[focusme].focus();
	}

}

function doBlogAddEntry_cancel(albumid) {

	if (confirm('Any Details entered will be lost.\n\nContinue?')) {

		if (albumid=="") {
			window.location.href="blog.php";
		} else {

			sUrl = "viewphotos.php?albumserial="  + albumid;
			window.location.href=sUrl;
		}
	}


}

/**
 * Handle adding/removing tags from the list of tags.
 *
 */
function doBlogAdd_toggleTag(sID) {

	fm = document.forms['addentry'];

	sTags = fm['tags'].value;

	if (sTags=="") {
		// simply add
		sNewTags = sID;
	} else {

		// need to split out the tags and check if this value is in there
		aTags = sTags.split(',');

		bFound = false;
		for(i=0; i<aTags.length; i++) {
			if (sID == aTags[i]) {
				// match found, so remove
				aTags.splice(i, 1);
				bFound = true;
			}
		}

		//G.F edited 01/08/07 to use byName so can be used on pages where tag is present multiple times
		objTag = document.getElementsByName('tag_' + sID);

		if (bFound) {
			// need to remove highlight from tag
			for(j=0; j<objTag.length; j++) {
				objTag[j].className = "tag_normal";
			}
		} else {

			// only add if there is enough space
			if (( sTags.length + sID.length)>100) {
				alert("maximum of 100 characters for tags.");
			} else {
				// add this item to the array
				aTags.push(sID);
				for(j=0; j<objTag.length; j++) {
					objTag[j].className = "tag_highlight";
				}
			}
		}

		// now return this array to the selection
		sNewTags = aTags.join(',');

	}
	fm['tags'].value = sNewTags;
	doBlog_updateTagHighlights();

}

function doBlog_removeSpacing(objField) {

	if (objField.value!="") {
		// MODIFICATION 19 SEP 2007 AP
		// replace any commas with a space
		//sValues = objField.value.split(',').join(' ');
		aValues = objField.value.split(',');
		aNew = new Array();

		for(i=0; i<aValues.length;i++) {
			sValue=aValues[i].replace(/^\s+|\s+$/g, '');

			if (sValue!="") {
				aNew.push(sValue);
			}
		}

		if (aNew!="") {
			objField.value= aNew.join(',');
		}
	}

}
function doBlog_updateTagHighlights() {
	fm = document.forms['addentry'];
	// clear highlight on tags loaded
	sAllTags = fm['alltags'].value;
	if (sAllTags!="") {
		aAllTags = sAllTags.split(',');
		for (i=0; i<aAllTags.length; i++) {
			//G.F edited 01/08/07 to use byName so can be used on pages where tag is present multiple times
			objTag = document.getElementsByName('tag_' + aAllTags[i].replace(/^\s+|\s+$/, ''));
			// need to loop node array for byName instead of straight "if"
			for(j=0; j<objTag.length; j++) {
				objTag[j].className = "tag_normal";
			}
		}
	}

	sSelectedTags = fm['tags'].value;

	// to get here have a string containing words, now loop through these and highlight any tags
	// MODIFICATION 19 SEP 2007 AP
	// replace any commas with a space
	//sSelectedTags = sSelectedTags.split(',').join(' ');

	aNewTags = sSelectedTags.split(',');

	for(i=0; i<aNewTags.length; i++) {
		//G.F edited 01/08/07 to use byName so can be used on pages where tag is present multiple times
		objTag = document.getElementsByName('tag_' + aNewTags[i].replace(/^\s+|\s+$/, ''));
		// need to loop node array for byName instead of straight "if"
		for(j=0; j<objTag.length; j++) {
			objTag[j].className="tag_highlight";
		}
	}

}

function doBlog_filterByDate(sValue) {

	document.forms['blogload']['mmyy'].value= sValue;
	document.forms['blogload'].submit();

}

function doBlogSubscriptions_remove() {

	if (confirm("This will remove the selected blogs from your subscriptions.\n\nContinue?")) {

		document.forms['managesubscriptions']['action'].value="removeall";
		document.forms['managesubscriptions'].submit();
	}


}

function doBlogSubscriptions_add() {

	// loop through and find what is selected
	document.forms['managesubscriptions']['action'].value="addall";
	document.forms['managesubscriptions'].submit();

}

function doBlogSubscriptions_loadCategory() {

	fm = document.forms['managesubscriptions'];

	category = fm['choosecategory'][fm['choosecategory'].selectedIndex].text;

	fm['action'].value="loadcategory";
	fm['category'].value=category;
	fm.submit();


}
