var showLoadingCount = 0;

function newObject() {
	var args = newObject.arguments;
	var objectType = args[0];
	var obj = eval(objectType + "Object");
	
	strLastLookUpFunction = null;
	strLastLookUpValue = null;
	
	if (obj.newPage.length > 0) {
		var otherArgs = "";
		
		if (typeof(args[1]) != "undefined") {
			otherArgs += "&" + args[1]
		}
		openWindow(Application["ApplicationPath"] + obj.newPage + "?ObjectType=" + objectType + obj.newQueryString + otherArgs, new Date().valueOf(), obj.newFormWidth, obj.newFormHeight, obj.newWindowFeatures);
	}
}

function editObject() {

	var args = editObject.arguments;
	
	var objectType = args[0];
	var objectID = new String(args[1]);

	if (objectID.length > 0) {
		var obj = eval(objectType + "Object");
		var otherArgs = "";
		
		if (typeof(args[2]) != "undefined") {
			otherArgs += "&" + args[2]
		}
		
		openWindow(Application["ApplicationPath"] + obj.editPage + "?ObjectType=" + objectType + "&ObjectID=" + objectID + otherArgs, objectType + objectID, obj.editFormWidth, obj.editFormHeight, obj.editWindowFeatures);
	} else {
		newObject(objectType, args[2]);
	}
}

function editObjectEx() {
	var args = editObjectEx.arguments;
	
	var pagetype = args[0];
	var objectType = args[1];
	var objectID = new String(args[2]);

	if (objectID.length > 0) {
		var obj = eval(objectType + "Object");
		var otherArgs = "";
		
		if (typeof(args[3]) != "undefined") {
			otherArgs += "&" + args[3]
		}

		switch (pagetype) {
			case "edit":
				openWindow(Application["ApplicationPath"] + obj.editPage + "?ObjectType=" + objectType + "&ObjectID=" + objectID + otherArgs, objectType + objectID, obj.editFormWidth, obj.editFormHeight, obj.editWindowFeatures);
				break;
			case "new":
				openWindow(Application["ApplicationPath"] + obj.newPage + "?ObjectType=" + objectType + "&ObjectID=" + objectID + otherArgs, objectType + objectID, obj.newFormWidth, obj.newFormHeight, obj.newWindowFeatures);
				break;
		}
	} else {
		newObject(objectType, args[3]);
	}
}

function gotoObject() {
	var args = gotoObject.arguments;
	
	var objectType = args[0];
	var objectID = new String(args[1]);

	if (objectID.length > 0) {
		var obj = eval(objectType + "Object");

		location.href = Application["ApplicationPath"] + obj.editPage + "?ObjectType=" + objectType + "&ObjectID=" + objectID + "&" + args[2];
	}
}

//********************************************

function lookupSelect() {
	var args = lookupSelect.arguments;
	var hasChanged = currentIDField.lastvalue != args[0];

	if (currentField.selectFunc) {
		eval(currentField.selectFunc + "(" + turnArrayToString(args) + ")");
	} else {
		currentField.value = args[1];
		currentIDField.value = args[0];
	}
	
	
	if ((hasChanged) && ((typeof(currentField.checkDirty) != "undefined") && (currentField.checkDirty.toLowerCase() == "true"))) {
		dirty = true;
	}

	if (hasChanged && currentField.postLookupFunc) {
		eval(currentField.postLookupFunc + "(" + turnArrayToString(args) + ")");
	}
	
	if (!currentField.selectFunc) {
		currentField.lastvalue = currentField.value;
		currentIDField.lastvalue = currentIDField.value;
	}
	
	if (typeof(divPage) == "undefined" || divPage.style.display != "none") {
		HidePopUp(divPopUp);
		HidePopUp(ifrmPopUp);
		currentField.focus();
		currentField.select();
	}
}

