
function keepMeAlive(imgName){
	$("#"+imgName).attr("src",dir+"active_session.php?"+Math.random());
}


function faceLoadError(){
	//$(".face-45").error(function(){$(this).attr("src",dir+"images/face-45.jpg");});
	$(".face-45").LoadImage(true,45,45,dir+"images/loading.gif","face-45.jpg");
}
var onWhich=false;
function login(){
	$(".footlogoin").show();
	$(".footlogout").hide();
}
function logout(){
	$(".footlogout").show();
	$(".footlogoin").hide();
}
	
function hideother(){
	$(".menudownlist").hide();
}
function show(divName){
	var divObject=document.getElementById(divName);
	if(divObject==null){
		//alert(divName);
			return;
	}
	divObject.style.display="block";
	divObject.style.visibility="visible";
}
function hide(divName){
	var divObject=document.getElementById(divName);
	divObject.style.display="none";
}
$(function(){
	$("#top_username").focus(function(){
		var val = $(this).val();
		if(val=="Email address") $(this).val('');
		onWhich="topLogin";
	});
	$("#top_password").focus(function(){
		/*var val = $(this).val();
		if(val=="Password") {
			$(this).val('');	
		}*/
		onWhich="topLogin";
	});
	$("#top_password_1").focus(function(){
		$(this).hide();
		$("#top_password").show();
		$("#top_password").focus();
		onWhich="topLogin";
	});
	$("#top_username").blur(function(){
		var val = $(this).val();
		if(val=="") $(this).val('Email address');
		//onWhich=false;
	});
	$("#top_password").blur(function(){
		var val = $(this).val();
		if(val=="") {
			$(this).hide();
			$("#top_password_1").show();
			//$("#top_password_1").val('Password');
		}
		//onWhich=false;
	});
	$("#top_password_1").blur(function(){
		var val = $(this).val();
		if(val=="") {
			$(this).val('Password');
		}
		//onWhich=false;
	});
	
	$("#top_search").focus(function(){
		var val = $("#top_search").val();
		if(val=="Search") $("#top_search").val('');
		onWhich="topSearch";
	});
	$("#top_search").blur(function(){
		var val = $("#top_search").val();
		if(val=="") $("#top_search").val('Search');
		//onWhich=false;
	});
	$("#top_search2").focus(function(){
		var val = $("#top_search2").val();
		if(val=="Search") $("#top_search2").val('');
		onWhich="topSearch2";
	});
	$("#top_search2").blur(function(){
		var val = $("#top_search2").val();
		if(val=="") $("#top_search2").val('Search');
		//onWhich=false;
	});
	$("#do_top_search").click(function(){
		var val = $("#top_search").val();
		//alert("topSearch : "+val);
	});
	$("#do_top_search2").click(function(){
		var val = $("#top_search2").val();
		//alert("topSearch2 : "+val);	
	});
	$(document).keydown(function(event){ 
		if(event.keyCode==13&&onWhich&&onWhich!="textarea"){
			if(onWhich=="topLogin"){
				//alert("topLogin");	
				//$("#logouttmarrow").click();
				loginAction();
			}else if(onWhich=="topSearch"){
				//alert("topSearch");
				$("#do_top_search").click();
			}else if(onWhich=="topSearch2"){
				//alert("topSearch");
				$("#do_top_search2").click();
			}else if(onWhich=="createThread"){
				//alert("createThread");	
				$("#ct_thread_submit").click();
			}else{
				//alert("no action");	
			}
		}
	}); 
	
	
	/////islogin
	//isLoginAction();
});

