
var APIreq;
var APIRequestData = "";
var xmlReq = null;
var Facebookenabled = 0;
var usersubmitted = 0;

function usersubmit() {
usersubmitted = 1;
}

function enablefacebook() {
	Facebookenabled = 1;
}

function APIRequest(url) {
	APIreq = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			APIreq = new XMLHttpRequest();
        } catch(e) {
			APIreq = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	APIreq = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		APIreq = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		APIreq = false;
        	}
		}
    }
	if(APIreq) {

		APIreq.open("GET", url, false);
		APIreq.send("");
		return APIreq.responseText;


	}
}



function FacebookRequest() {

  var user_box = document.getElementById("requestformat");

	try {
  // add in some XFBML. note that we set useyou=false so it doesn't display "you"
  user_box.innerHTML = "<fb:profile-pic uid=loggedinuser facebook-logo=false size=square></fb:profile-pic>:<fb:name uid=loggedinuser useyou=false></fb:name>";
  // because this is XFBML, we need to tell Facebook to re-process the document 
  FB.XFBML.Host.parseDomTree();
  } catch(err) {
  
  }
  	document.getElementById("requesthandler").value = user_box.innerHTML;
  	
  	  if (/silhouette/i.test(user_box.innerHTML)) {
 		 setTimeout("recheckfacebook()",2000);
      } else {
      		datastr = user_box.innerHTML;
      		document.getElementById("buddyicon").value = datastr.match(/src="(.+?)"/i)[1];  
 	 		document.getElementById("facebookid").value = datastr.match(/php\?id=([0-9]{1,15})/i)[1];    
 	 		document.getElementById("author").value = datastr.match(/title="(.+?)"/i)[1];  
 			updatePersonBox(1);
      }
}

function recheckfacebook() {

	
	 var user_box = document.getElementById("requestformat");
	document.getElementById("requesthandler").value = user_box.innerHTML;


  
  if (/silhouette/i.test(user_box.innerHTML)) {

  	document.getElementById("IDBoxTwitter").style.display = "none";
  	document.getElementById("IDBoxFlickr").style.display = "none";
  	document.getElementById("SocialWarning").style.display = "none";

  	
  	document.getElementById("SocialBox").style.height = "140px";
	document.getElementById("SocialBox").style.background = "transparent url(/media/social/comment/socialbackfacebook.gif) no-repeat bottom left";
	
  
  } else {
  
  
 	 	 datastr = user_box.innerHTML;
  
// 	 var rqstr = datastr.match(/<fb.+?php\?id=([0-9]{1,15}).+?src="(.+?)".+?title="(.+?)".+?$/i);
  
 	 document.getElementById("buddyicon").value = datastr.match(/src="(.+?)"/i)[1];  
 	 document.getElementById("facebookid").value = datastr.match(/php\?id=([0-9]{1,15})/i)[1];    
 	 document.getElementById("author").value = datastr.match(/title="(.+?)"/i)[1];  
 	 
 	 updatePersonBox(1);
 }


}

function submitform(f) {


	if (usersubmitted == 1) {
	try { 
		FB.Connect.showFeedDialog(67196519307, templatesDate); 
	} catch(err) {
	
	}
		// var file = f.action
		var file = "/includes/commentpost.asp";
		var str = getFormValues(f,"validate");
		xmlReq = getXML(file,str);
	}

//alert('Comment Submitted');




}


function TwitterRequest() {

	document.getElementById("IDBoxFlickr").style.display = "none";
	document.getElementById("SocialWarning").style.display = "none";
	document.getElementById("IDBoxTwitter").style.display = "block";
	document.getElementById("SocialBox").style.height = "182px";
	document.getElementById("SocialBox").style.background = "transparent url(/media/social/comment/socialbacktwitter.gif) no-repeat bottom left";
	document.getElementById("TwitterIDInput").focus();

}

function TwitterRequestGet() {

	document.getElementById("TwitterIDInput").style.background = "white url(/media/social/comment/indicator.white.gif) no-repeat 188px 6px";
	APIRequestData = APIRequest("http://www.baekdal.com/automate/twitterapi.asp?id=" + document.getElementById("TwitterIDInput").value);
  document.getElementById("requesthandler").value = APIRequestData;

	document.getElementById("TwitterIDInput").style.background = "white";
	if (/<error>Not found<\/error>/i.test(APIRequestData)) {
	
		document.getElementById("SocialWarning").style.display = "block";
	
	} else {

		if (/<name>(.+?)<\/name>/i.test(APIRequestData)) {
			document.getElementById("author").value = APIRequestData.match(/<name>(.+?)<\/name>/i)[1];
		}
	
		if (/<description>(.+?)<\/description>/i.test(APIRequestData)) {
			document.getElementById("description").value = APIRequestData.match(/<description>(.+?)<\/description>/i)[1];
		}
	
		if (/<profile_image_url>(.+?)<\/profile_image_url>/i.test(APIRequestData)) {
			document.getElementById("buddyicon").value = APIRequestData.match(/<profile_image_url>(.+?)<\/profile_image_url>/i)[1];
		}
	
		if (/<url>(.+?)<\/url>/i.test(APIRequestData)) {
			document.getElementById("website").value = APIRequestData.match(/<url>(.+?)<\/url>/i)[1];
		}
	
		if (/<screen_name>(.+?)<\/screen_name>/i.test(APIRequestData)) {
			document.getElementById("twitterid").value = APIRequestData.match(/<screen_name>(.+?)<\/screen_name>/i)[1];
		}
	
		updatePersonBox(1)
	
	}
	
}

