function fnEncode(sParam)
{
	sParam = encodeURIComponent(sParam);
	return sParam;
}
function fnSubmit(sFolder,sPage,sParam)
{  var oXmlHttp = fnGAJX();// alert(sFolder+sPage+sParam);
   if(oXmlHttp == null) alert("Request is not Completed");
   oXmlHttp.open("post",sFolder+sPage,false);
   oXmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
   oXmlHttp.send(sParam); //alert(oXmlHttp.responseText);
 if(oXmlHttp.responseText !="" && oXmlHttp.status==200)  {eval(oXmlHttp.responseText); }
 
} 


function fnGAJX()
{
   if(window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if(window.XMLHttpRequest ) return new XMLHttpRequest();
   return null;
}

  function fnPost(sFolder,sPage,sParam)
  {
	document.forms[0].method="post";
	document.forms[0].action=sFolder+sPage+sParam;
	//alert(sFolder+sPage+sParam);
	document.forms[0].submit();
	
 }
function fnDisplayErrorTd(sId,sMsg)
{
	sTmpErrNo = "E"+sMsg;
	document.all(sId).innerHTML = eval(sTmpErrNo);
	
}

function fnDisplayMsg(sID,sMsg)
{

document.all(sID).innerHTML = sMsg;
}

function fnSetMsgHdr(oCtrl,sMsg)
{
 sTmp = document.getElementById(oCtrl).innerHTML;
 document.getElementById(oCtrl).innerHTML = sTmp+" "+sMsg;	
}		 
function fnTrim(sStr)
{
    if(!sStr || typeof sStr != 'string')
    return null;
    return sStr.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}  

 function fnSplit(sVL,sDelm)
{
 arrVL = sVL.split(sDelm);
 return arrVL;
}

function fnPaste(sId)
{
    document.all(sId).value = window.clipboardData.getData("Text");
}

function fnControlV(nKeyNo)
{
  if(event.ctrlKey && event.keyCode==nKeyNo) fnPaste('txtAr'); 
}

function fnCopyClipBoard(sTableId,sTxtArId)
{
 oTable = document.all(sTableId);
 nTRows = oTable.rows.length;
 nTCols = oTable.rows[1].cells.length;
 sVL = document.all(sTxtArId).value;
 arrRowVL = fnSplit(sVL,"\n");
 oRowCurrent = event.srcElement.parentElement.parentElement
 nRowIndex = oRowCurrent.rowIndex;
 
 for(i=0;i<arrRowVL.length -1;i++)
 {
  oTakeRow = oTable.rows[nRowIndex];
  if(oTakeRow==null) continue;
  arrCell = fnSplit(arrRowVL[i],"\t");
  j = 0;
  for(c=0;c<nTCols;c++)
   {
      if(c==0 || c==1  || c==4 ) continue ;
        oTakeRow.cells[c].firstChild.value = arrCell[j];
		j++;
   }
   nRowIndex = nRowIndex+1
   if(nTRows==nRowIndex) break;
 }
 
 
}
function fnCallpaste()
{
 if(event.ctrlKey && event.keyCode==86)
 {
   event.srcElement.value="";
   fnPaste('txtAr'); 
   fnCopyClipBoard('MStone','txtAr');
 }
}

function fnCallpaste()
{
 if(event.ctrlKey && event.keyCode==86)
 {
   event.srcElement.value="";
   fnPaste('txtAr'); 
   fnCopyClipBoard('MStone','txtAr');
 }
}
 function fnMatrixNewRow(sTableId,sCType)
 {
	var sCellId="";
	
	oMatrix=document.all(sTableId);//Getting Matrix Object
    nRow=oMatrix.rows.length;//Getting total number of rows
    nTCol=oMatrix.rows[0].cells.length;//Getting total number of columns
	if(sCType=="") return;
	arrCType = sCType.split(",");
    var oNewRow=oMatrix.insertRow();//create row object
	nFlg = 0;

	 for(i=0;i<nTCol;i++)
	 {
	   oNewRow.insertCell();//create cell object
       sCellId = sTableId+"_"+nRow+"_"+i;
	   nFlg = 0;
	   if(arrCType[i]==null) return  oNewRow;
	  
	   if(arrCType[i].toUpperCase()=="checkbox")
	     {oMatrix.rows[nRow].cells[i].innerHTML='<INPUT  id='+sCellId+'  name='+sCellId+'  type="checkbox">';nFlg = 1;}
	  
	   if(arrCType[i].toUpperCase()=="TXT")
	    { oMatrix.rows[nRow].cells[i].innerHTML='<INPUT  id='+sCellId+'  name='+sCellId+'  type="text">';nFlg = 1;}
		 
	   if(arrCType[i].toUpperCase()=="IMG" )
	    { oMatrix.rows[nRow].cells[i].innerHTML='<IMG  id='+sCellId+'   name='+sCellId+'  src="../image/calendar_icon.gif" >';nFlg = 1;}
	   if(arrCType[i].toUpperCase()=="SELT")
	    {  oMatrix.rows[nRow].cells[i].innerHTML="<SELECT  id='"+sCellId+"'  name='"+sCellId+"'></SELECT>";nFlg = 1;}	
	   if(arrCType[i].toUpperCase()=="FILE")
	     {oMatrix.rows[nRow].cells[i].innerHTML='<INPUT id='+sCellId+'  name='+sCellId+'  type="file" size="25">';nFlg = 1;}
	  if(nFlg==1) 
	      oMatrix.rows[nRow].cells[i].children.item(0).mergeAttributes(oMatrix.rows[parseInt(nRow)-1].cells[i].children.item(0));
	 
	 }
	
	   return oNewRow;  
 }
 
 
 function fnMatrixNewRowCreate(sTableId,sCType)
 {
	var sCellId="";
	
	oMatrix=document.all(sTableId);//Getting Matrix Object
    nRow=oMatrix.rows.length;//Getting total number of rows
    nTCol=oMatrix.rows[0].cells.length;//Getting total number of columns
	if(sCType=="") return;
	arrCType = sCType.split(",");
    var oNewRow=oMatrix.insertRow();//create row object
	nFlg = 0;

	 for(i=0;i<nTCol;i++)
	 {
	   oNewRow.insertCell();//create cell object
       sCellId = sTableId+"_"+nRow+"_"+i;
	   nFlg = 0;
	   if(arrCType[i]==null) return  oNewRow;
	    
	    if(arrCType[i].toUpperCase()=="FILE")
	     {oMatrix.rows[nRow].cells[i].innerHTML='<INPUT  id='+sCellId+'  name='+sCellId+'  type="file" size="25">';nFlg = 1;}
	   if(arrCType[i].toUpperCase()=="CHK")
	     {oMatrix.rows[nRow].cells[i].innerHTML='<INPUT  id='+sCellId+'  name='+sCellId+'  type="checkbox">';nFlg = 1;}
	  
	   if(arrCType[i].toUpperCase()=="TXT")
	    { oMatrix.rows[nRow].cells[i].innerHTML='<INPUT  id='+sCellId+'  name='+sCellId+'  type="text">';nFlg = 1;}
		 
	   if(arrCType[i].toUpperCase()=="IMG" )
	    { oMatrix.rows[nRow].cells[i].innerHTML='<IMG  id='+sCellId+'   name='+sCellId+'  src="../image/calendar_icon.gif" >';nFlg = 1;}
	   if(arrCType[i].toUpperCase()=="SELT")
	    {  oMatrix.rows[nRow].cells[i].innerHTML="<SELECT  id='"+sCellId+"'  name='"+sCellId+"'></SELECT>";nFlg = 1;}	
	
			
	 }
	
	   return oNewRow;  
 }
 
 
function fnDeleteRow(sTableId)
{ 
 oTable = document.all(sTableId);
 nLen = oTable.rows.length;//no of rows in table
 
     for(i=nLen-1;i>0;i--)
      { 
	    oRow = oTable.rows[i];
		oCell = oRow.cells[1];
		chkObj = oCell.firstChild;
		if(chkObj.checked)
        oRow.removeNode(true);
      }
}


	function clearinputText1(sId)	
	
	{ document.all(sId).value= " ";
	return true;}
	
	function clearinputText(sTableId)	
	{
	oTable = document.all(sTableId);
    nLen = oTable.rows.length;//no of rows in table
     for(i=nLen-1;i>0;i--)
      { 
	    oRow = oTable.rows[i];
		oCell = oRow.cells[1];
		chkObj = oCell.firstChild;
		oCell2 = oRow.cells[2];
		inpObj = oCell2.firstChild;
		if(chkObj.checked)
        inpObj.value="";
      }
	  
}
	
	
	
	
function daysBetween(date1, date2)
 {
	 

    var DSTAdjust = 0;
    // constants used for our calculations below
    oneMinute = 1000 * 60;
    var oneDay = oneMinute * 60 * 24;
    // equalize times in case date objects have them
    date1.setHours(0);
    date1.setMinutes(0);
    date1.setSeconds(0);
    date2.setHours(0);
    date2.setMinutes(0);
    date2.setSeconds(0);
    // take care of spans across Daylight Saving Time changes
    if (date2 > date1) {
        DSTAdjust = 
            (date2.getTimezoneOffset( ) - date1.getTimezoneOffset( )) * oneMinute;
    } else {
        DSTAdjust = 
            (date1.getTimezoneOffset( ) - date2.getTimezoneOffset( )) * oneMinute;    
    }
    var diff = Math.abs(date2.getTime( ) - date1.getTime( )) - DSTAdjust;
    return Math.ceil(diff/oneDay);
}


sDateFormat = "dd/mm/yyyy";
	function fnGenrate(sId1,sId2)
	{
	   date1 = document.all(sId1).value;
       date2 = document.all(sId2).value;
	   date1= new Date(date1);
	   date2=new Date(date2);
	   nLen = daysBetween(date1, date2);
	    nC = 0
	   sTmp = date1;
	 
	  for(k=0;k<nLen;k++)
	  { 
	     sTmp = new Date(sTmp.setDate(sTmp.getDate()+nC))
		  nC=1
		 if (document.all('DLY_1_4').value == "Off")
		  { if (sTmp.getDay() == 0 || sTmp.getDay() == 6) continue;}
		 
		oRow = fnMatrixNewRow('DLY','CELL,TXT,IMG,TXT,IMG,SELT,SELT,TXT,TXT');
		oDt = oRow.cells[1].firstChild;
		sTmpDateFormat = fnConvertDate(sTmp);
		 
		sValue = document.all('DLY_1_6').value
		sVle = document.all('DLY_1_4').selectedIndex
		sVal = document.all('DLY_1_5').selectedIndex
	    fnDisplayVL(oDt.id,sTmpDateFormat);
		oRow.cells[1].firstChild.style.display = "none";
		oRow.cells[2].firstChild.style.display = "none";
		oRow.cells[4].firstChild.style.display = "none";
	    oDt = oRow.cells[3].firstChild;
	    fnDisplayVL(oDt.id,sTmpDateFormat );
		oRow.cells[5].firstChild.style.display = "none";
	    oCurn = oRow.cells[5].firstChild;
	    fnAddVLSelect(oCurn.id,"Select")
	    fnAddVLSelect(oCurn.id,"On","on")
	    fnAddVLSelect(oCurn.id,"Off","off")
		fnDisplayVL_DD(oCurn.id,sVle);
	    oCurn = oRow.cells[6].firstChild;
	    fnAddVLSelect(oCurn.id,"USD $","USD")
	    fnAddVLSelect(oCurn.id,"EUR €","EUR")
	    fnAddVLSelect(oCurn.id,"INR Rs.","Rs.")
		fnDisplayVL_DD(oCurn. id,sVal );
		oTXT = oRow.cells[7].firstChild;
	    fnDisplayVL(oTXT.id,sValue);
	  }
	}
	
	function fnConvertDate(sTmpDate)
	{   
	 

		oDay = sTmpDate.getDate();
	    oMonth = sTmpDate.getMonth()+1;
		oYear = sTmpDate.getYear();
		
		if (oDay < 10)
		oDay= "0"+ oDay;
		if (oMonth < 10)
		oMonth= "0"+ oMonth;
		
		sDate = oMonth+"/"+oDay+"/"+oYear;
		
		return sDate;
	}
	
	
	
 sDateFormat = "mm/dd/yyyy";
	function fnGenrateWeek(sId1,sId2)
	{
	  sVal = document.all('WEEK_1_6').selectedIndex
	   sValue = document.all('WEEK_1_7').value
	   date1 = document.all(sId1).value;
       date2 = document.all(sId2).value;
	   date1= new Date(date1);
	   date2=new Date(date2);
	   var sTmp = date1;
	   var sTmp1= date1;
	   nLen = daysBetween(date1, date2)/7;
	   nC = 0
	  for(k=0;k<nLen;k++)
	  {
		oRow = fnMatrixNewRow('WEEK','CELL,TXT,IMG,TXT,IMG,SELT,TXT,TXT');
		oDt = oRow.cells[1].firstChild;
		sTmp = new Date(sTmp.setDate(sTmp.getDate()+7))
		
        sTmp1 = new Date(sTmp)
		sTmp1 = new Date(sTmp1.setDate(sTmp1.getDate()-7+nC))
		var sTmpDate1 = fnConvertDate(sTmp1);
		nC=1
		var sTmpDateFormat = fnConvertDate(sTmp);
		
		
	    fnDisplayVL(oDt.id,sTmpDate1);
	    oDt = oRow.cells[3].firstChild;
	    fnDisplayVL(oDt.id,sTmpDateFormat);
	  	 oCurn = oRow.cells[5].firstChild;
	    fnAddVLSelect(oCurn.id,"USD $","USD")
	    fnAddVLSelect(oCurn.id,"EUR €","EUR")
	    fnAddVLSelect(oCurn.id,"INR Rs.","Rs")
		fnDisplayVL_DD(oCurn. id,sVal );
		oTXT = oRow.cells[6].firstChild;
	    fnDisplayVL(oTXT.id,sValue);
	  }
	}
	
	
	sDateFormat = "mm/dd/yyyy";
	function fnGenrateMonth(sId1,sId2)
	{
	  sVal = document.all('MON_1_6').selectedIndex
	  sValue = document.all('MON_1_7').value
	   date1 = document.all(sId1).value;
       date2 = document.all(sId2).value;
	   date1= new Date(date1);
	   date2=new Date(date2);
	   var sTmp = date1;
	   var sTmp1= date1;
	   nLen = date2.getMonth() - date1.getMonth();
	 // nLen = daysBetween(date1, date2)/30;
	   nC = 0
	  for(k=0;k<nLen;k++)
	  {
		oRow = fnMatrixNewRow('MON','CELL,TXT,IMG,TXT,IMG,SELT,TXT,TXT');
		oDt = oRow.cells[1].firstChild;
		 sTmp = new Date(sTmp)
		sTmp = new Date(sTmp.setMonth(sTmp.getMonth()+nC)) 
		nC=1
       
		sTmp1 = new Date(sTmp1.setMonth(sTmp1.getMonth()+1))
		var sTmpDate1 = fnConvertDate(sTmp1);
		
		var sTmpDateFormat = fnConvertDate(sTmp);
		
		
	    fnDisplayVL(oDt.id,sTmpDateFormat);
	    oDt = oRow.cells[3].firstChild;
	    fnDisplayVL(oDt.id,sTmpDate1);
	  	 oCurn = oRow.cells[5].firstChild;
	    fnAddVLSelect(oCurn.id,"USD $","USD")
	    fnAddVLSelect(oCurn.id,"EUR €","EUR")
	    fnAddVLSelect(oCurn.id,"INR Rs.","Rs")
		fnDisplayVL_DD(oCurn. id,sVal );
		oTXT = oRow.cells[6].firstChild;
	    fnDisplayVL(oTXT.id,sValue);
	  }
	} 
	function fnSelectCheck(sTableId,sChkAllId)
{
  oTable = document.all(sTableId);
  oChkAll = document.all(sChkAllId);
  nLen = oTable.rows.length;//no of rows in table
  if(oChkAll.checked) bFlg=true;
  else bFlg = false
     for(i=nLen-1;i>1;i--)
      { 
	    oRow = oTable.rows[i];
		oCell = oRow.cells[1];
		chkObj = oCell.firstChild;
		chkObj.checked = bFlg;
      }
}
function fnDisplayVL_DD(sId,sIn)
  { 
	document.all(sId).selectedIndex= sIn;
  }
	
function fnSelectUnCheck(sChkAllId)
{ 
 oBj = document.all(sChkAllId);
 oChk = event.srcElement;
 if(!oChk.checked)
 { oBj.checked = false; }
  return true;
}



  function fnDisplayVL(sId,sVL)
  { 
	document.all(sId).value = sVL;
  }

function fnAddRowAndDefault(sTableId)
{
	oRow = fnMatrixNewRow(sTableId,'CELL,CHK,TXT,TXT,IMG,SELT,TXT,TXT');
	//chkBox = oRow.cells[1].firstChild;

	oDt = oRow.cells[3].firstChild;

	fnDisplayVL(oDt.id,sDateFormat);
	
	oCurn = oRow.cells[5].firstChild;
	fnAddVLSelect(oCurn.id,"USD $","USD")
	fnAddVLSelect(oCurn.id,"EUR €","EUR")
	fnAddVLSelect(oCurn.id,"INR Rs.","Rs.")
	
	
	
}

//Append value to the select box
function fnAddVLSelect(sID,sTxt,sVL)
{  	var oSelect = document.getElementById(sID);
    var oOption = document.createElement('OPTION');
    oOption.text = sTxt;
    oOption.value = sVL;
	oSelect.add(oOption);
}

 
function fnAddRowFile(sTableId)
{       sVL = document.all("remove").innerHTML ;
        oRow = fnMatrixNewRow(sTableId,'CELL,FILE,CELL');
		oRow.cells[2].innerHTML=sVL ;
		//oRow.cells[2].style.display = "none";
		oRow.cells[2].style.color= "red";
		
		
}

//Append value to the select box
function fnAddVLSelect(sID,sTxt,sVL)
{  	var oSelect = document.getElementById(sID);
    var oOption = document.createElement('OPTION');
    oOption.text = sTxt;
    oOption.value = sVL;
	oSelect.add(oOption);
}

function fnHidden(sId,sDispId,sMsg)
{
	//event.srcElement.style.display="none";
document.all(sDispId).style.display = sMsg; 

}

function fnRemoveTXT(sSrcId)
{document.all(sSrcId).value="" ;
//alert(document.all(sSrcId).value)
}

function fnCopyFile(sSrcId,sDispId)
{  
	document.all(sDispId).innerHTML = document.all(sSrcId).value
	
}

function fnBrowse()
{
	oBjCurrent = event.srcElement;// getting the current object
	FileValue = oBjCurrent.value; //reading values from current object
	oBjParent = oBjCurrent.parentElement; //getting the parent object
	oBjParent.align ="center";
	oBjParent.style.fontWeight ="bold";
	oBjParent.style.color ="#AAAAAA";
	oRow = oBjParent.parentElement;
	oBjCurrent.removeNode(true); //remove parent cell
	arrDlmr = FileValue.split("\\");
	nLen = arrDlmr.length;
	FileName = arrDlmr[nLen-1];
	oBjParent.innerHTML = FileName;
	oRow.cells[2].style.display = "block";
	oRow = fnMatrixNewRowCreate('WORK','CELL,TXT');
	oRow.cells[1].firstChild.value = "Click to add title"
	oRow.cells[1].firstChild.style.color = "#AAAAAA"
	oRow=fnMatrixNewRowCreate('WORK','CELL,TXT');
	oRow.cells[1].firstChild.value = "Click to add duration"
	oRow.cells[1].firstChild.style.color = "#AAAAAA"
	oRow =fnMatrixNewRowCreate('WORK','CELL,TXT');
	oRow.cells[1].firstChild.value = "Click to add cost"
	oRow.cells[1].firstChild.style.color = "#AAAAAA"

	
	
}

function fnDeleteRowCreate(sTableId)
{ 
    oBjCurrent = oRow.cells[1];// getting the current object
	FileValue = oBjCurrent.value; //reading values from current object
	oBjParent = oBjCurrent; //gettin
    oRow = oBjParent.parentElement;
	oBjCurrent.removeNode(true); //remove parent cell
	  
    oBjCurrent = event.srcElement;// getting the current object
	FileValue = oBjCurrent.value; //reading values from current object
	oBjParent = oBjCurrent.parentElement; //getting the parent object
    oRow = oBjParent.parentElement;
	oBjCurrent.removeNode(true); //remove parent cell
}





 function Add()
{
  document.all("ttl").innerHTML.value =
    parseFloat(document.all('1_6').value) +
    parseFloat(document.all('2_6').value) +
	 parseFloat(document.all('3_6').value) +
    parseFloat(document.all('4_6').value);
}
