//FUNCTION Trim(STRING)
//DEVELOPED BY :: SUDIPTO LAHIRI
//COPYRIGHT :: EARTH TECHNOLOGY
//THIS FUNCTION IS USED TO TRIM WHITE SPACE FROM THE BEGINING AND END OF A STRING

function Trim(str)
{  
	while(str.charAt(0) == (" "))
	{  
		str = str.substring(1)
	}
	while(str.charAt(str.length-1) == " " )
	{  
		str = str.substring(0,str.length-1)
	}
	return str
}

//FUNCTION keyRestrict('Name', 'Valid Character')
//DEVELOPED BY :: SUDIPTO LAHIRI
//COPYRIGHT :: EARTH TECHNOLOGY
//THIS FUNCTION IS USED TO Restrict keys


function keyRestrict(e, validchars) {
	 var key='', keychar='';
	 key = getKeyCode(e);
	 if (key == null) return true;
	 keychar = String.fromCharCode(key);
	 keychar = keychar.toLowerCase();
	 validchars = validchars.toLowerCase();
	 if (validchars.indexOf(keychar) != -1)
	  return true;
	 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	  return true;
	 return false;
}
function getKeyCode(e) {
	 if (window.event)
		return window.event.keyCode;
	 else if (e)
		return e.which;
	 else
		return null;
}

//FUNCTION showStep()
//DEVELOPED BY :: SUDIPTO LAHIRI
//COPYRIGHT :: EARTH TECHNOLOGY
//THIS FUNCTION IS USED TO DISPLAY THE CALCULATED STEPS

function showStep()
{
	problemStringCatch = document.frm.hidSubString.value
	var bodyFormat = "<table width = '100%' align = 'center' valign = 'TOP' border = '0' cellspacing = '0' cellpadding = '0'>"
	var stepCount = 0
	var flag = true
	if(!isNaN(problemStringCatch))
	{
		document.getElementById('btnSubmit').disabled = true
	}
	for(var stepList = 0; stepList < arrStep1.length; stepList++)
	{
		stepCount = stepList + 1
		if(flag == true)
		{
			bodyFormat +="<TR bgcolor = '#FFFFFF'>"
			bodyFormat +="<TD WIDTH = '80%' ALIGN = 'center' VALIGN = 'TOP' HEIGHT = '50'>"
			bodyFormat +="<b>"+arrStep1[stepList]+"</b><br><b>"+arrStep2[stepList]+"</b>"
			bodyFormat +="</TD>"
			bodyFormat +="<TD WIDTH = '20%' ALIGN = 'center' VALIGN = 'middle' HEIGHT = '50'>"
			bodyFormat +="<b>Step "+stepCount+"</b>"
			bodyFormat +="</TD>"
			bodyFormat +="</TR>"
			flag = false
		}
		else
		{
			bodyFormat +="<TR bgcolor = '#E6F2FF'>"
			bodyFormat +="<TD WIDTH = '80%' ALIGN = 'center' VALIGN = 'TOP' HEIGHT = '50'>"
			bodyFormat +="<b>"+arrStep1[stepList]+"</b><br><b>"+arrStep2[stepList]+"<b>"
			bodyFormat +="</TD>"
			bodyFormat +="<TD WIDTH = '20%' ALIGN = 'center' VALIGN = 'middle' HEIGHT = '50'>"
			bodyFormat +="<b>Step "+stepCount+"</b>"
			bodyFormat +="</TD>"
			bodyFormat +="</TR>"
			flag = true
		}
	}
	if(!isNaN(problemStringCatch))
	{
			bodyFormat +="<TR bgcolor = '#CBCE93'>"
			bodyFormat +="<TD WIDTH = '80%' ALIGN = 'center' VALIGN = 'TOP' HEIGHT = '50'>"
			bodyFormat +="<font color = 'red'><b>"+problemStringCatch+"</b></font>"
			bodyFormat +="</TD>"
			bodyFormat +="<TD WIDTH = '20%' ALIGN = 'center' VALIGN = 'middle' HEIGHT = '50'>"
			bodyFormat +="<b>Answer</b>"
			bodyFormat +="</TD>"
			bodyFormat +="</TR>"
	}
	bodyFormat +="</table>"
	document.getElementById('tableStep').innerHTML = bodyFormat
}


//FUNCTION findOperatorLeftRight(POSITION OF MAIN OPERATOR, WORKING STRING)
//DEVELOPED BY :: SUDIPTO LAHIRI
//COPYRIGHT :: EARTH TECHNOLOGY
//THIS FUNCTION IS USED TO FIND POSITION OF THE OPERATOR PRESENT LEFT AND OR RIGHT OF THE WORKING OPERATOR THIS FUNCTION RETTURNS CONCATINATED VALUE OF	LEFT POSITION OF THE OPERATOR AND RIGHT POSITION OPERATOR AND IF NO SUCH OPERATOR FOUND IT RETURNS BLANK VALUE 

function findOperatorLeftRight(mainOperatorPosition, problemStringCatch2)
{
	var leftOccurance = ''
	var rightOccurance = ''
	for(var searchBackward = mainOperatorPosition-1; searchBackward>=0; searchBackward--)
	{
		if(isNaN(problemStringCatch2.charAt(searchBackward)) && problemStringCatch2.charAt(searchBackward)!='.')
		{
			leftOccurance = searchBackward
			break
		}
		
	}
	for(var searchForward = mainOperatorPosition+1; searchForward<=problemStringCatch2.length; searchForward++)
	{
		if(isNaN(problemStringCatch2.charAt(searchForward)) && problemStringCatch2.charAt(searchForward)!='.')
		{
			rightOccurance = searchForward
			break
		}
	}
	var concatValue =leftOccurance+":"+rightOccurance
	return concatValue
}