function lookupValidate_Return(obj, unlimitedList) {
	var x = currentField;

	if (unlimitedList != null && unlimitedList) {
		if (obj.length == 0) {
			obj = new Array(new Array(x.value, x.value));
		} else {
			obj.unshift(new Array(x.value, x.value));
		}
	} 
	
	if (obj.length == 0) {
		alert("Invalid value.");
		resetLookup();
		
	} else if (obj.length == 1) {
		var hasChanged = currentIDField.lastvalue != obj[0][0];
		if (x.selectFunc) {
			eval(x.selectFunc + "(" + turnArrayToString(obj[0]) + ")");
		} else {
			x.value = obj[0][1];
			currentIDField.value = obj[0][0];
		}
		
		if (hasChanged && x.postLookupFunc) {
			eval(x.postLookupFunc + "(" + turnArrayToString(obj[0]) + ")");
		}
		
		if (!x.selectFunc) {
			x.lastvalue = x.value;
			currentIDField.lastvalue = currentIDField.value;
		}
		
		if (typeof(divPage) == "undefined" || divPage.style.display != "none") {
			goToNextElement(x);
		}
		
	} else if (obj.length > 1) {
		var fieldsToShow;
		if (x.fieldsToShow) {
			fieldsToShow = x.fieldsToShow;
		} else {
			fieldsToShow = 1;
		}
		
		var columnHeadings;
		if (x.columnHeadings) {
			columnHeadings = x.columnHeadings.split(",");
		} else {
			columnHeadings = null;
		}
		
		var lookupWidth;
		if (x.lookupWidth) {
			lookupWidth = parseInt(x.lookupWidth);
		} else {
			lookupWidth = 300;
		}
		
		var lookupHeight;
		if (x.lookupHeight) {
			lookupHeight = parseInt(x.lookupHeight);
		} else {
			lookupHeight = 150;
		}
		
		LaunchSearch(currentImg, x.id, lookupWidth, lookupHeight, "lookupSelect", fieldsToShow, (columnHeadings != null), columnHeadings, obj);
	}
}

function resetLookup() {
	var x = currentField;
	
	x.value = x.lastvalue;
	currentIDField.value = nz(currentIDField.lastvalue, "");
	
	if (current2Field != null) {
		current2Field.value = current2Field.lastvalue;
	} 
	
	if (current2Field != null) {
		setFocus(current2Field);
	} else {
		setFocus(x);
	}
}

//********************************************

function lookupValidate(x, y, img, unlimitedList) {
	if (x.lastvalue != x.value && x.fieldName != ""){
		currentField = x;
		current2Field = null;
		currentIDField = y;
		currentIDField.value = "";
		currentImg = img;
	
		if (x.preLookupFunc) {
			if (!eval(x.preLookupFunc)) {
				x.value = x.lastvalue;
				return;
			}
		}
		
		if (x.value != ""){		

			if (x.customLookup) {
			
				strCurrentLookUpFunction = x.customLookup;
				params = eval(x.paramsLookupFunc + "(false)");
				
			} else {
				var fields;
				if (x.fields) {
					fields = x.fields;
				} else {
					fields = x.idFieldName + ", " + x.fieldName;
				}		

				var sql = "SELECT " + fields + " FROM " + x.tableName + " WHERE ";
				
				var strWhere;
				if (x.customWhere) {
					strWhere = eval(x.customWhere);
				} else {
					strWhere = x.fieldName + " LIKE '%" + formatSQL(x.value) + "%'";
				}
			
				sql += strWhere;
				
				if (x.whereConditionFunc) {
					var whereCon = eval(x.whereConditionFunc);
				
					if (whereCon.length > 0) {
						sql += " AND " + whereCon;
					}
				}
				
				sql += " GROUP BY " + fields;	
				if (x.orderBy) {
					sql += " ORDER BY " + x.orderBy;
				} else {	
					sql += " ORDER BY " + x.fieldName;
				}
				
				strCurrentLookUpFunction = "SQLRS";	
				var cn;
				if (x.connection) {
					cn = x.connection;
				} else {
					cn = "Connection";
				}
				params = new Array(cn, sql, (x.maxRows ? x.maxRows : 50));
			}
				
			var scriptPage;
			if (x.scriptPage) {
				scriptPage = x.scriptPage;
			} else {
				scriptPage = "Default.aspx";
			}
			
			strCurrentLookUpValue = params.toString();
			jsrsExecute(Application["ApplicationPath"] + "/ServerScript/" + scriptPage, lookupValidate_Return, strCurrentLookUpFunction, params, unlimitedList);
		}else{
			if (x.postLookupFunc) {
				eval(x.postLookupFunc + "()");
				if (typeof(divPage) == "undefined" || divPage.style.display != "none") {
					goToNextElement(x);
				}
			}
			x.lastvalue = x.value;
			currentIDField.lastvalue = currentIDField.value;
			
		}
	}
}

