function changeArea(){
	var BizCoun = document.AForm.BizCountry;
	var BizArea = document.AForm.BizArea;
	var bstr, trfl = 0, len = 0, i, j, stateCount = 0;

	document.getElementById("txtBarea").value = "";
	for (i = BizArea.options.length - 1; i >= 0; i -= 1) {
		BizArea.options[i] = null;
	}
	for (j = 0, len = BizCoun.options.length - 1; j < len; j += 1) {
		if (BizCoun.options[j].selected) {
			stateCount +=1;
			if (stateCount > 1) {
				for (i = BizArea.options.length - 1; i >= 0; i -= 1) {
					BizArea.options[i] = null;
				}
				trfl = 0;
				break;
			}
			switch (BizCoun.options[j].value) {
				case 'California':
				case 'New York':
				case 'Texas':
					BizArea.options[BizArea.options.length] = new Option("---All Areas Below---", "All");
					BizArea.options[BizArea.options.length] = new Option(BizCoun.options[j].value, BizCoun.options[j].value);
					changeAreaNew();
					trfl = 1;
					break;
				default:
					break;
			}//end of switch
		}//end of if 
	}//end of for
	if (trfl !== 1) {
		BizArea.options[BizArea.options.length] = new Option("----", "All");
	}
	BizArea.options[0].selected = true;
	storeArea();
} //end of function

function changeAreaNew(){
	var BizCoun = document.AForm.BizCountry;
	var BizArea = document.AForm.BizArea;
	var bstr, trfl, len = 0;
	
	document.getElementById("txtBarea").value = "";
	for (var i = BizArea.options.length - 1; i >= 0; i -= 1) {
		BizArea.options[i] = null;
	}
	bstr = "";
	
	for (var j = 0, len = BizCoun.options.length - 1; j < len; j += 1) {
		BizArea.options[0] = new Option("---All Areas Below---", "All")
		if (BizCoun.options[j].selected) {
				switch (BizCoun.options[j].value) {
					case 'California':
						BizArea.options[BizArea.options.length] = new Option("Northern California - All Northern California", "California");
						BizArea.options[BizArea.options.length] = new Option("Northern California - Bay Area", "California");
						BizArea.options[BizArea.options.length] = new Option("Southern California - All Southern California", "California");
						BizArea.options[BizArea.options.length] = new Option("Southern California - Los Angeles Area", "California");
						BizArea.options[BizArea.options.length] = new Option("Southern California - San Deigo Area", "California");
						BizArea.options[0].selected = true;
						break;						
					case 'New York':						
						//BizArea.options[BizArea.options.length] = new Option("All Areas in New York","All")
						BizArea.options[BizArea.options.length] = new Option("All of New York", "New York");
						BizArea.options[BizArea.options.length] = new Option("New York City Area", "New York");
						BizArea.options[BizArea.options.length] = new Option("Upstate New York", "New York");
						BizArea.options[BizArea.options.length] = new Option("Other New York", "New York");
						BizArea.options[0].selected = true;
						break;
					case 'Texas':
						//BizArea.options[BizArea.options.length] = new Option("All Areas in Texas","All")		
						BizArea.options[BizArea.options.length] = new Option("Austin", "Texas");
						BizArea.options[BizArea.options.length] = new Option("Central Texas", "Texas");
						BizArea.options[BizArea.options.length] = new Option("Dallas/Fort Worth", "Texas");
						BizArea.options[BizArea.options.length] = new Option("East Texas", "Texas");
						BizArea.options[BizArea.options.length] = new Option("Houston", "Texas");
						BizArea.options[BizArea.options.length] = new Option("San Antonio", "Texas");
						BizArea.options[BizArea.options.length] = new Option("West Texas", "Texas");
						BizArea.options[0].selected = true;
						break;
					default:
						var a = BizCoun.options[j].text;
						if (a.lastIndexOf("---") === 0) {
							BizArea.options[BizArea.options.length] = new Option(a.substring(3, a.length), "All");
						}
						else {
							if (BizCoun.options[j].text !== 'United States' && BizCoun.options[j].text !== 'Canada') {
								BizArea.options[BizArea.options.length] = new Option(BizCoun.options[j].text, "ALL");
							}
						}
						break;
				}//End Of Switch
		} //end of if			
	} //end of for	
} //end of function

