function CheckUsernameChar(stringIn)
{
	retval = false
     var i;
     for (i=0;i<=stringIn.length-1;i++) 
	 {
         if (((stringIn.charCodeAt(i) >= 48)&&(stringIn.charCodeAt(i) <= 57)) || ((stringIn.charCodeAt(i) > 64)&&(stringIn.charCodeAt(i) <= 90)) || ((stringIn.charCodeAt(i) >= 97)&&(stringIn.charCodeAt(i) <= 122)) ||(stringIn.charCodeAt(i)==95) || (stringIn.charCodeAt(i)==45))
		 {
           	retval = true;
         }
		 else
		 {
			retval = false;
			break;
         }
     }
     return retval;
}
function submit_handler(e)
{
	var asc = document.all ? event.keyCode : e.which;
	if(asc == 13) submitFormInvite();
	return asc != 13;
}
function submitFormInvite(strType,from,subm)
{
	if(!from)
		from = 'frminviteyahoo';
	var frm = $('#'+from);
	var bolcheckinvite = true;
	
	var stremail	= $('#txtemail',frm).val();
	var strpassword	= $('#txtpassword',frm).val();
	
	//kiem tra email
	if ( stremail.length <= 0 )
	{
		$('#err_email',frm).css('display','block');
		$('#txtemail',frm).focus();
		return false;
	}
	
	if ( stremail.indexOf('@') >= 0 )
	{
		if ( !checkEmail(stremail,false) )
		{
			$('#err_email',frm).css('display','block');
			$('#err_email',frm).html('Email không hợp lệ!');
			$('#txtemail',frm).focus();
			return false;
		}
	}
	else
	{
		if ( !CheckUsernameChar(stremail) )
		{
			$('#err_email',frm).css('display','block');
			$('#err_email',frm).html('Email không hợp lệ!');
			$('#txtemail',frm).focus();
			return false;
		}
		else
		{
			$('#err_email',frm).css('display','none');	
		}
	}
	//kiem tra password
	if ( strpassword.length <= 0 )
	{
		$('#err_pass',frm).css('display','block');
		$('#txtpassword',frm).focus();
		return false;
	}
	else
	{
		$('#err_pass',frm).css('display','none');
	}
	if(!subm){
		$('#email_pass_fail').css('display','none');
		$('#listemail_loading',frm).html('<img src="'+strPathImgLoading+'"/>');
		strEmailType = $('#txtemailtype',frm).val();
		$.post(YUME+'/friends/friend',{txtemail:stremail,txtpassword:strpassword,txtemailtype:strEmailType},function(data){if(data.error){$('#listemail_loading',frm).html('');$('#email_pass_fail').css('display','block');} else if(data.html){$('#load_listemail').html(data.html)}},'json');	
	}
	else{
		return true;
	}
}
function loadEmailType(strType)
{
	strType = strType?strType:'yahoo';	
	if(strType=='gmail')
	{
		if($('.imgyahoo img').hasClass('active'))
			$('.imgyahoo img').removeClass('active');
		if($('.imghotmail img').hasClass('active'))
			$('.imghotmail img').removeClass('active');
		$('.imggmail img').addClass('active');		
		$('#load_listemail').html('<div class="column_left h200"><form id="frminviteyahoo" name="frminviteyahoo" method="post"><p class="logo" id="logo_emailtype"><img src="images/graphics/blank.gif" style="background-image:url(images/graphics/friends_gmail_logo_86x38.gif)" width="300" height="50"/></p><p class="sub" id="emailtype_desc">Mời bạn trong danh sách Gmail</p><p class="note">Vui lòng nhập vào địa chỉ email và mật khẩu. <strong>Chúng tôi không lưu lại mật khẩu của bạn</strong>. Chúng tôi chỉ sử dụng tạm thời để lấy thông tin trong danh sách địa chỉ email.</p><span id="email_pass_fail" style="color:#FF0000; font-size:11px; font-family:tahoma; display:none">Email hoặc Mật khẩu không đúng</span><ul class="email"><li class="iserr"><div><span>Email:</span> <input name="txtemail" id="txtemail" type="text" onkeypress="submit_handler(event);" tabindex="1"/><select name="sel_email_type" id="sel_email_type"><option>@gmail.com</option></select></div><div class="errtext" id="err_email" style="display:none">Bạn chưa nhập Email</div></li><li class="iserr"><div><span>Mật khẩu:</span> <input name="txtpassword" id="txtpassword" type="password" onkeypress="submit_handler(event);" tabindex="2"/></div><div class="errtext" id="err_pass" style="display:none">Vui lòng nhập mật khẩu</div></li><li><span>&nbsp;</span><input name="btnsubmitinvite" id="btnsubmitinvite" type="button" value="Lấy danh sách" class="btn_105" onclick="submitFormInvite();return false"/></li></ul><input type="hidden" id="btninviteyahoo" name="btninviteyahoo" value="" /><input type="hidden" id="txtemailtype" name="txtemailtype" value="gmail" /><span id="listemail_loading" style="margin-left:250px; display:inline; float:left"></span></form></div>');
	}
	else if(strType=='hotmail')
	{
		if($('.imgyahoo img').hasClass('active'))
			$('.imgyahoo img').removeClass('active');
		if($('.imggmail img').hasClass('active'))
			$('.imggmail img').removeClass('active');
		$('.imghotmail img').addClass('active');
		$('#load_listemail').html('<div class="column_left h200"><form id="frminviteyahoo" name="frminviteyahoo" method="post"><p class="logo" id="logo_emailtype"><img src="images/graphics/blank.gif" style="background-image:url(images/graphics/friends_hotmail_logo_67x40.gif)" width="300" height="50"/></p><p class="sub" id="emailtype_desc">Mời bạn trong danh sách Hotmail</p><p class="note">Vui lòng nhập vào địa chỉ email và mật khẩu. <strong>Chúng tôi không lưu lại mật khẩu của bạn</strong>. Chúng tôi chỉ sử dụng tạm thời để lấy thông tin trong danh sách địa chỉ email.</p><span id="email_pass_fail" style="color:#FF0000; font-size:11px; font-family:tahoma; display:none">Email hoặc Mật khẩu không đúng</span><ul class="email"><li class="iserr"><div><span>Email:</span> <input name="txtemail" id="txtemail" type="text" onkeypress="submit_handler(event);" tabindex="1"/><select name="sel_email_type" id="sel_email_type"><option>@hotmail.com</option></select></div><div class="errtext" id="err_email" style="display:none">Bạn chưa nhập Email</div></li><li class="iserr"><div><span>Mật khẩu:</span> <input name="txtpassword" id="txtpassword" type="password" onkeypress="submit_handler(event);" tabindex="2"/></div><div class="errtext" id="err_pass" style="display:none">Vui lòng nhập mật khẩu</div></li><li><span>&nbsp;</span><input name="btnsubmitinvite" id="btnsubmitinvite" type="button" value="Lấy danh sách" class="btn_105" onclick="submitFormInvite();return false"/></li></ul><input type="hidden" id="btninviteyahoo" name="btninviteyahoo" value="" /><input type="hidden" id="txtemailtype" name="txtemailtype" value="hotmail" /><span id="listemail_loading" style="margin-left:250px; display:inline; float:left"></span></form></div>');
	}
	else
	{
		if($('.imghotmail img').hasClass('active'))
			$('.imghotmail img').removeClass('active');
		if($('.imggmail img').hasClass('active'))
			$('.imggmail img').removeClass('active');
		$('.imgyahoo img').addClass('active');				
		$('#load_listemail').html('<div class="column_left h200"><form id="frminviteyahoo" name="frminviteyahoo" method="post"><p class="logo" id="logo_emailtype"><img src="images/graphics/blank.gif" style="background-image:url(images/graphics/friends_yahoo_logo_180x26.gif)" width="300" height="50"/></p><p class="sub" id="emailtype_desc">Mời bạn trong danh sách Yahoo</p><p class="note">Vui lòng nhập vào địa chỉ email và mật khẩu. <strong>Chúng tôi không lưu lại mật khẩu của bạn</strong>. Chúng tôi chỉ sử dụng tạm thời để lấy thông tin trong danh sách địa chỉ email.</p><span id="email_pass_fail" style="color:#FF0000; font-size:11px; font-family:tahoma; display:none">Email hoặc Mật khẩu không đúng</span><ul class="email"><li class="iserr"><div><span>Email:</span> <input name="txtemail" id="txtemail" type="text" onkeypress="submit_handler(event);" tabindex="1"/><select name="sel_email_type" id="sel_email_type"><option>@yahoo.com</option></select></div><div class="errtext" id="err_email" style="display:none">Bạn chưa nhập Email</div></li><li class="iserr"><div><span>Mật khẩu:</span> <input name="txtpassword" id="txtpassword" type="password" onkeypress="submit_handler(event);" tabindex="2"/></div><div class="errtext" id="err_pass" style="display:none">Vui lòng nhập mật khẩu</div></li><li><span>&nbsp;</span><input name="btnsubmitinvite" id="btnsubmitinvite" type="button" value="Lấy danh sách" class="btn_105" onclick="submitFormInvite();return false"/></li></ul><input type="hidden" id="btninviteyahoo" name="btninviteyahoo" value="" /><input type="hidden" id="txtemailtype" name="txtemailtype" value="yahoo" /><span id="listemail_loading" style="margin-left:250px; display:inline; float:left"></span></form></div>');
			
	}
}
function checkAll()
{
  if($('#selall').attr('checked')==true)
  {
	$("INPUT[type='checkbox']").attr('checked',true);
  }
  else
  {
	$("INPUT[type='checkbox']").attr('checked',false);
  }
}
function checkEmail(strEmail,isArray)
{
	if(isArray==true)
	{
		arrEmail=strEmail.split(',');
		isValid = true;
		for(i=0;i<arrEmail.length;i++)
		{
			arrEmail[i]=arrEmail[i].replace(/^\s+|\s+$/g,"");
			if (arrEmail[i].search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1 && arrEmail[i]!="")
			{
				isValid = false;
			}	
		}
		return 	isValid;
	}
	else
	{
		if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
		{
			return false;
		}
		else
		return true
	}
}
function addEmail()
{
	intTotalSelected = $('input[@id^="selcontacts"][@checked]').length;
	if(intTotalSelected <= 0)
	{
		alert('Bạn chưa chọn bạn!');
		return false;
	}
	var strInfo = '';
	var strEmail = '';
	$('input:checkbox').each( function(i) {
                         var checked_status = this.checked;
						 var strLocalInfo='';
						 var strLocalEmail='';
                         if (checked_status && $(this).val()!='on')
                         {
								if(strInfo!='')
								 	strLocalInfo = ';'+$(this).val()+'|'+$(this).attr('fullname');
								else
									strLocalInfo = $(this).val()+'|'+$(this).attr('fullname');	
								 strInfo += strLocalInfo;
								if(strEmail!='')
								 	strLocalEmail = ', '+$(this).val();
								else
									strLocalEmail = $(this).val();	
								 strEmail += strLocalEmail;	
                         }
     });
	 strSendEmail = $.trim($('#txtsendmail').val())!=''?$.trim($('#txtsendmail').val())+', '+strEmail:strEmail;
	 strSendInfo = $.trim($('#txtfullinfo').val())!=''?$.trim($('#txtfullinfo').val())+';'+strInfo:strInfo;
	 $('#txtsendmail').val(strSendEmail);
	 $('#txtfullinfo').val(strSendInfo);
	 caobox.close_popup();
	 
}
function inviteFriends()
{
	strEmail = $.trim($('#txtsendmail').val());
	strInfo = $.trim($('#txtfullinfo').val());
	strMsg = $.trim($('#txtmesage').val().substr(0, 4000))
	if(strEmail=='')
	{
		alert('Bạn chưa nhập email người nhận!');
		$('#txtsendmail').focus();
		return false;
	}
	if(!checkEmail(strEmail,true))
	{
		alert('Chuỗi địa chỉ email không hợp lệ!');
		$('#txtsendmail').focus();
		return false;
		
	}
	$('#email_sending').html('<img src="'+strPathImgLoading+'"/>Sending...');
	return true;
	//ajax_inviteFriends(strEmail,strInfo,strMsg);
}
function checkSubmitSugest()
{
	intTotalSelected = $('input:checkbox[checked]',$('#check_id')).length;
	if(intTotalSelected <= 0)
	{
		alert('Bạn chưa chọn bạn nào!');
		return false;
	}	
	$('#email_sending').html('<img src="'+strPathImgLoading+'"/>Sending...');
	return true;
}
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
function showTextLabel(inttotal)
{
	if ( inttotal > 4000 )
		$('#txtmesage').val($('#txtmesage').val().substr(0, 4000));
}