// JavaScript Document

function login(formEle){
	if(formEle.u_login_id.value.length<1){
		alert('Please enter a username');
		return false
	}
	if(formEle.u_login_pass.value.length<1){
		alert('Please enter a password');
		return false
	}
	
	reg_type=$("input[name='iec_members']:checked").val();

	if(reg_type==undefined){
	  alert('Please choose a Membership');
	  return false
	}

	progress=$("input'[type=image]'#login");
	$(progress).css({visibility:'visible'});
	$.ajax({
		   type:'POST',
		   url:'common/ajax.php',
		   data: ({ action:'login', u_login_id:formEle.u_login_id.value , u_login_pass:formEle.u_login_pass.value, regtype:reg_type }),
		   success:function(response){
			   //alert(response); //return false;
			   tempA=response.split(';');
			   if(tempA[0]=='1'){
					//alert(tempA[1]);
					$(progress).css({visibility:'hidden'});
					window.location='index.php';
					//return false;
				}
				if(tempA[0]=='0'){
					alert(tempA[1]);
					$(progress).css({visibility:'hidden'});
					return false;
				}
		   }
	});
	return false;
}

function saveContent(iD,pgContent,ediEle){
	if(pgContent!=''){
		ediEle.setProgressState(1);
		$.ajax({
			   type:'POST',
			   url:'common/ajax.php',
			   data: ({ action:'page-save', id:iD , content : pgContent }),
			   success:function(response){
				   //alert(response);
				   tempA=response.split(';');
				   if(tempA[0]=='1'){
						alert('Content saved');
						ediEle.setProgressState(0);
						location.reload(true);
					}
					if(tempA[0]=='0'){
						alert('Database error Please again after sometime.');
						ediEle.setProgressState(0);
					}
			   }
		});
	}
}

function saveProfileInformation(_userID,_fieldName,_fieldValue,_ele){
	$.ajax({
		type:'POST',
		url:'common/ajax.php',
		data: ({ action:'profile-edit', userid : _userID , fieldName: _fieldName, fieldValue : _fieldValue }),
		success:function(response){ 	//alert(response);
			tempA=response.split('#');
			if(tempA[0]=='1'){
				$(_ele).children("img#progress").css({ display:'none' });
				$(_ele).children("a.save").css({ display:'none' });
				$(_ele).children("a.edit.cancel").removeClass('cancel').text('Edit');
				$(_ele).children('span#sta-cont').text(tempA[1]).css({display:'inline-block'});
				$(_ele).children('span#dyna-cont').css({display:'none'}).children("input").attr('value',tempA[1]);
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
				location.reload(true);
			}
	   }
	});
	
}

function saveProfileInformationTxt(_userID,_fieldName,_fieldValue,_ele,ediEle){
	ediEle.setProgressState(1);
	$.ajax({
		type:'POST',
		url:'common/ajax.php',
		data: ({ action:'profile-edit', userid : _userID , fieldName: _fieldName, fieldValue : _fieldValue }),
		success:function(response){ 	//alert(response);
			tempA=response.split('#');
			if(tempA[0]=='1'){
				ediEle.setProgressState(0);
				sel=$(_ele).children("a.edit").attr('rel');
				$(_ele).children("a.save").css({ display:'none' });
				$(_ele).children("a.edit").removeClass('cancel').text('Edit');
				$('div#'+sel).html(tempA[1]).css({display:'block'});
				$('div#'+sel+'-editor').css({display:'none'}).children("textarea").text(tempA[1]);
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
				location.reload(true);
			}
	   }
	});
}

function eventEdit(formObj,id){
	_queryString="action=edit-event&id="+id;
	$(formObj).find("input'[type=text]', input'[type=hidden]'").each(function(){
		if($(this).hasClass('reqd')){
			if($(this).attr('value')==''){ alert("Please enter event "+$(this).attr('name').toUpperCase() ); return; }
		}
		_queryString +="&"+$(this).attr('name')+"="+$(this).attr('value');
	});
	//_queryString="action=edit-event"+_queryString;
	//alert(_queryString);
	$.ajax({
		type:'POST',
		url:'common/ajax.php',
		data: _queryString,
		success:function(response){ 	//alert(response); return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				alert('Event Updated successfully');
				location.reload(true);
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
				location.reload(true);
			}
	   }
	});
}

function eventDelete(_id){
	$.ajax({
		type:'POST',
		url:'common/ajax.php',
		data: ({action:'delete-event' , id:_id}),
		success:function(response){ 	//alert(response); return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				alert('Event Deleted successfully');
				location.reload(true);
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
				location.reload(true);
			}
	   }
	});
}

