var global_barcodeTaken = false;

function m1121(){	
	menu.hide();
	var SQL = "select '1',id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_ from catalogue where media_ = 'Journal'"; 
	var SQLCOLS = "id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_";
	var RECEIPTCOLS = "id_,barcode_,journalyear_,journalissue_,location_,site_,shelf_,status_,receipt_";
	GridDiv("Journals");
	var	cataloguemenu = new dhtmlXMenuObject();
	cataloguemenu.setImagePath("codebase/imgs/");
	cataloguemenu.setIconsPath("imgs/");
	cataloguemenu.renderAsContextMenu();
	cataloguemenu.loadXML("_contextm1121.xml");
	GridStyle('800px','260px');
	var mygrid;
	mygrid = new dhtmlXGridObject('gridholder');
	mygrid.attachEvent("onXLE", function() {
		showLoading(false);
	});
    mygrid.attachEvent("onXLS", function() {
        showLoading(true);
    });
	mygrid.setHeader("&nbsp;,Id,Issn,Title,Class,Publisher,Media,Holding,Date,Subprice,Startsub"); 
	mygrid.setColTypes("sub_row_grid,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro");
	mygrid.setInitWidths("30,70,70,200,70,100,70,70,70,70,70"); 
	mygrid.setColAlign("left,left,left,left,left,left,left,left,left,left,left"); 
	mygrid.setColSorting("na,connector,connector,connector,connector,connector,connector,connector,connector,connector,connector");
	mygrid.enableContextMenu(cataloguemenu);
	SQLCOLS = "1," + SQLCOLS;
	GridInit(mygrid,SQL,SQLCOLS);
	global_mygrid = mygrid;
	AddPrintButton(mygrid);
	AddExportButton(SQL,SQLCOLS,"journals");
	AddNewButton(mygrid,"catalogue","m1121.xml",SQLCOLS,"issn_",'700','600',SQL);
//add the receipt grid
	mygrid.attachEvent("onSubGridCreated",function(subgrid,row_id,ind){
		subgrid.setHeader("Barcode,Year,Volume,Issue,Location,Site,Shelf,Status,Date"); 
		subgrid.setColAlign("left,left,left,left,left,left,left,left,left"); 
        subgrid.setInitWidths("80,80,80,80,80,80,80,80"); 
        subgrid.setColTypes("ro,ro,ro,ro,ro,ro,ro,ro,ro"); 
        subgrid.setSkin("blue"); 
        subgrid.init();
        var myId = mygrid.cellById(row_id, 1).getValue();
        JournalSubGrid(myId,subgrid);
        return false; //block default logic });
	});
	cataloguemenu.attachEvent("onClick", function(menuitemId, type){
	    var data = global_mygrid.contextID.split("_");
	    var line ='';
        var a = new Array();
    	a = SQLCOLS.split(',');
    	ClearRecord();
    	for(var i=0;i<a.length;i++){
    		var myField = global_mygrid.cellById(data[0], i).getValue();
    		line += myField + ",";
    	}
        eval(menuitemId);
	    return true;
  	});
}

//receipt
function m1121cm1(line){
	currentline_ = 1;
	InitOrderLines();
	order_lines[0] = 0;
	var dhxWins,w1,	tabbar;
	dhxWins = new dhtmlXWindows();
    dhxWins.enableAutoViewport(true);
    dhxWins.setImagePath("imgs/");
    w1 = dhxWins.createWindow("w1", 100, 100, 700, 450);
    dhxWins.attachEvent("onClose", function(win){
       	var SQL = "select '1',id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_ from catalogue where media_ = 'Journal' order by title_ asc"; 
    	var SQLCOLS = "1,id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_";
    	global_mygrid.clearAll();
    	global_mygrid.loadXML("connector.php?cmd="+SQL+"&cols="+SQLCOLS+"&");
    	return true; 
    	return true; 
    });
  
    w1.setText("Enter Records");
    tabbar = w1.attachTabbar();
    tabbar.setSkin("dhx_skyblue");
    tabbar.setImagePath("imgs/");
    tabbar.attachEvent("onXLE", function() { //row_id will be the first row
    	showLoading(false);
        SetMouseKeys();
    	SetValidator();
    	GoClearAll("isbn_");
    	MakeDisableOnNew();
    	ClearRecord();
    	$("#catalogue_").val(line.split(',')[1]);
    	GoToField("#barcode_");
    });
    tabbar.attachEvent("onXLS", function() {
        showLoading(true);
    });
    tabbar.loadXML("m1122.xml");
} 