function lookupFind(x, y, img) {
	var params;
	if (currentField != null && currentField.value != currentField.lastvalue) {
		window.event.cancelBubble = true;
	} else if (x.fieldName != "") {
		currentField = x;
		current2Field = null;
		currentIDField = y;

		if (currentField.disabled)
			return;

        if (divPopUp.persistCalendar != undefined && divPopUp.persistCalendar == true) {
            divPopUp.persistCalendar = false;
        }

		if (x.preLookupFunc) {
			if (!eval(x.preLookupFunc)) {
				return;
			}
		}
	
		if (x.customLookup) {
		
			strCurrentLookUpFunction = x.customLookup;
			params = eval(x.paramsLookupFunc + "(true)");
			
		} else {
			var fields;
			if (x.fields) {
				fields = x.fields;
			} else {
				fields = x.idFieldName + ", " + x.fieldName;
			}	
			var sql = "SELECT " + fields + " FROM " + x.tableName;

			if (x.whereConditionFunc) {
				var strWhere = eval(x.whereConditionFunc);

				if (strWhere.length > 0) {
					sql += " WHERE " + strWhere;
				}
			}	
			sql += " GROUP BY " + fields;	
			if (x.orderBy) {
				sql += " ORDER BY " + x.orderBy;
			} else {	
				sql += " ORDER BY " + x.fieldName;
			}
			
			strCurrentLookUpFunction = "SQLRS";	
			var cn;
			if (x.connection) {
				cn = x.connection;
			} else {
				cn = "Connection";
			}
			params = new Array(cn, sql, (x.maxRows ? x.maxRows : 50));
		}
		
		var fieldsToShow;
		if (x.fieldsToShow) {
			fieldsToShow = x.fieldsToShow;
		} else {
			fieldsToShow = 1;
		}
		
		var columnHeadings;
		if (x.columnHeadings) {
			columnHeadings = x.columnHeadings.split(",");
		} else {
			columnHeadings = null;
		}
		
		var lookupWidth;
		if (x.lookupWidth) {
			lookupWidth = parseInt(x.lookupWidth);
		} else {
			lookupWidth = 300;
		}
		
		var lookupHeight;
		if (x.lookupHeight) {
			lookupHeight = parseInt(x.lookupHeight);
		} else {
			lookupHeight = 150;
		}
		
		var scriptPage;
		if (x.scriptPage) {
			scriptPage = x.scriptPage;
		} else {
			scriptPage = "Default.aspx";
		}
		
		strCurrentLookUpValue = params.toString();

		Lookup(img, x.id, lookupWidth, lookupHeight, "lookupSelect", fieldsToShow, (columnHeadings != null), columnHeadings, Application["ApplicationPath"] + "/ServerScript/" + scriptPage, params);
		return;
	}
}