function loginAction(){
	var val_name = $("#top_username").val();
	var val_pass = $("#top_password").val();
	//alert(val_name+" | "+val_pass);
	if(val_name == ""||val_name == "Email address"){
		var msgInfos = {title:"",styleType:"2"};
		msgInfos.detail = "Please enter your email address.";
		$("#top_username").focus(); 
		alertMeg(msgInfos);
		setTimeout("hideMeg()",3000);
		return false;	
	}else if(!isEmail(val_name)){
		var msgInfos = {title:"",styleType:"2"};
		msgInfos.detail = "Please use correct email format.";
		$("#top_username").focus(); 
		alertMeg(msgInfos);
		setTimeout("hideMeg()",3000);
		return false;
		
	}else if(val_pass == ""||val_pass == "Password"){
		var msgInfos = {title:"",styleType:"2"};
		msgInfos.detail = "Please enter your password.";
		$("#top_password_1").focus(); 
		alertMeg(msgInfos);
		setTimeout("hideMeg()",3000);
		return false;
	}
	var params = "username="+val_name+"&password="+val_pass;
	//alert(params);
	$.ajax({
       url: dir+'do_login.php', 
       type:'post',         
       dataType:'json',     
       data:params,         
       success:loginResult 
     }); 
}
function loginResult (rs){
	//alert(rs.status);
	if(rs.status=="success"){
		var ishome = $("#ishome").val();
		var tourl = $('#poplogfromurl').val();				
		if(tourl!="" && typeof(tourl)!="undefined"){
			window.location.href = tourl;
		}else if(ishome=="yes"){
			window.location.href = dir+"my-profile";
		}else{
			window.location.reload();
		}
		
	}else{
		if(rs.type=="notactive"){
			var msgInfos = {title:"",styleType:"2"};
			msgInfos.detail = "Your account is not active. <br/>Please contact admin@worldwidehelpers.org for more information.";
			alertMeg(msgInfos);
			setTimeout("hideMeg()",3000);
		}else{
			var msgInfos = {title:"",styleType:"2"};
			msgInfos.detail = "Please check your username or password again.";
			$("#top_username").focus(); 
			alertMeg(msgInfos);
			setTimeout("hideMeg()",3000);
		}
	}
}
function logoutAction(){
	$.ajax({
       url: dir+'do_logout.php', 
       type:'post',         
       dataType:'string',     
       data:"logout=yes",         
       success:logoutResult 
     }); 
}
function logoutResult (htmlurl){
	//alert(htmlurl);return;
	if(htmlurl=="ok"){
		//show('logout'); hide('login'); logout();
		window.location.href = dir;
	}
}
function isLoginAction(){
	$.ajax({
       url: dir+'do_islogin.php', 
       type:'post',         
       dataType:'json',     
       data:"",         
       success:isLoginResult 
     }); 
}
function isLoginResult (rs){
	if(rs.status == 'success'){
		return true;
	}else{
		return false;
	}
}


function char_test(chr)
{
var i;
var smallch="abcdefghijklmnopqrstuvwxyz";
var bigch="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(i=0;i<26;i++)
if(chr==smallch.charAt(i) || chr==bigch.charAt(i))
  return(1);
return(0);
}

function spchar_test(chr)
{
var i;
var spch="_-.0123456789";
for (i=0;i<13;i++)
  if(chr==spch.charAt(i))
  return(1);
return(0);
}