function m1121cm2(line){
	var SQL = "seclect '1',id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_,renew_,supplier_,web_,email_"; 
	var SQLCOLS = "id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_,renew_,supplier_,web_,email_";
	var dhxWins,w1,	tabbar;
	dhxWins = new dhtmlXWindows();
    dhxWins.enableAutoViewport(true);
    dhxWins.setImagePath("imgs/");
    w1 = dhxWins.createWindow("w1", 100, 100, 700, 600);
    dhxWins.attachEvent("onClose", function(win){
    	var SQL = "select '1',id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_ from catalogue where media_ = 'Journal' order by title_ asc"; 
    	var SQLCOLS = "1,id_,issn_,title_,class_,publisher_,media_,holding_,catalogued_,subprice_,startsub_";
    	global_mygrid.clearAll();
    	global_mygrid.loadXML("connector.php?cmd="+SQL+"&cols="+SQLCOLS+"&");
    	return true; 
    });
    w1.setText("Enter Records");
    tabbar = w1.attachTabbar();
    tabbar.setSkin("dhx_skyblue");
    tabbar.setImagePath("imgs/");
    tabbar.attachEvent("onXLE", function() { //row_id will be the first row
        showLoading(false);
        var a = new Array();
    	a = SQLCOLS.split(',');
    	ClearRecord();
    	for(var i=0;i<a.length;i++){
    		var myField = line.split(',')[i+1];
    		$("#" + a[i]).val(myField);
    	}
    	SetMouseKeys();
    	SetValidator();
    	MakeDisable();
    	MakeDates();
    	GoToField("#issn_");
	});
    tabbar.attachEvent("onXLS", function() {
        showLoading(true);
    });
    tabbar.loadXML("m1121.xml");

} 
function m11221(){
	menu.hide();
	var SQL = "select '1',id_,name_ from route"; 
	var SQLCOLS = "id_,name_";
	GridDiv("Routing");
	var	cataloguemenu = new dhtmlXMenuObject();
	cataloguemenu.setImagePath("codebase/imgs/");
	cataloguemenu.setIconsPath("imgs/");
	cataloguemenu.renderAsContextMenu();
	cataloguemenu.loadXML("_contextm11221.xml");
	GridStyle('800px','260px');
	var mygrid;

	mygrid = new dhtmlXGridObject('gridholder');
	mygrid.attachEvent("onXLE", function() {
		showLoading(false);
	});
    mygrid.attachEvent("onXLS", function() {
        showLoading(true);
    });
	mygrid.setHeader("&nbsp;,Id,Name"); 
	mygrid.setColTypes("sub_row_grid,ro,ro");
	mygrid.setInitWidths("30,70,350"); 
	mygrid.setColAlign("left,left,left"); 
	mygrid.setColSorting("na,connector,connector");
	mygrid.enableContextMenu(cataloguemenu);
	SQLCOLS = "1," + SQLCOLS;
	GridInit(mygrid,SQL,SQLCOLS);
	global_mygrid = mygrid;
	AddPrintButton(mygrid);
	AddExportButton(SQL,SQLCOLS,"Routes");
	AddNewButton(mygrid,"route","m1123.xml",SQLCOLS,"name_",'500','420',SQL);
//add the sub grid

	mygrid.attachEvent("onSubGridCreated",function(subgrid,row_id,ind){
		subgrid.setHeader("Id,Route,Member,Priority"); 
		subgrid.setColAlign("left,left,left,left"); 
        subgrid.setInitWidths("80,80,80,80"); 
        subgrid.setColTypes("ro,ro,ro,ro"); 
        subgrid.setSkin("blue"); 
        subgrid.init();
        var myId = mygrid.cellById(row_id, 2).getValue();
        RouteSubGrid(myId,subgrid);
        return false; //block default logic });
	});
	cataloguemenu.attachEvent("onClick", function(menuitemId, type){
	    var data = global_mygrid.contextID.split("_");
	    var line ='';
        var a = new Array();
    	a = SQLCOLS.split(',');
    	ClearRecord();
    	for(var i=0;i<a.length;i++){
    		var myField = global_mygrid.cellById(data[0], i).getValue();
    		line += myField + ",";
    	}
        eval(menuitemId);
	    return true;
  	});
}
function m11221cm1(line){
	currentline_ = 1;
	InitOrderLines();
	order_lines[0] = 0;
	var dhxWins,w1,	tabbar;
	dhxWins = new dhtmlXWindows();
    dhxWins.enableAutoViewport(true);
    dhxWins.setImagePath("imgs/");
    w1 = dhxWins.createWindow("w1", 100, 100, 600, 300);
    dhxWins.attachEvent("onClose", function(win){
       	var SQL = "select '1',id_,name_ from route"; 
    	var SQLCOLS = "1,id_,name_";
    	global_mygrid.clearAll();
    	global_mygrid.loadXML("connector.php?cmd="+SQL+"&cols="+SQLCOLS+"&");
    	return true; 
    	return true; 
    });
  
    w1.setText("Enter Records");
    tabbar = w1.attachTabbar();
    tabbar.setSkin("dhx_skyblue");
    tabbar.setImagePath("imgs/");
    tabbar.attachEvent("onXLE", function() { //row_id will be the first row
    	showLoading(false);
        SetMouseKeys();
    	SetValidator();
    	MakeDisableOnNew();
    	ClearRecord();
    	$("#route_").val(line.split(',')[2]);
    	GoToField("#member_");
    });
    tabbar.attachEvent("onXLS", function() {
        showLoading(true);
    });
    tabbar.loadXML("m1124.xml");
} 