function lookup2Validate(x, y, z, img) {

	if ((x.lastvalue != x.value || y.lastvalue != y.value) && x.field1Name != ""){
		currentField = x;
		current2Field = y;
		currentIDField = z;
		currentIDField.value = "";
		currentImg = img;
	
		if (x.preLookupFunc) {
			if (!eval(x.preLookupFunc)) {
				x.value = x.lastvalue;
				y.value = y.lastvalue;
				return;
			}
		}	

		if (x.value != "" || y.value != ""){		

			if (x.customLookup) {
			
				strCurrentLookUpFunction = x.customLookup;
				params = eval(x.paramsLookupFunc + "(false)");
				
			} else {
				var fields;
				if (x.fields) {
					fields = x.fields;
				} else {
					fields = x.idFieldName + ", " + x.field1Name + ", " + x.field2Name;
				}		
				var sql = "SELECT " + fields + " FROM " + x.tableName;
				
				var strWhere = "";
				if (x.value != "") {
					if (strWhere.length > 0) {
						strWhere += " AND ";
					}
					strWhere += x.field2Name + " LIKE '%" + formatSQL(x.value) + "%' ";
				}
				if (y.value != "") {
					if (strWhere.length > 0) {
						strWhere += " AND ";
					}
					strWhere += x.field1Name + " LIKE '%" + formatSQL(y.value) + "%' ";
				}
				sql += " WHERE " + strWhere;
				if (x.whereConditionFunc) {
					strWhere = eval(x.whereConditionFunc);
				
					if (strWhere.length > 0) {
						sql += " AND " + strWhere;
					}
				}		
				sql += " GROUP BY " + fields;	
				sql += " ORDER BY " + x.orderBy;
				
				strCurrentLookUpFunction = "SQLRS";	
				var cn;
				if (x.connection) {
					cn = x.connection;
				} else {
					cn = "Connection";
				}
				params = new Array(cn, sql, (x.maxRows ? x.maxRows : 50));
			}
				
			var scriptPage;
			if (x.scriptPage) {
				scriptPage = x.scriptPage;
			} else {
				scriptPage = "Default.aspx";
			}
			
			strCurrentLookUpValue = params.toString();

			jsrsExecute(Application["ApplicationPath"] + "/ServerScript/" + scriptPage, lookupValidate_Return, strCurrentLookUpFunction, params);
		}else{
			x.lastvalue = x.value;
			y.lastvalue = y.value;
			currentIDField.lastvalue = currentIDField.value;

			if (x.postLookupFunc) {
				eval(x.postLookupFunc + "()");
				if (typeof(divPage) == "undefined" || divPage.style.display != "none") {
					goToNextElement(x);
				}
			}
		}
	}
}

function lookup2Find(x, y, z, img){
	var params;
	
	if ((currentField != null && currentField.value != currentField.lastvalue) || (currentField != null && current2Field.value != current2Field.lastvalue)) {
		window.event.cancelBubble = true;
	} else if (x.fieldName != "") {
		currentField = x;
		current2Field = y;
		currentIDField = z;
		
		if (currentField.disabled)
			return;

		if (x.preLookupFunc) {
			if (!eval(x.preLookupFunc)) {
				return;
			}
		}
	
		if (x.customLookup) {
		
			strCurrentLookUpFunction = x.customLookup;
			params = eval(x.paramsLookupFunc + "(true)");
			
		} else {
			var fields;
			if (x.fields) {
				fields = x.fields;
			} else {
				fields = x.idFieldName + ", " + x.field1Name + ", " + x.field2Name;
			}	
			var sql = "SELECT " + fields + " FROM " + x.tableName;

			if (x.whereConditionFunc) {
				var strWhere = eval(x.whereConditionFunc);

				if (strWhere.length > 0) {
					sql += " WHERE " + strWhere;
				}
			}		
			sql += " GROUP BY " + fields;	
			sql += " ORDER BY " + x.orderBy;
				
			strCurrentLookUpFunction = "SQLRS";	
			var cn;
			if (x.connection) {
				cn = x.connection;
			} else {
				cn = "Connection";
			}
			params = new Array(cn, sql, (x.maxRows ? x.maxRows : 50));
		}
		
		var fieldsToShow;
		if (x.fieldsToShow) {
			fieldsToShow = x.fieldsToShow;
		} else {
			fieldsToShow = 1;
		}
		
		var columnHeadings;
		if (x.columnHeadings) {
			columnHeadings = x.columnHeadings.split(",");
		} else {
			columnHeadings = null;
		}
		
		var lookupWidth;
		if (x.lookupWidth) {
			lookupWidth = parseInt(x.lookupWidth);
		} else {
			lookupWidth = 300;
		}
		
		var lookupHeight;
		if (x.lookupHeight) {
			lookupHeight = parseInt(x.lookupHeight);
		} else {
			lookupHeight = 150;
		}

		var scriptPage;
		if (x.scriptPage) {
			scriptPage = x.scriptPage;
		} else {
			scriptPage = "Default.aspx";
		}
		
		strCurrentLookUpValue = params.toString();
		
		Lookup(img, x.id, lookupWidth, lookupHeight, "lookupSelect", fieldsToShow, (columnHeadings != null), columnHeadings, Application["ApplicationPath"] + "/ServerScript/" + scriptPage, params);
		return;
	}
}

