// JavaScript Document

/* ADDED BY SATHEESH 10-03-2010 03:42PM */
function getMembershipList(source,selectedValue)
 {
 	$.getJSON("/utilities/utility/membershiplist/",{id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
       $.each(j.rows, function(i,item){
			if(item.optionValue==selectedValue) {
				options += '<option value="' + item.optionValue + '" selected="selected">' + item.optionDisplay + '</option>';
			} else {
				options += '<option value="' + item.optionValue + '">' + item.optionDisplay + '</option>';
			}	
	   })

      $("select#"+source).html(options);
    })
 }
 function getSourceList(source,selectedValue)
 {
 	$.getJSON("/utilities/utility/sourcelist/",{id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
       $.each(j.rows, function(i,item){
			if(item.optionValue==selectedValue) {
				options += '<option value="' + item.optionValue + '" selected="selected">' + item.optionDisplay + '</option>';
			} else {
				options += '<option value="' + item.optionValue + '">' + item.optionDisplay + '</option>';
			}	
	   })

      $("select#"+source).html(options);
    })
 }
 
  /*
 	Eg: SelectDropDown("value",document.addEmployement.fromMonth -- Element Name); 
 */
 function SelectDropDown(selectItem,listBox) {

  var intIndex = 0;
  var SelectList = listBox;
  for( intIndex = 0; intIndex < SelectList.options.length; intIndex++ ) {
	 
		if( SelectList.options[intIndex].value == selectItem ) {
			SelectList.selectedIndex = intIndex;
			break;
		}
  }    
}
/* *** START ***Ajax more info code*/
var lastid=0;
var flip=true;
function showDetails(id,type)
{
	var poststr='id='+id+'&type='+type;
	if( lastid!= 0 && lastid != id) {
		$('#span_'+lastid).hide('slow'); flip=true;
	}

	if(flip == true) {
		$('#span_'+id).show();
		$('#span_'+id).html('<br><img src="/images/loadingAjax.gif" />')
		$('#a_'+id).addClass('commonboldlink11 grey');
		$.getJSON("/utilities/utility/article-innovation/",poststr, function(data) {
		  $('#span_'+id).html(data.rows.description);  
		} );
	 
		flip=false;
	} else {
		flip = true; 
		$('#span_'+lastid).hide('slow');
	}
	lastid=id;
	
	
	return false;
}
function hidebox(id)
{
	flip=true;//$('#a_'+id).addClass('commonlink11 grey');
	$('#span_'+id).hide('slow');
	return false;
}
/* *** END ***Ajax more info code*/
/* *** START ***Pagination code*/
function setPagination(count,topId,downId,topDet,botDet)
{
	//alert(count);
	var perPage = pagination['resultsPerPage'];
	
	if( parseInt(count) > 0 && parseInt(count) > parseInt(perPage) ){
		
		if(topId!=''){
			$('#'+topId).html(pagination['pager']);
		}
		if(downId!=''){
			$('#'+downId).html(pagination['pager']);
		}
		if(topDet!=''){
			$('#'+topDet).html(pagination['resultsDetails']);
		}
		if(botDet!=''){
			$('#'+botDet).html(pagination['resultsDetails']);
		}
	}
	else   {
		if(topId!=''){
			$('#'+topId).html('');
		}
		if(downId!=''){
			$('#'+downId).html('');
		}
		if(topDet!=''){
			$('#'+topDet).html(pagination['resultsDetails']);
		}
		if(botDet!=''){
			$('#'+botDet).html(pagination['resultsDetails']);
		}
	}
}
function ajaxPager(page)
{
	 
	var perPage = pagination['resultsPerPage'];
	//Eg: var url = '/article/prediction/pagerlist/${article/id}/' + page + '/' + perPage; 
	var url = requestUrl + page +  '/' + perPage;
	reg = /\s+/;
	url = url.replace(reg,'-');
	//Eg :  var Params='category=' + $('#prediction_type').val();
	$.getJSON(url,Params, function(data) {
		  $('#listContent').html(data.output);
	      pagination['pages'] = data.pages;
	      pagination['pager'] = data.pager;
	      pagination['currentPage'] = data.currentPage;
		  pagination['resultsDetails'] = data.resultsDetails;
		  setPagination(data.resultsCount,'topPagerId','downPagerId',"topdet","botdet"); 
		} );
	 	
}
/* *** END *** Pagination code*/
/* *** START ***Comments Display and Pagination code*/
function sortingBy(contenttype,bid,pagesize,page,field,order,actionname,urlp)
	{
		
		var fieldorder='ASC',urlPath; 
		if(order == null) {
			document.getElementById('orderField').value = field;
	
			if(document.getElementById('order').value == 'ASC') {
				document.getElementById('order').value = 'DESC';
				fieldorder = document.getElementById('order').value ;
			}else {
				document.getElementById('order').value = 'ASC';
				fieldorder = document.getElementById('order').value ;			
			}
		}else {
			fieldorder = order;
		}
		
        if(actionname ==  undefined )
			urlPath = urlp+"index/";
		else
			urlPath = urlp+actionname+"";
		$.ajax({
		type: "POST",
		url: urlPath,
		data: "itemId="+bid+"&page="+page+"&pagesize="+pagesize+"&field="+field+"&fieldorder="+fieldorder,
		success: function(msg){
			$('#postedComments').html(msg);
		}
		});
		
		
	}