function m11221cm2(line){
	var SQL = "select '1',id_,name_ from route"; 
	var SQLCOLS = "id_,name_";
	var dhxWins,w1,	tabbar;
	dhxWins = new dhtmlXWindows();
    dhxWins.enableAutoViewport(true);
    dhxWins.setImagePath("imgs/");
    w1 = dhxWins.createWindow("w1", 100, 100, 600, 300);
    dhxWins.attachEvent("onClose", function(win){
    	var SQL = "select '1',id_,name_ from route"; 
    	var SQLCOLS = "1,id_,name_";
    	global_mygrid.clearAll();
    	global_mygrid.loadXML("connector.php?cmd="+SQL+"&cols="+SQLCOLS+"&");
    	return true; 
    });
    w1.setText("Enter Records");
    tabbar = w1.attachTabbar();
    tabbar.setSkin("dhx_skyblue");
    tabbar.setImagePath("imgs/");
    tabbar.attachEvent("onXLE", function() { //row_id will be the first row
        showLoading(false);
        var a = new Array();
    	a = SQLCOLS.split(',');
    	ClearRecord();
    	for(var i=0;i<a.length;i++){
    		var myField = line.split(',')[i+1];
    		$("#" + a[i]).val(myField);
    	}
    	SetMouseKeys();
    	SetValidator();
    	MakeDisable();
    	MakeDates();
    	GoToField("#name_");
	});
    tabbar.attachEvent("onXLS", function() {
        showLoading(true);
    });
    tabbar.loadXML("m1123.xml");

} 
function JournalSubGrid(id,subgrid){		//catalogue id
	var SQL = "select barcode_,journalyear_,journalvolume_,journalissue_,location_,site_,shelf_,status_,receipt_ from copies where catalogue_ = " + id + " order by receipt_ asc";
	var SQLCOLS = "barcode_,journalyear_,journalvolume_,journalissue_,location_,site_,shelf_,status_,receipt_";  
	var result = new Array();
	AjaxSql("tab",SQL,SQLCOLS,result);
	var cnt = result[0];
	var v = SQLCOLS.split(',');
	var SIZE = v.length;
	if(cnt > 0){
		for(var j=0;j<cnt;j++){
			var pos = (j*SIZE)+1;
			var dlist = '';
			for(i=0;i<SIZE;i++){
				var x = result[pos+i];
				if( x == "0000-00-00")
					x = GetDate();
				dlist += x + ',';
			}
			subgrid.addRow(j,dlist);
			subgrid.setRowTextStyle(j, "color: green; ");
		}	
	}
}