function showReports(queryString) {
	openWindow(Application["ApplicationPath"] + "/Report/Report.aspx?" + queryString, "Reports", 500, 525, "resizable,status");
}

function showChangePassword() {
	openWindow(Application["ApplicationPath"] + "/ChangePassword.aspx", "Change Password", 500, 160, "status");
}

function tabClicked(table, tab, div, functionName) {

	var currentTab = document.all(table.currentTab);
	var currentDiv = document.all(table.currentDiv);

	if (currentTab != tab){
		if (currentTab != null){
			currentTab.style.backgroundColor = clrTabBackground;
			currentTab.style.borderBottom = clrTabBorderBottom;
			hide(currentDiv);
		}
				
		currentTab = tab;
		table.currentTab = currentTab.id;
		
		currentTab.style.backgroundColor = clrTabSelectBackground;
		currentTab.style.borderBottom = "none";
		if (frmHead.CurrentTab) {
			frmHead.CurrentTab.value = currentTab.id;
		}

		currentDiv = div;
		table.currentDiv = currentDiv.id;
		
		show(currentDiv);

		if (nz(functionName, "") != "") {
			eval(functionName);
		}
	}

	
	if (currentTab.control1 && currentTab == tab) {
		if (!frmHead.ObjectID || frmHead.ObjectID.value == ""){

			if (currentTab.control1 != "null"){
				var control1 = document.all(currentTab.control1);
				if (control1 != undefined) {
					if (control1.type != "hidden" && control1.style.display != "none" && !control1.disabled) {
						control1.focus();
						if (control1.type == "text")
							control1.select();
					} else {
						goToNextElement(control1);
					}
				}
			}
		}else{
		
			if (currentTab.control2 != "null"){
				var control2 = document.all(currentTab.control2);
				if (control2 != undefined) {
					if (control2.type != "hidden" && control2.style.display != "none" && !control2.disabled) {
						control2.focus();
						if (control2.type == "text")
							control2.select();
					} else {
						goToNextElement(control2);
					}
				}
			}
		}
	} 
}

function BuildFunction() {  
	var func;
	var param;
	
	func = frmHead.JSCallFunction.value;
    func = func + "(";

    for (var i = 0; document.all("A" + i); i++) {
		param = document.all("A" + i).value;
		param = param.substring(1, param.length - 1); 
		func += param + ",";
    }
    
    if (func.substr(func.length - 1, 1) == ",")
		func = func.substr(0, func.length - 1);

    func = func + ")";
    
	return func;
}

function spanClick(x, y) {
	window.event.cancelBubble = true;

	if (typeof(x) == "undefined") {
		x = this;
		y = document.all(x.fieldName);
	}	
	
	var navParams;

	if (y.navParamsFunc) {
		navParams = eval(y.navParamsFunc);
	}

	editObject(x.objectType, document.all(x.IDFieldName).value, navParams);
}


function enableControl(control, enabled) {
	var span;
	
	if (enabled) {
		span = document.all("spn" + control.id);
		if (span != null) {
			span.className = "Link";
		}
		document.all("td" + control.id).className = "";
		control.disabled = false;
	} else {
		span = document.all("spn" + control.id);
		if (span != null) {
			span.className = "Disabled";
		}
		document.all("td" + control.id).className = "Disabled";
		control.disabled = true;
	}
}