function storeArea(){
	var BizArea = document.AForm.BizArea;
	document.getElementById("txtBarea").value = "";
	var cstr = "", dstr = "", trfl, tmpvar = '', tmpvar1 = '', len = 0;
	for (var j = 0, len = BizArea.options.length - 1; j < len; j += 1) {
		tmpvar = '';
		tmpvar1 = '';
		if (BizArea.options[j].selected) {
			switch (BizArea.options[j].value) {
				case 'All':
					trfl = 0;
					if (!(cstr)) {
						cstr = 'All';
					}
					if (!(dstr)) {
						dstr = 'All';
					}
					break;
				case '0':
					break;
				default:
					if (!(cstr)) {
						cstr = BizArea.options[j].text;
					}
					else {
						cstr = cstr + "," + BizArea.options[j].text;
					}
					if (!(dstr)) {
						dstr = BizArea.options[j].text;
					}
					else {
						dstr = dstr + "," + BizArea.options[j].text;
					}
					break;
			}
		}
		if (trfl === 0) {
			break;
		}
	}
	document.getElementById("txtBarea").value = cstr;
}

function getSearchstring(){
	var sCountry, sState, sArea, sInd, sIndseg;
	var tmpstr, tmpstr1, tmpstr2, tmpstr3, tmpstr4
	
	var Ind = document.AForm.Industry;
	var BizCoun = document.AForm.BizCountry;
	var BizArea = document.AForm.BizArea;
	
	//start of Industry and industry for display 
	sInd = '';
	sIndseg = '';
	for (var j = 0; j < Ind.options.length; j++) {
		tmpstr4 = Ind.options[j].value;
		tmpstr1 = '';
		tmpstr2 = '';
		
		if (tmpstr4.substring(0, 1) != 's') {
			//alert(tmpstr4)
			if (Ind.options[j].selected == 1) {
				if (tmpstr1 == '') {
					//tmpstr1 = "Business for sale>>"  +Ind.options[j].text+ ">>" 
					tmpstr1 = Ind.options[j].text + " >> "
					//tmpstr1 = Ind.options[j].text
				}
				else {
					//tmpstr1 = tmpstr1 + "Businesses for sale >> "  +Ind.options[j].text+ " >> " 
					tmpstr1 = tmpstr1 + "," + Ind.options[j].text + " >> "
				}
			}
			else {
				for (var i = j + 1; i < Ind.options.length; i++) {
					tmpstr4 = Ind.options[i].value
					if (tmpstr4.substring(0, 1) == 's') {
						if (Ind.options[i].selected == 1) {
							tmpstr2 = Ind.options[i].text
							if (tmpstr1 == '') {
							
								//alert(tmpstr1)
								tmpstr1 = Ind.options[j].text + " >> " + tmpstr2.substring(6, tmpstr2.length)
							}
							else {
								tmpstr1 = tmpstr1 + ", " + tmpstr2.substring(6, tmpstr2.length)
							}
						}// end of ==1
					} // end of ==s	
					else {
						break;
					}
					
				}//end of for	
			}//end of else 
		}
		if (tmpstr1 != '') {
			if (sInd == '') { //sInd = "<a href=finalsearch.asp> Business For Sale >></a> " + tmpstr1 
				sInd = tmpstr1
			}
			else {//sInd  = sInd  + "  Businesses For Sale " +   tmpstr1
				sInd = sInd + "|" + tmpstr1
			}
		}
		
		
	}//end of for 
	//alert(tmpstr2)	
	document.getElementById("txtindlist").value = sInd
	//alert(sInd)
	//end of industry and industrysegment
	
	//Start of country and states 
	document.getElementById("txtCountry").value = ''
	sCountry = ''
	sState = ''
	for (var j = 0; j < BizCoun.options.length; j++) {
		tmpstr = BizCoun.options[j].value
		tmpstr1 = ''
		tmpstr2 = ''
		
		if (tmpstr.substring(0, 4) == 'CNT:') {
			if (BizCoun.options[j].selected == 1) {
				if (tmpstr1 == '') {
					tmpstr1 = BizCoun.options[j].text + " >> ";
					//tmpstr1 = BizCoun.options[j].text
				}
				else {
					tmpstr1 = tmpstr1 + BizCoun.options[j].text + " >> ";
				}
			}
			else {
				for (var i = j + 1; i < BizCoun.options.length; i++) {
					tmpstr = BizCoun.options[i].value;
					if (tmpstr.substring(0, 4) != 'CNT:') {
						if (BizCoun.options[i].selected == 1) {
							tmpstr2 = BizCoun.options[i].text;
							if (tmpstr1 == '') {
								tmpstr1 = BizCoun.options[j].text + " >> " + tmpstr2.substring(3, tmpstr2.length);
							}
							else {
								tmpstr1 = tmpstr1 + ', ' + tmpstr2.substring(3, tmpstr2.length);
							}							
						}
					}
					else {
						break;
					}
					
				}//end for 
			}//end if 
		} //end of if 
		if (tmpstr1 != '') {
			if (sCountry == '') {
				sCountry = tmpstr1
			}
			else {
				sCountry = sCountry + "<br />" + tmpstr1
			}
		}
		
		if (tmpstr2 != '') {
			if (sState == '') {
				sState = tmpstr2
			}
			else {
				sState = sState + ',' + tmpstr2
			}
		}
	}//end of for 
	document.getElementById("txtCountry").value = sCountry
	//end of country and states segemnts
}// end of getsearchstring
function changeRegionStProv(){
	var BizCoun = document.AForm.BizCountry;
	var BizArea = document.AForm.BizArea;
	var cstr = '';
	var cnstr;
	var TFstr = 0;
	for (var j = 0; j < BizCoun.options.length; j++) {
		if (BizCoun.options[j].selected) {
			for (var i = 0; i < BizArea.options.length; i++) {
				if (BizArea.options[i].selected) {
					if (BizArea.options[i].value == 'All') {
						TFstr = 0;
						break;
					}
					if (BizCoun.options[j].value === BizArea.options[i].value) {
						TFstr = 1;
						break;
					}
					else {
						TFstr = 0;
					}
				}
			}
			if (TFstr === 0) {
				if (!(cstr)) {
					cstr = BizCoun.options[j].value;
					cnstr = BizCoun.options[j].text;
				}
				else {
					cstr = cstr + "," + BizCoun.options[j].value;
					cnstr = cnstr + "," + BizCoun.options[j].text;
				}
			}
		}
	}
	if (cstr == "") {
		cstr = "null";
	}
	document.getElementById("txtloclist").value = cstr;
}


