<!-- ----------------------------------------------------------------------------------
//
// Description:	Contains functions used to build a SQL statement that will call a stored
//				procedure.
//
//--------------------------------------------------------------------------------- -->
function SQL() {
	var stmt;
}

//-------------------------------------------------------------------------------------
// Function:	BuildSQL
//
// Synopsis:	Builds a SQL statment, to execute a stored procedure.
//
// Arguments:	oSQL			SQL statment being build.
//				sParameter		Parameter name.
//				sValue			Value to assign to parameter.
//				bNumber			Indicates if value is numeric.		
//				bIgnoreQuotes	Indicates if formatting of quotes should be ignored.	
//
// Returns:		none
//
// Notes:		none
//
//-------------------------------------------------------------------------------------
function BuildSQL(oSQL, sParameter, sValue, bNumber, bIgnoreQuotes) {

	//Trim leading blanks.
	if (bNumber != true) {
		sChar = sValue.charAt(0);
		while (sChar == " ") {
			sValue = sValue.substr(1);
			sChar = sValue.charAt(0);
		}
	}

	//Check for empty values, and make them NULL values.
	if (bNumber != true) {
		if (sValue.length == 0 || sValue == "") {sValue = "NULL";}
	}
	
	if (bNumber == true) {
		// Replace format characters.
		if (isNaN(sValue) == true) {
			sValue += "";
			sValue = sValue.replace(/$/g, "");
			sValue = sValue.replace(/,/g, "");
		}
				
		sValue = parseFloat(sValue);
		if (sValue < 0 || isNaN(sValue)) {sValue = "NULL";}
	}
	
	//If value is not numeric, wrap it with single quotes.
	if (bNumber != true && sValue != "NULL") {
		var sSingleQuote = "'";
		var sDoubleQuote = '"';
		var sNewValue;
		var p;
	
		if (bIgnoreQuotes == true) {
			//Just enclose string in single quotes.
			sValue = sSingleQuote + sValue + sSingleQuote;
		}	
		else {
			//Replace all single quotes with a set of three single quotes.
			p = sValue.indexOf(sSingleQuote);
			
			if (p) {
				sNewValue = "";
				while (p >= 0) {
					sNewValue += sValue.substring(0,p) + "''";
					sValue = sValue.substring(p+1);
					p = sValue.indexOf(sSingleQuote);
				}
		
				if (sValue.length) {
					sNewValue += sValue;
					sValue = sNewValue;
				}	
				else {
					sValue = sNewValue;
				}
			}	
		
			//Enclose string in single quotes.
			sValue = sSingleQuote + sValue + sSingleQuote;
		}	
	}
	
	//Prefix with a blank for esthetic reasons.
	oSQL.stmt += " " + sParameter + "=" + sValue + ", ";
}


//-------------------------------------------------------------------------------------
// Function:	BreakItUp
//
// Synopsis:	Breaks the HTML form variables into multiple form variables before
//				submitting the form.
//
// Arguments:	dbDoc	
//				sSQL
//
// Returns:		none
//
// Notes:		102,399 byte limit for each form variable (IIS 5.0 and before).
//
//-------------------------------------------------------------------------------------
function BreakItUp(dbDoc, sSQL) {

	//Set the limit for field size.
	var iFormLimit = 102399;

	//If the length of the SQL is greater than the limit, break it into multiple fields.
	if (sSQL.length > iFormLimit) {
		while (sSQL.length > 0)	{
			//var oInput = dbDoc.createElement("INPUT");
			//oInput.type = "TEXT";
			//oInput.name = "SQL";
			//oInput.value = sSQL.substr(0, iFormLimit);
			//dbDoc.all.frmDB.appendChild(oInput);
			//sSQL = sSQL.substr(iFormLimit);

			var oLabel = dbDoc.createElement("TEXTAREA");
			oLabel.name = "SQL";
			oLabel.value = sSQL.substr(0, iFormLimit);
			dbDoc.all.frmDB.appendChild(oLabel);
			sSQL = sSQL.substr(iFormLimit);
			
		}
	}
	else {
		dbDoc.all.SQL.value = sSQL;
	}
}