function turnArrayToString(obj) {
	var str = "";

	for (var i = 0; i < obj.length; i++) {
		if (i == 0) {
			str = "\"" + escape(obj[i]) + "\"";
		} else {
			str += ",\"" + escape(obj[i]) + "\"";
		}
	}
	return str;
}

function messageBox(title, buttons, width, height, buttonWidth, align, valign, objectType, args) {
	return window.showModalDialog(Application["ApplicationPath"] + "/Common/MessageBox.aspx?Title=" + title + "&Buttons=" + buttons + "&ButtonWidth=" + buttonWidth + "&Align=" + align + "&VAlign=" + valign + "&ObjectType=" + objectType, args, "center:yes;dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:no;");
}

function SQLMessageBox(title, buttons, width, height, SQL) {
	return window.showModalDialog(Application["ApplicationPath"] + "/Common/SQLMessageBox.aspx?Title=" + title + "&Buttons=" + buttons + "&SQL=" + SQL, null, "center:yes;dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:no;");
}

function writeLookUpRow(fieldName, IDFieldName, 
	caption, value, IDvalue,
	required, enabled, captionWidth, fieldWidth, 
	tab, objectType, fieldCustomCode,
	imgCustomCode) {
	
	var HTML = "";
	
	HTML += "<tr height=23>";
	
	HTML += writeLookUpControlAndCaption(fieldName, IDFieldName, caption, value, IDvalue, required, enabled, captionWidth, fieldWidth, tab, objectType, fieldCustomCode, imgCustomCode);
	
	HTML += "</tr>";
	
	return HTML;
}

function writeLookUpControlAndCaption(fieldName, IDFieldName, 
	caption, value, IDvalue,
	required, enabled, captionWidth, fieldWidth,
	tab, objectType, fieldCustomCode,
	imgCustomCode) {

	var HTML = "";
	
	HTML += writeControlCaption(fieldName, IDFieldName, caption, enabled, captionWidth, objectType);
	HTML += writeLookUpControl(fieldName, IDFieldName, caption, value, IDvalue,	required, enabled, fieldWidth, tab, fieldCustomCode, imgCustomCode);
	
	return HTML;
}

function writeLookUpControl(fieldName, IDFieldName, 
	caption, value, IDvalue,
	required, enabled, fieldWidth,
	tab, fieldCustomCode,
	imgCustomCode) {
	
	var HTML = "";
	
	HTML += "<td valign=top><input type=hidden id=" + IDFieldName + " name=" + IDFieldName + " value=\"" + IDvalue + "\" lastvalue=\"" + IDvalue + "\">";
    HTML += "<table border=0 cellspacing=0 cellpadding=0 width=100% ><tr><td><input type=text id=" + fieldName + " name=" + fieldName + " required=" + required + " title=\"" + formatHTML(caption) + "\" tab=td" + tab + " value=\"" + formatHTML(value) + "\" lastvalue=\"" + formatHTML(value) + "\" style=\"position:relative; width:" + fieldWidth + "\" checkDirty=\"false\" " + fieldCustomCode;
    if (!enabled) {
        HTML += " DISABLED";
    }
    HTML += "></td><td width=1><img id=img" + fieldName + " name=img" + fieldName + " src=\"" + Application["ApplicationPath"] + "/Image/Find.gif\" border=0 height=17 width=17 style=\"position:relative\" " + imgCustomCode + " onmouseover=\"if (!" + fieldName + ".disabled) this.style.cursor='hand';\"></td></tr></table></td>";
	
	return HTML;
}

