/*
File name = common.js
Site name = www.punjabizm.com
Author =  Amrinder Singh
Desc = Common javascript code to be used more often in the site
*/
var xmlHttp;

function createXMLHttpRequest(){	
    if(window.ActiveXObject){
    	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
    	xmlHttp = new XMLHttpRequest();
    }	
}

/*
Function name = checkMaxInput
Description = To check the limit on the input box's characters being input
*/
function checkMaxInput(id,length) { 
	var eleId=id;
	maxLen = length; // max number of characters allowed in the textbox
	if (document.getElementById(eleId).value.length > maxLen) // if too long.... trim it!
	document.getElementById(eleId).value = document.getElementById(eleId).value.substring(0, maxLen);
}
/*
Function name = MaxTextarea
Description = To check the limit on the input box's characters being input
*/
function MaxTextarea(id,rId,length) { 
	var eleId=id;
	var remId=rId;
	maxLen = length; // max number of characters allowed in the textbox
	var el=document.getElementById('description').value;
	alert(el);
	/*
	if (document.getElementById(eleId).value.length > maxLen) // if too long.... trim it!
	document.getElementById(eleId).value = document.getElementById(eleId).value.substring(0, maxLen);
	else document.getElementById(remId).innerHTML = maxLen - document.getElementById(eleId).value.length; */
}
/*
Function name = MaxTextarea
Description = To  the limit on the input box's characters being input
*/
function checkMaxTextarea(id,rId,length) { 
	var eleId=id;
	var remId=rId;
	maxLen = length; // max number of characters allowed in the textbox
	var el=document.getElementById(eleId).value;
	
	if (document.getElementById(eleId).value.length > maxLen) // if too long.... trim it!
	document.getElementById(eleId).value = document.getElementById(eleId).value.substring(0, maxLen);
	else document.getElementById(remId).innerHTML = maxLen - document.getElementById(eleId).value.length;
}

/*
Function Name = deleteMessage
Description = To delete the message in inbox and sent messages folders
*/
function deleteMessage(msgId, memId, type) {
	
	var msgType=type;
	
	if(confirm("Do you really want to delete this message?"))
	{
		window.location.href="deleteMessage.php?msgId="+msgId+"&memId="+memId+"&msgType="+msgType;
	}
}

/*
Function Name = addBookmark
Description = To add the page as a bookmark.
*/
function addBookmark(url,title) { 
if (window.sidebar) { 
window.sidebar.addPanel(title, url,""); 
} else if( document.all ) { 
window.external.AddFavorite( url, title); 
} else if( window.opera && window.print ) { 
return true;
} else if( document.layers ) {
window.alert( 'Please click OK and then press Ctrl+D to create a bookmark or \n click Bookmarks | Add Bookmark' );
} else {
window.alert( 'Please use your browser\'s bookmarking facility to create a bookmark' );
}
}


/*
Function Name = delGalleryImage
Description = To delete the orkut code from backend
*/
function delGalleryOrkutCode(imgId, delpass) {
	
	if(confirm("Do you really want to delete this code?"))
	{
		window.location.href="deleteGalleryImage.php?del=code&imgId="+imgId+delpass;
	}
}


/*
Function Name = deleteGalleryImage
Description = To delete the orkut code images from backend
*/
function deleteGalleryImage(imgId) {
	
	if(confirm("Do you really want to delete this image?"))
	{
		window.location.href="deleteGalleryImage.php?del=img&imgId="+imgId;
	}
}

/*
Function Name = updateCategory
Description =  To update the category of an image in orkut code section
*/
function updateCategory(imgId,catId)
{
	var categoryId=catId;
	var imageId=imgId;

	createXMLHttpRequest();		
	xmlHttp.open("GET", "../ajax/updateOrkutCodeCat.php?imageId="+imageId+"&categoryId="+categoryId, true);	
	xmlHttp.onreadystatechange = stateChangeUpdateCategory;
	xmlHttp.send(null);
}
function stateChangeUpdateCategory()
{
    if(xmlHttp.readyState==4){     
        var result=xmlHttp.responseText;
		document.getElementById('orkutCodeCategory').value=result;
    }//end if readystate==4
}
/*
Function Name = deletePbGalleryImage
Description = To delete the Punjab Gallery images from backend
*/
function deletePbGalleryImage(imgId)
{
	if (confirm('Delete this image?')) {
		window.location.href = 'deletePbGalleryImage.php?action=delImg&type=punjab&imgId=' + imgId;
	}
}
/*
Function Name = deleteDistGalleryImage
Description = To delete the Punjab Gallery images from backend
*/
function deleteDistGalleryImage(imgId)
{
	if (confirm('Delete this image?')) {
		window.location.href = 'deletePbGalleryImage.php?action=delImg&type=district&imgId=' + imgId;
	}
}

/* 
Function Name = updateForumModeratorStatus
Desctiption = To update the member's forum moderator status to yes or no 
*/
function updateForumModeratorStatus(id,memId)
{
	createXMLHttpRequest();		
	if(document.getElementById(id).checked==true)
	{
		xmlHttp.open("GET", "../ajax/updateForumModStatus.php?uid="+memId+"&val=y", true);	
		xmlHttp.onreadystatechange = handleStateUpdateForumModStatus;
		xmlHttp.send(null);
	}
	else
	{
		xmlHttp.open("GET", "../ajax/updateForumModStatus.php?uid="+memId+"&val=n", true);	
		xmlHttp.onreadystatechange = handleStateUpdateForumModStatus;
		xmlHttp.send(null);
	}
}