function RouteSubGrid(id,subgrid){		//catalogue id
	var SQL = "select id_,route_,member_,priority_ from routelist where route_ = '" + id + "'";
	var SQLCOLS = "id_,route_,member_,priority_";  
	var result = new Array();
	AjaxSql("tab",SQL,SQLCOLS,result);
	var cnt = result[0];
	var v = SQLCOLS.split(',');
	var SIZE = v.length;
	if(cnt > 0){
		for(var j=0;j<cnt;j++){
			var pos = (j*SIZE)+1;
			var dlist = '';
			for(i=0;i<SIZE;i++){
				var x = result[pos+i];
				dlist += x + ',';
			}
			subgrid.addRow(j,dlist);
			subgrid.setRowTextStyle(j, "color: green; ");
		}	
	}
}

function Goreceipt(mygrid,table,formname,COLS){
	var cnt = 1;
	currentline_ = 1;
	InitOrderLines();
	order_lines[0] = 0;
	var checked=mygrid.getCheckedRows(1);
	var a = checked.split(',');
	order_lines[0] = a.length;
	for(var i=0;i<a.length;i++){
		var myId = mygrid.cellById(a[i], 2).getValue();
		order_lines[i+1] = myId;
	}
	EntryForm(mygrid,formname,COLS);
}



function GoJournalExport(mygrid){
	

}


function m1124(){
	menu.hide();
	GridDiv("");
	loadTab("#wizard","m1124.div");
	SetMouseKeys();
	SetValidator();
	current_menu = 'm1124';
	currentline_ = 1;
	InitOrderLines();
	GoToField("#name_");
}


function m1121Save(){
	var v = $("#cmaForm").validate();
	if(v.form()){
		var id_ = $("#id_").val();
		var old = $("#id_").val();
		if(id_ != ''){
			var SQL = MakeUpdateSql($("#amend1").val());
			var result = UpdateRecord(SQL,"loading");
			if(isNaN(result))
				alert(result);
			else{		
				alert("Record amended");
				GoToField("#id_");
				GoClearAmend();
				$("#id_").val(old);
			}
		}
		else{
			//checkIsbn
			var SQL = MakeInsertSql($("#insert1").val(),'','');
			var result = InsertRecord(SQL);
			if(isNaN(result))
				alert(result);
			else{
				alert("Record saved");
				GoToField("#id_");
				GoClearAmend();
				$("#id_").val(old);
			}
		}
	}

}

