var siteUrl = "";
var FbAPI = "aea95e698e4e14a3c9672cd366950ac1";
FB.init(FbAPI, "/xd_receiver.htm"); 

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

var http = createRequestObject();
/* Post comment  */
function ckfblogin(dId){
  var isFbuser= document.getElementById('isFbUser').value; 
	if(isFbuser==1){ 
	 if(FB.Connect.get_loggedInUser()){
		 return true;
	 }else{
		 FB.Connect.requireSession(function(){
			var url="/flogin.php";
			var fbuid = FB.Connect.get_loggedInUser();
						
			var param = "fbUid="+fbuid;
			http.open('POST', url, true);
			http.onreadystatechange = function(){
				if(http.readyState == 4){
					saveComment(dId);
					return true;
				}
			};
			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
			http.send(param);			
		 });		 
	 }
	}else{
		return true;
	}

}
function postAUcomment(dId){
   var comm = document.getElementById('comment').value;
	comm = comm.replace(/^\s+/g, '');
	if(ckfblogin(dId)){
		
    if(comm==="")
	{
		alert("Please write comment");
	}
	else
	{
		saveComment(dId);
	}
	
	}
}

function saveComment(dId){
 var comm = document.getElementById('comment').value;
 var url= siteUrl+"/altuse/postcomment.php";
	var param = "mess="+comm+"&did="+dId+"&act=1";
	 http.open('POST', url, true);
	 http.onreadystatechange = function(){
	    if(http.readyState == 1){
			document.getElementById('loading').style.display = "block";
		}else if(http.readyState == 4){
			document.getElementById('loading').style.display = "none";
			var frm = document.getElementById('commentFrame');
			frm.src=frm.src;			
			postonfb(comm);
			document.getElementById('comment').value="";
			
		}
	  };
	  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	  http.send(param);
}

/* To delete the comment @param comment ID*/

function delcom(cid,dId){

	if(confirm("Are you sure you want to delete this comment?")){
		
	var url=siteUrl+"/altuse/postcomment.php";
	var param = "did="+dId+"&cid="+cid+"&act=2";
	 http.open('POST', url, true);
	 http.onreadystatechange = function(){
	    if(http.readyState == 1){
			parent.document.getElementById('loading').style.display = "block";
		}else if(http.readyState == 4){
			parent.document.getElementById('loading').style.display = "none";
			var frm = parent.document.getElementById('commentFrame');
			frm.src=frm.src;
			
		}
	  };
	  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	  http.send(param);
	}

}

function postonfb(comm){
	var isfbUser = document.getElementById('isFbUser').value;
	if(isfbUser==1){
		var useTile = document.getElementById('auTitle').value;
		var useSummery = document.getElementById('auSumry').value;
		var callUrl = document.getElementById('auUrl').value;
		var auDesc = document.getElementById('auDesc').value;

		var imgThmb = document.getElementById('auImgSrc').value;
		var action_links = {'text':'See More', 'href':callUrl}; 
		var Name = useSummery;
			
		var attachment = {'caption': '{*actor*} commented on the AltUse - '+useTile,'name':Name, 'href':callUrl,'description':auDesc,'media': [{'type': 'image', 'src': imgThmb, 'href':callUrl}]};
		
		var action_links = [{'text':'See more', 'href':callUrl}]; 
		FB.Connect.streamPublish(comm, attachment, action_links,'','Your comment',null,false);

		//FB.Connect.showFeedDialog('',attachment,null,'',50,FB.RequireConnect,null,'Your comment',comm);
	}
	
  
}
/// Pop Altuse Login window ///
function aulogin(){
	var lpObj = document.getElementById('loginpopup');
	var puBgObj = document.getElementById('popupBg');
	var srHt = getScrollHeight();
	hideSelectElement(1);	
	  var wt = 0;
	  var ht = 0;	  
     // For (mozilla/netscape/opera/IE7) 
	 if (typeof window.innerWidth != 'undefined'){
		wt = window.innerWidth;
		ht = window.innerHeight;				  
	 }// For IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	 else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
		wt = document.documentElement.clientWidth;
		ht = document.documentElement.clientHeight;				 
	} // for older versions of IE
	else{
		wt = document.getElementsByTagName('body')[0].clientWidth;
		ht = document.getElementsByTagName('body')[0].clientHeight;				   
	}
	 
	 
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;		
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;		
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;		
  	}


  puBgObj.style.width = wt+"px";
  puBgObj.style.height = (yWithScroll)+"px";
  puBgObj.style.display="block";

  var xQrd = wt/2 - 282;
  var yQrd = ht/2 - 175 + srHt;
  lpObj.style.left = xQrd+"px";
  lpObj.style.top = yQrd+"px";
  lpObj.style.display = "block";
	/// Getting the login popup box via ajax ////
	var url=siteUrl+"/popUplogin.php";
	var param = "act=getform";
	 http.open('POST', url);
	 http.onreadystatechange = function(){
	    if(http.readyState == 4){			
			lpObj.innerHTML = http.responseText;		
			
		}
	  };
	  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	  http.send(param);
}

