var returnvalue=-1;
var DataForm;
var hndDataForm_Open;
var opAdd = 1;
var opOpen = 2;
var opSave = 3;
var opSaveAs = 6;
var opDelete = 4;
var opHelp = 5;
var Operation = - 1;
var httpWhere = "";
var wHeight = "500";
var wWidth = "600";

function DataForm_onclick()
{	
GetDataform();
var oDoc = DataFormLookup.XMLDocument;
var root = oDoc.documentElement;
var Lookupstring,SearchDropDownFields,DisplayFields,DBFields;
Lookupstring = ""

Lookupstring = Lookupstring + "form=ControlForm";

SearchDropDownFields=NodeText(oDoc.selectSingleNode("Lookup/SearchDropDownFields"))
SearchDropDownFields=SearchDropDownFields.replace("#","Ó");

DBFields=NodeText(oDoc.selectSingleNode("Lookup/DBFields"));
DBFields=DBFields.replace("#","Ó");

DisplayFields=NodeText(oDoc.selectSingleNode("Lookup/DisplayFields"));
DisplayFields=DisplayFields.replace("#","Ó");
 
Lookupstring = Lookupstring + "&SearchDropDownFields=" + SearchDropDownFields;

Lookupstring = Lookupstring + "&GroupBy=" + NodeText(oDoc.selectSingleNode("Lookup/GroupBy"));

Lookupstring = Lookupstring + "&Listed=True&ImplementSearchFor=1"; // + NodeText(oDoc.selectSingleNode("Lookup/Listed"));

Lookupstring = Lookupstring + "&Refresh=True"; // + NodeText(oDoc.selectSingleNode("Lookup/Refresh"));

Lookupstring = Lookupstring + "&DispFields=" + DisplayFields;

Lookupstring = Lookupstring + "&SearchFields=" + NodeText(oDoc.selectSingleNode("Lookup/SearchFields"));

Lookupstring = Lookupstring + "&ShowFieldColumn=" + DBFields ;

Lookupstring = Lookupstring + "&DisplayIN=KeyID";

Lookupstring = Lookupstring + "&SortingCols=" + NodeText(oDoc.selectSingleNode("Lookup/SortOn"));


if(NodeText(oDoc.selectSingleNode("Lookup/KeyField"))!="")
	Lookupstring = Lookupstring + "&SearchFor=" + ReplaceAll(DataForm.item(NodeText(oDoc.selectSingleNode("Lookup/KeyField"))).value,"#","<~~>");

Lookupstring = Lookupstring + "&Criteria=" + NodeText(oDoc.selectSingleNode("Lookup/Criteria"));
Lookupstring = Lookupstring + "&DBTable=" + NodeText(oDoc.selectSingleNode("Lookup/Table"));
Lookupstring = Lookupstring + "&ExtraEvent=" + NodeText(oDoc.selectSingleNode("Lookup/ExtraEvent"));
Lookupstring = Lookupstring + "&GridName=" + NodeText(oDoc.selectSingleNode("Lookup/CourseGrid"));
Lookupstring = Lookupstring + "&GridCols=" + NodeText(oDoc.selectSingleNode("Lookup/GridCols"));
Lookupstring = Lookupstring + "&MultiFlag=" + NodeText(oDoc.selectSingleNode("Lookup/Multi"));
Lookupstring = Lookupstring + "&WindowTitle=" + NodeText(oDoc.selectSingleNode("Lookup/WindowTitle"));
Lookupstring = Lookupstring + "&HLinkField=" + NodeText(oDoc.selectSingleNode("Lookup/HLinkField"));



if(hndDataForm_Open=="[object]"){
if(hndDataForm_Open.closed == true)
 hndDataForm_Open=window.open("Library/FindWindow.asp?" + Lookupstring,"hndDataForm_Open","height=" + wHeight + ",width=" + wWidth +",left=150,top=10,scrollbars=yes");
else
 hndDataForm_Open.focus();
}
else
 hndDataForm_Open=window.open("Library/FindWindow.asp?" + Lookupstring,"hndDataForm_Open","height=" + wHeight + ",width=" + wWidth + ",left=150,top=10,scrollbars=yes");
}

function NodeText(oNode){
if(oNode!=null)
 return oNode.text;
else
 return "";
}

function GetDataform(){
for(frm in document.forms){ 
 if(document.forms(frm)) {  
   if(document.forms(frm).getAttribute("DataForm")!=null){
     DataForm = document.forms(frm); 
   }
 }
}

//return DataForm;
}
function Show_Validating_()
{
	if(document.getElementById("oSaveMessage") == "[object]")
		document.getElementById("oSaveMessage").innerText = "Wait!. Checking data..."
	if(document.getElementById("oSave") == "[object]")
		document.getElementById("oSave").disabled = true
}

