var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try 
  	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	} 
	catch (e) 
	{
		try 
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		} 
		catch (E) 
		{
			xmlhttp=false
		}
	}
@else
	xmlhttp=false
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
	try 
	{
		xmlhttp = new XMLHttpRequest ();
  	}
	catch (e) 
	{
		xmlhttp = false
	}
}

function myXMLHttpRequest ()
{
	var xmlhttplocal;
	try 
	{
  		xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")
  	}
  	catch (e) 
  	{
		try 
		{
			xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")
		}
		catch (E) 
		{
			xmlhttplocal = false;
		}
  	}

	if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			var xmlhttplocal = new XMLHttpRequest ();
		}
		catch (e) 
		{
	  		var xmlhttplocal = false;
		}
  	}
	return (xmlhttplocal);
}

var mnmxmlhttp = Array ();
var xvotesString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;

var vote_for_against;

function put_vote_value(val)
{	
	if(vote_submit())
	{
		if(val == 0 || val == 1)
		{
			document.getElementsByName("for_against")[0].value = val;
			document.getElementsByName("vote_comment_for_against")[0].value = val;
			document.getElementById("thisformComment").submit();
		}
	}
	else
	{
		return false;
	}
}

//for_against - Vote For or Against the topic

function vote(user, id, htmlid, md5, value, for_against)
{
	if(typeof for_against == "undefined")
	{
		for_against = '';
	}
	else
	{
		if(document.getElementsByName('vote_comment_for_against')[0])
		{
			document.getElementsByName('vote_comment_for_against')[0].value = for_against;
		}
		vote_for_against = for_against;
	}
	
	if (xmlhttp) 
 	{
		url = "http://www.takepanga.com/vote.php";
		//var mycontent = "id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value;
		var mycontent = "id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value + "&for_against=" + for_against;
		anonymous_vote = true;
        if (anonymous_vote == false && user == '0') 
        {
            window.location="http://www.takepanga.com/login.php?return="+location.href;
        } 
        else 
        {
        	mnmxmlhttp[htmlid] = new myXMLHttpRequest();
    		if (mnmxmlhttp) 
    		{
    			mnmxmlhttp[htmlid].open ("POST", url, true);
    			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
    
    			mnmxmlhttp[htmlid].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");    
    			target1 = document.getElementById ('xvotes-' + htmlid);
    			if(target1)
    			{    
    				mnmPrevColor[htmlid] = target1.style.backgroundColor;
    			}
    			mnmxmlhttp[htmlid].onreadystatechange = function () 
    			{
    				if (mnmxmlhttp[htmlid].readyState == 4) 
    				{
    					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
    					if (xvotesString[htmlid].match (errormatch)) 
    					{
    						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);
							changemnmvalues (htmlid, value, true);    					}
    					else 
    					{
							if (xvotesString[htmlid].substring (1, 6) == "ERROR") { alert(xvotesString[htmlid]);  } else {changemnmvalues (htmlid, value, false); }     					}
    				}
    			}
    		}
    		vote_for_against = '';
    	}
	}
}


function unvote (user, id, htmlid, md5, value)
{
  	if (xmlhttp) 
  	{
		url = "http://www.takepanga.com/vote.php";
		var mycontent = "unvote=true&id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value;
		anonymous_vote = true;
        if (anonymous_vote == false && user == '0') 
        {
            window.location="http://www.takepanga.com/login.php?return="+location.href;
        } 
        else 
        {
    		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
    		if (mnmxmlhttp) 
    		{
    			mnmxmlhttp[htmlid].open ("POST", url, true);
    			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');    
    			mnmxmlhttp[htmlid].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");    
				target1 = document.getElementById ('xvotes-' + htmlid);
  				target2 = document.getElementById ('xvote-' + htmlid);
  				if(target1)
  				{    
    				mnmPrevColor[htmlid] = target1.style.backgroundColor;
    			}
    			mnmxmlhttp[htmlid].onreadystatechange = function () 
    			{
    				if (mnmxmlhttp[htmlid].readyState == 4) 
    				{
    					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
    					if (xvotesString[htmlid].match (errormatch)) 
    					{
    						 xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);
							changemnmvalues (htmlid, value, true);target2.innerHTML = "<span>Vote</span>";						} 
						else 
						{
							if (xvotesString[htmlid].substring (1, 6) == "ERROR") { alert(xvotesString[htmlid]);  } else {target2.innerHTML = "<span>Vote</span>";changemnmvalues (htmlid, value, false); }							
    					}
    				}
    			}
    		}
    	}
	}
}


