var active_ele;
//chon themme
function pick_themme_function(notice_video)
{
	if($('url_video').value=='')
	{
		if($('global_video').value=='')
		{
			alert(notice_video);
			return false;
		}
		else{
			url='/greeting_video?action=template_picker&global_video='+$('global_video').value;
			$('clip_select').submit();
		}
	}
	else{
			if (checkURL())
			{
			if($('global_video').value=='')
			{
				alert(notice_video);
				return false;
			}else{
				$('clip_select').submit();
			}
			}
	}
	return false;
}
//lay video, themme, chuyen sang buoc 3
function pick_data_function(notice)
{
	if($('url_video').value=='')
	{
		if($('global_template').value=='')
		{
			alert(notice);
			return false;
		}
		else
		{
			url='/greeting_video?action=card_data&global_video='+$('global_video').value+"&global_template="+$('global_template').value;
			window.location=url;
		}
	}
	else
	{	
		checkURL();
		if($('global_template').value=='')
		{
			alert(notice);
			return false;			
		}
		else
		{
			url='/greeting_video?action=card_data&global_video='+$('global_video').value+"&global_template="+$('global_template').value;
			window.location=url;
		}
	}
	return true;
}
//Kiem tra dia chi url nhap vao, lay id cua video gan cho global_video
function checkURL()
{
	var str=$('url_video').value;
	if((str.match('^http://clip.vn/watch/')==null))
	{
		if(str.match('^http://clip.vn/mytv')==null)
		{
			alert('Địa chỉ không hợp lệ!');
			$('url_video').value='';
			return false;
		}
		else
		{
			str=str.split('movID=');
			str=str[1];	
			str=str.split('&');
			str=str[0];
		}
	}
	else
	{
		str=str.split('/');
		str=str[str.length-1];
		str=str.split(',');
		str=str[0];
		}
		
	$('global_video').value=str;
	return true;
}
function fill_url()
{
	uncheckallradios();
	unactivebox();
}

/*Template picker*/
function getTemplate(template)
{
	$('global_template').value=template;
}
//chuyen tu buoc 2 sang buoc 3, lay template
function pick_data_function_template(notice)
{
	
		if($('global_template').value=='')
		{
			alert(notice);
			return false;		
		}else{
		url='/greeting_video?action=card_data&global_template='+$('global_template').value;
		window.location=url;

		}
}
//chuyen toi buoc 1, cap nhat gia tri cho video, template
function update_globalValue()
{

		url='/greeting_video?action=video_picker&global_video='+$('global_video').value+"&global_template="+$('global_template').value;
		window.location=url;
		
}

/*Card data*/
//thuc hien chuc nang select tung nguoi ban trong danh sach ban be, hien thi noi dung trong text box: dia chi ban be
function updateValue()
{
	var ddlFriendList = $("ddlFriendList").value;
	var str =$('txtChoicedFriend').value;	
	if(str.match(ddlFriendList)==null )
	{
		if($('txtChoicedFriend').value =='')
		{
			$('txtChoicedFriend').value = '(' + ddlFriendList + ') ';
		}
		else
		{
			$('txtChoicedFriend').value += ', (' + ddlFriendList + ') ';
		}
	}
	return true;
}
//Thuc hien chuc nang chon tat ca ban be trong danh sach ban be
function getAll()
{
	$('txtChoicedFriend').value = $("selectAll").value;
}

//Kiem tra, lay du lieu email, username,... truoc khi send card
function send(notice,notice_textinput,notice_textinputName,notice_video,notice_theme)
{
	var user_email_var=$('user_email').value;
	user_email_var= user_email_var.replace(/\)/g,',');
		user_email_var= user_email_var.replace(/\(/g,',');
	user_email_var = user_email_var.split(',');
	var str = $('txtChoicedFriend').value;
	str = str.replace(/ /g,"");
	str = str.split(',');
	
	var IsMailList ='';
	var MailList='';
	if(str!='')
	{
		for(i=0; i<str.length;i++)
		{
			if(checkMail(str[i])){
				IsMailList += str[i]+',';
			}
			else
			{
					for(j=0;j<user_email_var.length;j++)
					{
						if((user_email_var[j].match('^'+str[i]))!=null&&(user_email_var[j].match('^'+str[i]))!='')
						{
							var tmp= user_email_var[j].slice((str[i].length)-1,-1);
							MailList += user_email_var[j] +', ';		
						}
					}	
			}
		}
	}
	$('ListEmail').value = MailList + IsMailList;
	if ($('ListEmail').value=='')
	{
		alert(notice);
		$('txtChoicedFriend').focus();
		return false;
	}
	else{
			if(($('sender_name').value).length >20){
			
				alert(notice_textinputName);
				$('sender_name').focus();
				return false;
				
			}
			if(($('message').value).length >500){
			
				alert(notice_textinput);
				$('message').focus();
				return false;
					
			}
			if($('global_video').value=='')
			{
				alert(notice_video);
				return false;
			}
				if($('global_template').value=='')
			{
				alert(notice_theme);
				return false;
			}
			$('message').value=($('message').value).replace(/\n/g,'<br/>');
		$('inviteFriend').submit();
	}
}
//Kiem tra co phai la dia chi email khong
function checkMail(str)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(str)) return true;
	else return false;
}
//Hien thi noi dung trong preview theo su thay doi trong text box: ten cua ban, và noi dung loi chuc
function update_template_function()
{
	var name='';
	var message_content='';
	 name= ($('sender_name').value).slice(0,100);
	 message_content= ($('message').value).slice(0,500);
	$('contentsender').innerHTML=name;
	$('contentmessage').innerHTML=message_content;
}
function clickclip_box(str,num)
{
	//gan gia tri
	$('url_video').value='';
	$('global_video').value=num;
	
	//Check va uncheck cac radio
	ele_id='radio_'+str;
	var el=$(ele_id);
	uncheckallradios();
	el.checked=true;
	
	//active box moi
	par_ele=$('box_'+str);
	activebox(str)
	//unactive box cu
	unactivebox();
	//alert(active_ele);
	active_ele=str;
	
}
function uncheckallradios()
{	
	var t=document.getElementsByTagName('input');
	var l=t.length;
	for (i=0;i<l;i++){
		if (t[i].type=='radio')
			t[i].checked=false;
	}
}
function activebox(box_id)
{
	box_id='box_'+box_id;
	$(box_id).addClassName("clipinfo_hover");
}
function unactivebox()
{
	box_id='box_'+active_ele;
	if ($(box_id))
	{
		
	$(box_id).removeClassName("clipinfo_hover");
	
	}
}
function hoverbox(box_id)
{
	if (box_id==active_ele) return false;
	box_id='box_'+box_id;
	$(box_id).addClassName("clipinfo_hover");
}
function unhoverbox(box_id)
{
	if (box_id!=active_ele) 
	{

	box_id='box_'+box_id;
	$(box_id).removeClassName("clipinfo_hover");
	}
}

document.onkeydown=onKeyDown;
function onKeyDown(evt)
	{
		evt = (evt) ? evt : event;
	    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
	        ((evt.which) ? evt.which : 0));

	   if (charCode==13)
	    { 
	//	alert(($('message').value==''));
			if($('message').value=='')
			{
			//	alert('dfasdf');
				$('message').focus();
				return false;
			}else{
				if ($('ListEmail').value=='')
				{

					$('txtChoicedFriend').focus();
					return false;
				}
			}
	    }
				
	}
