function toggle(el)
{
    lyr = document.getElementById(el);
    if (lyr.style.display != 'block') { lyr.style.display = 'block'; }
    else { lyr.style.display = 'none'; }
}
function ClipBoard(id)
{
	var ccc = MM_findObj(id);
    if (document.all){
	ccc.value = ccc.innerText;
	Copied = ccc.createTextRange();
	Copied.execCommand("Copy");
    }
    else
    {
        alert('Close this box and press \'CTL-c\' to copy');
        ccc.focus();
        ccc.select();
    }
}
function MM_findObj(n, d) { //v4.01
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function highlight(id) {
    var field = MM_findObj(id);
	field.focus();
  	field.select();
}
function checkBeforeEmail()
{
    semail=document.emailform.email.value;
    if(semail==''||semail.indexOf('@')==-1)
    {
       alert('please enter a valid email!');
       return false;
    }
    for (var i=0;i<document.emailform.elements.length;i++)
    {
       var e = document.emailform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox'&& !e.disabled)
       if(e.checked ==true) return true;
    }
    alert('Please select a image to email!');
    return false;
}
function checkAll()
{
    for (var i=0;i<document.myform.elements.length;i++)
    {
       var e = document.myform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox'&& !e.disabled)
       e.checked = document.myform.allbox.checked;
    }
}
function checkAll2()
{
    for (var i=0;i<document.emailform.elements.length;i++)
    {
       var e = document.emailform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox'&& !e.disabled)
       e.checked = document.emailform.allbox.checked;
    }
}
function opConfirm(text, conf)
{
    for (var i=0;i<document.myform.elements.length;i++)
    {
       var e = document.myform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox' && e.checked == 1 ) {
          if (conf) {
             return confirm(text);
          } else {
             return 1;
          }
       }
    }
    return false;
}

//added by alun
function getfilename( attaName )
{
	var s = attaName.lastIndexOf( '\\' );
	return attaName.substr(s+1, attaName.length - s -1);
}
function getexpirydate( nodays)
{
    var UTCstring;
    Today = new Date();
    nomilli=Date.parse(Today);
    Today.setTime(nomilli+nodays*24*60*60*1000);
    UTCstring = Today.toUTCString();
    return UTCstring;
}
function getcookie(cookiename)
{
    var cookiestring=""+document.cookie;
    var index1=cookiestring.indexOf(cookiename);
    if (index1==-1 || cookiename=="") return "";
    var index2=cookiestring.indexOf(';',index1);
    if (index2==-1) index2=cookiestring.length;
    return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function setcookie(name,value,duration)
{
	delcookie(name);
    if(!duration) duration=90;
    cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
    document.cookie=cookiestring;
}
function delcookie(name)
{
    cookiestring=name+"="+escape('')+";EXPIRES="+getexpirydate(-1);
    document.cookie=cookiestring;
}

function showcodes()
{
	if(!$('#imagecodes').is(":visible"))
	{
		$('#imagecodes').slideDown();
	}else{
		$('#imagecodes').slideUp();
	}

	
}


function showloginbox()
{
	$("#panel").animate({"top": "+=480px"}, "slow","swing");
		
}

function hideloginbox()
{
	$("#panel").animate({"top": "-=480px"}, "slow");
		
}



function report_image()
{
	if(!getcookie("reported_"+$("#tbl_img_id").val()))
	{
		$("#report-modal").modal();
	}else{
		alert('You\'ve already reported this image');
	}
}
function post_report()
{
	if(!$("#tbl_IP").val())			{alert('No ip');return false;}
	if(!$("#tbl_img_id").val())		{alert('No image id');return false;}
	if(!$("#tbl_fullname").val())	{alert('Please enter your name');return false;}
	if(!$("#tbl_email").val())		{alert('Please enter your email');return false;}
	if(!$("#tbl_problems").val())	{alert('Please select the reason');return false;}
	if(!$("#tbl_details").val())	{alert('Please enter the details');return false;}

	$("#reportdata").fadeOut();		

	$.ajax({
		 url: '/report.php',
		 type: 'POST',
		 data: {
					"tbl_IP":		$("#tbl_IP").val(), 
					"tbl_img_id":	$("#tbl_img_id").val(), 
					"tbl_fullname": $("#tbl_fullname").val(), 
					"tbl_email":	$("#tbl_email").val(), 
					"tbl_problems": $("#tbl_problems").val(), 
					"tbl_details": $("#tbl_details").val(), 
					"tbl_subject":	'Image report', 
					"done":	'1', 
					"tbl_is_gallery":	'0', 
				}, 
		 success: function(data, status) 
		 {
				
			$("#reportdata_sent").fadeIn();
			setcookie("reported_"+$("#tbl_img_id").val(),1,100);
		 }
	});
}


function save_settings()
{

	var datapost = $("#myform").serialize();

	$.ajax({
		 url: '/settings.php',
		 type: 'POST',
		 data: datapost, 
		 success: function(data, status) 
		 {
			alert(data);
			//$("#reportdata_sent").fadeIn();
			//setcookie("reported_"+$("#tbl_img_id").val(),1,100);
		 }
	});
}




function reset_settings()
{

	$.ajax({
		 url: '/settings.php',
		 type: 'POST',
		 data: "delsettings=1", 
		 success: function(data, status) 
		 {
			alert(data);
			//$("#reportdata_sent").fadeIn();
			//setcookie("reported_"+$("#tbl_img_id").val(),1,100);
		 }
	});
}
