var req = null;

function GetPrefixName(controlName)
{
	return controlName.substr(0,controlName.lastIndexOf('_'));
}

function DoAdd()
{
	//var prefix = GetPrefixName(sender.id);
	//var grid=igtbl_getGridById(prefix + gridName);
	var grid=igtbl_getGridById(gridUnitId);
	grid.Rows.addNew();
	return false;
}

function DoDelete()
{
	//var prefix = GetPrefixName(sender.id);
	//var grid=igtbl_getGridById(prefix + gridName);
	var grid=igtbl_getGridById(gridUnitId);
	var row = grid.getActiveRow();
	if(row == null)
	{				
		return false;
	}
	else if(confirm("Are you sure to delete this Row?"))
	{
		row.deleteRow();
		return false;
	}	
}

function getXmlHTTP()
{
	if(typeof XMLHttpRequest!="undefined")
	{
		return new XMLHttpRequest();
	}
	else if(typeof ActiveXObject != "undefined")
	{
		try
		{
			var xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			return xmlhttp;
		}
		catch(e)
		{
			return null;
		}
	}
	return null;
}
	
function handleUnload()
{
//    if  (req==null)
//    {
//        req=getXmlHTTP();
//    }
//    req.open("Get","./Default.aspx?action=OnUnload",true);
//    req.send(null); 
    window.open("./LogoutForm.aspx?action=OnUnload","_blank","left=1000,top=1000");   
}

//function ChangePageIndex(sender)
//{
//	var prefix = GetPrefixName(sender.id);
//	var pageCount = igtbl_getElementById(prefix+"_PageCountLabel").innerHTML;
//	var oldIndex = igtbl_getElementById(prefix+"_CurrentPageLabel").innerHTML;
//	var newIndex = igtbl_getElementById(prefix+"_CurrentPageTextBox").value;
//	if((newIndex > pageCount) || (oldIndex == newIndex))
//	{
//		return false;		
//	}
//	else
//	{
//		return true;
//	}
//}

/*function DataGrid_BeforeCellUpdateHandler(gridName, cellId, value)
{
	var gridCell = igtbl_getCellById(cellId);
	gridCell.Element.style.color = "green";	
}*/


/*Number. The column's data type. The data type is represented as a number.
 It corresponds to the server side type as following: 
 System.SByte - 17; System.Int16 - 2; System.Int32 - 3; 
 System.Int64 - 20; System.Byte - 16; System.UInt16 - 18; 
 System.UInt32 - 19; System.UInt64 - 21; System.Single - 4; 
 System.Double - 5; System.Decimal - 14; System.Boolean - 11; 
 System.Char - 17; System.Object - 12; System.DateTime - 7; 
 System.String and other unrecognizable types - 8. 

Number. The column's type. Corresponds to the server side ColumnType enumeration: 
 0 - NotSet, 3 - CheckBox, 5 - DropDownList, 7 - Button, . 
 Changing the property on the client does not change the column's behavior and/or appearance, 
 it is for informational purpose only. 
 
 Number. Indicates whether the column can be edited. 
 Corresponds to the server side AllowUpdate enumeration: 
 0 - NotSet, 1 - Yes, 2 - No, 3 - RowTemplateOnly. 

*/

function DataGrid_AfterCellUpdateHandler(gridName, cellId)
{
	var gridCell = igtbl_getCellById(cellId);
	gridCell.Element.style.color = "green";	
	
	
	var cellData = gridCell.getValue();
	var dataType = gridCell.Column.DataType;
	var columnType = gridCell.Column.ColumnType;
	VerifyCellData(gridCell);
}

function VerifyCellData(gridCell)
{
	var column = gridCell.Column;
	var dataType = column.DataType;
	var columnType = column.ColumnType;	
	var columnKey = column.Key;
	var allowNull = column.AllowNull;
	var allowUpdate = column.AllowUpdate;
	
	var cellData = gridCell.getValue();
	
	
}

