/* $Id: templib.js,v 1.15 2009/01/05 12:42:02 ganeshprabhu Exp $ */


/**
 * templib.js 
 *
 *
 * Created: Mon 21 Mar 2005 16:10
 *
 * @author <a href="mailto: vijayr">vijayr</a>
 * @version
 **/




function Templib() {

}

function trim(str)
{
		return str.replace(/\s+$/,'');
}	

Templib.searchContent = function(authName){

		  var st = document.searchForm.stext.value;

		  st = trim(st);

		  if ( st.length == 0 ){
			alert(Pleasespecifythewordyoutrytosearch);
		    	document.searchForm.stext.focus;
				return false;
		  }	  

		  if (authName != "publicViewer" ) {
		  	document.searchForm.action="/srchService.do";	
		  }
		  else
		  {
		  	document.searchForm.action="/srchService.im";	
		  }	
		
		  document.searchForm.actionval.value = "TemplateLibrarySearch";	
		  document.searchForm.method="Post";
		  document.searchForm.submit();
}	

Templib.loadPage = function (pageval,authName,val,act) {
		
		  document.templateForm.actionval.value = "TemplateLibrarySearch";
		  document.templateForm.stext.value = val;
		  document.templateForm.pageCount.value = pageval;

		  if (authName != "publicViewer" ) {
			if ( act == "SearchResult" ) 
			{
					 document.searchForm.actionval.value = "TemplateLibrarySearch";
                        		 document.searchForm.stext.value = val;
                        		 document.searchForm.spageCount.value = pageval;
					 document.searchForm.action="/srchService.do";	
			}	
			else {
					 document.templateForm.action="/templates.htm";	
				 }	
		  }
		  else
		  {

			if ( act == "SearchResult" ) 
				{
					  document.searchForm.actionval.value = "TemplateLibrarySearch";
                        		  document.searchForm.stext.value = val;
                        		  document.searchForm.spageCount.value = pageval;
					  document.searchForm.action="/srchService.im";	
				}	
				else {			  
			  		   document.templateForm.action="/templates.im";	
				}
		  }	
		

		  if ( act == "SearchResult" ) { 
		  	document.searchForm.method="Post";
		  	document.searchForm.submit();
		  }else{	
		  	document.templateForm.method="Post";
		  	document.templateForm.submit();
		  }	
	}




Templib.ldTemplate = function(tmp_id,authName) {

	var tmp_id = "" + tmp_id;

	document.templateViewform.TEMPLATE_ID.value = tmp_id;	
	document.templateViewform.tempAction.value = "gettempContent";

	if (authName != "publicViewer" ) {
		document.templateViewform.action="/templateView.do";	
	}
	else
	{
		document.templateViewform.action="/templateView.im";	
	}	
	
	document.templateViewform.method="Post";
	document.templateViewform.submit();

	}


Templib.ldMPopularTemplate = function (tmp_id,authName) {

	var tmp_id = "" + tmp_id;

	document.mptemplateViewform.TEMPLATE_ID.value = tmp_id;	
	document.mptemplateViewform.Type.value = "mostPopular";

	if (authName != "publicViewer" ) {
		document.mptemplateViewform.action="/mptemplateView.do";	
	}
	else
	{
		document.mptemplateViewform.action="/mptemplateView.im";	
	}	
	
	document.mptemplateViewform.method="Post";
	document.mptemplateViewform.submit();

	}


Templib.savefromTemplatelist = function(tempId,tempName,id) {
 

	var xmlhttp = new XMLHttpRequest();

    xmlhttp.open("POST", "moveTemplate.do", true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");

	var params="tempAction=transferTemplate";
    	params+="&TEMPLATE_ID="+tempId;
	params+="&templateName="+tempName;	
	params = params + "&" + csrfParamName + "=" + csrfToken;
        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4) {
			
			var msg=xmlhttp.responseText;				
			
			if ( msg == "success" ) {

				var temp = "" + tempId;

				var msg = document.getElementById(temp);
				
				var pos = id * 220;				

				msg.style.top = pos;					

				msg.style.display="";	
		
				setTimeout(function() { msg.style.display="none"; },2000);		
			}
	
		}
	}

	xmlhttp.send(params);
}




Templib.saveTemplate = function(tempId,tempName) {
 
	var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", "moveTemplate.do", true);
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");

	var params="tempAction=transferTemplate";
	    params+="&TEMPLATE_ID="+tempId;
	    params+="&templateName="+tempName;	
	    params = params + "&" + csrfParamName + "=" + csrfToken;	

        xmlhttp.onreadystatechange = function() {

                if (xmlhttp.readyState == 4) {
			
			var msg=xmlhttp.responseText;				
			
			if ( msg == "success" ) {

				var msg = document.getElementById("wo_templatelibmsg_div");
				
				msg.style.display="";	
		
				setTimeout(function() { msg.style.display="none"; },2000);		
			}
	
		}
	}

	xmlhttp.send(params);
}

Templib.tempcmt = function() {

	
        var comment = document.templatecomment.comment.value;
        var temid = document.templatecomment.tempId.value;
        var count = document.templatecomment.commentcount.value;
	var author_name = document.templatecomment.aName.value;

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", "temComments.do", true);

        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        var params="tempAction=temComments";
        params+="&TEMPLATE_ID="+temid;
	params+="&comment="+comment;	
	params+="&authorname="+author_name;	

        xmlhttp.onreadystatechange = function() {

                if (xmlhttp.readyState == 4) {

		var str="<span class=\"userlabel\" style=\"border-bottom: 1px dotted #CCC;display:block\">"+author_name+" <span style=\"\"> Says, </span>  </span> &nbsp;<br>";
                str+="<p>"+comment+"</p><br>";
			
		var el=document.getElementById("temcoms");
		if(el){ el.innerHTML+=str; }

		var commentCount=document.getElementById("commentcount");
		var t3=parseInt(count)+1;
		commentCount.innerHTML=t3;
		document.templatecomment.commentcount.value=t3;
	
		}
        }

	xmlhttp.send(params);

	return false;
}


Templib.getTempTypes = function(types){

	alert("Types : " + types);

	var typ = types.split(",");	

	var typeid = document.getElementById("selectType");

	for(i=0;i<typ.length;i++)
	{
		alert("Type " + typ[i]);	
		typeid.length = i+1;
	}

	

	

}

Templib.tempComment = function(temp_id) {

	alert("In validate" + temp_id );	

	var mail = document.templateForm.mail.value;
	var comment = document.templateForm.comment.value; 
		
	document.templateForm.TEMPLATE_ID.value = temp_id;	
	document.templateForm.tempAction.value = "postComment";
	document.templateForm.mail.value = mail;
	document.templateForm.comment.value = comment;

	document.templateForm.action="/templates.do";
        document.templateForm.method="Post";
        document.templateForm.enctype="multipart/form-data";
        document.templateForm.submit();	

	alert("In flase");
	
	return false;

}