function writeTextBox(fieldName, caption, value, required, enabled, captionWidth, textBoxWidth, tab, customcode, maxLength, checkDirty)
{
	var HTML = "";
	
	if (caption.length > 0) {
		HTML += writeControlCaption(fieldName, "", caption, enabled, captionWidth, "");
	}

	HTML += "<td width=" + textBoxWidth + "><input type=text id=" + fieldName + " name=" + fieldName + " required=" + required + " title=\"" + formatHTML(caption) + "\" tab=td" + tab + " " + customcode + " style=\"width:100%\" value=\"" + formatHTML(value) + "\" maxlength=" + maxLength;
    
	if (checkDirty)
		HTML += " onchange=\"dirty=true;\"";
		
	if (!enabled)
		HTML += " DISABLED";
          
	HTML += "></td>";
	return HTML;
} 

function writeDateTextBox(fieldName, caption, value, required, enabled, captionWidth, textBoxWidth, tab, customcode, checkDirty, useCalendar)
{
	var HTML = "";
	
	if (caption.length > 0) {
		HTML += writeControlCaption(fieldName, "", caption, enabled, captionWidth, "");
	}

	HTML += "<td width=" + textBoxWidth + "><input type=text id=" + fieldName + " name=" + fieldName + " required=" + required + " title=\"" + formatHTML(caption) + "\" tab=td" + tab + " " + customcode + " style=\"width:100%\" value=\"" + formatHTML(value) + "\" maxlength=50";

	if (customcode.Length > 0) {
		HTML += " " + customcode;
    } else {
		HTML += " onblur=\"date_onblur(this)\" onmousewheel=\"return date_onmousewheel(this)\"";
	}

	if (checkDirty)
		HTML += " onchange=\"dirty=true;\"";
		
	if (!enabled)
		HTML += " DISABLED";
          
	HTML += "></td><td width=1>";

    if (useCalendar) {
        HTML += "<img id=" + fieldName + " name=" + fieldName + " src=\"" + Application["ApplicationPath"] + "/Image/Calendar.bmp\" border=0 height=17 width=16 hspace=1 style=\"position:relative;\" onclick=\"return launchCalendarPopup(" + fieldName + ", this)\" onmouseover=\"if (!" + fieldName + ".disabled) this.style.cursor='hand';\">";
    } else {
        HTML += "&nbsp;";
    }
	
	HTML += "</td>";
	
	return HTML;
} 

function writeControlCaption(fieldName, IDFieldName, caption, enabled, captionWidth, objectType) {
	var HTML = "";

	HTML += "<td width=" + captionWidth + " id=td" + fieldName;
    if (!enabled) {
        HTML += " class=Disabled";
    }
    HTML += " onclick=\"setFocus(" + fieldName + ")\"><span";
    if (objectType.length > 0 && (Session[objectType + "Permissions"].indexOf("Read,") > -1)) {
        HTML += " id=spn" + fieldName + " objectType=\"" + objectType + "\" IDFieldName=\"" + IDFieldName + "\" fieldName=\"" + fieldName + "\"";
        HTML += " onclick=\"spanClick(this, " + fieldName + ")\" onmouseover=\"MouseHover(this)\" onmouseout=\"MouseOffHover(this)\"";
		if (!enabled) {
            HTML += " class=Disabled";
        } else {
			HTML += " class=Link";
        }
    }
    HTML += ">" + formatHTML(caption) + ":</span></td>";
	return HTML;
}

function writeHiddenTextBox(fieldName, value) {
	return "<input type=hidden id=" + fieldName + " name=" + fieldName + " value=\"" + value + "\">";
}

function hideLoading() {
	showLoadingCount--;
	if (showLoadingCount < 1) {
		hide(divLoading);
		hide(ifrmLoading);
		stopLoadingTick();
	}
}

function showLoading(text) {
	if (showLoadingCount < 1) {
		showLoadingCount = 1;
		text = (text == undefined || text == null || text == '' ? 'Loading' : text);
		document.getElementById("tdLoading").innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + text + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		show(divLoading);
		show(ifrmLoading);
		document.getElementById("imgLoading").src = Application["ApplicationPath"] + "/Image/spinner.gif";
		startLoadingTick(text);
	} else {
		showLoadingCount++;	
	}
}

