var surl;

function replaceAll(text, strA, strB)
{
    while ( text.indexOf(strA) != -1)
    {
        text = text.replace(strA,strB);
    }
    return text;
}

function createRequestObject_digg() 
	{
		var ro_digg;
		var browser_digg = navigator.appName;
		if(browser_digg == "Microsoft Internet Explorer")
			{
				ro_digg = new ActiveXObject("Microsoft.XMLHTTP");
			}
		else
			{
				ro_digg = new XMLHttpRequest();
			}
		return ro_digg;
	}
	
	
function fundowntick(topicid)
	{
		var userid_val=document.getElementById('hiddLoginUserId').value;
		if(userid_val=="0")
			{
				alert("Please login to continue.");
			}
		else
			{ 
			document.getElementById("uptickimage_"+topicid).className="nonetickimage";
			document.getElementById('hiddActionTopicId').value=topicid;
			http_digg_downtick = createRequestObject_digg();
			var url =surl+"ajaxdigg.php?userid="+userid_val+"&topic="+topicid+"&ajaxfor=downtick";
			http_digg_downtick.open('get', url);
			http_digg_downtick.onreadystatechange = handleResponse_digg_downtick;
			http_digg_downtick.send(null);
			}
	}
	
	
function handleResponse_digg_downtick()
	{
		if(http_digg_downtick.readyState==4)
		{
		if(http_digg_downtick.status==200)
			{ 
			var SResponse1=http_digg_downtick.responseText;
			var topicdiv=document.getElementById('hiddActionTopicId').value;
			document.getElementById("topicuptickli_"+topicdiv).style.display="none";
			document.getElementById("topicdowntickli_"+topicdiv).style.display="none";
			document.getElementById("uptickimage_"+topicdiv).className="downtickimage";
			}
		}
	}


function funstyledowntick(topicid)
	{

		var userid_val=document.getElementById('hiddLoginUserId').value;
		if(userid_val=="0")
			{
				alert("Please login to continue.");
			}
		else
			{ 
			document.getElementById("uptickimage_"+topicid).className="nonetickimage";
			document.getElementById('hiddActionTopicId').value=topicid;
			http_digg_styledowntick = createRequestObject_digg();
			var url =surl+"ajaxdigg.php?userid="+userid_val+"&topic="+topicid+"&ajaxfor=downtick";
			http_digg_styledowntick.open('get', url);
			http_digg_styledowntick.onreadystatechange = handleResponse_digg_styledowntick;
			http_digg_styledowntick.send(null);
			}
	}
	
	
function handleResponse_digg_styledowntick()
	{

		if(http_digg_styledowntick.readyState==4)
		{
		if(http_digg_styledowntick.status==200)
			{ 
			var SResponse1=http_digg_styledowntick.responseText;
			var topicdiv=document.getElementById('hiddActionTopicId').value;
			document.getElementById("topicuptickli_"+topicdiv).style.display="none";
			document.getElementById("topicdowntickli_"+topicdiv).style.display="none";
			document.getElementById("uptickimage_"+topicdiv).className="styleuptickimage";
			}
		}
	}


function funuptick(topicid)
	{
		var userid_val=document.getElementById('hiddLoginUserId').value;
		if(userid_val=="0")
			{
				alert("Please login to continue.");
			}
		else
			{
			document.getElementById('hiddActionTopicId').value=topicid;
			http_digg_uptick = createRequestObject_digg();
			
			var url =surl+"ajaxdigg.php?userid="+userid_val+"&topic="+topicid+"&ajaxfor=uptick";
			http_digg_uptick.open('get', url);
			http_digg_uptick.onreadystatechange = handleResponse_digg_uptick;
			http_digg_uptick.send(null);
			}
	}
	
	
function handleResponse_digg_uptick()
	{
		if(http_digg_uptick.readyState==4)
		{
		if(http_digg_uptick.status==200)
			{ 
			var SResponse1=http_digg_uptick.responseText;
			var topicdiv=document.getElementById('hiddActionTopicId').value;
			document.getElementById("topicuptickli_"+topicdiv).style.display="none";
			document.getElementById("topicdowntickli_"+topicdiv).style.display="none";
			document.getElementById("uptickimage_"+topicdiv).innerHTML=SResponse1;
			}
		}
	}
	
	
function openshare(tid)
	{
		var myWidth = 0, myHeight = 0;
		var userid_val=document.getElementById('hiddLoginUserId').value;
		if(userid_val=="0")
			{
				alert("Please login to continue.");
			}
		else
			{
			document.getElementById("shartopicmaindiv").style.display="block";
			
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
document.getElementById("shartopicmaindiv").style.left="0px";
document.getElementById("shartopicmaindiv").style.height=myHeight+"px";
document.getElementById("shartopicmaindiv").style.width=myWidth+"px";
var innerheight=(myHeight-230)/2;
var innerwidth=(myWidth-500)/2;
document.getElementById("shartopicdiv").style.top=innerheight+"px";
document.getElementById("shartopicdiv").style.left=innerwidth+"px";

document.getElementById("sharehiddentopicid").value=tid;

			}
	}