function handleStateUpdateForumModStatus()
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		alert(xmlHttp.responseText);
	}
}

/* 
Function Name = delTopic
Desctiption = To delete the topic in the community
*/
function delTopic(topicId)
{
	if (confirm('Delete this topic?')) {
		window.location.href = "deleteTopic.php?topicId="+topicId;
	}
}

/*
Function Name = approveOrkutCode
Desription = To approve the orkut code 
*/
function approveOrkutCode(codeId)
{
	var codeId=codeId;
	
	if(document.getElementById('approved_'+codeId).checked==true)
	{
		var status='y';
	}
	else
	{
		var status='n';	
	}
	createXMLHttpRequest();		
	xmlHttp.open("GET", "../ajax/approveOrkutCode.php?codeId="+codeId+"&status="+status, true);	
	xmlHttp.onreadystatechange = stateChangeApproveOrkutCode;
	xmlHttp.send(null);
}
function stateChangeApproveOrkutCode()
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		alert('Success..!!');
	}
}


/*
Function Name = deleteFriend
Description = To delete the Friend from the friend list
*/
function deleteFriend(friendId, pg) {
	
	if(confirm("Do you really want to delete the friend?"))
	{
		window.location.href="deleteFriends.php?friendId="+friendId+"&pg="+pg;
	}
}

/*
Function Name = deleteIgnoredUser
Description = To delete the user from the ignore list
*/
function delUserIgnored(ignoredUserId, pg) {
	if(confirm("Do you really want to delete the User from the list?"))
	{
		window.location.href="deleteIgnoredUser.php?ignoredUserId="+ignoredUserId+"&pg="+pg;
	}
}


/* 
Function Name = editTopic
Desctiption = To edit the topic in the community
*/
function editTopic(topicId)
{
	document.getElementById('desc_'+topicId).style.display="none";
	document.getElementById('editDesc_'+topicId).style.display="";
}
/* 
Function Name = cancelEditDesc
Desctiption = To Cancel the editing of topic in the community
*/
function cancelEditDesc(topicId)
{
	document.getElementById('desc_'+topicId).style.display="";
	document.getElementById('editDesc_'+topicId).style.display="none";
}

/* 
Function Name = submitEditedDesc
Desctiption = Submit the editing of topic in the community
*/

function submitEditedDesc(topicId)
{
	createXMLHttpRequest();		

	//var desc=document.getElementById('description_'+topicId).value;
	var desc=tinyMCE.get('description_'+topicId).getContent();
	if(desc=="")
	{
		alert("Please enter some text.");
		return false;	
	}
	   
	
	var url = "ajax/updateTopicDescription.php";
	var params = "topicId="+topicId+"&desc="+desc;

	xmlHttp.open("POST", url, true);
	
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
		if(xmlHttp.readyState != 4) {
			document.getElementById('desc_'+topicId).style.display="";
			document.getElementById('editDesc_'+topicId).style.display="none";
			document.getElementById('desc_'+topicId).innerHTML="<img src='images/ajax_loader.gif'>";
		}
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			//alert(xmlHttp.responseText);
			document.getElementById('desc_'+topicId).style.display="";
			document.getElementById('desc_'+topicId).innerHTML=xmlHttp.responseText;
			document.getElementById('editDesc_'+topicId).style.display="none";
		}
	}
	xmlHttp.send(params);

}


/* 
Function Name = validateEmail
Desctiption = Function to validate the email
*/
function validateEmail(addr)
{
	if(addr=="")
	{
		return false;
	}

	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|#';
	for (i=0; i<invalidChars.length; i++)
	{
		if (addr.indexOf(invalidChars.charAt(i),0) > -1)
		{
			return false;
		}
	}
	var atPos = addr.indexOf('@',0);
	if (atPos == -1)
	{
		return false;
	}
	if (atPos == 0)
	{
		return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1)
	{
		return false;
	}
	if (addr.indexOf ('.', atPos) == -1)
	{
		return false;
	}
	if (addr.indexOf('@.',0) != -1)
	{
		return false;
	}
	if (addr.indexOf('.@',0) != -1)
	{
		return false;
	}
	if (addr.indexOf('..',0) != -1)
	{
		return false;
	}
	var suffix = addr.substring(addr.lastIndexOf ('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum' && suffix != 'in')
	{
		return false;
	}
	return true;
}


/* 
Function Name = validateInviteEmail
Desctiption = Function to valiate the invitation email
*/
function validateInviteEmail()
{
	var email = document.getElementById('inviteEmail').value;	
	if(validateEmail(email)==false)
	{
		alert("Please enter a valid email address");
		document.getElementById('inviteEmail').focus();	
		return false;
	}
	return true;
}

/* 
Function Name = ignoreuser
Desctiption = Function to ignore a user
*/
function ignoreUser(memId)
{
	if(confirm("Do you really want to add the User to the ignore list?"))
	{
		window.location.href="ignoreUser.php?memId="+memId;
	}
} 