// JavaScript Document
function setVisibility(id, visibility) 
{
	document.getElementById(id).style.display = visibility;
}

function showKet(shID)
{
	if (document.getElementById(shID).style.display!='block')
		document.getElementById(shID).style.display='block';
	else
		document.getElementById(shID).style.display='none';
}

function showIsi(shID, penulis)
{
	if (document.getElementById(shID).style.display!='block')
	{
		document.getElementById(shID).style.display='block';
		hitung_pembaca1(shID, penulis);
	}
	else
		document.getElementById(shID).style.display='none';
}

function showStory(shID, penulis)
{
	if (document.getElementById(shID).style.display!='block')
	{
		document.getElementById(shID).style.display='block';
		hitung_pembaca_story1(shID, penulis);
	}
	else
		document.getElementById(shID).style.display='none';
}

function counter( field, count, limit )
{
	 if( field.value.length > limit )
	 {
			field.value = field.value.substring( 0, limit );
	 }
	 else
	 {
			count.value = field.value.length;
	 }
}

function counter( field, count, limit )
{
	 if( field.value.length > limit )
	 {
			field.value = field.value.substring( 0, limit );
	 }
	 else
	 {
			count.value = field.value.length;
	 }
}

// ====================================== Check EMAIL
function cekEmail(emailID)
{
	if ((emailID==null)||(emailID==""))
	{
		alert("Email harus diisi.");
		return false;
	}
	if (echeck(emailID)==false)
	{
		return false;
	}
	return true;
 }
 
function echeck(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){
		alert("Alamat Email Tidak Valid.")
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert("Alamat Email Tidak Valid.")
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		 alert("Alamat Email Tidak Valid.")
		 return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		 alert("Alamat Email Tidak Valid.")
		 return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		 alert("Alamat Email Tidak Valid.")
		 return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		 alert("Alamat Email Tidak Valid.")
		 return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		 alert("Alamat Email Tidak Valid.")
		 return false
	 }
	 
	 if(CharsInBag(str)==false)
	 {
		 alert("Alamat Email Tidak Valid.")
		 return false
	 }
	 
	 var arrEmail=str.split("@")
	 var ldot=arrEmail[1].indexOf(".")
	 
	 if(isInteger(arrEmail[1].substring(ldot+1))==false)
	 {
		 alert("Alamat Email Tidak Valid.")
		 return false
	 }

	 return true					
}

var bugchars = '!#$^&*()+|}{[]?><~%:;/,=`"\'';
function CharsInBag(s)
{   var i;
	var lchar="";
    // Search through string's characters one by one.
    // If character is not in bag.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
		if(i>0)lchar=s.charAt(i-1)
        if (bugchars.indexOf(c) != -1 || (lchar=="." && c==".")) return false;
    }
    return true;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is not a number.
        var c = s.charAt(i);
        if ((c >= "0") && (c <= "9") && (c != ".")) return false;
    }
    // All characters are numbers.
    return true;
}
// ====================================== Check EMAIL


function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

// ======================================= reize text area
function resize_ta(theArea)
{
	var theLines = theArea.value.replace((new RegExp(".{"+theArea.cols+"}","g")),"\n").split("\n");
	if(theLines[theLines.length-1]=="") 
		theLines.length--;
		
	theArea.rows=theLines.length+1;
}
