/*
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;
	var desc=tinyMCE.get('description_'+topicId).getContent();
	
	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, profile) {
	
	if(confirm("Do you really want to delete the friend?"))
	{
		window.location.href="deleteFriends.php?friendId="+friendId+"&pg="+pg+"&profile="+profile;
	}
}

/*
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();
	alert(desc);
	if(desc=="")
	{
		alert("Please enter some text.");
		return false;	
	}
	   
	
	var url = "ajax/updateTopicDescription.php";
	var params = "topicId="+topicId+"&desc="+desc;
alert(params);
	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;
	}
} 
/* start tooltip.js */

var tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 300;
	var speed = 10;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				t = document.createElement('div');
				t.setAttribute('id',id + 'top');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(t);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			tt.style.width = w ? w + 'px' : 'auto';
			if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (u - h) + 'px';
			tt.style.left = (l + left) + 'px';
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();

/* end tooltip.js */

/* start menu-drop-down.js */

var open_menu;
var current_timeout = new Array();

function showMenu(id1)
{
  if($(id1))
  {
    if($(id1).style.display == 'none')
    {
      if($(open_menu)) { hideMenu($(open_menu)); }
      $(id1).style.display='inline';
      startMenuTimeout($(id1));
      $(id1).addEvent('mouseover', function(e) { killMenuTimeout(this); });
      $(id1).addEvent('mouseout', function(e) { startMenuTimeout(this); });
      open_menu = id1;
    }
  }
}

function killMenuTimeout(divEl)
{
  clearTimeout(current_timeout[divEl.id]);
  current_timeout[divEl.id] = '';
}

function startMenuTimeout(divEl)
{
  if(current_timeout[divEl.id] == '') {
    current_timeout[divEl.id] = setTimeout(function() { hideMenu(divEl); }, 1000);
  }
}

function hideMenu(divEl)
{
  divEl.style.display = 'none'; 
  current_timeout[divEl.id] = '';
  divEl.removeEvent('mouseover', function(e) { killMenuTimeout(this); });
  divEl.removeEvent('mouseout', function(e) { startMenuTimeout(this); });
}

function SwapOut(id1) {
  $(id1).src = Rollarrow1.src;
  return true;
}

function SwapBack(id1) {
  $(id1).src = Rollarrow0.src;
  return true;
}

/* End menu-drop-down.js */