function shareUpdate(login_id,content,type,busy){
	busy.css({visibility:'visible'});
	$.ajax({
		type:'POST',
		url:'common/ajax.php',
		data: ({action:'add-update' , id:login_id, content:content,type:type }),
		dataType: 'json',
		success:function(response){ 	//alert(response); //return;
			/*alert(response.status);
			//tempA=response.split(';'); */
			/*for ( res in response){
				alert(res+": "+response[res]);
			}
			return*/
			if(response.status=='1'){
				busy.css({visibility:'hidden'});
				if(type!=2){
					$("ul.update").prepend("<li><img src='"+response['image']+"' alt='' class='imgleft' /><p><strong><a href='profile.php?id="+login_id+"'>"+response['name']+":</a></strong>&nbsp;"+content+"</p><span class='quicklinks'>"+response['post_date']+"</span><a href='#delete' rel='"+response['post_id']+"' class='delete'>Delete</a><div class='clear'></div><ul class='comments'></ul><div class='quicklinks'><a id='reply' href='javascript:void(0);'>Comment</a></div><div class='comment reply'><textarea id='textarea' name='comment'>Drop a comment..</textarea><a rel='"+response['post_id']+"' href='javascript:void(0)' id='comment' class='submit'>Comment</a><img id='busy' src='images/login-ajax-loader.gif'></div></li>");
				}
			}
			if(response.status=='0'){
				alert('Database Error please try after sometime');
			}
	   }
	});
}
/*shareUpdateComment(_login_id,_contentArea.val(),_shareBusy,_parentLI);*/
function shareUpdateComment(login_id,comment_parent_id,content,busy,parentLI){
	$.ajax({
		type:'POST',
		url:'common/ajax.php',
		data: ({action:'add-update-comment' , id:login_id, parent_id:comment_parent_id, content:content }),
		success:function(response){ 	//alert(response); return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				busy.css({display:'none'});
				$(parentLI).children("ul.comments").prepend("<li><img src='"+tempA[1]+"' alt='' class='imgleft' width='32' /><strong><a href='profile.php?id="+login_id+"'>"+tempA[2]+":</a></strong>&nbsp;"+content+"<div class='clear'></div></li>");
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
			}
	   }
	});
}

function createAlbum(login_id,album_name,album_desc,_busyObj){
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'add-album' , id:login_id , album_name:album_name , album_desc:album_desc }),
		success:function(response){ 	//alert(response); return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				_busyObj.css({visibility:'hidden'});
				if(confirm("Do you want to add images in album now?")){
					//alert(tempA[1]);
					window.location="albums-editor.php?id="+tempA[1];
				}
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
			}
	   }
	});
}

function markON(_albumId,_imageId,_obJ){
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'album-mark-dp' , album:_albumId , image:_imageId }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(_obJ).toggleClass('off').toggleClass('on');
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
			}
	   }
	});
}

function deleteComments(_obj){
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'comment-delete' , id:$(_obj).attr('rel') }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(_obj).parent('li').fadeOut('slow');
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
			}
	   }
	});
}

function deleteImage(_obj){
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'image-delete' , id:$(_obj).attr('rel') }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(_obj).parents('tr'+$(_obj).attr('href')).fadeOut('slow');
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
			}
	   }
	});
}

function sendMessage(_from,_to,_subject,_content,_type, _obj){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"15px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	
	var _toArr=new Array();	
	if(typeof(_to)=='object'){
		$(_to).each(function(){ 
			_toArr.push($(this).attr('id'));
		});
	}else if(typeof(_to)=='number'){
		_toArr.push(_to);
	}
	/*alert(_toArr.length);
	return;*/
	
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'send-message' , from:_from, to:_toArr, subject:_subject, content:_content ,type:_type}),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			$(_obj).css({visibility:'hidden'});
			if(tempA[0]=='1'){
				_notificationBox.text("Message Sent Successfully").prependTo(".message_wrraper").fadeIn(200);
				setTimeout("$(_notificationBox).fadeOut('slow');",5000);
				$.fancybox.close();
			}
			if(tempA[0]=='0'){
				_notificationBox.text("Message Was Not Send. Please try again after sometime.").prependTo(".message_wrraper").fadeIn(200);
				setTimeout("$(_notificationBox).fadeOut('slow');",5000);
				$.fancybox.close();
			}
	   }
	});
}

