var nextDisplayObj;
var nextObj;
var chkStatusObj;
var uploadUrlObj;
var video_exchange_ids=0;
var previewObj;

function showNewsEditor()
{
	if(document.getElementById('newscreate').style.display=='none')
	{
		document.getElementById('newscreate').style.display='block';
		document.getElementById('wysiwygnewstext').contentWindow.document.body.innerHTML='Type Here';
		document.getElementById('wysiwygnewstext').contentWindow.document.designMode='on';
		document.getElementById('wysiwygnewstext').contentWindow.document.focus();
	}
	else
	{
		document.getElementById('newscreate').style.display='none';
	}
}

function createRequestObject()
{
  var obj;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer")
  	obj = new ActiveXObject("Microsoft.XMLHTTP");
  else 
  	obj = new XMLHttpRequest();
   return obj;
}
function getUploaded_File()
{
	document.uploadotherfrm.uploadvideo.value="";
	document.uploadotherfrm.subbtn.disabled=false;
	document.uploadotherfrm.caption.value="";
	document.getElementById("viduploadresult").innerHTML="<div style='height:150px;' class='textfont'><img src='"+web_lite+"/images/loading_blu.gif' />&nbsp;File Uploaded & Loading File...</div>";	
	chkStatusObj=createRequestObject();
	url=dir_point+"ajxfiles/chkUploadedNewsFile.php?";
	//alert(url);
	//window.open(url);
	chkStatusObj.open('get', url+"sid="+Math.random());
	chkStatusObj.onreadystatechange =displayUpFile;
	chkStatusObj.send(null);
}
function displayUpFile()
{
   try
   { 
   	if(chkStatusObj.readyState == 4)
	{
	    try{
		  if(chkStatusObj.status == 200)
		   {
		   	document.getElementById("viduploadresult").innerHTML=chkStatusObj.responseText;
		   	resScripts=document.getElementById("viduploadresult").getElementsByTagName("script");
		   	for(i=0;i<resScripts.length;i++)
		   	{
		   		eval(resScripts[i].innerHTML);
		   	}
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function changePage(pageNo,media_type,dispObj)
{
	nextDisplayObj=dispObj;
	document.getElementById(nextDisplayObj).innerHTML="<div style='height:150px;'><img src='"+web_lite+"/images/loading_blu.gif' /></div>";	
	nextObj=createRequestObject();
	url=dir_point+"ajxfiles/previewPaging.php?page="+pageNo+"&media_type="+media_type;
	nextObj.open('get', url+"&sid="+Math.random());
	nextObj.onreadystatechange =displayNext;
	nextObj.send(null);
}
function displayNext()
{
   try
   { 
   	if(nextObj.readyState == 4)
	{
	    try{
		  if(nextObj.status == 200)
		   {
		   	document.getElementById(nextDisplayObj).innerHTML=nextObj.responseText;
		   	resScripts=document.getElementById(nextDisplayObj).getElementsByTagName("script");
		   	for(i=0;i<resScripts.length;i++)
		   	{
		   		eval(resScripts[i].innerHTML);
		   	}
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function wysiwyg_callmyvid(obj)
{
	obj.className="current";
	document.getElementById("wysiwyg_myvidcontent").style.display='block';
	document.getElementById("wysiwyg_uploadvid").className="";
	document.getElementById("wysiwyg_uploadvidcontent").style.display='none';
	document.getElementById("wysiwyg_urlpathvid").className="";
	document.getElementById("wysiwyg_urlpathvidcontent").style.display='none';
}
function wysiwyg_calluploadvid(obj)
{
	obj.className="current";
	document.getElementById("wysiwyg_uploadvidcontent").style.display='block';
	document.getElementById("wysiwyg_myvidlink").className="";
	document.getElementById("wysiwyg_myvidcontent").style.display='none';
	document.getElementById("wysiwyg_urlpathvid").className="";
	document.getElementById("wysiwyg_urlpathvidcontent").style.display='none';
}
function wysiwyg_callurlpathvid(obj)
{
	obj.className="current";
	document.getElementById("wysiwyg_urlpathvidcontent").style.display='block';
	document.getElementById("wysiwyg_uploadvid").className="";
	document.getElementById("wysiwyg_uploadvidcontent").style.display='none';
	document.getElementById("wysiwyg_myvidlink").className="";
	document.getElementById("wysiwyg_myvidcontent").style.display='none';
}
function validateuploadvideourlpath(btn)
{
	if(document.uploadvideourlpathfrm.uploadvideourlpath.value=="")
	{
		document.getElementById("newsurlpathvideoerror").innerHTML = "* ENTER a video URL.";
		error =1;
		return false;
	}
	if(document.uploadvideourlpathfrm.uploadvideourlpath.value!="")
	{
		 var videourldetails =document.uploadvideourlpathfrm.uploadvideourlpath.value;
		 var pos1=document.uploadvideourlpathfrm.uploadvideourlpath.value.indexOf('.');
		 var pos2=document.uploadvideourlpathfrm.uploadvideourlpath.value.indexOf('.com');
		 var pos3=(pos2-pos1)-1;
		 var sitename= videourldetails.substr(pos1+1,pos3);
		 sitename= sitename.toLowerCase();
		
		 if(sitename!="youtube" && sitename!="google")
		 {
		 	document.getElementById("newsurlpathvideoerror").innerHTML = "* Only Youtube/Google Video URL allowed.";
			error =1;
			return false;
		 }
		
	}
	document.getElementById('vidurlresult').innerHTML="<div style='height:150px;' class='textfont'><img src='"+web_lite+"/images/loading_blu.gif' />&nbsp;Uploading File...</div>";	
	btn.disabled=true;
	
	media_file_type=document.uploadvideourlpathfrm.media_file_type.value;
	upload_file_type=document.uploadvideourlpathfrm.upload_file_type.value;
	videourl=document.uploadvideourlpathfrm.uploadvideourlpath.value;
	caption=document.uploadvideourlpathfrm.caption.value;
	communityid=document.uploadvideourlpathfrm.community_id.value;
	
	var url = dir_point+"NewsUploads.php";
	var params = "videourl="+escape(encodeURI(videourl))+"&media_file_type="+escape(encodeURI(media_file_type))+"&upload_file_type="+escape(encodeURI(upload_file_type))+"&caption="+escape(encodeURI(caption))+"&communityid="+escape(encodeURI(communityid));
	
	uploadUrlObj=createRequestObject();	
	uploadUrlObj.open("POST", url, true);
	uploadUrlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	uploadUrlObj.setRequestHeader("Content-length", params.length);
	uploadUrlObj.setRequestHeader("Connection", "close");
	uploadUrlObj.onreadystatechange =showVidUrlResult;
	uploadUrlObj.send(params);
		
	
}
function showVidUrlResult()
{
try
   { 
   	if(uploadUrlObj.readyState == 4)
	{
	    try{
		  if(uploadUrlObj.status == 200)
		   {
		   	document.getElementById("vidurlresult").innerHTML=uploadUrlObj.responseText;
		   	resScripts=document.getElementById("vidurlresult").getElementsByTagName("script");
		   	for(i=0;i<resScripts.length;i++)
		   	{
		   		eval(resScripts[i].innerHTML);
		   	}
		   	document.uploadvideourlpathfrm.uploadvideourlpath.value="";
		   	document.uploadvideourlpathfrm.caption.value="";
			document.uploadvideourlpathfrm.subbtn.disabled=false;
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}

function validateother(btn)
{
	
	if(document.uploadotherfrm.uploadvideo.value=="")
	{
		document.getElementById("genvideomediauperr").innerHTML = "* Select/Enter a video to upload!";
		error =1;
		return false;
	}
	if(document.uploadotherfrm.uploadvideo.value!="")
	{
		 var filename =document.uploadotherfrm.uploadvideo.value;
		 var pos=document.uploadotherfrm.uploadvideo.value.lastIndexOf('.');
		 var format= filename.substr(pos+1,filename.length);
		 format=format.toUpperCase();
		 if(format!="3GP" && format!="AVI" && format!="MPG" && format!="MPEG" && format!="MP4" && format!="WMV")
		 { 
		 	document.getElementById("genvideomediauperr").innerHTML = "* File Format not supported!";
			error =1;
			return false;
		 }
	}
	
	document.getElementById('uploadotherfrm').target = 'upload_target';
	document.getElementById('upload_target').onload=function() {
			getUploaded_File();
		}
	document.getElementById('viduploadresult').innerHTML="<div style='height:150px;' class='textfont'><img src='"+web_lite+"/images/loading_blu.gif' />&nbsp;Uploading File...</div>";	
	btn.disabled=true;
	document.uploadotherfrm.submit();
	
}
function addThisVideo(vidObj)
{
	
	video_exchange_ids++;
	videoDet=vidObj.parentNode.parentNode.getElementsByTagName("div").item(0).innerHTML;
	imageDet=vidObj.parentNode.getElementsByTagName("div").item(0).innerHTML;
	image_exchange_id="myCFLI_exchange"+video_exchange_ids;
	fullDet="<div id="+image_exchange_id+">"+imageDet+"</div>";
	window.insertHTML(fullDet, 'newstext');
	document.getElementById('videoselect').style.display='none';
	fade_out('fadeDiv',0.6,0);
	
}
function addThisImage(real_image,image_id,image_type)
{

	fullDet = '<img src="' + real_image + '" myCFLI_PhotoId="' + image_id + '" myCFLI_PhotoType="' + image_type + '">';
 	window.insertHTML(fullDet, 'newstext');
 	document.getElementById('photoselect').style.display='none';
	fade_out('fadeDiv',0.6,0);
  
}
function showPreview(n)
{
	
	frameDoc=document.getElementById("wysiwyg" + n).contentWindow.document;
	editorText=frameDoc.body.innerHTML;
	
	prep_pop();
	obj=document.getElementById("wysiwyg" + n);
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	curleft-=30;
	curtop-=120;
	
	document.getElementById("newspreview").style.left=curleft+'px';
	document.getElementById("newspreview").style.top=curtop+'px';
	document.getElementById("newspreview").style.display='block';
	
	document.getElementById('newsprevcont').innerHTML="<div style='height:150px;' class='textfont'><img src='"+web_lite+"/images/loading_blu.gif' />&nbsp;Creating Preview...</div>";
	
	
	var url = dir_point+"ajxfiles/formatEditorText.php";
	var params = "editorText="+escape(encodeURI(editorText));
	
	previewObj=createRequestObject();	
	previewObj.open("POST", url, true);
	previewObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	previewObj.setRequestHeader("Content-length", params.length);
	previewObj.setRequestHeader("Connection", "close");
	previewObj.onreadystatechange =showPreviewResult;
	previewObj.send(params);
}
function showPreviewResult()
{
try
   { 
   	if(previewObj.readyState == 4)
	{
	    try{
		  if(previewObj.status == 200)
		   {
		   
		   	document.getElementById("newsprevcont").innerHTML=previewObj.responseText;
		   	document.getElementById("newsprevtitle").innerHTML=document.frmNews.newstitle.value;
		   	resScripts=document.getElementById("newsprevcont").getElementsByTagName("script");
		   	for(i=0;i<resScripts.length;i++)
		   	{
		   		eval(resScripts[i].innerHTML);
		   	}
		   	
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function fade_in(fadeid,next,limit)
{
	if(next < limit)
	{
		document.getElementById(fadeid).style.opacity = next;
		iestart=next*100;
		document.getElementById(fadeid).style.filter = 'alpha(opacity='+iestart+')';
		next=next+0.1;
		setTimeout((function(){fade_in(fadeid,next,limit);}), 100);
	}
}
function fade_out(fadeid,next,limit)
{
	if(next > limit)
	{
		document.getElementById(fadeid).style.opacity = next;
		iestart=next*100;
		document.getElementById(fadeid).style.filter = 'alpha(opacity='+iestart+')';
		next=next-0.1;
		setTimeout((function(){fade_out(fadeid,next,limit);}), 100);
	}
	else
	{
		document.getElementById(fadeid).style.height='0px';	
		document.getElementById(fadeid).style.width='0px';
		document.getElementById(fadeid).style.display='none';	
	}
}
function prep_pop()
{
	obj=document.getElementById("mainBody");
	var curwidth = curheight = 0;
	curwidth = obj.offsetWidth;
	curheight = obj.offsetHeight;
	document.getElementById("fadeDiv").style.height=curheight+'px';	
	document.getElementById("fadeDiv").style.width='900px';
	document.getElementById("fadeDiv").style.display='block';
	obj=obj.getElementsByTagName("div").item(0);
	var curtop = curleft  = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	document.getElementById("fadeDiv").style.left=curleft+'px';
	document.getElementById("fadeDiv").style.top=curtop+'px';
	
	fade_in("fadeDiv",0,0.7);
	
}
function showVideoSelector(obj)
{
	prep_pop();
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	curleft-=30;
	curtop-=120;
	
	document.getElementById("videoselect").style.left=curleft+'px';
	document.getElementById("videoselect").style.top=curtop+'px';
	document.getElementById("videoselect").style.display='block';
	
}
function wysiwyg_callmyalbum(obj)
{
	obj.className="current";
	document.getElementById("wysiwyg_myalbumcontent").style.display='block';
	document.getElementById("wysiwyg_uploadimg").className="";
	document.getElementById("wysiwyg_uploadcontent").style.display='none';
	document.getElementById("wysiwyg_urlpathimg").className="";
	document.getElementById("wysiwyg_urlpathcontent").style.display='none';
}
function wysiwyg_callupload(obj)
{
	obj.className="current";
	document.getElementById("wysiwyg_uploadcontent").style.display='block';
	document.getElementById("wysiwyg_myalbum").className="";
	document.getElementById("wysiwyg_myalbumcontent").style.display='none';
	document.getElementById("wysiwyg_urlpathimg").className="";
	document.getElementById("wysiwyg_urlpathcontent").style.display='none';
}
function wysiwyg_callurlpath(obj)
{
	obj.className="current";
	document.getElementById("wysiwyg_urlpathcontent").style.display='block';
	document.getElementById("wysiwyg_uploadimg").className="";
	document.getElementById("wysiwyg_uploadcontent").style.display='none';
	document.getElementById("wysiwyg_myalbum").className="";
	document.getElementById("wysiwyg_myalbumcontent").style.display='none';
}
function validatephotoupload(btn)
{
	var error=0;
	if(document.uploadphotofrm.upldotherphoto.value=="")
	{		
		document.getElementById("newsuploadphotoerror").innerHTML = "* Select a image to upload!";
		error =1;
		return false;
	}
	if(document.uploadphotofrm.upldotherphoto.value!="")
	{
		 var filename =document.uploadphotofrm.upldotherphoto.value;
		 var pos=document.uploadphotofrm.upldotherphoto.value.lastIndexOf('.');
		 var format= filename.substr(pos+1,filename.length);
		  format=format.toLowerCase();
		
		 if (format!="gif" && format!="jpg" && format!="bmp" && format!="jpeg" && format!="png")
		 { 
		 	document.getElementById("newsuploadphotoerror").innerHTML = "* File Format not supported!";
			error =1;
			return false;
		 }
	}
	if(error==0)
	{
		document.getElementById('uploadphotofrm').target = 'upload_targetphoto';
		document.getElementById('upload_targetphoto').onload=function() {
			getUploaded_PhotoFile();
		}
		document.getElementById('photouploadresult').innerHTML="<div style='height:150px;' class='textfont'><img src='"+web_lite+"/images/loading_blu.gif' />&nbsp;Uploading File...</div>";	
		btn.disabled=true;
		document.uploadphotofrm.submit();
	}
}
function getUploaded_PhotoFile()
{
	document.uploadphotofrm.upldotherphoto.value="";
	document.uploadphotofrm.subbtn.disabled=false;
	document.uploadphotofrm.caption.value="";
	document.getElementById("photouploadresult").innerHTML="<div style='height:150px;' class='textfont'><img src='"+web_lite+"/images/loading_blu.gif' />&nbsp;File Uploaded & Loading File...</div>";	
	chkStatusObj=createRequestObject();
	url=dir_point+"ajxfiles/chkUploadedNewsFile.php?";
	//alert(url);
	//window.open(url);
	chkStatusObj.open('get', url+"sid="+Math.random());
	chkStatusObj.onreadystatechange =displayUpPhotoFile;
	chkStatusObj.send(null);
}
function displayUpPhotoFile()
{
   try
   { 
   	if(chkStatusObj.readyState == 4)
	{
	    try{
		  if(chkStatusObj.status == 200)
		   {
		   	document.getElementById("photouploadresult").innerHTML=chkStatusObj.responseText;
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function validateuploadphotourlpath(btn)
{
	var error=0;
	if(document.uploadphotourlpathfrm.uploadphotourlpath.value=="")
	{
		document.getElementById("newsurlpathphotoerror").innerHTML = "* Enter Image Url/Path!";
		error =1;
		return false;
	}
	if(document.uploadphotourlpathfrm.uploadphotourlpath.value!="")
	{
		 var filename =document.uploadphotourlpathfrm.uploadphotourlpath.value;
		 var pos=document.uploadphotourlpathfrm.uploadphotourlpath.value.lastIndexOf('.');
		 var format= filename.substr(pos+1,filename.length);
		  format=format.toLowerCase();
		
		 if (format!="gif" && format!="jpg" && format!="bmp" && format!="jpeg" && format!="png")
		 { 
		 	document.getElementById("newsurlpathphotoerror").innerHTML = "* File Format not supported!";
			error =1;
			return false;
		 }
	}
	if(error==0)
	{
		document.getElementById('photourlresult').innerHTML="<div style='height:150px;' class='textfont'><img src='"+web_lite+"/images/loading_blu.gif' />&nbsp;Uploading File...</div>";	
		btn.disabled=true;
		
		media_file_type=document.uploadphotourlpathfrm.media_file_type.value;
		upload_file_type=document.uploadphotourlpathfrm.upload_file_type.value;
		photourl=document.uploadphotourlpathfrm.uploadphotourlpath.value;
		caption=document.uploadphotourlpathfrm.caption.value;
		communityid=document.uploadphotourlpathfrm.community_id.value;
		
		
		var url = dir_point+"NewsUploads.php";
		var params = "photourl="+escape(encodeURI(photourl))+"&media_file_type="+escape(encodeURI(media_file_type))+"&upload_file_type="+escape(encodeURI(upload_file_type))+"&caption="+escape(encodeURI(caption))+"&communityid="+escape(encodeURI(communityid));
		
		uploadUrlObj=createRequestObject();	
		uploadUrlObj.open("POST", url, true);
		uploadUrlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		uploadUrlObj.setRequestHeader("Content-length", params.length);
		uploadUrlObj.setRequestHeader("Connection", "close");
		uploadUrlObj.onreadystatechange =showPhotoUrlResult;
		uploadUrlObj.send(params);
	}
}
function showPhotoUrlResult()
{
try
   { 
   	if(uploadUrlObj.readyState == 4)
	{
	    try{
		  if(uploadUrlObj.status == 200)
		   {
		   	document.getElementById("photourlresult").innerHTML=uploadUrlObj.responseText;
		   	document.uploadphotourlpathfrm.uploadphotourlpath.value="";
		   	document.uploadphotourlpathfrm.caption.value="";
			document.uploadphotourlpathfrm.subbtn.disabled=false;
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function showPhotoSelector(obj)
{
	prep_pop();
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	curleft-=130;
	curtop-=120;
	
	document.getElementById("photoselect").style.left=curleft+'px';
	document.getElementById("photoselect").style.top=curtop+'px';
	document.getElementById("photoselect").style.display='block';
}
function createnewscheck()
{
	var errormsg=0;
	
	var newstitle=document.frmNews.newstitle.value;
	var newsmsg=document.getElementById("wysiwygnewstext").contentWindow.document.body.innerHTML;
		
	if(newstitle=="")
	{
		document.getElementById("newserrormsg").innerHTML="* Title cannot be empty!";
		newstitle.focus();
		errormsg=1;
		return false;
	}
	if(newstitle!="")
	{
		if(newstitle.length < 3)
		{
			document.getElementById("newserrormsg").innerHTML = "* Title Length must be less atleast 3 letters.";
			newstitle.focus();
			errormsg=1;
			return false;
		}
		if(newstitle.length > 180)
		{
			document.getElementById("newserrormsg").innerHTML = "* Title Length must be less than 180 letters.";
			newstitle.focus();
			errormsg=1;
			return false;
		}
		else if(newstitle.length <=180)
		{
			var unameRegExp = /^[\w\-\.\@\$\?\s*]+$/;
			if(!newstitle.match(unameRegExp))
			{
				document.getElementById("newserrormsg").innerHTML = "* Only Letters,Numbers,Underscore,Hyphen and a single dot (.) and space are allowed.";
				newstitle.focus();
				errormsg =1;
				return false;
			}
		}
	}
	if(newsmsg=="" || newsmsg=="Type here")
	{
		document.getElementById("newserrormsg").innerHTML="* Message cannot be empty!";
		newsmsg.focus();
		errormsg=1;
		return false;
	}
	if(newsmsg!="")
	{
		if(newsmsg.length < 3)
		{
			document.getElementById("newserrormsg").innerHTML = "* Message Length must be atleast 3 letters.";
			newsmsg.focus();
			errormsg=1;
			return false;
		}
	}
	
	if(errormsg==0)
	{
		updateTextArea('newstext');
		document.frmNews.submit();
	}
}
function _selectAll(obj)
{
	checks=document.getElementById("inmessages").getElementsByTagName("input");
	for(j=0;j<checks.length;j++)
	{
		if(obj.checked==false)
			checks[j].checked=false;
		else
			checks[j].checked=true;
	}
}
function deleteNews(news_id)
{
	document.delForm.newsid.value=news_id;
	document.delForm.delType.value="news";
	document.delForm.submit();
}
function delSel()
{
	checks=document.getElementById("inmessages").getElementsByTagName("input");
	deleteForm=document.grdelForm;
	k=0;
	var newsIds = new Array();
	for(j=0;j<checks.length;j++)
	{
		if(checks[j].checked==true)
		{
			newsIds[k]=checks[j].value;
			k++;
		}
	}
	deleteForm.grnewsid.value=newsIds;	
	deleteForm.grdelType.value="news";
	deleteForm.submit();
}
