var xmlHttp;
var siteURL			= "http://www.path10.com";
var photoSiteURL	= "http://photos.path10.com";
var videoSiteURL	= "http://videos.path10.com";
var musicSiteURL	= "http://music.path10.com";
var blogsSiteURL	= "http://blogs.path10.com";
var favoriteSiteURL	= "http://favorites.path10.com";
var forumsSiteURL	= "http://forums.path10.com";

//function to check profileId already exists or not

function strtrim(inputString) 
{
	if (typeof inputString != "string") 
	{
		return inputString; 
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") 
	{
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ")
	{
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1)
	{
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; 
}

function check(senderId,receiverId,messageId,idMessage)
{
	if(strtrim(document.getElementById('idMessage').value) == '')
	{
		alert('Please write message!!');
		document.getElementById('idMessage').value	=	strtrim(document.getElementById('idMessage').value);
		document.getElementById('idMessage').focus();
		return false;
	}
	else if(strtrim(document.getElementById('idMessage').value) != '' && strtrim(document.getElementById('idMessage').value.length) > 3500)
	{
		alert('Please write your message below 3500 characters!!');
		document.getElementById('idMessage').value	=	strtrim(document.getElementById('idMessage').value);
		document.getElementById('idMessage').focus();
		return false;
	}
	else
	{
		var url =	siteURL+"/member/send-reply-message.php?senderId="+senderId+"&receiverId="+receiverId+"&messageId="+messageId+"&action="+idMessage;
		commonFunc1(url,'','showReplyForm'+messageId);
	}
}


//function to change caption of member uploaded photos
function editPhotoCaption(albumId,photoId,action)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	var url =	siteURL+"/member/photos/edit-photo-caption.php?albumId="+albumId+"&photoId="+photoId+"&action="+action;
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChanged2(photoId);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged2(photoId)
{
	document.getElementById("editProfile"+photoId).innerHTML = xmlHttp.responseText;
}


function updateCaption(albumId,photoId,photoName,coverPhoto)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	if(coverPhoto)
	{
		coverPhoto	=	1;
	}
	else
	{
		coverPhoto	=	0;
	}
	var url =	siteURL+"/member/photos/edit-photo-caption.php?albumId="+albumId+"&photoId="+photoId+"&photoName="+photoName+"&coverPhoto="+coverPhoto;
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChanged3(photoId);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged3(photoId)
{
	document.getElementById("editProfile"+photoId).innerHTML = xmlHttp.responseText;
}


function updateAlbumCaption(albumId,albumName)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	var url =	siteURL+"/member/photos/edit-album-caption.php?albumId="+albumId+"&albumName="+albumName;
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChanged5(albumId,albumName);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged5(albumId,albumName)
{
	document.getElementById("reflectTitle"+albumId).innerHTML = albumName;
	document.getElementById("editAlbum"+albumId).innerHTML = xmlHttp.responseText;
}


function textCounter(field,countfield,maxlimit)
{
	if(field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else
	{
		countfield.value = maxlimit - field.value.length;
	}
}


function editFriend(friendId,action,friendType)
{
	var text = "";
	xmlHttp  = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	if(friendType)
	{
		var a_friend	= friendType.split("|");
		var	friendType	= a_friend[0];
		text			= a_friend[1];
	}
	var url =	siteURL+"/member/edit-friend.php?friendId="+friendId+"&action="+action+"&friendType="+friendType;
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChanged9(friendId,text);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged9(friendId,text)
{
	if(text)
	{
		document.getElementById("reflectType"+friendId).innerHTML = "Friend Type: "+text;
	}
	document.getElementById("showEditForm"+friendId).innerHTML = xmlHttp.responseText;
}

function checkFriend(friendId,memberId)
{	
	var friendType	=	"";
	var friend		=	document.getElementsByName('friend');
	for(i=0;i<friend.length;i++)
	{
		if(friend[i].checked)
		{
			friendType = friend[i].value;
			break;
		}
	}
	if(friendType)
	{
		editFriend(friendId,memberId,friendType);
	}
}

function deleteComment(commentId,resourceType)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	var confirmation = window.confirm("Are you sure to delete this comment?");
	if(confirmation == true)
	{
		var url="";
		if(resourceType == 1)
		{
			url  = videoSiteURL;
		}
		else if(resourceType == 2)
		{
			url  = musicSiteURL;
		}
		else if(resourceType == 4)
		{
			url  = blogsSiteURL;
		}
		else if(resourceType == 5)
		{
			url  = favoriteSiteURL;
		}
		else if(resourceType == 6)
		{
			url  = forumsSiteURL;
		}
		else
		{
			url	=	photoSiteURL;
		}
		url =	url+"/delete-comment.php?commentId="+commentId+"&resourceType="+resourceType;
		xmlHttp.onreadystatechange = function()
		{
			if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
			{
				stateChanged10(commentId);
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function stateChanged10(commentId)
{
	document.getElementById("commentMsg"+commentId).innerHTML = xmlHttp.responseText;
}

function doLogin(registrationId,memberPassword,receiverId,msg)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	if(msg)
	{
		msg = "&msg=1";
	}
	else
	{
		msg = ""
	}
	url =	siteURL + "/member/add-friend.php?registrationId="+registrationId+"&memberPassword="+memberPassword+"&receiverId="+receiverId+msg;
	
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			loginChange();
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function loginChange()
{
	var t = document.getElementById('showForm');
	t.innerHTML = xmlHttp.responseText;
	t.style.display = "block";
}

function commonFunc(url,divId)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChanged(divId);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged(divId)
{
	var t = document.getElementById(divId);
	t.innerHTML = xmlHttp.responseText;
	AssignPosition(t);
	t.style.display = "block";
}

function commonFunc1(url,extra,divId,securityCode)
{
	/*if(extra == "" || extra == "Enter Your Text Message")
	{
		alert("Please fill the textbox!!");
		return false;
	}*/
	if(extra != "")
	{
		url += extra;
	}
	if(securityCode)
	{
		url += "&securityCode="+securityCode;
	}
	//alert(url);
	//return false;

	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChange(divId);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChange(divId)
{
	var t = document.getElementById(divId);
	t.innerHTML = xmlHttp.responseText;
}


function commonFunc2(url,extra,divId,divId2)
{
	if(extra != "")
	{
		url += extra;
	}
	
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChangeCat(divId,divId2);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChangeCat(divId,divId2)
{
	var t = document.getElementById(divId);
	t.innerHTML = xmlHttp.responseText;
	if(divId2)
	{
		var t2 = document.getElementById(divId2);
		t2.innerHTML = "";
	}
}



function track_resource(id,resourceType)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	if(resourceType == 1)
	{
		url  = videoSiteURL;
	}
	else if(resourceType == 2)
	{
		url  = musicSiteURL;
	}
	else if(resourceType == 4)
	{
		url  = blogsSiteURL;
	}
	else if(resourceType == 5)
	{
		url  = favoriteSiteURL;
	}
	else if(resourceType == 6)
	{
		url  = forumsSiteURL;
	}
	else
	{
		url	=	photoSiteURL;
	}
	url =	url+"/update-view-points.php?id="+id;
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function setHomePagePoint(url)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	url =	url+"/home-page-point.php";
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function readMessage(messageId,url)
{
	var xmlHttp = GetXmlHttpObject();
	if(xmlHttp	==	null)
	{
		alert("Brwser does not support");
		return;
	}
	
	url	=	url+"/read-message.php?messageId="+messageId;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function sendCardMessage(url,message,cardId,divId)
{
	url = url+"&msg="+message+"&cardId="+cardId;
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP REQUEST");
		return;
	}
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 || xmlHttp.readyState =='complete')
		{
			stateChange(divId);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChange(divId)
{
	var t = document.getElementById(divId);
	t.innerHTML = xmlHttp.responseText;
}

function GetXmlHttpObject()
{
	var xmlHttp = null;
	try{
		xmlHttp = new XMLHttpRequest();
		}
	catch(e)
	{
	try
		{
			xmlHttp = new ActiveXObject("MSxml2.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}