
var debug
debug = false
//debug = true

var criteriaStatement = ""
var orderStatement = ""




function buildSearchString(frm) {

	if (debug) alert ("building the string")

	var criteriaChar = ""		// set the start character to be an empty string
	var orderChar = ""		// set the start character to be an empty string

	// iterate through all elements in the form and add their values to the criteria string
	for (elementsIndex = 0; elementsIndex < frm.elements.length; elementsIndex++) {
		myObject = frm.elements[elementsIndex]
		
		// if the next element has value
		if (myObject.value != "") {

			// if the next element is the correct type, add it to the criteria string
			switch (myObject.type) {
			
				case "text":

					myObject.value = myObject.value.replace("'","''")

					matchObjectName = myObject.name + "_match"
					wildCard = ""
					//if (frm.elements[matchObjectName]) {
					//	matchObjectValue = frm.elements[matchObjectName].checked

						//if (matchObjectValue == false)
						//{
							wildCard = "%"
						//}
					//}
					if (myObject.value == "null")
					{
						criteriaStatement += criteriaChar + "@" + myObject.name + "=" + myObject.value
						criteriaChar = ", " // set the start character to be close quote and apostraphy (,)
					}
					else
					{
						if (myObject.name == "keywords")
						{
							keyString=""
							if (myObject.value != "") {
								keyString = myObject.value
								keyWords = keyString.split(",")
								for (i=0; i<keyWords.length; i++)
								{
								  keyWords[i] = keyWords[i].replace( /^\s+/g, "" );
  								  keyWords[i].replace( /\s+$/g, "" );
								}
								keySearch = "%" + keyWords.join(" or ") + "%"

						//		lastChar = keySearch.substring(keySearch.length - 1);
						//		if (lastChar == "|"){
						//			keySearch = keySearch.substring(keySearch, keySearch.substring(keySearch.length - 1))
						//		}
								criteriaStatement += criteriaChar + "@" + myObject.name + "='" + keySearch
								criteriaChar = "', " // set the start character to be close quote and apostraphy (',)
							}
							break
						}
						else
						{
						criteriaStatement += criteriaChar + "@" + myObject.name + "='" + myObject.value + wildCard
						criteriaChar = "', " // set the start character to be close quote and apostraphy (',)
						}
					}
					break
					
				case "select-one":
					for (optionsIndex = 0; optionsIndex < myObject.length; optionsIndex++)
					{
						if (myObject.options[optionsIndex].selected == true)
						{
							if (myObject.name.indexOf("orderBy") != -1)
							{
								orderStatement += orderChar + "@" + myObject.name + "='" + myObject.options[optionsIndex].value
								orderChar = "', " // set the start character to be close quote and apostraphy (',)
							}
							else
							{
								criteriaStatement += criteriaChar + "@" + myObject.name + "='" + myObject.options[optionsIndex].value
								criteriaChar = "', " // set the start character to be close quote and apostraphy (',)
							}
						}
					}
					break

				case "select-multiple":
					if (myObject.name == "selectedColumns") {
						// do nothing
					} else {
						criteriaStatement += criteriaChar + "@" + myObject.name + "='"
						criteriaChar = "" // set the start character to be an empty string
						for (optionsIndex = 0; optionsIndex < myObject.length; optionsIndex++) {
							if (myObject.options[optionsIndex].selected == true) {
								criteriaStatement += criteriaChar + myObject.options[optionsIndex].value
								criteriaChar = "|" // set the start character to be a pipe (|)
							}
						}
						criteriaStatement += criteriaChar // add the final pipe
						criteriaChar = "', " // set the start character to be close quote and apostraphy (',)
					}
					break
									
				case "radio":
					if ((myObject.checked == true) && (myObject.value != "")) {				
						criteriaStatement += criteriaChar + "@" + myObject.name + "='" + myObject.value
						criteriaChar = "', " // set the start character to be close quote and apostraphy (',)
					}					
					break
			  
			  case "checkbox":
					if ((myObject.checked == true) && (myObject.value != "")) {				
						criteriaStatement += criteriaChar + "@" + myObject.name + "='" + myObject.value
						criteriaChar = "', " // set the start character to be close quote and apostraphy (',)
					}					
					break
					
				case "textarea":
					keyString=""
					if (myObject.value != "") {
						keyString = myObject.value
						keyWords = keyString.split(",")
						keySearch = "%" + keyWords.join("%|%") + "%|"
						
						criteriaStatement += criteriaChar + "@" + myObject.name + "='" + keySearch
						criteriaChar = "', " // set the start character to be close quote and apostraphy (',)
					}
					break
					
				default:
					// do nothing
			}
		}
	}
	
	// Add the final quote character (')
	if (criteriaStatement != "")
		if (criteriaStatement.substring(criteriaStatement.length - 4,criteriaStatement.length)=="null")
		{
		}
		else
		{
			criteriaStatement += "'"
		}
	else criteriaStatement = "no search criteria"
	if (orderStatement != "") orderStatement += "'"	
		else orderStatment = "no sorting"
	
}