function addEventHandler(element, eventName, eventHandler) {
    if (document.addEventListener) { //Firefox
		element.addEventListener(eventName, eventHandler, false);
    } else if (document.attachEvent) { //IE
        element.attachEvent("on" + eventName, eventHandler);
    }
}

function gridLoading() {
	return "<div class=GridLoading align=center><table border=0 cellspacing=0 cellpadding=0 height=100%><tr height=60%><td colspan=3>&nbsp;</td></tr><tr><td id=tdLoading class=Loading colspan=3>Loading</td></tr><tr height=1><td width=50%>&nbsp;</td><td><img id=\"imgLoading\" src=\"" + Application["ApplicationPath"] + "/Image/spinner.gif\"></td><td width=50%>&nbsp;</td></tr><tr height=40%><td colspan=3>&nbsp;</td></tr></table></div>";
}

function viewLoading() {
	return "<div class=ViewLoading align=center><table border=0 cellspacing=0 cellpadding=0 height=100%><tr height=60%><td colspan=3>&nbsp;</td></tr><tr><td id=tdLoading class=Loading colspan=3>Loading</td></tr><tr height=1><td width=50%>&nbsp;</td><td><img id=\"imgLoading\" src=\"" + Application["ApplicationPath"] + "/Image/spinner.gif\"></td><td width=50%>&nbsp;</td></tr><tr height=40%><td colspan=3>&nbsp;</td></tr></table></div>";
}

function Description(objectType) {
	// internal
	this.objectType = objectType;
	this.whereConditions = new Array();
	this.whichConditions = new Array();
	this.freeTextConditions = new Array();
	
	// properties
	this.CountDescription =	"";
	this.TotalCount = null;
	
	// methods
	this.getMessage = function() {
		var message = "Showing ";

		if (this.TotalCount == null) {
			message += " first " + this.CountDescription;
		} else {
			message += " all " + this.TotalCount;
		}
		message += " " + this.objectType;
		
		if (this.whichConditions.length > 0) {
			message += " which " + this.whichConditionsMessage();
		}
		if (this.whereConditions.length > 0) {
			message += " where " + this.whereConditionsMessage();
		}
		if (this.freeTextConditions.length > 0) {
			message +=  this.freeTextConditionsMessage();
		}
		
		return message;
		//return "Showing " + ((this.TotalCount == null) ? " first " + this.CountDescription : " all " + this.TotalCount) + " " + this.objectType + ((this.whereConditions.length > 0) ? " where " + this.whereConditionsMessage() : "");
	}
	
	this.whereConditionsMessage = function() {
		var message = this.whereConditions.pop();
		
		if (this.whereConditions.length > 0) {
			return this.whereConditions.join(", ") + ", and " + message;
		} else {
			return message;
		}
	}
	
	this.whichConditionsMessage = function() {
		var message = this.whichConditions.pop();
		
		if (this.whichConditions.length > 0) {
			if (this.whereConditions.length > 0) {
				message = this.whichConditions.join(", ") + ", " + message;
			} else {
				message = this.whichConditions.join(", ") + ", and " + message;
			}
		} else {
			message = message;
		}
		
		if (this.whereConditions.length > 0) {
			message += ", and";
		}
		
		return message;
	}
	
	this.freeTextConditionsMessage = function() {
		var message = "";		
		for(var i=0; i<this.freeTextConditions.length; i++){		
			message += this.freeTextConditions[i];
		}	
		return message;
	//	var message = this.freeTextConditions.pop();	
	//	if (this.freeTextConditions.length > 0) {
	//		return this.whereConditions.join(", ") + message;
	//	} else {
	//		return message;
	//	}	
	}
	
	this.addWhereCondition = function(whereCondition) {
		this.whereConditions.push(whereCondition);
	}
	
	this.addWhichCondition = function(whichCondition) {
		this.whichConditions.push(whichCondition);
	}
		
	this.addFreeTextCondition = function(freeCondition) {
		this.freeTextConditions.push(freeCondition);
	}
}

function hasPermission(securityContext, permission) {
	return Session[securityContext + "Permissions"].indexOf(permission + ",") > -1;
}