function FlickrRequest() {
	
	document.getElementById("IDBoxTwitter").style.display = "none";
	document.getElementById("SocialWarning").style.display = "none";
	

	document.getElementById("IDBoxFlickr").style.display = "block";
	document.getElementById("SocialBox").style.height = "182px";
	document.getElementById("SocialBox").style.background = "transparent url(/media/social/comment/socialbackflickr.gif) no-repeat bottom left";
	
	document.getElementById("FlickrIDInput").focus();
	
	
}

function FlickrRequestGet() {

	document.getElementById("FlickrIDsubmit").style.background = "white url(/media/social/comment/indicator.white.gif) no-repeat 188px 6px";

	APIRequestData = APIRequest("http://www.baekdal.com/automate/flickrapi.asp?id=" + document.getElementById("FlickrIDInput").value);
	document.getElementById("requesthandler").value = APIRequestData;
	
	document.getElementById("FlickrIDsubmit").style.background = "white";

	if (/msg="User not found"/i.test(APIRequestData)) {
	
		document.getElementById("SocialWarning").style.display = "block";
	
	} else {
	
		if (/<realname>(.+?)<\/realname>/i.test(APIRequestData)) {
			document.getElementById("author").value = APIRequestData.match(/<realname>(.+?)<\/realname>/i)[1];
		} else if (/<username>(.+?)<\/username>/i.test(APIRequestData)) {
			document.getElementById("author").value = APIRequestData.match(/<username>(.+?)<\/username>/i)[1];
		}
		if (/<photosurl>(.+?)<\/photosurl>/i.test(APIRequestData)) {
			document.getElementById("flickrid").value = APIRequestData.match(/<photosurl>(.+?)<\/photosurl>/i)[1];
		}
		if (/nsid="(.+?)".+?iconserver="(.+?)".+?iconfarm="(.+?)"/i.test(APIRequestData)) {
			document.getElementById("buddyicon").value = "http://farm" + APIRequestData.match(/nsid="(.+?)".+?iconserver="(.+?)".+?iconfarm="(.+?)"/i)[3] + ".static.flickr.com/" + APIRequestData.match(/nsid="(.+?)".+?iconserver="(.+?)".+?iconfarm="(.+?)"/i)[2] + "/buddyicons/" + APIRequestData.match(/nsid="(.+?)".+?iconserver="(.+?)".+?iconfarm="(.+?)"/i)[1] + ".jpg";
		}
		
		updatePersonBox(1)
	}
}


function updateEmail() {

	if (document.getElementById("email").value != "") {
		APIRequestData = APIRequest("http://www.baekdal.com/automate/gravatar.asp?id=" + document.getElementById("email").value);
		document.getElementById("buddyicon").value = APIRequestData;

	}
	

updatePersonBox(0);

}

