
function isValidEmail(text_box_class_name, err_msg_id)
{	
	err_msg = 'Your email is invalid format.';
	email = $('.'+text_box_class_name);			
	if(!/^([a-zA-Z0-9_\.])+\@([a-zA-Z0-9\-])+\.([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?$/.test(email.val()))
	{			
		email.focus();
		if(err_msg_id!=null) $('#'+err_msg_id).html(err_msg);
		else alert(err_msg);
		return false;
	}
	return true;
}

function link_to(link)
{
	window.location = index()+link; 
}

function IsNumeric(strString, option) 
{ 
	if(option==null || option==false)
	    var strValidChars = "0123456789"; 
	else var strValidChars = "0123456789."; 	
    var strChar; 
    var blnResult = true; 

    if (strString.length == 0) 
        return false; 
    for (i = 0; i < strString.length && blnResult == true; i++) 
    { 
        strChar = strString.charAt(i); 
        if (strValidChars.indexOf(strChar) == -1) 
        { 
            blnResult = false; 
        } 
     } 
	return blnResult; 
}
$(function(){
	init_page();
})

function init_page()
{
	$('#email_friend_link').click(function() {
			
	})
		
	$("#viettest").click(function() {
		var your_name = $('#your_name').val();
		var your_email = $('#your_email').val();
		var friend_name = $('#friend_name').val();
		var friend_email = $('#friend_email').val();
		var message = $('#message').val();
		var captchaword = $('#captchaword').val();
		var m_acc_id = $('#m_acc_id').val();
		var m_acc_image_id = $('#m_acc_image_id').val();
		
		if(your_name == "")
			document.getElementById('div_err').innerHTML = 'Plsease enter your name';
		else if(your_email == "")
			document.getElementById('div_err').innerHTML = 'Plsease enter your email';
		else if(!isValidEmail_sendmail(your_email))
			document.getElementById('div_err').innerHTML = 'Your email is invalid';
		else if(friend_name =="")
			document.getElementById('div_err').innerHTML = 'Please enter name your friend';
		else if(friend_email =="")
			document.getElementById('div_err').innerHTML = 'Please enter email your friend';
		else if(!isValidEmail_sendmail(friend_email))
			document.getElementById('div_err').innerHTML = 'Your friend email is invalid';
		else if(message == "")
			document.getElementById('div_err').innerHTML = 'Please enter your messages';
		else if(captchaword == "")
			document.getElementById('div_err').innerHTML = 'Please enter captcha';
		else
		{
			var error = false;
				var string_content = '';

				$('.contact_input').each(function(){
					value = $.trim($(this).val());
					if(value)
					{
						$(this).next().html('');
						string_content+= $(this).attr('name')+'='+value+'&';
					}else{
						$(this).next().html(' <font color="red">required</font>');
						error = true;
					}						
				});
				
				if(error)	return false;
				
				$('.contact_msg').each(function(){
					value = $.trim($(this).val());
					if(value)
					{
						$(this).next().html('');	
						string_content+= $(this).attr('name')+'='+value+'&';						
					}else{
						$(this).next().html(' <font color="red">*invalid</font>');
						error = true;
					}						
				});
				
				if(error)	return false;
				
				$('.contact_security').each(function(){
					value = $.trim($(this).val());
					if(value)
					{
						$(this).next().html('');	
						string_content+= $(this).attr('name')+'='+value+'&';						
					}else{
						$(this).next().html(' <font color="red">*invalid</font>');
						error = true;
					}						
				});
				
				if(error)	return false;
				
				element = $(this);
			$.post( index()+'modules.accommodation_unit.send_mail/sendmail', {data:string_content},function(html){		
				if(html == 'Please fill your captcha correctly')	
				{
					document.getElementById('div_err').innerHTML = 'Please fill your captcha correctly';
					return false;
				}
				else
				{
					alert(html);
					parent.close_block();
				}
				
			});
		}
	})
}

function isValidEmail_sendmail(email)
{	
	if(!/^([a-zA-Z0-9_\.])+\@([a-zA-Z0-9\-])+\.([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?$/.test(email))
		return false;
	else
		return true;
}