var requiredFields = new Array("txtKeyword");
var fieldNames = new Array("Keyword or Listing ID");
var FormOK = KeywordOK = true;

function checkFields(input){
    //alert("amin")
    if (!(checkRequiredFieldsBC(input))) {
        return false;
    }
    if (!(KeywordOK)) {
        alert("The Keyword field requires valid input");
        return false;
    }
    else {
        return true;
    }
}

function checkFields2(input){
    //alert("bc"); 
    //return false; 
    if ((AForm2.txtKeywordBasic.value == "") || (AForm2.txtKeywordBasic.value == " ")) {
        alert("You must enter a Keyword or Listing ID");
        return false;
    }
    ////else { return true; }	
    
    else {
        //alert("go to results.asp ..."); 
        //totalListings = document.AForm.txtTotalListings.value;
        //thisPgNum = document.AForm.txtThisPgNum.value;
        //nxPgNum = (thisPgNum-0) + 1;
        //noListingsDisplayed = (thisPgNum-0) * 15;
        //noListingsRemain = (totalListings-0) - noListingsDisplayed;
        
        //keywordString = document.AForm.txtKeywordString.value;
        //industryId = document.AForm.txtIndustryId.value;
        //industrySegmentId = document.AForm.txtIndustrySegmentId.value;
        //country = document.AForm.txtCountry.value;
        //region = document.AForm.txtRegion.value;
        
        //price = document.AForm.txtPrice.value;
        //cashFlow = document.AForm.txtCashFlow.value;
        //revenue = document.AForm.txtRevenue.value;
        //ascDesc = document.AForm.txtAscDesc.value;
        
        //window.location.href="results.asp?keywordStr=" + keywordString + "&indId=" + industryId + "&indSegId=" + industrySegmentId + "&country=" + country + "&region=" + region + "&p=" + price + "&c=" + cashFlow + "&r=" + revenue + "&o=" + ascDesc + "&pg=" + nxPgNum;
        
        ////keywordString = document.AForm.txtKeywordBasic.value;
        ////nxPgNum = 1;
        
        ////window.location.href="results.asp?keywordStr=" + keywordString + "&pg=" + nxPgNum;
        ////server will handle paging
        return true;
    }
    
}