function updatePersonBox(i) {

	var srbuddyicon = document.getElementById("buddyicon").value;
	var srauthor = document.getElementById("author").value;
	var sremail = document.getElementById("email").value;
	var srdescription = document.getElementById("description").value;
	var srfacebookid = document.getElementById("facebookid").value;
	var srtwitterid = document.getElementById("twitterid").value;
	var srflickrid = document.getElementById("flickrid").value;
	var srwebsite = document.getElementById("website").value;
	var srsociallinks = 0;
	var srstr = "";

	// buddyicon
	if (srbuddyicon != "") {
		document.getElementById("PersonImg").src = srbuddyicon;
	} else {
		document.getElementById("PersonImg").src = "/media/social/comment/blankperson.gif";
	}

	// name
	if (srauthor != "") {
		document.getElementById("PersonName").innerHTML = srauthor + " <span id=\"mini\" onclick=\"toogleForms();\">(EDIT)<\/span>";
	} else {
		document.getElementById("PersonName").innerHTML = "Anonymous" + " <span id=\"mini\" onclick=\"toogleForms();\">(EDIT)<\/span>";
	}

	// description
	if (srdescription != "") {
		document.getElementById("PersonDescription").innerHTML = srdescription;
	} else {
		document.getElementById("PersonDescription").innerHTML = "(no description)"
	}

	// links
		
	if (srfacebookid != "" || srtwitterid != ""  || srflickrid != "") {
		srstr = "follow me on ";
		if (srfacebookid != "") {
			srstr = srstr + "<a href=\"http:\/\/www.facebook.com\/profile.php?id=" + srfacebookid + "\">Facebook<\/a>";
			srsociallinks = 1;
		}
		if (srtwitterid != "") {
			if (srsociallinks == 1) { srstr = srstr + ", "; }
			srstr = srstr +  "<a href=\"http:\/\/twitter.com\/" + srtwitterid + "\">Twitter<\/a>";
			srsociallinks = 1;
		}
		if (srflickrid != "") {
			if (srsociallinks == 1) { srstr = srstr + ", "; }
			srstr = srstr +  "<a href=\"" + srflickrid + "\">Flickr<\/a>";
			srsociallinks = 1;
		}
		if (srwebsite != "") {
			if (srsociallinks == 1) { srstr = srstr + ", "; }
			srstr = srstr +  "or visit <a href=\"" + srwebsite + "\">my website<\/a>";
		}
		
		
		
	} else {
		if (srwebsite != "") {
			srstr = "visit <a href=\"" + srwebsite + "\">my website<\/a>";

		}
	
	}
	
	document.getElementById("PersonLinks").innerHTML = srstr;
	
	// set cookies
	
	createCookie("bcombuddyicon",escape(srbuddyicon),365)
	createCookie("bcomauthor",escape(srauthor),365)
	createCookie("bcomemail",escape(sremail),365)
//	createCookie("bcomdescription",escape(srdescription.replace(/,/i,"[c]"),365)
	createCookie("bcomdescription",escape(srdescription),365)
	createCookie("bcomfacebookid",escape(srfacebookid),365)
	createCookie("bcomtwitterid",escape(srtwitterid),365)
	createCookie("bcomflickrid",escape(srflickrid),365)
	createCookie("bcomwebsite",escape(srwebsite),365)

	
	
	
	// close social box
	if (i != 0) {
  	document.getElementById("IDBoxTwitter").style.display = "none";
  	document.getElementById("IDBoxFlickr").style.display = "none";
  	document.getElementById("SocialWarning").style.display = "none";
  	document.getElementById("FormSettings").style.display = "none";
	
	
	document.getElementById("SocialBox").style.height = "70px	";
	document.getElementById("SocialBox").style.background = "transparent";
	
	document.getElementById("FormMain").style.display = "none";
	document.getElementById("Person").style.display = "block";
	}

}

function toogleForms() {
	document.getElementById("FormMain").style.display = "block";

}


function detectEnter(e,btarget) {
	document.getElementById("SocialWarning").style.display = "none";

	if(window.event) { keynum = e.keyCode; }
	else if(e.which) { keynum = e.which; }

	if (keynum == 13) {
		if (e.cancelable) { e.preventDefault(); }
		document.getElementById(btarget).click();
		return false;
	}
}

function OpenSettings() {

	document.getElementById("FormSettings").style.display = "block";

}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function handleCommentResponse(i) {
	if (i.match(/ERROR: TB99/i)) {
		document.getElementById("FormError").style.display = "block";
		document.getElementById("FormError").innerHTML = i;
	} else {
		document.getElementById("IDBoxTwitter").style.display = "none";
		document.getElementById("mini").style.display = "none";
  		document.getElementById("IDBoxFlickr").style.display = "none";
  		document.getElementById("SocialWarning").style.display = "none";
  		document.getElementById("FormSettings").style.display = "none";
	
	
		document.getElementById("SocialBox").style.height = "70px	";
		document.getElementById("SocialBox").style.background = "transparent";
	
		document.getElementById("FormMain").style.display = "none";
		document.getElementById("Person").style.display = "block";

		document.getElementById("FormComment").innerHTML = i;
	}	
	
}


function getXML(file,str) {
	var doc = null
       if (typeof window.ActiveXObject != 'undefined' ) {
           doc = new ActiveXObject("Microsoft.XMLHTTP");
       } else {
           doc = new XMLHttpRequest();
       }

       doc.open( "POST", file, false );
       doc.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
       doc.send(str);
       handleCommentResponse(doc.responseText);
       return true;

    }

    function getFormValues(fobj,valFunc) {
       var str = "";
       var valueArr = null;
       var val = "";
       var cmd = "";
       for(var i = 0;i < fobj.elements.length;i++) {
     		if (fobj.elements[i].type != "submit") { str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; }
       }
       str = str.substr(0,(str.length - 1));
       return str;
    }