function Show_Saving_()
{
	if(document.getElementById("oSaveMessage") == "[object]")
		document.getElementById("oSaveMessage").innerText = "Saving..."
	if(document.getElementById("oSave") == "[object]")
		document.getElementById("oSave").disabled = true	
	
}
function Hide_Saving_()
{
	if(document.getElementById("oSaveMessage") == "[object]")
		document.getElementById("oSaveMessage").innerText = ""
	if(document.getElementById("oSave") == "[object]")
		document.getElementById("oSave").disabled = false	
}

function Bar_Click(Operation)
{

var validflag=true;
GetDataform();

DataForm.item("Operation").value = Operation;
ControlForm.item('Operation').value=Operation;

if (Operation == opAdd)
{
ControlForm.item('KeyID').value = -1;
ControlForm.submit();
return true;
}
if (Operation == opOpen){

 DataForm_onclick();
}
if(Operation == opSave || Operation == opSaveAs)
{
  DataForm.item('Save').click();
}
if (Operation == opDelete){
 if (DeleteCheck()){ 
   if(DataForm.getAttribute("onDelete")!= '' && DataForm.getAttribute("onDelete")!=null)
   validflag = CallValidator(DataForm.getAttribute("onDelete"))
 if(validflag){
 if(confirm("Are you sure you want to delete the entry")) {
   ControlForm.item('KeyID').value = DataForm.item('KeyID').value;
   ControlForm.submit();
  }
 }
}
}

if (Operation == opHelp){
window.open("Help.asp","LookUp" ,"height=400,width=450,left=200,top=100,scrollbars=yes");
}

return true;
}

function DeleteCheck(){
var key;
key = DataForm.item('KeyID').value;
if(key <= 0 )
{
alert('There is no entry currently open. Please open the entry you would like to delete then select the Delete Entry icon again to delete the entry.');
return false;
}
else 
return true;
}

function CheckCode()
{
	
 try
 {
	var oDoc = DataFormLookup.XMLDocument;
 }

 catch(obj)
 {
 return true;
 }
 	var uniquecheck = oDoc.selectSingleNode("Lookup/CheckCode");
	if (uniquecheck) 
	{
		CheckConditions(uniquecheck)
		var field = NodeText(oDoc.selectSingleNode("Lookup/KeyField"));
		var table = NodeText(oDoc.selectSingleNode("Lookup/Table"));
		var httpWhere = NodeText(oDoc.selectSingleNode("Lookup/httpWhere"));				
		if (FormString(DataForm.item(field).value)!="")
		{			
			if (HTTPValidate(table,DataForm.item("KeyID").value,field,DataForm.item(field).value,httpWhere))
			 {
				alert(DataForm.item(field).getAttribute("caption") + "  \"" + DataForm.item(field).value + "\"  already exists.")
				return false;
			}
		}
	}
	return true;

}

// Action 7723, Fixed Govind's work

function CheckCodeSaveAs(){
	
	 try{
		var oDoc = DataFormLookup.XMLDocument;
	 }
	 catch(obj){
	 return true;
	}
 	var uniquecheck = oDoc.selectSingleNode("Lookup/CheckCode");
	if (uniquecheck) {
		CheckConditions(uniquecheck)
		var field = NodeText(oDoc.selectSingleNode("Lookup/KeyField"));
		var table = NodeText(oDoc.selectSingleNode("Lookup/Table"));
		var httpWhere = NodeText(oDoc.selectSingleNode("Lookup/httpWhere"));				
		if (FormString(DataForm.item(field).value)!="")
		{		
			if (HTTPValidateSaveAs(table,DataForm.item("KeyID").value,field,DataForm.item(field).value,httpWhere))
			 {
				alert(DataForm.item(field).getAttribute("caption") + "  \"" + DataForm.item(field).value + "\"  already exists.")
				return false;
			}
		}
	}
}


function CheckConditions(node){
	httpWhere = ""
	if (node.hasChildNodes){
		var childs = node.firstChild
			while (childs){
				if (httpWhere!="")
				httpWhere+=httpWhere + " and "
				if (childs.attributes.item(2).value=="bssInt"){
					httpWhere+=" IsNull(" + childs.attributes.item(0).value + ",0)=" 
					//httpWhere+=FormLong(DataForm.item(childs.attributes.item(1).value).value);
					//alert(document.all(childs.attributes.item(1).value).value)
					httpWhere+=FormLong(document.all(childs.attributes.item(1).value).value);		
				}else{
					httpWhere+=" " + childs.attributes.item(0).value + "='" + DataForm.item(childs.attributes.item(1).value).value + "'"; 	
				}
				childs = childs.nextSibling;
			}
	}
}