/* *** END ***Comments Display and Pagination code*/
function validateComment(textId)
{
	if(lg == true) {
		if($.trim($("#"+textId).val())=='')
		{
			alert('Please enter a comment');
			$("#"+textId).focus();
			return false;
		}
	} else {
		if(confirm('You need to be logged in to Post a Comment.')) {
			window.location = "/utilities/utility/trackback?url="+encodeURI(window.location.toString());
			
		}
		return false;
	}	
}
function SelectOptionInList(selectItem,listBox) {

  var intIndex = 0;
  var SelectList = listBox;
  for( intIndex = 0; intIndex < SelectList.options.length; intIndex++ ) {
		if( SelectList.options[intIndex].value == selectItem ) {
			SelectList.selectedIndex = intIndex;
			break;
		}
  }    
}

function showIrreleventBox(contentId,url,type,titleId)
{
	var contentTitle = $("#"+titleId).text();
	//alert(contentTitle);
	//alert(document.getElementById(titleId).value);return false;
	//alert(type);return false;
	if(lg == true) {

	$.ajax({
			type: "POST",
		   	dataType: "json",
		   	url: "/utilities/utility/save-irrelevant",
		   	data:  {contentId:contentId,url:url,type:type,title:contentTitle},
		   	success: function(data){
				if(data.success==1){	
					alert('This comment has been reported as abuse.');
				} else {
					alert('This comment already reported as abuse.');
				}
	   	    }
		});
	} else {
		if(confirm('You need to be logged in to report this comment as abuse.')) 
			window.location = '/utilities/utility/trackback';
	}
	
}
function showShareBox(url)
{
	if(lg == true) {
		location.href=url;
	} else {
		if(confirm('You need to be logged in to Share this.')) 
			window.location = '/utilities/utility/trackback';
	}
}
function focusc(key)
{
	if( key.value == key.title )
		key.value = '';
}
function blurc(key)
{
	if( key.value == '')
		key.value = key.title; 
}
function validateSearchform(key,id)
{
	key = document.getElementById(key);
	if( key.value == '' || key.value == key.title ){
		alert('Please enter your search word');
		return false;
	}
	else 
		$("#"+id).submit();
}
function addBookmark(){
	var title =  document.title;
	var url = location.href;
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
	else 
		alert('You need to press Command/Cmd + D to bookmark our site.');
}

function textAreaLimit( field, maxlimit ) {
  var fieldValue = document.getElementById(field).value;
  if ( fieldValue.length > maxlimit )
  {
    var fieldValue1 = fieldValue.substring( 0, maxlimit );
    document.getElementById(field).value=fieldValue1;
  }
  
}
function addSubscribe(friendId)
{
	
		$.ajax({
		type: "POST",
		url: "/user/profile/add-subscribe/",
		data: "friendId="+friendId+"&fromInnovation=yes",
		success: function(msg){
			$('#subscribeDiv').html(msg);
		}
		});
		
}
function deleteSubscribe(friendId)
{
	
		$.ajax({
		type: "POST",
		url: "/user/profile/delete-subscribe/",
		data: "friendId="+friendId+"&fromInnovation=yes",
		success: function(msg){
			$('#subscribeDiv').html(msg);
		}
		});
		
}

