function validateEmail(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;				
}

function trim(s)
{   
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not a whitespace, append to returnString.
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (c != " ") returnString += c;
	}
	return returnString;
}

function checksearch()
{
	var count = 0;
	var error ='';
	
	if ($('input[name=author]').val() == "" && $('input[name=checkauthor]').attr('checked')) {
		error = author_error;
		count = count+1;
	}
	
	if(count>0){
		$('#author_error').html(error);
		return false;		
	}
	else {
		$('#author_error').html('');					
	}
	return true;	
}	

//page 99
function checkInfo()
{
	var count = 0;			
	if($('input[name=customer-name]').val() == ""  )
	{
		error = name_error;
		count = count+1;
		if (count > 0)
		{
			$('#name_error').html(error);
			
			return false;
		}	
	}
	else {
		$('#name_error').html('');					
	}
	var email = $('input[name=email]').val();
	if(email == "")
	{
		error = email_error ;									
		count = count+1;
		if (count > 0)
		{
			$('#email_error').html(error);			
			return false;
		}
		
	}
	else
	{
		
		if(!validateEmail(email))
		{
			error = email_invalid ;
			count = count+1;
			if (count > 0)
			{
				$('#email_error').html(error);
				
				return false;
			}	
		}						
		else
		{
			$('#email_error').html('');
		}						
	}
						
	var comment = $('textarea[name=message]').val();
	if(trim(comment) == "")
	{
		error = comment_error ;
		count = count+1;
		if (count > 0)
		{
			$('#comment_error').html(error);
			
			return false;
		}	
	}		
	else
	{
		$('#comment_error').html('');
	}				
		
	return true;

}
function checkInput()
{		
	var error="";
	var count = 0;	
	var username = jQuery('input[name=username]').val();
	if(username == "")
	{
		error += username_empty + "<br />";	
		count = count +1;
	}
	else
	{
		for(var i=0;i<username.length;i++)
		{			
			if(username.charAt(i) == ' ')
			{				
				error += username_invalid + "<br />";	
				count = count +1;
				break;
			}
		}
	}

	var pass = jQuery('input[name=password]').val();
	if(pass == "")
	{
		error += password_empty + "<br />";
		count = count +1;
	}
	else
	{
		var confirmpass = jQuery('input[name=confirm_password]').val();
		if(confirmpass == "")
		{
			error += confirmpassword_empty + "<br/>";
			count = count +1;
		}
		else if(pass != confirmpass)
		{
			error += confirmpassword_incorrect + "<br/>";
			count = count +1;
		}
	}
	var email = jQuery('input[name=user_email]').val();
	if(email=="")
	{
		error += email_empty + "<br/>";
		count = count +1;
	}
	else
	{
		if(!validateEmail(email))
		{
			error += email_incorrect + "<br/>";
			count = count + 1;
		}
	}
	if(jQuery('input[name=user_profile]').val() == "")
	{
			error += profile_empty;
			count = count + 1;
	}
	
	if(count>0)
	{		
		jQuery('#errorRegister').html(error);
		return false;
	}	
	return true;
}
function checkLogin()
{	
	count = 0;
	var error_login = '';
	var username = jQuery('input[name=username_login]').val();
	if(username=="")
	{
		error_login += username_login_empty + "<br />";
		count += 1;
	}
	var pass = jQuery('input[name=password_login]').val();
	if(pass == "")
	{
		error_login += password_login_empty + "<br />";
		count += 1;
	}
	if(count>0)
	{
		jQuery('#errorLogin').html(error_login);
		return false;
	}
	else
	{
		jQuery('#errorLogin').html('');
		return true;
	}	
}
function checkEmail()
{	
	var error_email = '';
	var count = 0;
	var email = jQuery('input[name=email_lost_password]').val(); 
	if(email=="")
	{
		error_email += email_lost_password_empty + "<br />";
		count += 1;
	}
	else
	{		
		if(!validateEmail(email))
		{
			error_email += email_lost_password_invalid + "<br />";
			count += 1;
		}
	}
	if(count>0)
	{
		jQuery('#error_email_lost_password').html(error_email);
		return false;
	}
	else
	{
		jQuery('#error_email_lost_password').html('');
		return true;
	}
}

