/* 
File: blog_perm.asp
Version: 0.1
Module: blog_xml
 
Coded by: Ervin Kosch and Clinton LaForest
Date of Coding: 

Development Notes:

VERSION 0.1
-----------
* Added Generic Development Comments

VERSION 0.2
-----------
* Created and Tested update_element

*/

//CHARACTER REPLACE

function WebString(instring){

outstring = instring;

outstring.replace("&", "&amp;");
outstring.replace("<", "&lt;");
outstring.replace(">", "&gt;");
outstring.replace("'", "&apos;");
outstring.replace(String.fromCharCode(11), " ");
outstring.replace(String.fromCharCode(127), "");
outstring.replace(String.fromCharCode(128), "");
outstring.replace(String.fromCharCode(129), "");
outstring.replace(String.fromCharCode(130), "&apos;");
outstring.replace(String.fromCharCode(131), "");
outstring.replace(String.fromCharCode(132), "");
outstring.replace(String.fromCharCode(133), "...");
outstring.replace(String.fromCharCode(134), "");
outstring.replace(String.fromCharCode(135), "");
outstring.replace(String.fromCharCode(136), "");
outstring.replace(String.fromCharCode(137), "");
outstring.replace(String.fromCharCode(138), "");
outstring.replace(String.fromCharCode(139), "");
outstring.replace(String.fromCharCode(140), "");
outstring.replace(String.fromCharCode(141), "");
outstring.replace(String.fromCharCode(142), "");
outstring.replace(String.fromCharCode(143), "");
outstring.replace(String.fromCharCode(144), "");
outstring.replace(String.fromCharCode(145), "&apos;");
outstring.replace(String.fromCharCode(146), "&apos;");
outstring.replace(String.fromCharCode(147), "\"");
outstring.replace(String.fromCharCode(148), "\"");
outstring.replace(String.fromCharCode(149), "");
outstring.replace(String.fromCharCode(150), "");
outstring.replace(String.fromCharCode(151), "");
outstring.replace(String.fromCharCode(152), "");
outstring.replace(String.fromCharCode(153), "...");
outstring.replace(String.fromCharCode(154), "");
outstring.replace(String.fromCharCode(155), "");
outstring.replace(String.fromCharCode(156), "");
outstring.replace(String.fromCharCode(157), "");
outstring.replace(String.fromCharCode(158), "");
outstring.replace(String.fromCharCode(159), "");
outstring.replace(String.fromCharCode(160), "");
outstring.replace(String.fromCharCode(161), "");
outstring.replace(String.fromCharCode(162), "");
outstring.replace(String.fromCharCode(163), "");
outstring.replace(String.fromCharCode(164), "");
outstring.replace(String.fromCharCode(165), "");
outstring.replace(String.fromCharCode(166), "");
outstring.replace(String.fromCharCode(167), "");
outstring.replace(String.fromCharCode(168), "");
outstring.replace(String.fromCharCode(169), "...");
outstring.replace(String.fromCharCode(170), "");
outstring.replace(String.fromCharCode(171), "");
outstring.replace(String.fromCharCode(172), "");
outstring.replace(String.fromCharCode(173), "");
outstring.replace(String.fromCharCode(174), "...");
outstring.replace(String.fromCharCode(175), "");
outstring.replace(String.fromCharCode(176), "");
outstring.replace(String.fromCharCode(177), "");
outstring.replace(String.fromCharCode(178), "");
outstring.replace(String.fromCharCode(179), "");
outstring.replace(String.fromCharCode(180), "");
outstring.replace(String.fromCharCode(181), "");
outstring.replace(String.fromCharCode(182), "");
outstring.replace(String.fromCharCode(183), "");
outstring.replace(String.fromCharCode(184), ",");
outstring.replace(String.fromCharCode(185), "&apos;");
outstring.replace(String.fromCharCode(186), "");
outstring.replace(String.fromCharCode(187), "");
outstring.replace(String.fromCharCode(188), "");
outstring.replace(String.fromCharCode(189), "");
outstring.replace(String.fromCharCode(190), "");
outstring.replace(String.fromCharCode(191), "");
outstring.replace(String.fromCharCode(192), "A");
outstring.replace(String.fromCharCode(193), "A");
outstring.replace(String.fromCharCode(194), "A");
outstring.replace(String.fromCharCode(195), "A");
outstring.replace(String.fromCharCode(196), "A");
outstring.replace(String.fromCharCode(197), "A");
outstring.replace(String.fromCharCode(198), "A");
outstring.replace(String.fromCharCode(199), "C");
outstring.replace(String.fromCharCode(200), "E");
outstring.replace(String.fromCharCode(201), "E");
outstring.replace(String.fromCharCode(202), "E");
outstring.replace(String.fromCharCode(203), "E");
outstring.replace(String.fromCharCode(204), "I");
outstring.replace(String.fromCharCode(205), "I");
outstring.replace(String.fromCharCode(206), "I");
outstring.replace(String.fromCharCode(207), "I");
outstring.replace(String.fromCharCode(208), "D");
outstring.replace(String.fromCharCode(209), "N");
outstring.replace(String.fromCharCode(210), "O");
outstring.replace(String.fromCharCode(211), "O");
outstring.replace(String.fromCharCode(212), "O");
outstring.replace(String.fromCharCode(213), "O");
outstring.replace(String.fromCharCode(214), "O");
outstring.replace(String.fromCharCode(215), "X");
outstring.replace(String.fromCharCode(216), "O");
outstring.replace(String.fromCharCode(217), "U");
outstring.replace(String.fromCharCode(218), "U");
outstring.replace(String.fromCharCode(219), "U");
outstring.replace(String.fromCharCode(220), "U");
outstring.replace(String.fromCharCode(221), "Y");
outstring.replace(String.fromCharCode(222), "");
outstring.replace(String.fromCharCode(223), "");
outstring.replace(String.fromCharCode(224), "a");
outstring.replace(String.fromCharCode(225), "a");
outstring.replace(String.fromCharCode(226), "a");
outstring.replace(String.fromCharCode(227), "a");
outstring.replace(String.fromCharCode(228), "a");
outstring.replace(String.fromCharCode(229), "a");
outstring.replace(String.fromCharCode(230), "a");
outstring.replace(String.fromCharCode(231), "c");
outstring.replace(String.fromCharCode(232), "e");
outstring.replace(String.fromCharCode(233), "e");
outstring.replace(String.fromCharCode(234), "e");
outstring.replace(String.fromCharCode(235), "e");
outstring.replace(String.fromCharCode(236), "i");
outstring.replace(String.fromCharCode(237), "i");
outstring.replace(String.fromCharCode(238), "i");
outstring.replace(String.fromCharCode(239), "i");
outstring.replace(String.fromCharCode(240), "o");
outstring.replace(String.fromCharCode(241), "n");
outstring.replace(String.fromCharCode(242), "o");
outstring.replace(String.fromCharCode(243), "o");
outstring.replace(String.fromCharCode(244), "o");
outstring.replace(String.fromCharCode(245), "o");
outstring.replace(String.fromCharCode(246), "o");
outstring.replace(String.fromCharCode(247), "");
outstring.replace(String.fromCharCode(248), "o");
outstring.replace(String.fromCharCode(249), "u");
outstring.replace(String.fromCharCode(250), "u");
outstring.replace(String.fromCharCode(251), "u");
outstring.replace(String.fromCharCode(252), "u");
outstring.replace(String.fromCharCode(253), "y");
outstring.replace(String.fromCharCode(254), "");
outstring.replace(String.fromCharCode(255), "u");

return outstring;

}