function m1123Save(){
	var v = $("#cmaForm").validate();
	if(v.form()){
		var id_ = $("#id_").val();
		var old = $("#id_").val();
		if(id_ != ''){
			var SQL = MakeUpdateSql($("#amend1").val());
			var result = UpdateRecord(SQL,"loading");
			if(isNaN(result))
				alert(result);
			else{		
				alert("Record amended");
				GoToField("#id_");
				GoClearAmend();
				$("#id_").val(old);
			}
		}
		else{
			var SQL = MakeInsertSql($("#insert1").val(),'','');
			var result = InsertRecord(SQL);
			if(isNaN(result))
				alert(result);
			else{
				alert("Record saved");
				GoToField("#id_");
				GoClearAmend();
				$("#id_").val(old);
			}
		}
	}

}
function m1124Save(){
	var v = $("#cmaForm").validate();
	if(v.form()){
		var id_ = $("#id_").val();
		var old = $("#id_").val();
		if(id_ != ''){
			var SQL = MakeUpdateSql($("#amend1").val());
			var result = UpdateRecord(SQL,"loading");
			if(isNaN(result))
				alert(result);
			else{		
				alert("Record amended");
				GoToField("#id_");
				GoClearAmend();
				$("#id_").val(old);
			}
		}
		else{
			var SQL = MakeInsertSql($("#insert1").val(),'member_',$("#barcode_").val());
			prompt("",SQL);
			var result = InsertRecord(SQL);
			if(isNaN(result))
				alert(result);
			else{
				alert("Record saved");
				GoToField("#id_");
				GoClearAmend();
				$("#id_").val(old);
			}
		}
	}

}
function GoJournal(){
	//barcode format 
	//9 771234 567010 01 raw
	//977123456701001
	//...journal code 0-
	//   .......issn 3-11
	//          ..varient
	//            .check digit
	//             ..issue number
	var myValue = $("#barcode_").val();
	if(myValue == ''){
		GoNext();
		global_barcodeTaken = true;
	}
	else{
		myValue = myValue.replace(/ /g,"");
		myValue = myValue.replace(/-/g,"");
		myValue = myValue.replace(/\./g,"");
		$("#barcode_").val(myValue + "/copy 1");
//get issn
		var issn = myValue.substr(3,7);
//get issue
		var issue = myValue.substr(13,2);
		$("#journalissue_").val(issue);
		if(myValue != ''){
			var copyfrom = $("#copyfrom").val(); 
			Populate("gophptable.php",copyfrom,"issn_",issn);
		}
		global_barcodeTaken = false;
	}
}


function GoClearJournal(){

	var v = $("#cmaForm").validate();
	$("#cmaForm").clearForm();
	v.prepareForm();
	v.hideErrors();
$('.gc').each(function(x){
	var field = ($(this).attr("id"));
	var value = ($(this).attr("default"));
	$("#" + field).val(value);
});
$("#receipt_").val(GetDate());
$("#barcode_").focus('');
$("#journalyear_").val(GetYear());
}
function GoRouteAuthority(fieldname,header,phpfile,seed,popit,multi,finder,func){
	var copyfrom='';
	var ihtml=	"<div>Please choose the correct term</div>" +
	"	<div id=\"combo\" style=\"width:200px; height:15px;\"></div>";
	var myValue = $(popit).val();
	if(myValue == '')
		myValue = seed;
		Tip(
		ihtml,CLICKSTICKY, true, CLICKCLOSE, false, 
		CLOSEBTN, true,	
		TITLE, header + ' Authority', PADDING, 9,FIX, [570, 300],
		WIDTH,400,FONTFACE, 'Arial, Helvetica, sans-serif', FONTSIZE, '10pt',TITLEALIGN, 'center');
		var z = new dhtmlXCombo("combo", "alfa", 400);
		z.enableFilteringMode(true, phpfile, true, true);
		z.setComboText(myValue);
		z.setOptionHeight(300);
		if(finder != '')		//see if there is a valid entry already in the box
			copyfrom = $("#copyfrom").val();
		z.attachEvent("onChange", function(value,state){func(z);});
}

function RouteFMPop(z){
	UnTip();
	var x = z.getComboText().split('\t');
	$("#find_member_").val(x[2]);
	var route = $("#find_route_").val();
	if(route != ''){
		var copyfrom = "select id_,route_,member_,priority_ from routelist where route_ = '" + route + "' and $param = '$param_value' limit 1";
		Populate("gophptable.php",copyfrom,"member_",$("#find_member_").val());
	}
}

function RouteFRPop(z){
	UnTip();
	var x = z.getComboText();
	$("#find_route_").val(x);
	var member = $("#find_member_").val();
	if(member != ''){
		var copyfrom = "select id_,route_,member_,priority_ from routelist where route_ = '" + x + "' and $param = '$param_value' limit 1";
		Populate("gophptable.php",copyfrom,"member_",$("#find_member_").val());
	}
}
function RouteMPop(z){
	UnTip();
	var x = z.getComboText().split('\t');
	$("#member_").val(x[0]);
	$("#barcode_").val(x[2]);
	GoToField("#route_");
}

function RouteRPop(z){
	UnTip();
	var x = z.getComboText();
	$("#route_").val(x);
	GoToField("#priority_");
}
