var CurrDesc = -1;

function ShowDesc(n){
	if(CurrDesc>-1){
		var oldDesc = document.getElementById("desc_"+CurrDesc);
		oldDesc.style.display="none";
	}
	
	if(CurrDesc==n){
		CurrDesc=-1;
	}else{
		var desc = document.getElementById("desc_"+n);
		desc.style.display="block";
		
		CurrDesc= n;
	}
}

function AddBlog(){
	var title = document.getElementById("blog_title").value;
	var content = document.getElementById("blog_content").value;
	var bdate = document.getElementById("blog_date").value;
	var func = "AddPost";
	//alert("Adding Blog: "+title);
	
	var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'blog_code.php', true);
	//	alert("Saving Project");
		// The callback function
		xmlhttp.onreadystatechange = function() 
		{
			//alert("Saving Project: "+xmlhttp.readyState);
			if (xmlhttp.readyState == 4) {
			//	alert("Status: "+xmlhttp.status);
				if (xmlhttp.status == 200) {						
						var myXML= xmlhttp.responseText;
						var result= myXML.split("~");
					if (result[0] == "true") {
						
						AddImageUploader(result[1]);
					 } else {
						alert(myXML);
						alert('There was a problem adding the blog post. Please try again');
					}

				  }
				}


		}
				
			//	alert('mail=' + mail+'&pass='+pass+'&LJ='+addLJ+'&global=' + addGlobal+'&send=' + sendMail)
		 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		 xmlhttp.send('title=' + title+'&desc=' + content+'&date=' + bdate+'&func=' + func);
}

function AddImageUploader(n){
	document.getElementById('blog_submit').style.display="none";
	//alert("ADDING UPLOADER FOR ID: "+n);
	var z = document.createElement('div');
	var upload_area = document.getElementById("upload_area");
	var uploader = '<h4>Blog Image</h4><form method="post" enctype="multipart/form-data"  id="blog_image_form"   action="upload_step.php">';	
	uploader+='<input type="file" accept=".png, .jpg" name="blog_image" id="blog_image" onchange="UploadBlogImage('+n+')" />';
	uploader+='<button type="submit" id="btn_upload" style="display:none">Upload File</button>';
	uploader+='<div id="blog_image"></div>';
	uploader+='<div class="divide-xs"></div></form>';			
	z.innerHTML=uploader;	
	upload_area.appendChild(z);
}

/************************ ADD BLOG IMAGE **************************/
function UploadBlogImage(n) {
	
  	var stepData = new FormData();
	//document.getElementById("stepresponse_"+totalSteps).innerHTML = "Uploading Photo. . ."
	var myrow=document.getElementById("blog_image");
	var len = myrow.files.length;	
	var file;
	for ( var i = 0; i < len; i++ ) {
		file = myrow.files[i];
		
		if (!!file.type.match(/image.*/)) {
		//	alert(file.type)
			if ( window.FileReader ) {
				reader = new FileReader();
				reader.onloadend = function (e) { 
					//showUploadedItem(e.target.result, file.fileName);
				};
				reader.readAsDataURL(file);
			}
			if (stepData) {
				stepData.append("images[]", file);
				stepData.append("bid", n);
			}
		}	
	}
	if (stepData) {
		$.ajax({
			url: "upload_blog.php",
			type: "POST",
			data: stepData,
			processData: false,
			contentType: false,
			success: function (res) {
				var resp= res;
			//	document.getElementById("stepresponse_"+totalSteps).innerHTML = res; 
				if(resp=="true"){
					window.location.href = "blog_posts.php";
				}
			}
		});
	}
}

function deleteBlog(n){
	var id = n;
	var func = "DeletePost";
	
	var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'blog_code.php', true);
	//	alert("Saving Project");
		// The callback function
		xmlhttp.onreadystatechange = function() 
		{
			//alert("Saving Project: "+xmlhttp.readyState);
			if (xmlhttp.readyState == 4) {
			//	alert("Status: "+xmlhttp.status);
				if (xmlhttp.status == 200) {						
						var myXML= xmlhttp.responseText;
						var result= myXML;
					if (result == "true") {
						
						window.location.href = "blog_posts.php";
					 } else {
						alert(myXML);
						alert('There was a problem deleting the blog post. Please try again');
					}

				  }
				}


		}
				
			//	alert('mail=' + mail+'&pass='+pass+'&LJ='+addLJ+'&global=' + addGlobal+'&send=' + sendMail)
		 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		 xmlhttp.send('id=' + id+'&func=' + func);
}
function validate(v, s)
{
	var elem= document.getElementById(v);
	var x = escape(elem.value);
	tester=(x.replace(/^\W+/,'')).replace(/\W+$/,'');
    if (tester == ""){
		alert("You need to assign "+s);
		elem.focus();
		return false;
    }else{
		return true;
    }
}