// UPDATE ELEMENT FUNCTION
function update_element(elementid, process, variablelist)
{

	//if(process != ''){ alert(process);}
	
	//alert(elementid);
	
	var redirect_found = false;
	document.getElementById('loading').style.display = "block";
	if(process == "update")
	{
		//prompt("**", "/modules/blog_xml/blog_process.asp?process="+process+"&"+variablelist+"");
		ajaxpage("/modules/blog_xml/blog_process.asp?process="+process+"&"+variablelist+"", elementid);
		redirect_found = true;
	}
	
	if(redirect_found == false)
	{
		//alert("Not process found!");
		ajaxpage("/modules/blog_xml/blog_process.asp?process="+process+"&"+variablelist+"", elementid);
	}
	document.getElementById('loading').style.display = "none";
}


// UPDATE ELEMENT BY POST FUNCTION
function update_element_post(elementid, process, variablelist)
{

	//if(process != ''){ alert(process);}
	
	//alert(elementid);
	
	var redirect_found = false;
	document.getElementById('loading').style.display = "block";
	if(process == "update")
	{
		//prompt("**", "/modules/blog_xml/blog_process.asp?process="+process+"&"+variablelist+"");
		ajaxpagepost("/modules/blog_xml/blog_process.asp?process="+process, "process="+process+"&"+variablelist+"", elementid)
		//ajaxpage("/modules/blog_xml/blog_process.asp?process="+process+"&"+variablelist+"", elementid);
		redirect_found = true;
	}
	
	if(redirect_found == false)
	{
		//alert("Not process found!");
		//ajaxpage("/modules/blog_xml/blog_process.asp?process="+process+"&"+variablelist+"", elementid);
		ajaxpagepost("/modules/blog_xml/blog_process.asp?process="+process, "process="+process+"&"+variablelist+"", elementid);
	}
	document.getElementById('loading').style.display = "none";
}