function isLetter(c)
{  
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit(c)
{  
	return ((c >= "0") && (c <= "9"))
}

function isWhite(c)
{	
	return ((c == ' ') || (c == '\t') || (c == '\n') || (c== '\r'))
}


// Temporarily commented out to test the other  isAlphanumeric function 

function isAlphanumeric(s)

{ 
	var i;

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
		
        // Check that current character is number or letter.
        var c = s.charAt(i);
		if (! isWhite(c))
		{
			if (! (	isLetter(c) ||
					isDigit(c)
						||	c == "~"
						||	c == "`"
						||	c == "!"
						||	c == "@"
						||	c == "#"
						||	c == "$"
						||	c == "%"
						||	c == "^"
						||	c == "&"
						||	c == "*"
						||	c == "("
						||	c == ")"
						||	c == "-"
						||	c == "_"
						||	c == "+"
						||	c == "="
						||	c == "{"
						||	c == "}"
						||	c == "["
						||	c == "]"
						||	c == "\\"
						||	c == "|"
						||	c == ":"
						||	c == ";"
						||	c == "\""
						||	c == "<"
						||	c == ">"
						||	c == ","
						||	c == "."
						||	c == "?"
						||	c == "/"
						||	c == "'"

			))
			return false;
		}
    }

    // All characters are numbers or letters.
    return true;
}





// Validate Search Information

function validate(frm)
{

	var msg="";


	if (frm.extended_search.value == "Y")
	{

//	if (((frm.con_zip.value.length > 0) && (isNaN(frm.con_zip.value)))
//		|| ((frm.con_zip_suffix.value.length > 0) && (isNaN(frm.con_zip_suffix.value))))
//		msg += " * Invalid Contact Zip Code\n";

//	if (
//			(
//				(frm.con_day_phone.value.length > 0)
//			)
//			&&
//			(
//				isNaN(frm.con_day_phone.value)
//			)
//		)
//		msg += " * Invalid Contact Phone Number\n";



//	if (
//			(
//				(frm.con_cell_phone.value.length > 0)
//			)
//			&&
//			(
//				isNaN(frm.con_cell_phone.value)
//			)
//		)
//		msg += " * Invalid Contact Cell Phone Number\n";


//	if ((frm.con_last_name.value.length > 0) && (isAlphanumeric(frm.con_last_name.value) == false))
//		msg += " * Invalid Contact Last Name Value\n";

//	if ((frm.con_first_name.value.length > 0) && (isAlphanumeric(frm.con_first_name.value) == false))
//		msg += " * Invalid Contact First Name Value\n";

//	if ((frm.con_middle_name.value.length > 0) && (isAlphanumeric(frm.con_middle_name.value) == false))
//		msg += " * Invalid Contact Middle Name Value\n";

//	if ((frm.con_address1.value.length > 0) && (isAlphanumeric(frm.con_address1.value) == false))
//		msg += " * Invalid Contact Address1 Value\n";

//	if ((frm.con_address2.value.length > 0) && (isAlphanumeric(frm.con_address2.value) == false))
//		msg += " * Invalid Contact Address2 Value\n";

//	if ((frm.con_city.value.length > 0) && (isAlphanumeric(frm.con_city.value) == false))
//		msg += " * Invalid Contact City Value\n";




	if ((frm.business_name.value.length > 0) && (isAlphanumeric(frm.business_name.value) == false))
		msg += " * Invalid Business Name Value\n";

//	if ((frm.address1.value.length > 0) && (isAlphanumeric(frm.address1.value) == false))
//		msg += " * Invalid Address1 Name Value\n";
	
//	if ((frm.address2.value.length > 0) && (isAlphanumeric(frm.address1.value) == false))
//		msg += " * Invalid Address1 Name Value\n";
		

	if ((frm.city.value.length > 0) && (isAlphanumeric(frm.city.value) == false))
		msg += " * Invalid City Value\n";

	if (((frm.zipcode.value.length > 0) && (isNaN(frm.zipcode.value)))
		|| ((frm.zip_suffix.value.length > 0) && (isNaN(frm.zip_suffix.value))))
		msg += " * Invalid Zip Code\n";




	if ((frm.DUNS.value.length > 0) && (isAlphanumeric(frm.DUNS.value) == false))
		msg += " * Invalid DUNS Value\n";

	if ((frm.NAICS.value.length > 0) && (isAlphanumeric(frm.NAICS.value) == false))
		msg += " * Invalid NAICS Value\n";

//	if ((frm.pronet_id.value.length > 0) && (isAlphanumeric(frm.pronet_id.value) == false))
//		msg += " * Invalid Pronet Value\n";

	if ((frm.cage_code.value.length > 0) && (isAlphanumeric(frm.cage_code.value) == false))
		msg += " * Invalid Cage Code Value\n";

	if ((frm.keywords.value.length > 0) && (isAlphanumeric(frm.keywords.value) == false))
		msg += " * Invalid Keywords Value\n";

	}
	else
	{
		

		if ((frm.business_name.value.length > 0) && (isAlphanumeric(frm.business_name.value) == false)) 
			msg += " * Invalid Business Name Value\n";

		if ((frm.keywords.value.length > 0) && (isAlphanumeric(frm.keywords.value) == false))
		msg += " * Invalid Keywords Value\n";

	}



	if (msg== "")
	{
	//	frm.submit();
	}

	else
	{
		alert("The following field(s) have errors:\n" + msg);
		return false;
	}







	if (debug) alert ("starting validation" + criteriaStatement)



	buildSearchString(frm)
	
	if (debug) alert ("Search String Built" + criteriaStatement)
	frm.criteriaStatement.value = criteriaStatement
	frm.orderStatement.value = orderStatement
	frm.action = "results.asp"
	frm.pageNumber.value="1"
	frm.submit()

	criteriaStatement = "";

	if (debug) alert ("finished validation")

	return true;
	
}