function cvote (user, id, htmlid, md5, value)
{
	if (xmlhttp) 
	{
		url = "http://www.takepanga.com/cvote.php";
		mycontent = "id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value;
		anonymous_vote = true;

        if (anonymous_vote == false && user == '0') 
        {
            window.location="http://www.takepanga.com/login.php?return="+location.href;
        } 
        else 
        {
    		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
    		if (mnmxmlhttp) 
    		{
    			mnmxmlhttp[htmlid].open ("POST", url, true);
    			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');    
    			mnmxmlhttp[htmlid].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");
    			target1 = document.getElementById ('cvote-' + htmlid);        
    			mnmPrevColor[htmlid] = target1.style.backgroundColor;
    			target1.style.backgroundColor = '#FFFFFF';
    			mnmxmlhttp[htmlid].onreadystatechange = function () 
    			{
    				if (mnmxmlhttp[htmlid].readyState == 4) 
    				{
    					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
    					if (xvotesString[htmlid].match (errormatch)) 
    					{
    						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);						
    						changecvotevalues (htmlid, true);							
    					} 
    					else 
    					{
							target1 = document.getElementById ('ratebuttons-' + id);
							target1.style.display = "none";
							target2 = document.getElementById ('ratetext-' + id);
							target2.innerHTML = "Thank you for rating this comment.";
							changecvotevalues (htmlid, false);
    					}
    				}
    			}
    		}
    	}
	}
}

function change_for_against_vote_values(id, value, error)
{
	xml_obj = create_xml_obj(xvotesString[id]);
	document_element = xml_obj.documentElement; 
	xml_votes_for = document_element.getElementsByTagName("votes_for")[0].firstChild.data;
	xml_votes_against = document_element.getElementsByTagName("votes_against")[0].firstChild.data;
	xml_votes_total = document_element.getElementsByTagName("votes_total")[0].firstChild.data;
	
	target1 = document.getElementById ('xvotes-' + id);
	target2 = document.getElementById ('xvote-' + id);
	target3 = document.getElementById ('xreport-' + id);
	target4 = document.getElementById ('xnews-' + id);
	
	if (error) 
	{
		if (value > 0) 
		{ 
			target2.innerHTML = "<span>Voted</span> "; 
		}
		else if (value < 0) 
		{ 
			target2.innerHTML = "<span>Buried</span> "; 
		}
		return false;
	}
	if (xml_votes_for) 
	{
		target1.innerHTML = xml_votes_for;
		target1.style.backgroundColor = mnmPrevColor[id];
		new Effect.Fade(target3);
		if (value > 0) 
		{ 
			target2.innerHTML = "<span>Voted</span> "; 
		}
		else if (value < 0 ) 
		{ 
			target2.innerHTML = "<span>Buried</span> ";
			new Effect.Opacity(target4, {duration:0.8, from:1.0, to:0.3}); 
		}
	}

	target1 = document.getElementById ('xvotes-' + id + '-1');
	target2 = document.getElementById ('xvote-' + id + '-1');
	//target3 = document.getElementById ('xreport-' + id + '-1');
	//target4 = document.getElementById ('xnews-' + id + '-1');
	
	if (error) 
	{
		if (value > 0) 
		{ 
			target2.innerHTML = "<span>Voted</span> "; 
		}
		else if (value < 0) 
		{ 
			target2.innerHTML = "<span>Buried</span> "; 
		}
		return false;
	}
	if (xml_votes_against) 
	{
		target1.innerHTML = xml_votes_against;
		target1.style.backgroundColor = mnmPrevColor[id];
		new Effect.Fade(target3);
		if (value > 0) 
		{ 
			target2.innerHTML = "<span>Voted</span> "; 
		}
		else if (value < 0 ) 
		{ 
			target2.innerHTML = "<span>Buried</span> ";
			new Effect.Opacity(target4, {duration:0.8, from:1.0, to:0.3}); 
		}
	}
	return false;
}