// VERIFY BLOG/COMMENT FUNCTION
function verify_blog(type, formid, action)
{
	
	
	if(type == "entry"){
	// ADD/UPDATE/DELETE BLOG ENTRY
		
		var user_id = 		formid.user_id.value;
	
		if(action == "add" || action == "update"){
		
			goodVar = 1;
			if(formid.uid){
				var uid = 			formid.uid.value;
			}
			var title = 		formid.title.value;
			var body = 			formid.body.value;
			//var allowed = 		formid.allowed.value;
			//var uid = 			formid.uid.value;
			
			var folder = 		formid.folder.value;
			var vlink = 		formid.vlink.value;
			
			var PostMonth = 	formid.PostMonth.value;
			var PostDay = 		formid.PostDay.value;
			var PostYear = 		formid.PostYear.value;
			
			var EndMonth = 		formid.EndMonth.value;
			var EndDay = 		formid.EndDay.value;
			var EndYear = 		formid.EndYear.value;
			
			/*var CutOffDay = 	formid.CutOffDay.value;
			var CutOffMonth = 	formid.CutOffMonth.value;
			var CutOffYear = 	formid.CutOffYear.value;*/
			
			
		
			
			
	
			//Textbox
			if (formid.title.value == "" && goodVar == 1) {	
    			alert( "Please enter the title." );
   				formid.title.focus();
    			goodVar = 0;
  			}
			
			
			//Start Date missing a day, month, or year
			if ((formid.PostMonth.value == "" || formid.PostDay.value == "" || formid.PostYear.value == "" ) && goodVar == 1) {	
    			alert( "Please enter a valid start date." );
   				formid.title.focus();
    			goodVar = 0;
  			}
			
			// Start date - Is in the past?
			if( goodVar == 1 ){
				var mathStartDate = new Date;
				mathStartDate.setDate(formid.PostDay.value);
				mathStartDate.setMonth(formid.PostMonth.value - 1); // January = 0
				mathStartDate.setFullYear(formid.PostYear.value);
				
				var today = new Date();
				
				if (mathStartDate < today){
					alert('Please enter a date that is not in the past.');
	    			goodVar = 0;
				}
			}
			
			if ((formid.PostMonth.value == "" || formid.PostDay.value == "" || formid.PostYear.value == "" ) && goodVar == 1) {	
    			alert( "Please enter a valid start date." );
   				formid.title.focus();
    			goodVar = 0;
  			}
			
	
			
			
			//End Date
			var is_enddate_set = (formid.EndMonth.value == "" && formid.EndDay.value == "" && formid.EndYear.value == "" );
			var is_enddate_not_set = (formid.EndMonth.value == "" && formid.EndDay.value == "" && formid.EndYear.value == "" );
			var is_good = (is_enddate_set & is_enddate_not_set);
			
			if (is_good == 0  && goodVar == 1) {	
    			alert( "Please enter a valid end date." );
   				formid.title.focus();
    			goodVar = 0;
  			}		
			
		
			// Start date/End Date - Is the endate after the start date?
			if( is_enddate_not_set = false && goodVar == 1 ){
				var mathEndDate = new Date;
				mathEndDate.setDate(formid.EndDay.value);
				mathEndDate.setMonth(formid.EndMonth.value - 1); // January = 0
				mathEndDate.setFullYear(formid.EndYear.value);
				
			
				if (mathStartDate < mathEndDate){
					alert('Please enter an end date that is not before the start date.');
	    			goodVar = 0;
				}
			}
			
		
			//Textbox
			if (formid.body.value == "" && goodVar == 1) {
    			alert( "Please enter the body." );
   				formid.body.focus();
    			goodVar = 0;
  			}
			
			
			if(goodVar == 1){
			var title = 		formid.title.value;
			var body = 			formid.body.value;
			//body = WebString(body);
			body = body;
			//var allowed = 		formid.allowed.value;
			//var uid = 			formid.uid.value;
			
			var folder = 		formid.folder.value;
			var vlink = 		formid.vlink.value;
			
			var PostMonth = 	formid.PostMonth.value;
			var PostDay = 		formid.PostDay.value;
			var PostYear = 		formid.PostYear.value;
			
			var EndMonth = 		formid.EndMonth.value;
			var EndDay = 		formid.EndDay.value;
			var EndYear = 		formid.EndYear.value;
			
			/*var CutOffDay = 	formid.CutOffDay.value;
			var CutOffMonth = 	formid.CutOffMonth.value;
			var CutOffYear = 	formid.CutOffYear.value;*/
			
				var buildVARs = 'type=entry&title=' + title + 
								'&body=' + encodeURIComponent(body) + '&uid=' + encodeURIComponent(uid) +'&userid=' + encodeURIComponent(user_id) +
								'&folder=' + encodeURIComponent(folder) +			'&PostMonth=' + encodeURIComponent(PostMonth) +
								'&PostDay=' + encodeURIComponent(PostDay) + 		'&PostYear=' + encodeURIComponent(PostYear) +
								'&EndMonth=' + encodeURIComponent(EndMonth) + 		'&EndDay=' + encodeURIComponent(EndDay) +
								'&link=' + encodeURIComponent(vlink) +				'&EndYear=' + encodeURIComponent(EndYear);
								//+ '&CutOffDay=' + CutOffDay +
								//'&CutOffMonth=' + CutOffMonth + '&CutOffYear=' + CutOffYear;
				
				//alert(buildVARs);
				
				update_element_post('entry','update', buildVARs);
				//setTimeout("update_element('comments','display','type=entry&title='+title+'&body='+body+'&uid='+uid+'')",5000);
				setTimeout("update_element('entry','display', '"+buildVARs+"');",1000);
				setTimeout("update_element('comments','display','type=commentsonly&uid="+uid+"&folder="+folder+"&userid="+user_id+"')",1000);
				setTimeout("update_element('formadd','display','type=commentform&folder="+folder+"&userid="+user_id+"')",1000);
				update_element('rightcolumn','display','type=side&folder='+folder+'&userid='+user_id+'');
			}else{
			
			}
		
		}
	
	}else if(type == "comment"){
	// ADD/UPDATE/DELETE COMMENT
		
		if(action == "add" || action == "update"){
			var title	=	formid.title.value;
			var body	=	formid.body.value;
			var uid		=	formid.uid.value;
			var commentid	=	formid.uid.value;
			var folder = 		formid.Folder.value;
			
			if(formid.user_id){
				var user_id = formid.user_id.value;
			}
			
			goodVar = 1;
		
			//Textbox
			if (formid.title.value == "" && goodVar == 1) {
    			alert( "Please enter the title." );
   				formid.title.focus();
    			goodVar = 0;
  			}
			
			//Textbox
			if (formid.body.value == "" && goodVar == 1) {
    			alert( "Please enter the body." );
   				formid.body.focus();
    			goodVar = 0;
  			}
			
			if(goodVar == 1){
				//alert('type=comment&title='+title+'&body='+body+'&uid='+uid+'&commentid='+commentid+'&folder='+folder+'&userid='+user_id);
				update_element_post('formadd','update',
					'type=comment&title='+encodeURIComponent(title)+'&body='+encodeURIComponent(body)+'&uid='+encodeURIComponent(uid)+'&commentid='+encodeURIComponent(commentid)+'&folder='+encodeURIComponent(folder)+'&userid='+encodeURIComponent(user_id));
				setTimeout("update_element('comments','display','type=commentsonly&uid="+uid+"&folder="+folder+"&userid="+user_id+"');",1000);
			}
		
		}
	
	}

}

// Expand JS for Archives Tree
function expand(elemenid) {
	// Set Element For Expansion
	styleObj=document.getElementById(elemenid).style;
	imgID=elemenid+'img'
	imgObj=document.getElementById(imgID);
	// Cut Up Current String Text For Change
	//titleText=currenttag.innerHTML.substring(3);
	// Alter Data
	if (styleObj.display=='none')
	{
		styleObj.display='block';
		imgObj.src='http://edu-core.org/modules/blog_xml/images/arrowdown.gif';
		//plusBox='[-]';
		//currenttag.innerHTML=plusBox+titleText;
	}
	else {
		styleObj.display='none';
		imgObj.src='http://edu-core.org/modules/blog_xml/images/arrowright.gif';
		//plusBox='[+]';
		//currenttag.innerHTML=plusBox+titleText;
	}
}