function replyMessage(_from,_to,_subject,_content,_type, _obj){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"15px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	
	var _toArr=new Array();	
	if(typeof(_to)=='object'){
		$(_to).each(function(){ 
			_toArr.push($(this).attr('id'));
		});
	}else if(typeof(_to)=='number'){
		_toArr.push(_to);
	}
	/*alert(_toArr.length);
	return;*/
	
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'reply-message' , from:_from, to:_toArr, subject:_subject, content:_content ,type:_type}),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			$(_obj).css({visibility:'hidden'});
			if(tempA[0]=='1'){
				_notificationBox.text("Message Sent Successfully").prependTo(".message_wrraper").fadeIn(200);
				setTimeout("$(_notificationBox).fadeOut('slow');",5000);
				$.fancybox.close();
			}
			if(tempA[0]=='0'){
				_notificationBox.text("Message Was Not Send. Please try again after sometime.").prependTo(".message_wrraper").fadeIn(200);
				setTimeout("$(_notificationBox).fadeOut('slow');",5000);
				$.fancybox.close();
			}
	   }
	});
}

function deleteMessage(_obj){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"15px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	if($(_obj).attr('rev')=="sent"){ _action="delete-sent-message"; }else{ _action="delete-message"; }
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:_action , id:$(_obj).attr('rel') }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(".msg_board "+$(_obj).attr('href')).fadeOut('slow');
			}
			if(tempA[0]=='0'){
				_notificationBox.text("Message Was Not Deleted. Please try again after sometime.").prependTo(".message_wrraper").fadeIn(200);
				setTimeout("$(_notificationBox).fadeOut('slow');",5000);
				$.fancybox.close();
			}
	   }
	});
}

function sendRegReq(userInfo,_obj){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"15px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'reg-req' , name:userInfo._fname+" "+userInfo._lname, email:userInfo._email, extrainfo:userInfo._extraInfo, memtype: userInfo._memtype, regtype: userInfo._regtype }),
		success:function(response){ 	//alert(response); //return;
			$(_obj).css({ visibility:'hidden' });
			tempA=response.split(';');
			if(tempA[0]=='1'){
				_notificationBox.text("Request Submitted to Admin. You will receive email with your password when admin will approve the registration").prependTo(".tell_friend").fadeIn(200);
				setTimeout("$.fancybox.close();",7000);
			}
			if(tempA[0]=='0'){
				_notificationBox.text("Message Was Not Deleted. Please try again after sometime.").prependTo(".tell_friend").fadeIn(200);
				setTimeout("$.fancybox.close();",3000);
			}
	   }
	});
}

function changePass(_currPass,_newPass,_id){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"15px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'change-pass' , curr_pass:_currPass, new_pass:_newPass, id:_id }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				alert("Password changed you will be logged out now\r\nPlease login using new password");
				setTimeout("$.fancybox.close(); window.location='logout.php'; ",2000);
			}
			if(tempA[0]=='0'){
				$(".err #curr_pass").text(tempA[1]).css({ fontWeight:"bold" }).show("slow");
				setTimeout("$.fancybox.close();",3000);
			}
	   }
	});
}

function saveImageCaption(_string,_id,_parentEle){
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'image-caption' , caption:_string, id:_id }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(_parentEle).children("span#sta-cont").text(_string);
				$(_parentEle).children("input#dyn-input").val(_string);
				$(_parentEle).children("span#dyn-cont").fadeOut('slow',function(){
					$(_parentEle).children("span#sta-cont").fadeIn('slow').css({ display:'inline' });
				});
				$(_parentEle).children("span#dyn-cont").children("input#dyn-input").removeClass('block').removeAttr('readonly');
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
				$(_parentEle).children("input#dyn-input").val('');
				$(_parentEle).children("span#dyn-cont").fadeOut('slow',function(){
					$(_parentEle).children("span#sta-cont").fadeIn('slow').css({ display:'inline' });
				});
				$(_parentEle).children("span#dyn-cont").children("input#dyn-input").removeClass('block').removeAttr('readonly');
			}
	   }
	});
}

function deleteAlbum(_obj){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"15px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	_notificationBox.prependTo("div.album");
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'album-delete' , id:$(_obj).attr('rel') }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(".album "+$(_obj).attr('href')).fadeOut('slow');
			}
			if(tempA[0]=='0'){
				_notificationBox.text(tempA[1]).fadeIn(20);
			}
	   }
	});
}

function changeAlbumInfo(_string,_id,_parentEle,_field){
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'album-'+_field , caption:_string, id:_id }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(_parentEle).children("span#sta-cont").text(_string+" (click to edit)");
				$(_parentEle).children("input#dyn-input").val(_string);
				$(_parentEle).children("span#dyn-cont").fadeOut('slow',function(){
					$(_parentEle).children("span#sta-cont").fadeIn('slow').css({ display:'inline' });
				});
				$(_parentEle).children("span#dyn-cont").children("input#dyn-input").removeClass('block').removeAttr('readonly');
			}
			if(tempA[0]=='0'){
				alert('Database Error please try after sometime');
				$(_parentEle).children("input#dyn-input").val('');
				$(_parentEle).children("span#dyn-cont").fadeOut('slow',function(){
					$(_parentEle).children("span#sta-cont").fadeIn('slow').css({ display:'inline' });
				});
				$(_parentEle).children("span#dyn-cont").children("input#dyn-input").removeClass('block').removeAttr('readonly');
			}
	   }
	});
}