function create_xml_obj(str_xml) 
{
	if(window.ActiveXObject) 
	{
		var xml_obj;
		try
		{
			xml_obj = new ActiveXObject("Msxml2.DOMDocument.3.0");
		}
		catch(e)
		{
			try 
			{
				xml_obj = new ActiveXObject("Microsoft.XMLDOM");
			
			}
			catch (E) { }
		}
		
		xml_obj.async = false;
		xml_obj.loadXML(str_xml);
	}
	else if(document.implementation && document.implementation.createDocument) 
	{
		objDOMParser = new DOMParser();
		xml_obj = objDOMParser.parseFromString(str_xml, "text/xml");
	}
	return xml_obj;
}

function changemnmvalues (id, value, error)
{
	if(typeof vote_for_against == "number")
	{
		change_for_against_vote_values(id, value, error);
		return false;
	}
	else
	{
		split = new RegExp ("~--~");
		b = xvotesString[id].split (split);
		target1 = document.getElementById ('xvotes-' + id);
		target2 = document.getElementById ('xvote-' + id);
		target3 = document.getElementById ('xreport-' + id);
		target4 = document.getElementById ('xnews-' + id);
		if (error) 
		{
			if (value > 0) 
			{ 
				target2.innerHTML = "<span>Voted</span> "; 
			}
			else if (value < 0) 
			{ 
				target2.innerHTML = "<span>Buried</span> "; 
			}
			return false;
		}
		if (b.length <= 3) 
		{
			if(target1)
			{
				target1.innerHTML = b[0];
				target1.style.backgroundColor = mnmPrevColor[id];
			}
			new Effect.Fade(target3);
			if (value > 0) 
			{ 
				if(target2)
				{
					target2.innerHTML = "<span>Voted</span> ";
				} 
			}
			else if (value < 0 ) 
			{ 
				if(target2)
				{
					target2.innerHTML = "<span>Buried</span> ";
					new Effect.Opacity(target4, {duration:0.8, from:1.0, to:0.3});
				} 
			}
		}
		return false;
	}	
}

function changecvotevalues (id, error)
{
	split = new RegExp ("~--~");
	b = xvotesString[id].split (split);
	target1 = document.getElementById ('cvote-' + id);
	if (error) 
	{
		return false;
	}
	if (b.length <= 3) 
	{
		target1.innerHTML = b[0];
		target1.style.backgroundColor = mnmPrevColor[id];
	}
	return false;
}

function enablebutton (button, button2, target)
{
	var string = target.value;
	button2.disabled = false;
	if (string.length > 0) 
	{
		button.disabled = false;
	} 
	else 
	{
		button.disabled = true;
	}
	
	return false;
}

function checkfield (type, form, field)
{
	url = 'http://www.takepanga.com/checkfield.php?type='+type+'&name=' + field.value;
	checkitxmlhttp = new myXMLHttpRequest ();
	checkitxmlhttp.open ("GET", url, true);
	checkitxmlhttp.onreadystatechange = function () 
	{
		if (checkitxmlhttp.readyState == 4) 
		{
			responsestring = checkitxmlhttp.responseText;
			if (responsestring == 'OK') 
			{
				document.getElementById (type+'checkitvalue').innerHTML = '<span style="color:black">"' + field.value +	'": ' + responsestring + '</span>';
				form.submit.disabled = '';
			} 
			else 
			{
				document.getElementById (type+'checkitvalue').innerHTML = '<span style="color:red">"' + field.value + '": ' +
				responsestring + '</span>';
				form.submit.disabled = '';
			}
		}
	}
	checkitxmlhttp.send (null);
	return false;
}