function cnt(str)
{	
	var r = 0;
	str=str.replace(/\s/g,' ');
	str=str.split(' ');
	
	for (i=0; i<str.length; i++) 
	{
		if (str[i].length > 0) r++;
	}
	return r;
}
function titleWordCounter(e, n)
{
	var title= jQuery('input[name=title_article]').val();
	var r = 0;
	title=title.replace(/\s/g,' ');
	title=title.split(' ');
	
	for (i=0; i<title.length; i++) 
	{
		if (title[i].length > 0) r++;
	}
	r = title.length;
	var remain =  n-r;
	if(remain<0)
		remain = 0;
	jQuery('#title_word_remain').html(remain);
	
	var keypressed = null;

	if (window.event)
	{
		keypressed = window.event.keyCode; 
	}
	else
	{
		keypressed = e.which; //NON-IE, Standard
	}

	var reValue;
	if (r>=n && keypressed!=8 && keypressed!=127)
	{
		reValue=false;
	}
	else reValue=true;	
	return reValue;
}

function summaryWordCounter(e, n)
{	
	var summary = jQuery('textarea[name=summary_article]').val();
	var r = 0;
	summary = summary.replace(/\s/g,' ');
	summary = summary.split(' ');
	
	for (i=0; i<summary.length; i++) 
	{
		if (summary[i].length > 0) r++;
	}	
	r = summary.length;
	var remain = n-r;
	if(remain < 0)
		remain = 0;
	jQuery('#summary_word_remain').html(remain);	
	var keypressed = null;

	if (window.event)
	{
		keypressed = window.event.keyCode; 
	}
	else
	{
		keypressed = e.which; //NON-IE, Standard
	}

	var reValue;
	if (r>=n && keypressed!=8 && keypressed!=127)
	{
		reValue=false;
	}
	else reValue=true;	
	return reValue;
}
function bodyWordCounter(e, n)
{	
	var y = tinyMCE.get('body_content').getContent();
	alert(y);
//	n=6;
	var summary = jQuery('textarea[name=body_article]').val();
	var r = 0;
	summary = summary.replace(/\s/g,' ');
	summary = summary.split(' ');
	
//	for (i=0; i<summary.length; i++) 
//	{
//		if (summary[i].length > 0) r++;
//	}
	
	r = summary.length;
	var remain = n-r;
	if(remain < 0)
		remain = 0;
	jQuery('#body_word_remain').html(remain);
	
	var keypressed = null;

	if (window.event)
	{
		keypressed = window.event.keyCode; 
	}
	else
	{
		keypressed = e.which; //NON-IE, Standard
	}

	var reValue;
	if (r>=n && keypressed!=8 && keypressed!=127)
	{
		reValue=false;
	}
	else reValue=true;	
	return reValue;
}
function keywordsCounter(e, n)
{		
	var keywords = jQuery('input[name=keywords]').val();
	var r = 0;
	keywords = keywords.replace(/\s/g,' ');
	keywords = keywords.split(',');	
//	for (i=0; i<keywords.length; i++) 
//	{
//		if (keywords[i].length > 0) r++;
//	}	
	r = keywords.length;
	var keyworks_remain = n-r;
	if(keyworks_remain < 0)
		keyworks_remain = 0;
	jQuery('#keywords_remain').html(keyworks_remain);	
	var keypressed = null;
	if (window.event)
	{
		keypressed = window.event.keyCode; 
	}
	else
	{
		keypressed = e.which; //NON-IE, Standard
	}
	var reValue;
	if (r>n && keypressed!=8 && keypressed!=127)
	{		
		reValue = false;
	}	
	else reValue=true;	
	return reValue;
}
function check_input_article()
{	
	var body = tinyMCE.get('body_content').getContent();
	var check = jQuery('input[name=check_agree]').is(':checked');	
	var category = jQuery('select[name=category_article]').val();	
	var error = '';
	var count = 0;
	var security_code = jQuery('#sc').val();	
	jQuery('.body_content').html(body);
	
	if(security_code == '')
	{
		error += security_code_empty + "<br/>";
		count += 1;
	}
	if(!check)
	{
		error += nocheck + "<br/>";
		count += 1;
	}
	if(category=="" || category=="NoSelected")
	{
		error += category_empty + "<br/>";
		count += 1;
	}
//	if(jQuery('select[name=subcategory_article]').val() == "")
//	{
//		error += subcategory_empty + "<br/>";
//		count += 1;
//	}
	if(jQuery('input[name=title_article]').val()=="")
	{
		error += title_empty + "<br/>";		
		count += 1;
	}
	else if(cnt(jQuery('input[name=title_article]').val()) > title_max)
	{
		error += title_error + "<br/>";		
		count += 1;
	}
	var summary = jQuery('textarea[name=summary_article]').val();
	if(jQuery('textarea[name=summary_article]').val()=="")
	{
		error += sumary_empty + "<br/>";		
		count += 1;
	}
	else if(cnt(summary) > summary_max)
	{
		error += summary_error + "<br/>";		
		count += 1;
	}
	if(body=="")
	{
		error += body_empty + "<br/>";		
		count += 1;
	}
	else
	{
		if(cnt(body)<body_minimum)
		{			
			error += body_error + "<br/>";		
			count += 1;
		}
		if(cnt(body)>body_max)
		{			
			error += body_error_max + "<br/>";		
			count += 1;
		}		
	}
	if(count>0)
	{
		jQuery('#error_write_article').html(error);
		$("div#source_div_id").appendTo("div#error_write_article");
		var local = document.location.href;
		local = local.substring(0, local.indexOf('#error_write_article'));
		document.location = local + '#error_write_article';
		return false;
	}
	else
	{
		jQuery('#error_write_article').html('');
		return true;
	}
}
//profile
function checkprofile()
{
	var count = 0;			
	if($('input[name=firstname]').val() == ""  )
	{
		error =firstname_error ;
		count = count+1;
		if (count > 0)
		{
			$('#firstname_error').html(error);
			
			return false;
		}	
	}
	else {
		$('#firstname_error').html('');					
	}
	
	if($('input[name=lastname]').val() == ""  )
	{
		error = lastname_error ;
		count = count+1;
		if (count > 0)
		{
			$('#lastname_error').html(error);
			
			return false;
		}
	}
	else {
		$('#lastname_error').html('');					
	}
	
	var email = $('input[name=email]').val();
	if(email == "")
	{
		error = email_error ;									
		count = count+1;
		if (count > 0)
		{
			$('#email_error').html(error);
			
			return false;
		}
	}
	else
	{
		if(!validateEmail(email))
		{
			error = email_incorrect ;
			
			count = count+1;
			if (count > 0)
			{
				$('#email_error').html(error);
				
				return false;
			}
		}						
		else
		{
			$('#email_error').html('');
		}						
	}
	
	if($('input[name=address1]').val() == ""  )
	{
		error = address1_error;
		count = count+1;
		if (count > 0)
		{
			$('#address1_error').html(error);
			
			return false;
		}
	}
	else {
		$('#address1_error').html('');					
	}
	
	
	var profilename = $('input[name=yourprofile]').val();
	if(trim(profilename)=="")
	{
		error = yourprofile_error ;
		
		count = count+1;
		if (count > 0)
		{
			$('#yourprofile_error').html(error);
			
			return false;
		}
	}
	else
	{
		$('#yourprofile_error').html('');
	}		
	
	
	return true;
}

