﻿function isEnterPress(e) {
    var key = e.keyCode || e.which;
    if(key == 13) {
        return true;
    }
    return false;
}

function urlEncode(str) {
    return str.replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}

function ajaxloadBlog(url, containerid, condition) {
	try { // Browser object detection		
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
		new ActiveXObject("Microsoft.XMLHTTP");		
	}
	catch (e){alert(e.description)} // browser doesn't support ajax. handle however you want
	xmlhttp.onreadystatechange = function(){statuschangedBlog(containerid,condition)}; // every time ready status changes, statuschanged() function executes
	xmlhttp.open("GET", url, true); // Usage: open(HTTP method, url, and asynchronous = true or false)
	xmlhttp.send(null); // send the request.
}

function statuschangedBlog(containerid, condition){
	if((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { // readyState codes: 0=Uninitialised 1=Loading 2=Loaded 3=Interactive 4=Completed; status code: 200=OK		
		if(xmlhttp.responseText != "") {
			if(condition == "h") { // populating a hidden field
				top.document.getElementById(containerid).value = xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}
			else if(condition == "a") { // if condition is append
				top.document.getElementById(containerid).innerHTML += xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}
			else {							
				top.document.getElementById(containerid).innerHTML = xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}			
		}
	}
	else { // else, readyState is not yet Completed and status is not yet OK, so here we display loading text
	    top.document.getElementById(containerid).innerHTML = 
	        "<div id='loading'>" +
	            "<div class='left'><img src='/App_Themes/Default/Images/loading.gif' alt='Loading...' /></div>" +
	            "<div class='right'>Loading...</div>" +
	            "<div class='clear'></div>" +
	        "</div>";
	}
}

var categoryId = "";
var keyword = "";

function setCategory(id, catName) {



    window.location = '/Web-Marketing-Blog/Topics/' + urlEncode(catName) + '/' + urlEncode(id);
}

function setKeyword() {
    keyword = trim(document.getElementById("txtKeyWordSearch").value);
    window.location = '/Web-Marketing-Blog/Search/' + urlEncode(keyword);

}

function goToBlog(id, url) {
    var url = "Blog.aspx?id=" + id + "&url=" + url;
    ajaxloadBlog(url,"main","");    
}

function loadComments(id, page) {    

    var url = "/_BlogServe/CommentsLoad.";
    
    url += "asp";
    url += "x";
    url += "?id=" + id + "&page=" + page;
    
    ajaxloadBlog(url,"comments","");    
    
}

function reloadComments(id) {
    loadComments(id, 1);
    resetForm(false);
}

function resetForm(showMessage) {
    if(showMessage) {
        alert('Thank You!\nYour comment has been submitted for approval');
    }
    top.document.getElementById("txtBlogName").value = "";
    top.document.getElementById("txtBlogDescription").value = "";
}

var httpSendComment = new getHTTPObject();

function addComment(id) {

    // check for hack input
    if (document.getElementById("txtSmart").value != "")
    {
        return;
    }
    var name = urlEncode(document.getElementById("txtBlogName").value);
    var blogUrl = urlEncode(location.href);
    
    var description = urlEncode(document.getElementById("txtBlogDescription").value);
    if(description == "") {
        alert("Error! You must enter a comment!");
        return;
    }
    
	sUrl = "/_blogserve/PostComment";
	
	sUrl += ".";
	sUrl += "asp";	
	sUrl += "x";	
	
    var query = "u=" + blogUrl + "&id=" + id + "&name=" + name + "&description=" + description;

	httpSendComment.open("POST", sUrl, true);
    httpSendComment.onreadystatechange = processResponseForNotesUpdate;
    httpSendComment.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    httpSendComment.setRequestHeader("Connection", "close");    
    
    httpSendComment.send(query);
    
	setTimeout("reloadComments(" + id + ");", 750);    
    
}

function processResponseForNotesUpdate() {

  if (httpSendComment.readyState == 4) {

    //alert(httpSendComment.responseText);
		
  }
}


function getHTTPObject() {

  var xmlhttp;

/*@cc_on

  @if (@_jscript_version >= 5)

  try {

    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

  } catch (e) {

    try {

      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    } catch (E) {

      xmlhttp = false;

    }

  }

  @else

    xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

  return xmlhttp;

}