function forgotPass(_emailID){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"12px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'forgot-password', email:_emailID }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				_notificationBox.text("A mail has been send to the email with new password.").appendTo(".err").fadeIn("slow");
				setTimeout("$.fancybox.close();",3000);
			}
			if(tempA[0]=='0'){
				$(".err #email_id").text(tempA[1]).css({ fontWeight:"bold" }).show("slow");
			}
	   }
	});
}

function addArticle(_artID,_artTitle,_selectObj){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"12px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	
	/*$(_selectObj).parents(".editor").size() ?_selectorId=$(_selectObj).parents(".editor").attr('id') :  _selectorId="#albuminfo";
	alert(_selectorId);
	return;*/
	
	$(_selectObj).each(function(){
		//alert($(this).attr('name'));
		switch($(this).attr('name')){
			case 'category':cate=$(this).val(); break;
			case 'art_type':by_cate=$(this).val(); break;
			case 'art_type_2':art_type=$(this).val(); break;
		}
	});
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'add-article', title:_artTitle, id:_artID, category:cate, by_cate:by_cate, art_type:art_type }),
		success:function(response){ 	//alert(response); //return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				if($(_selectObj).parents(".editor").size()){
					_selectorId=$(_selectObj).parents(".editor").attr('id');
					_parentLI=$(_selectObj).parents("li#"+_selectorId);
					$(_parentLI).children("#albuminfo img#busy").css({ visibility:'hidden' });
					_parentLI.children("a#art_title").text(_artTitle.charAt(0).toUpperCase()+_artTitle.substring(1).toLowerCase());
					$("#albuminfo select'[name=category]'").eq(0).find("option").each(function(){
						//alert($(this).attr('value')+" cate:"+cate);
						if($(this).attr('value')==cate){
							_cateName=$(this).text()
							_parentLI.find("span#art_cate").children("a").text(_cateName);
						}
					});
					$(".article.more").slideUp("slow");
					$(_selectObj).parents("li#"+_selectorId).removeClass('notpub').children("div#edit").children("a").text("Edit");
					_notificationBox.text("Article Updated to the selected category").appendTo("#"+_selectorId).fadeIn("slow");
					setTimeout("_notificationBox.fadeOut('slow')",3000);
					
				}else{
					$(".article.more").slideUp("slow");
					_notificationBox.text("Article Added to the selected category").appendTo(".articlemain #albuminfo").fadeIn("slow");
				}
				//setTimeout("location.reload(true)",3000);
			}
			if(tempA[0]=='0'){
				$(".err #email_id").text(tempA[1]).css({ fontWeight:"bold" }).show("slow");
			}
	   }
	});
}

function test(){
	_notificationBox=$("<div></div>");
	_notificationBox.css({ borderWidth:"1px", borderColor:"#123456", borderStyle:"solid", backgroundColor:"#37719E", color:"white", padding:"5px",fontSize:"12px", fontFamily:"tahoma", fontWeight:"bold", margin:"0 0 5px", display:"none"   });
	_arr = new Array(4,5,56);
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'test', arr:_arr }),
		success:function(response){ 	alert(response); return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				_notificationBox.text("A mail has been send to the email with new password.").appendTo(".err").fadeIn("slow");
				setTimeout("$.fancybox.close();",3000);
			}
			if(tempA[0]=='0'){
				$(".err #email_id").text(tempA[1]).css({ fontWeight:"bold" }).show("slow");
			}
	   }
	});
}

function deteleArticle(_obj){
	_parentLI=$(_obj).parents("li"+$(_obj).attr("href"));	//alert($(_parentLI).size());
	
	_articleURL=$(_parentLI).find("a#art_title").attr("href");
	
	$.ajax({
	  type: 'POST',
	  url:'common/ajax.php',
		data: ({action:'delete-article', id:$(_obj).attr("rel"), filepath:_articleURL }),
		success:function(response){ 	//alert(response); return;
			tempA=response.split(';');
			if(tempA[0]=='1'){
				$(_parentLI).fadeTo(400,0,function(){ $(this).slideUp(function(){ $(this).remove(); }); });
				//_notificationBox.text("Article has been delete from the database").appendTo(".err").fadeIn("slow");
				//setTimeout("$.fancybox.close();",3000);
			}
			if(tempA[0]=='0'){
				alert("Database error please try again later.");
			}
	   }
	});
}