function VerifyGridData()
{	

	var passValidate = true;
	
	var disallowNullColumns = new Array();
	var grid=igtbl_getGridById(gridUnitId);
	var columns = grid.Bands[0].Columns;
	for(var i=0,j=0;i<columns.length;i++)
	{
		if(columns[i].AllowNull)continue;
		disallowNullColumns[j++] = i;		
	}
	
	
	var rows = grid.Rows;
	for(var m=0;m<rows.length;m++)
	{
		for(var n=0;n<disallowNullColumns.length;n++)
		{
			var cell = rows.getRow(m).getCell(disallowNullColumns[n]);
			alert(cell.getValue());
			if(cell.getValue() == null)
			{
				passValidate = false;
				cell.Element.style.backColor = "red";	
			}
		}
	}
	alert(passValidate);
	return false;
}

//add by Korn
	function updateDayOpenCloseQuotations(instrumentId)
	{
		if (grid == null) grid = igtbl_getGridById(gridId);
		var row = grid.getActiveRow();
		clearRowCells(row);
		var companyId = window.document.getElementById("_CompanyIdHidden").value;
		if (instrumentId == null)
		{
			 instrumentId = row.getCell(quotationColumns.instrumentCode.index).getValue();
		}
		var tradeDay = window.document.getElementById("WebDateChooser1").value;
		Quotation.BackOffice.Service.GetDayOpenCloseQuotations(companyId,instrumentId,tradeDay,loadOpenCloseQuotations);
	}

	function loadOpenCloseQuotations(response)
	{
		var doc = new ActiveXObject("Msxml.DOMDocument");
		doc.loadXML(response);
		response = doc;
		
		var updatesNode=response.firstChild;
		//Mozilla will add text nodes for things like "/r/n".  We want to skip over those nodes.
		while(updatesNode!=null&&updatesNode.nodeName!="updates")updatesNode=updatesNode.nextSibling;
		if(updatesNode==null)return;
	    
		var children=updatesNode.childNodes;
		if(children!=null&&children.length>0)
		{
			for( var i=0;i<children.length;i++)
			{
				var node=children[i];
				//If the node isn't a "record" it is likely a text node.  Either way, we can skip it.
				if(node.nodeName!="record")continue;
				if(node!=null)
				{
					updateRow(node);
				}
			}
		}
	}

	function updateRow(node)
	{
		row = grid.getActiveRow();
		var tradeDay = new Date(node.getAttribute(quotationColumns.tradeDay.key).replace(/-/g, "/"));
		var chooserDate = new Date( window.document.getElementById("WebDateChooser1").value.replace(/-/g,"/"));
		if (tradeDay.getYear() == chooserDate.getYear() && tradeDay.getMonth() == chooserDate.getMonth() && tradeDay.getDay() == chooserDate.getDay())
		{
			row.getCell(quotationColumns.high.index).setValue(node.getAttribute(quotationColumns.high.key));
			row.getCell(quotationColumns.low.index).setValue(node.getAttribute(quotationColumns.low.key));
			row.getCell(quotationColumns.open.index).setValue(node.getAttribute(quotationColumns.open.key));
			row.getCell(quotationColumns.close.index).setValue(node.getAttribute(quotationColumns.close.key));
		}
		else
		{
			row.getCell(quotationColumns.preOpen.index).setValue(node.getAttribute(quotationColumns.open.key));
			row.getCell(quotationColumns.preClose.index).setValue(node.getAttribute(quotationColumns.close.key));
		}
	}
	
	function clearRowCells(row)
	{
		var nullText = null;
		
		row.getCell(quotationColumns.high.index).setValue(nullText);
		row.getCell(quotationColumns.low.index).setValue(nullText);
		row.getCell(quotationColumns.open.index).setValue(nullText);
		row.getCell(quotationColumns.close.index).setValue(nullText);
		row.getCell(quotationColumns.preOpen.index).setValue(nullText);
		row.getCell(quotationColumns.preClose.index).setValue(nullText);
	}
	
	 function trimString(string)
    {
        //remove the '' on the top of the string 
        while(string.length>0)
        {
           if(string.substring(0,1)!=' ')
           {
              break;
           }
           else
           {
              string=string.substring(1);
           }
        }
         // remove th '' on the end of the string 
        while(string.length>0)
        {
            if(string.substring(string.length-1,string.length)!=' ')
            {
                break;
            }
            else
            {
                string=string.substring(0,string.length-1);
            }
        }
       return string;
    }
