function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function toggle_todo(oCheck){

	if(oCheck.checked){
		$.getJSON('todo/set_todo_completed/'+$(oCheck).val()+'/1',function(oResponse){
		
        var iTTopC = parseInt($('#itaskscounttop').html());
        if(iTTopC>0)iTTopC--;
        $('#itaskscounttop').html(iTTopC)
				//alert('completed')
				if($('#group_completed_todos_after').length>0){
					
					$('#iListMessage'+oResponse.id).remove();
					$('#iNoCompletedTodosMsg').remove();
					
					for(var j=0;j<oResponse.children_ids.length;j++)$('#iListMessage'+oResponse.children_ids[j]).remove();
					
					$('#group_completed_todos_after').after('<p id="iListMessage'+oResponse.id+'"></p>');
					
					$('#iListMessage'+oResponse.id).load('todo/message_list_row/'+oResponse.id+'/1'
														,function(){
															$(this).effect('highlight',{},1000);
															})
				} else if ($('.message_top_title_content').length>0){
				
					$('.message_top_title_content').css('text-decoration','line-through')
					$('.percentbarc').css('width',$('.percentbarh').width()+'px')
          $('.percentbarl').html('100%')
		
				} else {
						$('#iListMessage'+oResponse.id).load('todo/message_list_row/'+oResponse.id+''
														,function(){
															$(this).effect('highlight',{},1000);
															})
															
						for(var j=0;j<oResponse.children_ids.length;j++)$('#iListMessage'+oResponse.children_ids[j]).load('todo/message_list_row/'+oResponse.children_ids[j]+''
														,function(){
															$(this).effect('highlight',{},1000);
															})
				}
		
		})
	} else {
		$.getJSON('todo/set_todo_completed/'+$(oCheck).val()+'/0',function(oResponse){
		
		        var iTTopC = parseInt($('#itaskscounttop').html());
         iTTopC++;
        $('#itaskscounttop').html(iTTopC)
		
			if($('#group_completed_todos_after').length>0){
				window.location.reload()
			}else if ($('.message_top_title_content').length>0){
				$('.message_top_title_content').css('text-decoration','none')
				
					$('.percentbarc').css('width',($('.percentbarh').width()*.9)+'px')
          $('.percentbarl').html('90%')
				
			} else {
							$('#iListMessage'+oResponse.id).load('todo/message_list_row/'+oResponse.id+''
														,function(){
															$(this).effect('highlight',{},1000);
															})
															
						for(var j=0;j<oResponse.children_ids.length;j++)$('#iListMessage'+oResponse.children_ids[j]).load('todo/message_list_row/'+oResponse.children_ids[j]+''
														,function(){
															$(this).effect('highlight',{},1000);
															})		
			}
		})
	}
	
}

aErrorsTbl = Array();

aPendingAction = {param:'',action:'',hold:false};
aPendingLoginAction = {param:'',action:'',hold:false};

function float_login(sForwardUrl){
	
						var t =  null;
                    	var a = '/profile/login?back_url='+sForwardUrl+'&width=350&height=220';
                    	var g = false;
						tb_show(t,a,g);

}


function check_form_id(oField){
	if(oField){
		if($(oField.form).attr('id')==''){
			var  iForms = aErrorsTbl.length;
			$(oField.form).attr('id','form'+iForms);
		}
		//alert(typeof(aErrorsTbl[$(oField.form).attr('id')]))
		if(typeof(aErrorsTbl[$(oField.form).attr('id')])!='object')	aErrorsTbl[$(oField.form).attr('id')] = Array()
	}
}

function system_message(msg){

	alert(unescape(msg))

}

function system_ask(msg){

	return confirm(msg)

}

function trigger_error(msg,oField){
	
	if(oField){
	
		check_form_id(oField)

	
		$(oField).addClass('error');
		//alert($(oField).attr('id'));
		$('#'+$(oField).attr('id')+'_errormsg').remove();
		
		
		var oError = $("<p>"+msg+"</p>").addClass('formerror').attr('id',oField.id+'_errormsg');
		
		if($('#'+$(oField).attr('id')+'_errorholder').get().length>0){
		
			$('#'+$(oField).attr('id')+'_errorholder').html('').append(oError).show();
			
		} else {
	
			$(oField).after(oError);
		
		}
		
		aErrorsTbl[$(oField.form).attr('id')][oField.id] = 1;
	}
}

function clear_error(oField){
	if(oField){
		check_form_id(oField)
		aErrorsTbl[$(oField.form).attr('id')][oField.id] = 0;
		$(oField).removeClass('error');
		$('#'+$(oField).attr('id')+'_errormsg').remove();
		if($('#'+$(oField).attr('id')+'_errorholder').get().length>0){
			$('#'+$(oField).attr('id')+'_errorholder').hide()
		}
	}

}

function check_before_submit(oForm)
{


	//	aTmp = oForm.elements;
	
	//$(oForm.elements).each(function(i){alert(this.value)})
	for(var i=0;i<oForm.elements.length;i++){
	
		if($(oForm.elements[i]).attr('type')!='submit')$(oForm.elements[i]).trigger('blur')
		//	console.log(i+' '+aErrorsTbl)
	}
	
	

	if(typeof(aErrorsTbl[$(oForm).attr('id')])=='object'){
			
		for (var i in aErrorsTbl[$(oForm).attr('id')]) { 
		//console.log(i+' '+aErrorsTbl[$(oForm).attr('id')][i])
		   				if(aErrorsTbl[$(oForm).attr('id')][i]==1){
		   				
		   					return false
		   				
		   				}	
		};
		
	}
	return true;
}

function removeElementById(id){
    var oTmp = document.getElementById(id);

    if(oTmp)oTmp.parentNode.removeChild(oTmp);
}



 function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 } 
 
 


 function is_valid_email (email)
      {

      return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);

      }