function emailto (original_id, htmlid, instpath, address_count)
{
	email_message = document.getElementById('email_message' + htmlid).value;
	email_address = document.getElementById('email_address_1_' + htmlid).value;
	if (address_count > 1)
	{
		for ($i = 2; $i <= address_count; $i++) 
		{
			if(document.getElementById('email_address_' + $i + '_' + htmlid).value != "") 
			{
				email_address = email_address + ', ' + document.getElementById('email_address_' + $i + '_' + htmlid).value;
			}
		}
	}
	
	if (xmlhttp) 
	{
		url = "http://www.takepanga.com/recommend.php";
		mycontent = "email_address=" + escape(email_address) + "&email_to_submit=submit&email_message=" + escape(email_message) + "&original_id=" + original_id + "&backup=0";
		target2 = document.getElementById ('emailto-' + htmlid);
		target2.innerHTML = "<br>Sending, please wait....";
		
		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
		if (mnmxmlhttp) 
		{
			mnmxmlhttp[htmlid].open ("POST", url, true);
			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');

			mnmxmlhttp[htmlid].send (mycontent);
			errormatch = new RegExp ("^ERROR:");
			target1 = document.getElementById ('emailto-' + htmlid);
			mnmxmlhttp[htmlid].onreadystatechange = function () 
			{
				if (mnmxmlhttp[htmlid].readyState == 4) 
				{
					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
			
					if (xvotesString[htmlid].match (errormatch)) 
					{
						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);						
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];						
					} 
					else 
					{
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];
					}
				}
			}
		}
	}
}

function show_recommend(htmlid, linkid, instpath)
{
	var emailtodisplay=document.getElementById('emailto-' + htmlid).style.display ? '' : 'none';
	document.getElementById('emailto-' + htmlid).style.display = emailtodisplay;

	if (xmlhttp)  
	{
		url = "http://www.takepanga.com/recommend.php";
		mycontent = "draw=small&htmlid=" + htmlid + "&linkid=" + linkid;
		target2 = document.getElementById ('emailto-' + htmlid);
		target2.innerHTML = "<br>Loading, please wait....";		
		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
		
		if (mnmxmlhttp) {
			mnmxmlhttp[htmlid].open ("POST", url, true);
			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');

			mnmxmlhttp[htmlid].send (mycontent);
			errormatch = new RegExp ("^ERROR:");
			target1 = document.getElementById ('emailto-' + htmlid);
			mnmxmlhttp[htmlid].onreadystatechange = function () 
			{
				if (mnmxmlhttp[htmlid].readyState == 4) 
				{
					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;			
					if (xvotesString[htmlid].match (errormatch)) 
					{
						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);						
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];						
					} 
					else 
					{
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];
					}
				}
			}
		}
	}
}

function visualize_vote(for_against)
{
	base_path = 'http://www.takepanga.com/templates/yget';
	if(document.getElementById("visualize_vote_image"))
	{
		if(for_against == 0)
		{
			document.getElementById("visualize_vote_image").src = base_path + "/images/thumbs_up.gif";
			document.getElementById("vote_submit").innerHTML = '<button onclick="return put_vote_value(0)" style="width: 75px;" class="button_image" type="button"><img src="http://www.takepanga.com/templates/yget/images/submit_button.png" width="65" height="20" /></button>';
		}
		else if(for_against == 1)
		{
			document.getElementById("visualize_vote_image").src = base_path + "/images/thumbs_down.gif";
			document.getElementById("vote_submit").innerHTML = '<button onclick="return put_vote_value(1)" style="width: 75px;" class="button_image" type="button"><img src="http://www.takepanga.com/templates/yget/images/submit_button.png" width="65" height="20" /></button>';			
		}
	}
}

function comment_vote(url, for_against)
{
	if(document.getElementsByName('vote_comment_content')[0])
	{
		if(document.getElementById('vote_comment').style.display == 'none')
		{
			new Effect.toggle('vote_comment','blind', {queue: 'end'});
			try
			{
				document.getElementsByName('vote_comment_content')[0].focus();
			}
			catch(e) {}
			visualize_vote(for_against);
		}
		else
		{
			try
			{
				document.getElementsByName('vote_comment_content')[0].focus();
			}
			catch(e) {}
			visualize_vote(for_against);
		} 
	}
	else
	{
		if(document.getElementById('vote_comment').style.display == 'none')
		{
			new Effect.toggle('vote_comment','blind', {queue: 'end'});
			visualize_vote(for_against);
		}
	}
}

function submit_vote_comment(form)
{
	if(vote_for_against != '')
	{
		document.getElementsByName('vote_comment_for_against')[0].value = vote_for_against;
		alert(document.getElementsByName('vote_comment_for_against')[0].value);
		form.submit();
	}
}