function funposttopicshare()	
	{
	var frm=document.frmsharetopic;
	document.getElementById("form_action").value="sendsharefrom";
	formvalidation(frm);
	}

function funhideshareform()
	{
		document.getElementById("shartopicmaindiv").style.display="none";
	}
	
function funposttopiccomment()	
	{
	var frm=document.frmposttopiccomment;
	document.getElementById("form_action").value="posttopiccomment";
	document.getElementById("commentsuccessmsgid").style.display="none";
	document.getElementById("commenterrormsgid").style.display="none";
	formvalidation(frm);
	}
	
function funtopicorder(orderby)
	{
	document.getElementById("sortby").value=orderby;
	document.getElementById("sort_order").value="desc";
	document.frmpaging.submit();
	}
	
function funposttopic()
	{
		var userid_val=document.getElementById('hiddLoginUserId').value;
		if(userid_val=="0")
			{
				alert("Please log in or register your account.");
			}
		else
			{
			window.location=surl+"zingiling/add/";
			}
	}
	
function funviewall()
	{
		
		window.location=surl+"zingiling/";
	}
	
function funRandomZingIling(topicid)	
	{
			window.location=surl+"zingiling/"+topicid+"/";
	}



function show_preview_topic()
	{
	 document.getElementById("zingilng_preview").style.display="block";
	 document.getElementById("topictitle_preview").innerHTML=document.getElementById("txtTopicTitle").value.substring(0,60);
	 var selected_index=document.getElementById("selTopicCategory").selectedIndex;
	 document.getElementById("topiccat_preview").innerHTML=document.getElementById("selTopicCategory").options[selected_index].text;
	document.getElementById("topicdesc_preview").innerHTML=document.getElementById("txtTopicDesc").value.substring(0,350);
	document.getElementById("topicimages_preview").src=document.getElementById("topic_images_path").value;
	var media_value=get_radio_value(document.getElementsByName("radiotopicmedia"));
	if(media_value=="Video")
		mediaimage="video_icon.gif";
	else if(media_value=="Audio")
		mediaimage="podcast_icon.png";
	else
		mediaimage="newsletter_icon.png";
	
	document.getElementById("topicmedia_preview").src=surl+ "templates/images/"+mediaimage;
	
	}

function fun_preview_category()
	{
	var selected_index=document.getElementById("selTopicCategory").selectedIndex;
	 document.getElementById("topiccat_preview").innerHTML=document.getElementById("selTopicCategory").options[selected_index].text;
	}
	
function caltopicimages()
	{
	var URLtext=trim(document.getElementById("txtTopicURL").value);
	var existing_url=trim(document.getElementById("hide_enter_url").value);
	if(trim(URLtext)!="" && existing_url!=URLtext)
		{
		if(URLtext.search("http"))
		    URLtext="http://"+URLtext;    
		var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
		if (regexp.test(URLtext) != true)
			{
			alert("Please enter a valid URL.");
			}
		else
			{
			//popup('popUpDiv');	
			imagestr='<img align="center" src="'+surl+'templates/images/loading.gif" width="16" height="16" />';	
			document.getElementById("URL_loading_container").innerHTML="<br />"+imagestr+"&nbsp;We are retrieving information from the web page. It will be posted momentarily.";
			imagestr='<img align="center" src="'+surl+'templates/images/loading.gif" width="16" height="16" style="vertical-align:middle;margin-top:45px;"  />';	
			document.getElementById("imagecontainer0").innerHTML=imagestr;
			document.getElementById("imagecontainer1").innerHTML=imagestr;
			document.getElementById("imagecontainer2").innerHTML=imagestr;
			http_digg_topic_images = createRequestObject_digg();
			URLtext=replaceAll(URLtext,"/","B_A_C_K");
			URLtext=replaceAll(URLtext,":","C_O_L_L_O_N");
			URLtext=replaceAll(URLtext,".","D_O_T");
			URLtext=replaceAll(URLtext,"&","A_N_D");
			URLtext=replaceAll(URLtext,"?","Q_U_E");
			var url =surl+"ajaxdigg.php?urltext="+URLtext+"&ajaxfor=topicimages";
			//document.write(url);
			http_digg_topic_images.open('get', url);
			http_digg_topic_images.onreadystatechange = handleResponse_topic_images;
			http_digg_topic_images.send(null);
			}
		}
	}
	
	
