var mod_content_id;
var new_caption;
var captionObj;
var newcaptiondiv;
var profilephotochange;
var nextDisplayObj;
var msgDisplayObj;
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 showUploadImage()
{
	if(document.getElementById('genphotomediaup').style.display=='none')
	{
		document.getElementById('genphotomediaup').style.display='block';
	}
	else
	{
		document.getElementById('genphotomediaup').style.display='none';
	}
}
function showUploadVideo()
{
	if(document.getElementById('genvideomediaup').style.display=='none')
	{
		document.getElementById('genvideomediaup').style.display='block';
	}
	else
	{
		document.getElementById('genvideomediaup').style.display='none';
	}
}

function editCaption()
{

	var msgerror=0;
	
	captionObj=createRequestObject();
	new_caption=document.getElementById("capt"+mod_content_id).value;
	
	if(new_caption.length>30)
	{
		document.getElementById("genvideomsg").innerHTML = "* Caption length must be less than 30 characters!";
		msgerror=1;
		return false;
	}
	
	if(msgerror==0)
	{		
		document.getElementById("genvideomsg").innerHTML = "";
		url=dir_point+"ajxfiles/editMediaVideoCaption.php?video_id="+mod_content_id+"&newcaption="+new_caption;
		captionObj.open('get', url+"&sid="+Math.random());
		captionObj.onreadystatechange =displayavail;
		captionObj.send(null);
	}
}
function displayavail()
{
   try
   { 
   	if(captionObj.readyState == 4)
	{
	    try{
		  if(captionObj.status == 200)
		   {
		   	if(captionObj.responseText=="yes")
		   	{
		   		wrappedcaption=new_caption.substr(0,20)+"<br>"+new_caption.substr(20,10);
		   		document.getElementById(newcaptiondiv).innerHTML=wrappedcaption;
		   		document.getElementById(newcaptiondiv).parentNode.getElementsByTagName("div").item(0).innerHTML=new_caption;
		   	}
		   	elseif(captionObj.responseText=="capterror")
		   	{
		   		document.getElementById("genvideomsg").innerHTML = "* Caption length must be less than 30 characters!";
		   	}
		   		
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function cancelCaption()
{
	document.getElementById(newcaptiondiv).innerHTML=captionnow;
}
function editPhCaption()
{

	var msgerror=0;
	
	phcaptionObj=createRequestObject();
	new_caption=document.getElementById("phcapt"+mod_content_id).value;
	
	if(new_caption.length>30)
	{
		document.getElementById("genphotomsg").innerHTML = "* Caption length must be less than 30 characters!";
		msgerror=1;
		return false;
	}
	
	if(msgerror==0)
	{		
		document.getElementById("genphotomsg").innerHTML = "";
		url=dir_point+"ajxfiles/editMediaPhotoCaption.php?photo_id="+mod_content_id+"&newcaption="+new_caption;
		phcaptionObj.open('get', url+"&sid="+Math.random());
		phcaptionObj.onreadystatechange =displayphavail;
		phcaptionObj.send(null);
	}
}
function displayphavail()
{
   try
   { 
   	if(phcaptionObj.readyState == 4)
	{
	    try{
		  if(phcaptionObj.status == 200)
		   {
		   	if(phcaptionObj.responseText=="yes")
		   	{
		   		wrappedcaption=new_caption.substr(0,20)+"<br>"+new_caption.substr(20,10);
		   		document.getElementById(newcaptiondiv).innerHTML=wrappedcaption;
		   		document.getElementById(newcaptiondiv).parentNode.getElementsByTagName("div").item(0).innerHTML=new_caption;
		   	}
		   	elseif(phcaptionObj.responseText=="capterror")
		   	{
		   		document.getElementById("genphotomsg").innerHTML = "* Caption length must be less than 30 characters!";
		   	}
		   		
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function cancelPhCaption()
{
	document.getElementById(newcaptiondiv).innerHTML=captionnow;
}
var optionObj;
var curfile;
function validateother()
{
	var error=0;
	if(document.uploadotherfrm.uploadvideo.value==""  && document.uploadotherfrm.videourl.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;
		 }
	}
	if(document.uploadotherfrm.videourl.value!="")
	{
		 var videourldetails =document.uploadotherfrm.videourl.value;
		 var pos1=document.uploadotherfrm.videourl.value.indexOf('.');
		 var pos2=document.uploadotherfrm.videourl.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("genvideomediauperr").innerHTML = "* Only Youtube/Google Video URL allowed.";
			error =1;
			return false;
		 }
	}
	if(document.uploadotherfrm.uploadvideo.value!="" && document.uploadotherfrm.videourl.value!="")
	{
		document.getElementById("genvideomediauperr").innerHTML = "* Either SELECT a video file or ENTER a video URL.";
		error =1;
		return false;
	}
	
	if(error==0)
	{
		document.getElementById("modbusyvid").innerHTML="<img src='"+web_lite+"/images/loading_blu.gif'>&nbsp;Uploading Video...Please Wait.";
		document.uploadotherfrm.submit();
	}
}
function deletePhoto(photoID,up_type,photo_owner)
{
	document.deletephotofrm.deletephotoid.value=photoID;
	document.deletephotofrm.upload_type.value=up_type;
	document.deletephotofrm.photo_owner.value=photo_owner;
	document.deletephotofrm.submit();
}
function modifyCaption(contentID,curdiv,width)
{
	mod_content_id=contentID;
	newcaptiondiv=curdiv;
		
	captionnow=document.getElementById(curdiv).parentNode.getElementsByTagName("div").item(0).innerHTML;
	document.getElementById(curdiv).innerHTML="";
	
	currentinput = document.createElement("input");
	currentinput.setAttribute("type", "text");
	currentinput.setAttribute("id", "capt"+mod_content_id);
	currentinput.setAttribute("value", captionnow);
	currentinput.setAttribute("class","captfont");
	currentinput.style.width=width;
	
	currentbutton1 = document.createElement("input");
	currentbutton1.setAttribute("type", "button");
	currentbutton1.setAttribute("value", "Save");
	currentbutton1.setAttribute("class","btnblue");
	currentbutton1.onclick=editCaption;
	
	currentbutton2 = document.createElement("input");
	currentbutton2.setAttribute("type", "button");
	currentbutton2.setAttribute("value", "Cancel");
	currentbutton2.setAttribute("class","btnblue");
	currentbutton2.onclick=cancelCaption;
	
	document.getElementById(curdiv).appendChild(currentinput);
	document.getElementById(curdiv).appendChild(currentbutton1);
	document.getElementById(curdiv).appendChild(currentbutton2);
}
function modifyPhCaption(contentID,curdiv,width)
{
	mod_content_id=contentID;
	newcaptiondiv=curdiv;
		
	captionnow=document.getElementById(curdiv).parentNode.getElementsByTagName("div").item(0).innerHTML;
	document.getElementById(curdiv).innerHTML="";
	
	currentinput = document.createElement("input");
	currentinput.setAttribute("type", "text");
	currentinput.setAttribute("id", "phcapt"+mod_content_id);
	currentinput.setAttribute("value", captionnow);
	currentinput.setAttribute("class","captfont");
	currentinput.style.width=width;
	
	currentbutton1 = document.createElement("input");
	currentbutton1.setAttribute("type", "button");
	currentbutton1.setAttribute("value", "Save");
	currentbutton1.setAttribute("class","btnblue");
	currentbutton1.onclick=editPhCaption;
	
	currentbutton2 = document.createElement("input");
	currentbutton2.setAttribute("type", "button");
	currentbutton2.setAttribute("value", "Cancel");
	currentbutton2.setAttribute("class","btnblue");
	currentbutton2.onclick=cancelPhCaption;
	
	document.getElementById(curdiv).appendChild(currentinput);
	document.getElementById(curdiv).appendChild(currentbutton1);
	document.getElementById(curdiv).appendChild(currentbutton2);
}
function validatesearch()
{
	document.searchform.submit();
}
function changePage(pageNo,media_type,media_category,dispObj,curcommid,owner)
{
	nextDisplayObj=dispObj;
	document.getElementById(nextDisplayObj).innerHTML="<div style='height:200px;'><img src='"+web_lite+"/images/loading_blu.gif' /></div>";	
	nextObj=createRequestObject();
	url=dir_point+"ajxfiles/mediaPaging.php?page="+pageNo+"&media_type="+media_type+"&media_category="+media_category+"&cur_commid="+curcommid+"&owner="+owner;
	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 deleteVideo(videoid,video_extn,upload_type,videoOwner)
{
	document.deletevideofrm.deletevideoid.value=videoid;
	document.deletevideofrm.deletevideoid_withextn.value=video_extn;
	document.deletevideofrm.upload_type.value=upload_type;
	document.deletevideofrm.videoOwner.value=videoOwner;
	document.deletevideofrm.submit();
}
function addtomyVideos(video_id,video_owner,msgdisp)
{
	var videoid=video_id;
	var cur_username=video_owner;
	msgDisplayObj=msgdisp;
	
	document.getElementById(msgDisplayObj).innerHTML="<img src='"+web_lite+"/images/loading_blu.gif' />";

	addtomylist=createRequestObject();	
	url=dir_point+"ajxfiles/addtomyVideos_media.php?videoid="+videoid+"&cur_username="+cur_username;
	addtomylist.open('get', url+"&sid="+Math.random());
	addtomylist.onreadystatechange =displaymyVideolist;
	addtomylist.send(null);
}

function changeWish()
{
	try{if(ajxObject.readyState == 4){
		if(ajxObject.status == 200){
			if(gwobj)
			{
				check=ajxObject.responseText.split("c2m_redirect:");
				if(check.length>1)
				{
					window.location.href=check[1];
					return;
				}
				else
				{
					if(ajxObject.responseText=="done")
					{
						
						document.getElementById(gwobj).innerHTML="<img src='"+web_lite+"/images/wish_saved.png' title='added to wish list' />";
						document.forms[gform].wishinput.value=1;
						//document.getElementById("addmsg").innerHTML="<img src='"+web_lite+"/images/wish_saved.png' title='added to wish list' />";
						document.getElementById("wish2id").style.display='none';
						document.getElementById("wishid").style.display='block';
					}
					else
						document.getElementById(gwobj).innerHTML="<img src='"+web_lite+"/images/wishlist.png' title='add to your wish list' />";	
				}
			}
		}else{}
	}
	}catch(e){alert(e.message);}
}


function displaymyVideolist()
{
   try
   { 
   	if(addtomylist.readyState == 4)
	{
	    try{
		  if(addtomylist.status == 200)
		   {
		   		check=addtomylist.responseText.split("cant_redirect=");
				if(check.length>1)
				{
					window.location.href=check[1];
					return;
				}
				else
				{
				   	if(addtomylist.responseText!="")
				   	{
					   	document.getElementById(msgDisplayObj).innerHTML =addtomylist.responseText;
				   	}
				   }			   		
		   }
		   else{}
		}catch(e){}
	}}
	catch(e){}
}
function validatephoto()
{
	var error=0;
	if(document.uploadphotofrm.upldotherphoto.value=="")
	{
		document.getElementById("genphotomediauperr").innerHTML = "* Select a photo 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("genphotomediauperr").innerHTML = "* File Format not supported!";
			error =1;
			return false;
		 }
	}
	if(error ==0)
	{
		document.getElementById("modbusyimg").innerHTML="<img src='"+web_lite+"/images/loading_blu.gif'>&nbsp;Uploading Image...Please Wait.";
		document.uploadphotofrm.submit();
	}
}
