// Show/hide comment post box
function showhide_comments_post_box()
	{
		if (document.getElementById('comments_post_box').style.display == '')
			{
				document.getElementById('comments_post_box').style.display = 'none';
			}
		else
			{
				document.getElementById('comments_post_box').style.display = '';
			}
	}

function validate_showhide_comments_post_box(id)
	{
        var status = getCookie('script_'+id+'_comment')
        
        if ((status==null) || (status==""))
            showhide_comments_post_box();
		else
			alert('You have already posted your comments here!');	
	}

// Shows a message inside page through a hidden div layer
function message(msg_str)
	{
		if(msg_str == 0)
			{
				document.getElementById('message').innerHTML='';
				document.getElementById('message').style.display = 'none';
			}
		else
			{
				document.getElementById('message').innerHTML= msg_str;
				document.getElementById('message').style.display = '';
			}
	}
	
// Function to count characters in form	
function count_char(field, maxlimit) 
	{
		if (field.value.length > maxlimit)
			field.value = field.value.substring(0, maxlimit);
		else
			message('Charecters Left: '+(maxlimit - field.value.length));
	}
	
// Function to validate email
function email_check (emailStr) 
	{
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)
		
		if (matchArray==null) 
			{
				return false
			}
		var user=matchArray[1]
		var domain=matchArray[2]
		
		if (user.match(userPat)==null) 
			{
				return false
			}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null)
			{
				// this is an IP address
				for (var i=1;i<=4;i++) 
					{
						if (IPArray[i]>255) 
							{
								return false
							}
					}
				return true
			}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) 
			{
				return false
			}
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
			{
				return false
			}
		if (len<2) 
			{
				return false
			}
		return true;
	}
	
function inst_write_new_item()
	{			
		var date = today_date();
		
		if(post_box.email.value !='')
			name_code = '<a href="mailto:'+post_box.email.value+'">'+post_box.name.value+'</a>';
		else
			name_code = post_box.name.value;
					
		var code = '<div class="item"><div class= "name">[ '+date+' ] :: '+name_code+'</div>'+post_box.comment.value;+'</div>';
			
		document.getElementById('new_post').innerHTML= code;
		document.getElementById('new_post').style.display = '';
	}
	
// submit_comments
function submit_comment(id)
{
	if (post_box.name.value=='')
	{
		message('Please provide your name!')
		document.post_box.name.focus();
		return false
	}
	
	if (post_box.comment.value=='')
	{
		message('You should provide your comment!');
		document.post_box.comment.focus();
		return false
	}
	
	if ((post_box.email.value !='') && (!email_check(post_box.email.value)))
	{
		message('Your email seems invalid!');
		document.post_box.email.focus();
		return false
	}
	document.post_box.submit();	
	alert("Thank you posting your valuable comments!");
	//document.getElementById('comments_post_box').style.display = 'none';
	inst_write_new_item();
	//setCookie('script_'+id+'_comment', '1', 3) // once a user puts comment he cannot recomment within 3 days
	post_box.name.value = '';
	post_box.email.value = '';
	post_box.comment.value = '';
	message(0);
	return false
}


//function to find out todays date	
function today_date()
	{
		var today			= new Date();
		var today_year		= today.getFullYear();
		var today_month		= today.getMonth()+1;
		var today_date		= today.getDate();
		var date			= today_year+'-'+today_month+'-'+today_date
		return date;	
	}


function write_comments_control(id)
	{
		document.write('<div class="header">');
		//document.write('<div class="post_link">[ <a href="#comments_post_box" onclick="javascript:validate_showhide_comments_post_box('+id+'); message(0);">Post</a> ]</div>');
		document.write('<div class="text">Comments:</div>');
		document.write('</div>');
		
		// Post Box
		
		
		document.write('<form name="post_box" action="comments_ram.php" method="post" target="ifrrate">');		
		document.write('<div id="comments_post_box">');
		
		// Hidden Informations about this form
		document.write('<input type="hidden" name="action" value="post">');
		document.write('<input type="hidden" name="id" value="'+id+'">');
		
		document.write('Name:<br>');
		document.write('<input type="text" name="name" size="45" onkeydown="count_char(this.form.name, 50);" onkeyup="count_char(this.form.name, 50);"><br>');
		document.write('Email:<br>');
		document.write('<input type="text" name="email" size="45" onkeydown="count_char(this.form.email, 50);" onkeyup="count_char(this.form.email, 50);"><br>');
		document.write('Comment:<br>');
		document.write('<textarea rows="3" cols="38" name="comment" onkeydown="count_char(this.form.comment, 300);" onkeyup="count_char(this.form.comment, 300);"></textarea><br>');

		// Message
		document.write('<br><div id="message" style="display:none"></div><br>');
		document.write('<div class="button">');
		document.write('<input type="submit" onclick="return submit_comment('+id+');" value="   Post   ">');
		document.write('<input type="reset" onclick="message(0);" value="   Clear  ">');
		document.write('<input type="button" onclick="showhide_comments_post_box();" value="   Close  "></div>');
		
		document.write('</div>');
		// End Post Box		
		document.write('</form>');
		
		
		
		document.write('<div id="new_post" style="display:none"></div>');
	}
	

// Stops Javascript Errors
function stoperror()
    {
        //alert('You have an javascript error in this page');
        return true;
    }

// Add hyperlink to div tags	
function go(url)
    {
        window.location=url;
    }
      
// Closes a window
function close_window()
	{
		window.close();
	}
    
// Url encode to lowercase and remove whitespace and '&'
function url_enc(str)
	{
		str = str.toLowerCase();
		str = str.replace(/ & /, "_");
		str = str.replace(/ /, "_");
		str = str.replace(/ /, "_");
		str = str.replace(/ /, "_");
		str = str.replace(/'/, "");
		return str;
	}
	
// function to get a cookie
function getCookie(c_name)
{
    if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=")
            if (c_start!=-1)
                { 
                    c_start=c_start + c_name.length+1 
                    c_end=document.cookie.indexOf(";",c_start)
                    if (c_end==-1) c_end=document.cookie.length
                    return unescape(document.cookie.substring(c_start,c_end))
                    } 
        }
            return ""
}
    

    
// function to set a cookie
function setCookie(c_name,value,expiredays)
{
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) // If then else statement ?:
}