function handleResponse_topic_images()
	{
		if(http_digg_topic_images.readyState==4)
		{
		if(http_digg_topic_images.status==200)
			{
			document.getElementById("imagecontainer0").innerHTML="";
			document.getElementById("imagecontainer1").innerHTML="";
			document.getElementById("imagecontainer2").innerHTML="";
			document.getElementById("URL_loading_container").innerHTML="";
			//popup('popUpDiv');
			var SResponse_topic_img=http_digg_topic_images.responseText;
			var responsearray_topic=SResponse_topic_img.split("@@###$$###@@");
			var SResponse1=responsearray_topic[0];
			document.getElementById("txtTopicTitle").value=responsearray_topic[1];
			document.getElementById("remLentitle").value=(60 - parseInt((responsearray_topic[1]).length));
			
			document.getElementById("txtTopicDesc").value=responsearray_topic[2];
			document.getElementById("remLen1").value=(350 - parseInt((responsearray_topic[2]).length));
			if(trim(SResponse1)!=="error")
				{
				var responsearray=SResponse1.split("#@@@@$###$@@@@#");	
				for(l=0;l<responsearray.length;l++)
					{
					var imagestr="";	
					var imagesname=responsearray[l];
					imagestdname="imagecontainer"+l;
					//alert(imagestdname)
					imagestr='<img src="'+imagesname+'" style="vertical-align:middle;border:1px solid gray;" width="89" height="73" /><input type="radio" class="topicimageinput" id="topicimagesflag" name="topicimagesflag" value="'+imagesname+'" onclick="funchanges_rev_images(this);">';
					document.getElementById(imagestdname).innerHTML=imagestr;
					}
				for(k=responsearray.length;k<3;k++)
					{
					imagestdname="imagecontainer"+k;	
					imagestr='<img src="'+surl+'templates/images/zingiling_image/add_images_not_available.jpg" style="vertical-align:middle;border:0px solid gray;" width="89" height="73" />';
					document.getElementById(imagestdname).innerHTML=imagestr;
					}
					
				}
			else
				{
				for(k=0;k<3;k++)
					{
					imagestdname="imagecontainer"+k;	
					imagestr='<img src="'+surl+'templates/images/zingiling_image/images_not_available.jpg" style="vertical-align:middle;border:0px solid gray;" width="89" height="73" />';
					document.getElementById(imagestdname).innerHTML=imagestr;
					}	
				}
				
			/*var topicdiv=document.getElementById('hiddActionTopicId').value;
			document.getElementById("topicuptickli_"+topicdiv).style.display="none";
			document.getElementById("topicdowntickli_"+topicdiv).style.display="none";
			document.getElementById("uptickimage_"+topicdiv).innerHTML=SResponse1;*/
			}
		show_preview_topic();	
		}
		
		document.getElementById("hide_enter_url").value=document.getElementById("txtTopicURL").value;
		document.getElementById("txtTopicTitle").focus();
	}
	
function funchanges_rev_images(topicimages_name)
{
	document.getElementById("topic_images_path").value=topicimages_name.value;
	setpreviewImage();
	
}


function setpreviewImage()
{
	var topicimages_name=document.getElementById("topic_images_path").value;
	if(topicimages_name=="custom")
		{
		document.getElementById("custom_images_hidden").style.display="block";
		}
	else
		{
		document.getElementById("custom_images_hidden").style.display="none";
		}
	if(topicimages_name!="no" && topicimages_name!="custom")
		{
		var imagesname='<img  src="'+topicimages_name+'" width="40" height="40" border="1" alt="Topic Image" />';
		document.getElementById("topicimages_preview").style.display="block";
		document.getElementById("topicimages_preview").innerHTML=imagesname;
		}
	else
		{
		document.getElementById("topicimages_preview").style.display="none";	
		}
	
}

function openCreateCat()
{
  var newcat_container=document.getElementById("create_newcat_container");
  if(newcat_container.style.display=="none")
	{
	newcat_container.style.display="block";
	document.getElementById("txtNewCat").setAttribute("class", "form-text_topic mandatory");
	document.getElementById("selTopicCategory").setAttribute("class", "form-text_topic");
	}
  else
	{
	document.getElementById("txtNewCat").value="";
	document.getElementById("txtNewCat").setAttribute("class", "form-text_topic");
	document.getElementById("selTopicCategory").setAttribute("class", "form-text_topic mandatory");
	newcat_container.style.display="none";
	}
}

	



function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	
	
	//

var winW = 630, winH = 460;

if (parseInt(navigator.appVersion)>3) {
 if (navigator.appName=="Netscape") {
  winW = window.innerWidth;
  winH = window.innerHeight;
 }
 if (navigator.appName.indexOf("Microsoft")!=-1) {
  winW = document.body.offsetWidth;
  winH = document.body.offsetHeight;
 }
}

	
	//
	
	popUpDiv_height=250;//winH/2 -150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}

function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);
}

function get_radio_value(radio_element)
{
for (var i=0; i < radio_element.length; i++)
   {
   if (radio_element[i].checked)
      {
      var rad_val = radio_element[i].value;
      }
   }
 return rad_val; 
}

function checkBlur(e)
{
var key;
if(window.event)
	key = window.event.keyCode;     //IE
else
	key = e.which;     //firefox
if(key == 13)   // If User Press enter 
	{
	caltopicimages();
	}
}