function markabuse(cId){
	
	if(confirm("This comment will be flagged as 'Abusive'.  Do you wish to continue?")){
		
	var url=siteUrl+"/altuse/postcomment.php";
	var param = "cid="+cId+"&act=3";
	 http.open('POST', url, true);
	 http.onreadystatechange = function(){
	    if(http.readyState == 1){
			 parent.document.getElementById('loading').style.display = "block";
		}else if(http.readyState == 4){
			 parent.document.getElementById('loading').style.display = "none";
			var frm = parent.document.getElementById('commentFrame');
			frm.src=frm.src;
			
		}
	  };
	  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	  http.send(param);
	}
}

function pu_close(elId){
	 hideSelectElement(2);
	 document.getElementById(elId).style.display='none';
	 document.getElementById('popupBg').style.display='none';
}

// Login in altuse using popup login winodw //
function validateLogin()
{
	/// To validate input fields (username and password)//
	var nameObj = document.getElementById('login');
	var passObj = document.getElementById('pass');
	
	if(trimValue(nameObj.value)==""){
		 alert(getMessage(MANDATORY,'User Name'));
		 nameObj.focus();
	}
	else if(passObj.value==""){
		alert(getMessage(MANDATORY,'Password'));
		passObj.focus();
	}
	else
	{
		/// Send the login request using ajax ///
		var url=siteUrl+"/popUplogin.php";
		var param = "act=formsubmit&txtLogin="+nameObj.value+"&txtPassword="+passObj.value;
		 http.open('POST', url);
		 http.onreadystatechange = function(){
			if(http.readyState == 1){
				document.getElementById('loginError').innerHTML ="<img src='"+siteUrl+"/images/saving.gif' border='0' />";
			}else if(http.readyState == 4){
				if(http.responseText=="OK"){
					window.location.reload();
				}else{
					document.getElementById('loginError').innerHTML = http.responseText;	
				}
				
			}
		  };
		  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
		  http.send(param);
	}
	return false;
}

// Disabled select element for IE6 //
function hideSelectElement(ind){
	var selElObj = document.body.getElementsByTagName("select");
	var totSelEl = selElObj.length;

	var browser = navigator.appName;
    var version = navigator.appVersion;
	var version1 = version.substring(22,25);
	if(browser== "Microsoft Internet Explorer" && version1=="6.0"){

	switch(ind){
		/// Disabled the select element
		case 1: 
			for(var i=0; i<totSelEl; i++){
			selElObj[i].style.display = 'none';
		}
			break;
		/// Enabled the select element
		case 2:
			for(var i=0; i<totSelEl; i++){
			selElObj[i].style.display = 'block';
		}
			break;
	}
  }
}


