/* Global class for global functions							*/
/* Author:B.Sathish Kumar									   */
var globalFunctions = function()
{
	/* To Initiate form intial control focus 					   */
	/* focInitCtrl function focusses passes control id			   */
	this.focInitCtrl = function(frmName,ctlName)
	{
		this.formName=document.getElementById(frmName);
		this.controlName=document.getElementById(ctlName);
		this.controlName.focus();
		return true;
	}
	/* To Initiate form intial control focus 					   */
	/* focInitCtrl function focusses passes control id			   */
	this.postFormId = function(frmName,ctlName,value,actionName,chkFlagControl)
	{	
		this.formName=document.getElementById(frmName);
		this.controlName=document.getElementById(ctlName);
		
		if(document.getElementById('hideFindDtl'))
		{
			if(document.getElementById('hideFindDtl').value == 1)
			{
				document.getElementById('hideFindDtl').value=2;
			}
		}
				
		if(ctlName == 'showListId')
		{
			document.getElementById('cateId').value=999;
			if(value == 3)
			{
				document.getElementById('searchTxt').value="";
			}
		}
		
		if(chkFlagControl)
		{
			this.chkFlagControl=document.getElementById(chkFlagControl);
			this.chkFlagControl.value='';
		}
		this.controlName.value=value;
		if(actionName)
			this.formName.action=actionName;
		this.formName.submit();
		return true;
	}

	this.openAllItems = function(decodeType, typeFlag , sessId , path)
	{
		var hideFindDtl="";var categoryList="";var searchTxt="";
		
		if(document.getElementById('hideFindDtl'))
			hideFindDtl=document.getElementById('hideFindDtl').value;
		if(hideFindDtl == 2)
			hideFindDtl=1;
		if(document.getElementById('searchTxt'))
			searchTxt=document.getElementById('searchTxt').value;
		if(document.getElementById('categoryList'))
			categoryList=document.getElementById('categoryList').value;
		if(categoryList == 0)
			categoryList =999;
		if(!path) path='';
		
		if(typeFlag == 0)
			document.location = path + "home.php?"+sessId;	
		else if(typeFlag == 5)
		{
			if(hideFindDtl == 1)
				document.location = path + "communities/viewCommunities.php?itemType="+decodeType+"&"+sessId+"&hideFindDtl="+hideFindDtl+"&searchTxt="+searchTxt+"&categoryList="+categoryList;
			else
				document.location = path + "communities/index.php?itemType="+decodeType+"&"+sessId+"&hideFindDtl="+hideFindDtl+"&searchTxt="+searchTxt+"&categoryList="+categoryList;
		}
		else if(typeFlag == 6)
		{
			document.location.href = path + "comedyZone.php?"+sessId;				
		}
		else if(typeFlag == 8)
		{
			document.location.href = path+"channels.php?"+sessId;
		}
		else
			document.location = path + "templates/showTemplate.php?itemType="+decodeType+"&"+sessId+"&hideFindDtl="+hideFindDtl+"&searchTxt="+searchTxt+"&categoryList="+categoryList;
	}
	
	//Function to trim a string
	this.trim = function(inputString)
	{
		var retValue = inputString;
		var ch = retValue.substring(0, 1);
		// Check for spaces at the beginning of the string
		while (ch == " ")
		{
		   retValue = retValue.substring(1, retValue.length);
		   ch = retValue.substring(0, 1);
		}
		ch = retValue.substring(retValue.length-1, retValue.length);
		// Check for spaces at the end of the string
		while (ch == " ")
		{
		   retValue = retValue.substring(0, retValue.length-1);
		   ch = retValue.substring(retValue.length-1, retValue.length);
		}
		while (retValue.indexOf("  ") != -1)
		{ // Note that there are two spaces in the string - look for multiple spaces within the string
		   retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
		}
		return retValue; // Return the trimmed string back to the user
	}	
	
	//Validate the Login ID Pattern(Email Pattern).
	this.validLoginPatternFun = function(loginTxt,pwdTxt,formName)
	{
	
		//formName=document.indexFrm;
		formName=document.getElementById(formName);
		//document.getElementById('searchTxt').value ="";
		var realphanumericpat=/^[A-Za-z0-9]+$/;
		var loginTxt = document.getElementById(loginTxt);
		var pwdTxt = document.getElementById(pwdTxt);
		if(!loginTxt.value)
		{
			alert("Please enter the Login ID.");
			loginTxt.focus();
			return false;
		}
		if(!realphanumericpat.test(loginTxt.value))
		{
			alert("Please enter the valid Login ID.");
			loginTxt.select();
			return false;
		}
		if(!noSplCharpat.test(pwdTxt.value))
		{
			alert('Special Character < > are not allowed.');
			pwdTxt.select();
			return false;
		}
		
		if(!pwdTxt.value)
		{
			alert("Please enter the Password.");
			pwdTxt.focus();
			return false;
		}

		document.getElementById('hideSetPost').value=2;
		formName.submit();
		return true;
	}
	this.XmlPostData=function(tdId,passId,SESSID,arrTds,ctrlNameDis,postchk)
	{		
			//alert(arrTds+"iiii")
			var arrTd = arrTds.split("|");
			//this.ctrlNameDis=ctrlNameDis;
			tdId=document.getElementById(tdId);
			if(tdId)
			{
				for(var idx=0;idx < arrTd.length;idx++)
				{
					if( tdId.id == document.getElementById(arrTd[idx]).id)
					{
						tdId.className='bdr2';
					}
					else
					{
						if(document.getElementById(arrTd[idx]))
						{
							document.getElementById(arrTd[idx]).className='bdr3';
						}
					}
				}
			}
			
			if( postchk == 'all' )
			{
			 	document.getElementById('videoTdRated').className='bdr2';
			 	document.getElementById('videoTdViewed').className='bdr2';
			 	document.getElementById('videoTdEdited').className='bdr2';
				document.getElementById('divRated').style.visibility="visible";
				document.getElementById('divViewed').style.visibility="visible";
				document.getElementById('divEdited').style.visibility="visible";
			}
			else if( postchk == 'view' ) 
			{
				document.getElementById('videoTdRated').className='bdr2';
				document.getElementById('videoTdEdited').className='bdr2';
				document.getElementById('divViewed').style.visibility="visible";
			}
			else if( postchk == 'rate' )
			{
			 	document.getElementById('videoTdViewed').className='bdr2';
			 	document.getElementById('videoTdEdited').className='bdr2';
				document.getElementById('divRated').style.visibility="visible";
			}
			else if( postchk == 'edit' )
			{
			 	document.getElementById('videoTdViewed').className='bdr2';
				document.getElementById('videoTdRated').className='bdr2';
				document.getElementById('divEdited').style.visibility="visible";
			}
			if (req)
			{
				//Request via POST method
				req.open('POST', 'home.php?'+SESSID);
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				//alert("catViewId="+passId+"&postChk="+postchk+"&catRateId="+passId+"&catEditId="+passId);
				if( postchk == 'all' )
					req.send("catViewId="+passId+"&postChk="+postchk+"&catRateId="+passId+"&catEditId="+passId);
				else if( postchk == 'view' )
					req.send("catViewId="+passId+"&postChk="+postchk);
				else if( postchk == 'rate' )
					req.send("catRateId="+passId+"&postChk="+postchk);
				else if( postchk == 'edit' )
					req.send("catEditId="+passId+"&postChk="+postchk);
				
				req.onreadystatechange = this.processReqChange;
			}
			else
			{
				 //problem while creating XMLHttp object
				 alert("Your browser does not support XMLHttpRequest technology!");
				 doesNotSupport = false;
			}
	}
	this.XmlPostDataSkin=function(tdId,passId,SESSID,arrTds,ctrlNameDis,postchk,fileName,ccid)
	{
			
			if(browser == "Microsoft Internet Explorer")
			{
				/* Create the object using MSIE's method */
				try
				{
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					alert(e.message);
				}
			}
			else
			{
				/* Create the object using other browser's method */
				try
				{
					req = new XMLHttpRequest();
				}
				catch(e)
				{
					alert(e.message);
				}
			}	
			
			var arrTd = arrTds.split("|");
			this.ctrlNameDis=ctrlNameDis;
			tdId=document.getElementById(tdId);
			if(tdId)
			{
				for(var idx=0;idx < arrTd.length;idx++)
				{
					if( tdId.id == document.getElementById(arrTd[idx]).id)
					{
						tdId.className='bdr2';
					}
					else
					{
						if(document.getElementById(arrTd[idx]))
						{
							document.getElementById(arrTd[idx]).className='bdr3';
						}
					}
				}
			}
			if( postchk == 'all' )
			{
			 	document.getElementById('videoTdRated').className='bdr2';
				document.getElementById('divRated').style.visibility="visible";
				document.getElementById('divViewed').style.visibility="visible";
			}
			else if( postchk == 'view' ) 
			{
				document.getElementById('videoTdRated').className='bdr2';
				document.getElementById('divViewed').style.visibility="visible";
				
			}
			else if( postchk == 'rate' )
			{
			 	document.getElementById('videoTdViewed').className='bdr2';
				document.getElementById('divRated').style.visibility="visible";
			}
			
			if (req)
			{
				//Request via POST method
				req.open('POST', fileName+'?'+SESSID);
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				if( postchk == 'all' )
					req.send("catViewId="+passId+"&postChk="+postchk+"&catRateId="+passId+"&ccId="+ccid);
				else if( postchk == 'view' )
				{
					req.send("catViewId="+passId+"&postChk="+postchk+"&ccId="+ccid);
			
				}
				else if( postchk == 'rate' )
					req.send("catRateId="+passId+"&postChk="+postchk+"&ccId="+ccid);
				req.onreadystatechange = this.processReqSkin;
			}
			else
			{
				 //problem while creating XMLHttp object
				 alert("Your browser does not support XMLHttpRequest technology!");
				 doesNotSupport = false;
			}
	}
	
	/*
	*	for processing html display in ajax
	*/
	this.processReqChange=function()
	{
		// only if req shows "loaded"
		if (req.readyState == 4)
		{
			
			var response = req.responseText;
			
			response = response.split("|");
			
			document.getElementById('resultSetRated').innerHTML = response[0];
	//		alert(response[0])
			document.getElementById('resultSetViewed').innerHTML = response[1];
			document.getElementById('resultSetEdited').innerHTML = response[2];
			document.getElementById('divRated').style.visibility="hidden";
			document.getElementById('divViewed').style.visibility="hidden";
			document.getElementById('divEdited').style.visibility="hidden";
		}
	}
	/*
	*	for processing html display in ajax
	*/
	this.processReqSkin=function()
	{
		// only if req shows "loaded"
		if (req.readyState == 4)
		{
			var response = req.responseText;
			response = response.split("|");
			document.getElementById('resultSetViewed').innerHTML = response[0];
			document.getElementById('resultSetRated').innerHTML = response[1];
			document.getElementById('divRated').style.visibility="hidden";
			document.getElementById('divViewed').style.visibility="hidden";
		}
	}
	/*
	*	To display the hidden div tag
	*/	
	this.showHideDiv=function(defaultText,hiddenText,spanId,divId)//Show the how it works div tag
	{
		if(document.getElementById(divId).style.display == "none")
		{
			document.getElementById(divId).style.display="block";
			document.getElementById(spanId).innerHTML=defaultText;
			showDivFlag=0;
		}
		else
		{
			document.getElementById(divId).style.display="none";
			document.getElementById(spanId).innerHTML=hiddenText;
			showDivFlag=1;
		}
	}
	this.submitForm =function(e,type,sid,formName,staticId)
	{
		
		var unicode=e.keyCode? e.keyCode : e.charCode;
		if(unicode == 13)
		{
			
			if(type =="search")
			{
						
				if(document.getElementById('loginTxt')) document.getElementById('loginTxt').value ="";
				if(document.getElementById('pwdTxt')) document.getElementById('pwdTxt').value ="";
				
				searchObj.searchFunc(sid,formName,staticId);
			}
			if(type == "clearResult")
			{
						
				if(document.getElementById('loginTxt')) document.getElementById('loginTxt').value ="";
				if(document.getElementById('pwdTxt')) document.getElementById('pwdTxt').value ="";
				
				searchObj.clearSearchTxt(sid,formName,staticId);
			}	
			if(type =="login")					
			{
				//document.getElementById('searchTxt').value ="";
				return gbJsObj.validLoginPatternFun('loginTxt','pwdTxt',formName);
			}
		}
	}	
}
var gbJsObj = new globalFunctions();