function cancelAction(tar)
	{
		//if(confirm('You will loose all the data, Are You Sure To Continue?'))
			window.location = tar;
	}

function showPreviewAdmin(titvar,descvar)
{
	var fckcontent = FCKeditorAPI.GetInstance(descvar).GetXHTML();
	document.getElementById('popupContact').style.display = '';
	document.getElementById('backgroundPopup').style.display = '';
	document.getElementById('popupContact').innerHTML = '<a id="popupContactClose" onclick="hidePop()">x</a><h1>'+document.getElementById(titvar).value+'</h1><p id="contactArea">'+fckcontent+'</p>';
}

function hidePop()
{
	document.getElementById('popupContact').style.display = 'none';
	document.getElementById('backgroundPopup').style.display = 'none';
}

//Printing Intenernal Page 
	function openPrint(bid,type)
	{
		window.open('/blog/index/print?id='+bid+'&type='+type);
	}
//Printing Modulewise Page 
	function openPrintModule(type)
	{
		window.open('/blog/index/print-module?type='+type);
	}
//Email Function 
function getEmail(bid,type)
	{
		if(lg == true)
		{
			window.location = '/utilities/share/send-email?id='+bid+'&type='+type;
		} else {
		if(confirm('You need to be logged in to Send Email.')) {
			window.location = "/utilities/utility/trackback?url="+encodeURI(window.location.toString());
		}
		}
	}
function pages(contenttype,bid,pagesize,page,field,order,actionname,urlp)
	{
		//alert(order);
		//alert(document.getElementById('order').value);
		//var fieldorder='ASC',urlPath; 
		if(order == null) {
			document.getElementById('orderField').value = field;
	
			if(document.getElementById('order').value == 'ASC') {
				document.getElementById('order').value = 'DESC';
				fieldorder = document.getElementById('order').value ;
			}else {
				document.getElementById('order').value = 'ASC';
				fieldorder = document.getElementById('order').value ;			
			}
		}else {
			fieldorder = order;
			//alert(fieldorder);
		}
		
        if(actionname ==  undefined )
			urlPath = urlp+"index/";
		else
			urlPath = urlp+actionname+"";
			//alert(urlPath);
		$.ajax({
		type: "POST",
	    dataType: "json",
		url: urlPath,
		data: "itemId="+bid+"&page="+page+"&pagesize="+pagesize+"&field="+field+"&fieldorder="+order+"&contentType="+contenttype,
		success: function(msg){
			if(contenttype =='')
			{
				$('#postedComments').html(msg.output);
			}
			else
			{
				//alert(contenttype);
				//$('#commentsId').show('slow');
				$('#postedComments').html(msg.output);
				//$('#commentsOrder').hide('slow');
			}
			
		}
		});
		
	}
/////for video Player					
function getVideoPlayer(divId,videoId)
{
	$.ajax({
			type: "POST",
			dataType: "json",
			url: "/utilities/utility/video-player/"+videoId,
			success: function(data){
						
			$('#'+divId).html(data.output);
		 }
		});
}		
////for submiting the form to equibase
function getEquibaseData(urltodisplay)
{
	
	$('#equibaseURL').val(urltodisplay);
	document.getElementById('equibaseForm').submit();
}

/* ADDED BY SATHEESH 07-06-2010 02:35PM */
function addMyFavorite(bid,type)
{
	if(lg == true)
	{
		$.ajax({
		type: "POST",
	    dataType: "json",
		url: '/blog/index/add-my-favorite',
		data: "id="+bid+"&type="+type,
		success: function(data){
			if(data.success > 0)
				alert('Added to Favorites successfully');
			else
				alert('Adding to Favorites failed as this is already added');
			
		}
		})
	} else {
	if(confirm('You need to be logged to Add to Your Favorites.')) {
		window.location = "/utilities/utility/trackback?url="+encodeURI(window.location.toString());
	}
	}
}
/* END */
