function makeRequest(url,divID) 
{
   var http_request = false;

	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	
	http_request.onreadystatechange = function() { alertContents(http_request,divID); };
	http_request.open('GET', url, true);

	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.setRequestHeader("Cache-Control", "no-cache");


	http_request.send(null);
}

function alertContents(http_request,divID) 
{
	if(http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			var err_text = http_request.responseText;
			if (err_text == "error")
			{
				document.getElementById ("error_check").innerHTML = "<!--comments-->Oops! One or more words entered in your Comments is too long. Please break the words using spaces.";
				document.getElementById(divID).innerHTML = "";
			}
			else if(err_text == "error1")
			{
				document.getElementById ("error_check").innerHTML = "<!--comments-->Oops! Your comment is too long. Please restrict it to less than 250 characters.";
				document.getElementById(divID).innerHTML = "";
			}
			else
			{
				if(document.getElementById ("error_check"))
				{
					document.getElementById ("error_check").innerHTML = "";
					var str_comment = "";
					document.getElementById ("comments").value = str_comment;
					if(document.getElementById('editor_val').value!='N')
					{
						var objfck = FCKeditorAPI.GetInstance('comments') ;
						objfck.SetHTML(str_comment,true);
					}
				}
				if (divID=="res_check")
				{
					document.getElementById(divID).innerHTML = "";
					document.getElementById("comment_display").innerHTML = http_request.responseText;
				}
				else
				{
					document.getElementById(divID).innerHTML = http_request.responseText;
				}
				
			}
			
		}
		else
		{
			alert("Error: While trying to fetch records, please try again later");
			//document.getElementById(divId).innerHTML = "";
		}
		
	}
	else
	{
		document.getElementById(divID).innerHTML = "<span id=\"\" valign=\"middle\" align=\"center\"><img src=\"/zones-ssi/js-css-images/loading.gif\" width=\"63\" height=\"13\" border=\"0\" alt=\"Loading...\"><span>";
	}
}

function postRequest(url,parameters,divID) 
{
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
    } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
    }
    if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
    //alert(divID);
    http_request.onreadystatechange = function() { alertContents(http_request,divID); };
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

/*
If changed following functions please make similar changes in zone-my-zone.js
*/
/*************************************************************************/
function stripHTML(val)
{
		var re= /<\S[^><]*>/g;
		var nwval="";
	
		nwval=val.replace(re, '');
		return nwval;
}

function html2entities(val)
{
	var re=/[<>"'&]/g;
	var nwval="";
	
	nwval=val.replace(re, function(m){return replacechar(m)})

	return nwval;
}

function replacechar(match)
{
	if (match=="<")
		return "&lt;"
	else if (match==">")
		return "&gt;"
	else if (match=="\"")
		return "&quot;"
	else if (match=="'")
		return "&#039;"
	else if (match=="&")
		return "&amp;"
}

function entities2html(val)
{
	var nwval="";
	nwval=val.replace(/&lt;/g, "<");
	nwval=nwval.replace(/&gt;/g, ">")
	nwval=nwval.replace(/&quot;/g, "\"")
	nwval=nwval.replace(/&#039;/g, "'")
	nwval=nwval.replace(/&amp;/g, "&")
	nwval=nwval.replace(/&nbsp;/g, " ")

	return nwval;
}
/*************************************************************************/

function recAction(txttype)
{
    //alert("In recAction");
	if(document.getElementById('editor_val').value=='')
	{
		return false;
	}
	
	if (txttype=='plain')
	{
        if(document.getElementById('editor_val').value=='No')
		{
			return false;
		}
		else if(document.getElementById('editor_val').value=='N')
		{
			document.getElementById('div_editortab').innerHTML = "Switch to <a href=\"#\" class=\"medium link noborder\" onclick=\"javascript:recAction('rich'); return false;\">Rich Text</a>";
			document.getElementById('comment_editor1').style.display="block";
			document.getElementById('comment_editor').style.display="none";
		}
		else
		{
			var objfck = FCKeditorAPI.GetInstance('comments') ;
			if (objfck.GetXHTML(false)!="")
			{
				if(confirm("Converting to 'Plain Text' will remove the formatting. \nDo you want to proceed?"))
				{
					document.getElementById('div_editortab').innerHTML = "Switch to <a href=\"#\" class=\"medium link noborder\" onclick=\"javascript:recAction('rich'); return false;\">Rich Text</a>";
					var comnt = "";
					comnt = stripHTML(objfck.GetHTML(false)); 
					comnt = entities2html(comnt);
					document.getElementById('comments').value = comnt; 
					document.getElementById('comment_editor1').style.display="block";
					document.getElementById('comment_editor').style.display="none";

					var objfck = FCKeditorAPI.GetInstance('comments') ;
					objfck.SetHTML("",true);

					document.getElementById('editor_val').value = "txt";
				}
			}
			else
			{
				document.getElementById('div_editortab').innerHTML = "Switch to <a href=\"#\" class=\"medium link noborder\" onclick=\"javascript:recAction('rich'); return false;\">Rich Text</a>";
				document.getElementById('comments').value = ""; 
				document.getElementById('comment_editor1').style.display="block";
				document.getElementById('comment_editor').style.display="none";
				var objfck = FCKeditorAPI.GetInstance('comments') ;
				objfck.SetHTML("",true);
				document.getElementById('editor_val').value = "txt";
			}
			
		}
	}
	else if(txttype=='rich')
	{
		if(document.getElementById('editor_val').value=='N')
		{
			getEditor();
			document.getElementById('editor_val').value='';
		}
		else
		{
			document.getElementById('comment_editor1').style.display="none";
			document.getElementById('comment_editor').style.display="block";
		
			set_editor(document.getElementById('comments').value);
			document.getElementById('editor_val').value = "fck";
			document.getElementById('div_editortab').innerHTML = "Switch to <a href=\"#\" class=\"medium link noborder\" onclick=\"recAction('plain'); return false;\">Plain Text</a>";
		}
		
	}
}

var isEditorReady = false;
function FCKeditor_OnComplete( editorInstance )
{
	isEditorReady = true ;
	if(document.getElementById('comment_editor1'))
	{
		document.getElementById('comment_editor1').style.display="none";
		document.getElementById('comment_editor').style.display="block";
	}
	if(document.getElementById('editor_val'))
	{	
		set_editor(document.getElementById('comments').value);
		document.getElementById('editor_val').value = "fck";
		document.getElementById('div_editortab').innerHTML = "Switch to <a href=\"#\" class=\"medium link noborder\" onclick=\"recAction('plain'); return false;\">Plain Text</a>";
	}
}

function set_editor(val)
{
	var str_comment;
	str_comment = "";
	
	
	str_comment = html2entities(val);
	var objfck = FCKeditorAPI.GetInstance('comments') ;
	objfck.SetHTML(str_comment,true);

	document.getElementById('comments').value="";
	
	return false;
}