/**
 * @alias invent.lib
 * @author WilC <wilz04@gmail.com>
 * @since 2008
 */

var invent = {
	
	custommer: {
/* ---------------------------------------------------------------- */
		Index: {
			
			datagrid: null,
			
			init: function () {
				if (document.getElementById("result") != null) {
					invent.custommer.Index.dataGrid = new DataGrid("result", {colors: {hilight: new Hex(0xD0, 0xDB, 0xDB)}});
				}
			},
			
			bAdvanced_onClick: function () {
				var search = new Frame("search.htm");
				search.show();
			}
			
		},
/* ---------------------------------------------------------------- */
		Search: {
			
			init: function () {
				var lang = new LangChooser("lcLang");
				lang.addInto("lang");
				
				var dcFrom = new DateChooser("dcFrom", {
					firstYear: invent.admin.FIRST_YEAR,
					format: "m-d-y"
				});
				dcFrom.addInto("from");
				
				var dcTo = new DateChooser("dcTo", {
					firstYear: invent.admin.FIRST_YEAR,
					format: "m-d-y"
				});
				dcTo.addInto("to");
				
				var form = new Form("fSearch");
				
				form.onSubmitException(function (e) {
					OptionPane.showMessageDialog(1);
					return false;
				});
				
				form.onSubmitSuccess(function () {
					var comboGroup = [
						"cbTitle",
						"cbAuthor",
						"cbPublisher",
						"cbISBN",
						"cbYear",
						"cbCountry",
						"cbLang",
						"cbSummary",
						"cbDate"
					];
					var i;
					for (i in comboGroup) {
						if (document.getElementById(comboGroup[i]).checked) {
							return true;
						}
					}
					OptionPane.showMessageDialog(2);
					return false;
				});
			}
			
		},
/* ---------------------------------------------------------------- */
		Result: {
			
			datagrid: null,
			
			init: function () {
				invent.custommer.Result.dataGrid = new DataGrid("result", {colors: {hilight: new Hex(0xD0, 0xDB, 0xDB)}});
			},
			
			link_onClick: function (url) {
				opener.location.href = url;
				return false;
			}
			
		}
/* ---------------------------------------------------------------- */
	},
	
	admin: {
		
		URL: "/invent/admin/",
		FIRST_YEAR: 1980,
/* ---------------------------------------------------------------- */
		Master: {
		
			init: function () {
				var bar = new menubar.MenuBar();
				
				var mCat = new menubar.Menu("Categories");
				var mBok = new menubar.Menu("Products");
				
				var miCShow = new menubar.MenuItem("Show", {
					onClick: function () {
						invent.admin.Master.getURL(invent.admin.URL + "categories/show.php");
					}
				});
				var miCAdd = new menubar.MenuItem("Add", {
					onClick: function () {
						invent.admin.Master.getURL(invent.admin.URL + "categories/add.php");
					}
				});
				var miBShow = new menubar.MenuItem("Show", {
					onClick: function () {
						invent.admin.Master.getURL(invent.admin.URL + "products/show.php");
					}
				});
				var miBAdd = new menubar.MenuItem("Add", {
					onClick: function () {
						invent.admin.Master.getURL(invent.admin.URL + "products/add.php");
					}
				});
				
				if (invent.admin.categories.Show.length > 0) {
					mCat.add(miCShow);
				}
				mCat.add(miCAdd);
				if (invent.admin.products.Show.length > 0) {
					mBok.add(miBShow);
				}
				mBok.add(miBAdd);
				
				bar.add(mCat);
				bar.add(mBok);
				
				bar.addInto("menubar");
			},
			
			getURL: function (url) {
				window.open(url, "_self");
			}
			
		},
/* ---------------------------------------------------------------- */
		categories: {
/* ---------------------------------------------------------------- */
			Show: {
				
				dataGrid: null,
				length: 0,
				
				init: function (message) {
					invent.admin.Master.init();
					
					invent.admin.categories.Show.dataGrid = new DataGrid("datagrid");
					
					if (message != null) {
						OptionPane.showMessageDialog(message);
					}
				},
				
				checkId: function () {
					var rgId = new RadioGroup("rId");
					if (rgId.isChecked()) {
						return rgId.getValue();
					} else {
						OptionPane.showMessageDialog(2);
						return null;
					}
				},
				
				bEdit_onClick: function () {
					var id = invent.admin.categories.Show.checkId();
					if (id != null) {
						var cat = new Frame(invent.admin.URL + "categories/edit.php?id=" + id.toString());
						cat.style.height = 200;
						cat.show();
					}
				},
				
				bDel_onClick: function () {
					var id = invent.admin.categories.Show.checkId();
					if (id != null) {
						if (OptionPane.showConfirmDialog(5)) {
							invent.admin.Master.getURL(invent.admin.URL + "categories/delete.php?id=" + id.toString());
						}
					}
				}
				
			},
/* ---------------------------------------------------------------- */
			Add: {
				
				init: function () {
					invent.admin.Master.init();
					
					var form = new Form("fAdmin");
					form.onSubmitException(function (e) {
						OptionPane.showMessageDialog(1);
						return false;
					});
				}
				
			},
/* ---------------------------------------------------------------- */
			Edit: {
				
				init: function (message) {
					var form = new Form("fAdmin");
					form.onSubmitException(function (e) {
						OptionPane.showMessageDialog(1);
						return false;
					});
					
					if (message != null) {
						OptionPane.showMessageDialog(message);
					}
				}
				
			}
/* ---------------------------------------------------------------- */
		},
		
		products: {
/* ---------------------------------------------------------------- */
			Show: {
				
				dataGrid: null,
				length: 0,
				
				init: function (message) {
					invent.admin.Master.init();
					
					invent.admin.products.Show.dataGrid = new DataGrid("datagrid");
					
					if (message != null) {
						OptionPane.showMessageDialog(message);
					}
				},
				
				sCat_onChange: function () {
					var cbCat = new ComboBox("sCat");
					window.location.href = "?cat=" + cbCat.getValue();
				},
				
				checkId: function () {
					var rgId = new RadioGroup("rId");
					if (rgId.isChecked()) {
						return rgId.getValue();
					} else {
						OptionPane.showMessageDialog(2);
						return null;
					}
				},
				
				bShow_onClick: function () {
					var id = invent.admin.products.Show.checkId();
					if (id != null) {
						var prod = new Frame(invent.admin.URL + "products/details.php?id=" + id.toString());
						prod.style.width = 660;
						prod.style.height = 400;
						prod.show();
					}
				},
				
				bEdit_onClick: function () {
					var id = invent.admin.products.Show.checkId();
					if (id != null) {
						var prod = new Frame(invent.admin.URL + "products/edit.php?id=" + id.toString());
						prod.style.height = 710;
						prod.show();
					}
				},
				
				bDel_onClick: function () {
					var id = invent.admin.products.Show.checkId();
					if (id != null) {
						if (OptionPane.showConfirmDialog(5)) {
							invent.admin.Master.getURL(invent.admin.URL + "products/delete.php?id=" + id.toString());
						}
					}
				}
				
			},
/* ---------------------------------------------------------------- */
			Details: {
				
				dataGrid: null,
				
				init: function (message) {
					invent.admin.products.Details.dataGrid = new DataGrid("dgFiles");
				}
				
			},
/* ---------------------------------------------------------------- */
			Add: {
				
				filelist: null,
				uploadedFiles: [],
				
				init: function () {
					invent.admin.Master.init();
					
					var date = new DateChooser("dcDatePublished", {
						firstYear: invent.admin.FIRST_YEAR,
						format: "m-d-y"
					});
					date.addInto("datepublished");
					
					invent.admin.products.Add.filelist = new filelist.FileList("flFiles");
					invent.admin.products.Add.filelist.addInto("files");
					
					var country = new CountryChooser("ccCountry");
					country.addInto("country");
					
					var lang = new LangChooser("lcLang", {multiple:true});
					lang.addInto("lang");
					
					var form = new Form("fAdmin");
					form.onSubmitException(function (e) {
						switch (e.message) {
							case "Null data exception":
							OptionPane.showMessageDialog(1);
							break;
							case "Incompatible types":
							OptionPane.showMessageDialog(3);
							break;
							case "Duplicate data exception":
							OptionPane.showMessageDialog(4);
						}
						return false;
					});
					form.onSubmitSuccess(function () {
						var success = true;
						var oldfiles = invent.admin.products.Add.uploadedFiles;
						var newfiles = invent.admin.products.Add.filelist;
						var tmpfile;
						var i;
						var j;
						var k = newfiles.getLength();
						//newfiles.blur();
						for (i=0; i<k; i++) {
							for (j in oldfiles) {
								tmpfile = newfiles.getFile(i);
								if (tmpfile.getParent() != null) {
									if (oldfiles[j] == tmpfile.getFileURL().split("\\").pop()) {
										tmpfile.focus();
										return OptionPane.showMessageDialog(4);
									}
								}
							}
						}
						return true;
					});
				}
				
			},
/* ---------------------------------------------------------------- */
			Edit: {
				
				country: null,
				lang: null,
				rating: 10,
				filelist: null,
				uploadedFiles: [],
				
				init: function (message) {
					var date = new DateChooser("dcDatePublished", {
						date: invent.admin.products.Edit.getDate(),
						firstYear: invent.admin.FIRST_YEAR,
						format: "m-d-y"
					});
					date.addInto("datepublished");
					
					invent.admin.products.Edit.filelist = new filelist.FileList("flFiles");
					invent.admin.products.Edit.filelist.addFiles(invent.admin.products.Edit.getFiles());
					invent.admin.products.Edit.filelist.addInto("files");
					
					var country = new CountryChooser("ccCountry");
					country.addInto("country");
					country.setSelectedCountry(invent.admin.products.Edit.country);
					
					var lang = new LangChooser("lcLang", {multiple:true});
					lang.addInto("lang");
					lang.setSelectedLangs(invent.admin.products.Edit.lang);
					
					var rating = new ComboBox("sRating");
					rating.setSelectedItem(new Option(invent.admin.products.Edit.rating));
					
					var form = new Form("fAdmin");
					form.onSubmitException(function (e) {
						switch (e.message) {
							case "Null data exception":
							OptionPane.showMessageDialog(1);
							break;
							case "Incompatible types":
							OptionPane.showMessageDialog(3);
							break;
							case "Duplicate data exception":
							OptionPane.showMessageDialog(4);
						}
						return false;
					});
					form.onSubmitSuccess(function () {
						var success = true;
						var oldfiles = invent.admin.products.Edit.uploadedFiles;
						var newfiles = invent.admin.products.Edit.filelist;
						var tmpfile;
						var i;
						var j;
						var k = newfiles.getLength();
						for (i=0; i<k; i++) {
							for (j in oldfiles) {
								tmpfile = newfiles.getFile(i);
								if (tmpfile.getParent() != null) {
									if (oldfiles[j] == tmpfile.getFileURL().split("\\").pop()) {
										tmpfile.focus();
										return OptionPane.showMessageDialog(4);
									}
								}
							}
						}
						return true;
					});
					
					if (message != null) {
						OptionPane.showMessageDialog(message);
					}
				},
				
				getDate: function () {},
				
				getFiles: function () {}
				
			}
/* ---------------------------------------------------------------- */
		}
		
	}
	
};