function isEmail(str)
{
	
var i,flag=0;
var at_symbol=0;//

var dot_symbol=0;//

if(str=="")
      {
            return false;
      }

if(char_test(str.charAt(0))==0 && spchar_test(str.charAt(0))==0)
return false;//


for (i=1;i<str.length;i++)
if(str.charAt(i)=='@')
  {
  at_symbol=i;
  break;
  }
//

if(at_symbol==str.length-1 || at_symbol==0)
return false;
//

if(at_symbol<1)
return false;
//
if(at_symbol>130 )
return false;
//
for(i=1;i<at_symbol;i++)
if(char_test(str.charAt(i))==0 && spchar_test(str.charAt(i))==0)
  return false;
for(i=at_symbol+1;i<str.length;i++)
{
if(char_test(str.charAt(i))==0 && spchar_test(str.charAt(i))==0)
  return false;
}
//
 
for(i=at_symbol+1;i<str.length;i++)
if(str.charAt(i)=='.') dot_symbol=i;
for(i=at_symbol+1;i<str.length;i++)
if(dot_symbol==0 || dot_symbol==str.length-1)
//
return false;
 
return true;
//
}
function alertMeg(info){
	var styleType = "";
	if(typeof(info.styleType)!='undefined'){
		if(info.styleType=="2"){
			styleType = "2";
		}
	}
	//info.title = !info.title ? "&nbsp;" : info.title;
	info.title = "&nbsp;";
	$("#msgtitle"+styleType).html(info.title);
	$("#msgdetail"+styleType).html(info.detail);

	$("#msgokbtn"+styleType).unbind('click');
	$("#msgcancelbtn"+styleType).unbind('click');
	if(info.okbtnshow===true){
		
		$("#msgokbtn"+styleType).css('display','inline');
		$("#msgokbtn"+styleType).bind('click', function() {
			$("#popbg").hide();
			$("#alertMsg"+styleType).hide();
			if(typeof(info.okact)=='function') (info.okact)(info.okact_data);
			$("#msgokbtn"+styleType).css('display','none');
			
		});
		
		$("#msgcancelbtn"+styleType).css('display','inline');
		$("#msgcancelbtn"+styleType).bind('click', function() {
			$("#popbg").hide();
			$("#alertMsg"+styleType).hide();
			if(typeof(info.cancelact)=='function'){
				(info.cancelact)();
			}
		});
	}else{
		$("#msgokbtn"+styleType).css('display','none');
		$("#msgcancelbtn"+styleType).css('display','none');
	}
	
	$("#popbg").show();
	$("#alertMsg"+styleType).show();
}
function setAlertMsg(info){
	var styleType = "";
	if(typeof(info.styleType)!='undefined'){
		if(info.styleType=="2"){
			styleType = "2";
		}
	}
	info.title = "&nbsp;";
	$("#msgtitle"+styleType).html(info.title);
	$("#msgdetail"+styleType).html(info.detail);
	if(typeof(info.title)!='undefined'){
		$("#msgtitle"+styleType).html(info.title);
	}
	if(typeof(info.detail)!='undefined'){
		$("#msgdetail"+styleType).html(info.detail);
	}
}
function hideMeg(){
	$("#popbg").hide();
	$(".alertMsg").hide();
}
/*
getParams

*/
function getParams(cls,beforeCheckFun){
	////////////////////////////////////////////// get param start
	var paramsElm = $("."+cls);
	var formData = new Array();
	var elmForm = new Array();
	paramsElm.each(function(index, element) {
		var n, v ;
		n = element.name;
		v = element.value;

		if(element.type=='checkbox' ||element.type=='radio'){
			//alert(element.name+"-"+element.value+"-"+element.checked);
			if(element.checked){
				formData.push( { name: n, value: v } );
			}
		}else{
			formData.push( { name: n, value: v } );
		}
		elmForm.push(element);
	});
	/////////////////////////////////////////////// get param end
	
	/****************** check form ********************/
	if(beforeCheckFun(formData,elmForm)){
		var queryString = $.param(formData);
		return queryString;
	}else{
		return false;
	}
}

function in_array(a, v) {
  var i;
  for (i = 0; i < a.length; i++) {
    if (v === a[i]) {
      return i;
    }
  }
  return -1;
} 
	
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase){
if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
}else{
return this.replace(reallyDo, replaceWith);
}
}
 
//add by liucong
function searchSubmit(){
	var search_key = $('#top_search').val();
	search_key = $.trim(search_key);
	if(search_key.length>0 && search_key != null && search_key != 'Search' &&  search_key != ''){
		return true;
	}else {
		$("#top_search").focus();
		return false;
	}
}
function searchSubmit2(){
	var search_key = $('#top_search2').val();
	search_key = $.trim(search_key);
	if(search_key.length>0 && search_key != null && search_key != 'Search' &&  search_key != ''){
		return true;
	}else {
		$("#top_search2").focus();
		return false;
	}
}

function gotoLogin(){
	hide('popbg');
	$("#top_username").focus();
}

function projectSearch(url){
	window.location.href=dir+url; 
}

function checkLimits(o){
  var sh = (o.scrollHeight)-6;
  var row = Math.ceil(sh/14);
  if(row>2){
	 o.value = o.value.substr(0,o.value.length-1);
	 checkLimits(o); 
  } 
}

function imgWHFormat(cls,w,h){
	cls = !cls ? 'autoItemTitleImg' : cls;
	w = !w ? 104 : parseInt(w) ;
	h = !h ? 113 : parseInt(h) ;
	$("."+cls).LoadImage(true,w,h,dir+"images/loading.gif");
}

function TransferStringBR(content)
{
	var string = content;
	try{
		string=string.replace(/\r\n/g,"<br>")
    	string=string.replace(/\n/g,"<br>");
	}catch(e) {
		//alert(e.message);
	}
	return string;
}
function htmlspecialchars(string){
	var data = [];
	for(var i = 0 ;i <string.length;i++) {
		data.push( "&#"+string.charCodeAt(i)+";");
	}
	return data.join("");
}


var wwhUserColor = {'vol':'#0081c7','org':'#018039','admin':'#ff6c00'};