function checkRequiredFieldsBC(input){

    var fieldCheck = true;
    var fieldsNeeded = "\nA value must be entered in the field:\n\n\t";
    
    for (var fieldNum = 0; fieldNum < requiredFields.length; fieldNum++) {
        if ((input.elements[requiredFields[fieldNum]].value == "") ||
        (input.elements[requiredFields[fieldNum]].value == " ")) {
        
            fieldsNeeded += fieldNames[fieldNum] + "\n\t";
            fieldCheck = false;
        }
    }
    
    if (fieldCheck == true) {
        FormOK = true
        return true;
    }
    
    else {
        FormOK = false
        alert(fieldsNeeded);
        return false;
    }
}

function checkIndDropDown(){

    Ind = (document.AForm.Industry.selectedIndex) - 0;
    if (Ind == 0) {
        if (document.AForm.Industry.value == "null") {
            alert("The Industry field requires valid input");
            return false;
        }
    }
    
}

//function to check min and max selected list 
function sanchklistcount(cs){
	var lCount, j, len, region, BArea;
	if (cs === 1) {
		Ind = document.AForm.Industry;
		lcount = 0;
		for (j = 0, len = Ind.options.length; j < len; j += 1) {
			if (Ind.options[j].selected) {
				lcount += 1;
			}
		}
		if (lcount > 10) {
			alert("You can select a maximum of 10 Industries.  Please change your selection.");
			return false;
		}
		else {
			if (!lcount) {
				alert("The Industry field requires valid input");
				return false;
			}
		}
	}
	if (cs === 2) //Business location validation
	{
		region = document.AForm.BizCountry;
		lcount = 0;
		for (j = 0, len = region.options.length; j < len; j += 1) {
			if (region.options[j].selected) {
				lcount += 1;
			}
		}
		if (lcount > 10) {
			alert("You can select a maximum of 10 Business Locations.  Please change your selection.");
			return false;
		}
		else {
			if (!lcount) {
				alert("Business location field requires valid input");
				return false;
			}
		}		
	}
	if (cs === 3) {
		BArea = document.AForm.BizArea;
		lcount = 0;
		for (j = 0, len = BArea.options.length; j < len; j += 1) {
			if (BArea.options[j].selected) {
				lcount += 1;
			}
		}
		if (lcount > 3) {
			//alert("\nYou can select Maximum 3 Business Area\n\n\tKeyword");
			alert("You can select a maximum of 3 business areas.  Please change your selection.");
			return false;
		}
	}
}

////function submitMe(input) {
//alert("submit")
////	if (document.AForm.txtKeyword.value == "" && document.AForm.txtindlist.value=="")
////	 { 	alert("A value must be entered in the Keyword field:"); 		return false; }
////	else {		
////	return true; 
////	}
////}

//1/18/07 - call when page loads
function submitMe(input){
    if (input.elements["txtKeyword"].value == "" && document.AForm.Industry.selectedIndex == 0) {
        alert("\nA value must be entered in the Keyword field:\n\n\tKeyword");
        return false;
    }
    else {
        return true;
    }
}

//function to check which form to submit 
function sanclick(clk){
	changeRegionStProv();
	getSearchstring();
	storeArea();
	
	if (clk === 1) {
		return sanchklistcount(1) && sanchklistcount(2) && sanchklistcount(3);
	}
	if (clk === 2) {
		if (document.getElementById("txtKeyword").value === "") {
			alert("\nA value must be entered in the Keyword field:\n\n\tKeyword");
			return false;
		}
		else {
			return true;
		}
	}
}
