
function validateintegers(Val)
{
	var alp ="1234567890";	
	var ret=true;
	for (var i=0;i<Val.value.length;i++)
	{
		temp=Val.value.substring(i,i+1);
		//alert(temp);
		//alert(alp.indexOf(temp));
		if (alp.indexOf(temp)==-1)
		{
			ret=false;
		}
	}
	return ret;
	// closing the for loop
	
}
	

var locationArray = new Array();
var zipsArray = new Array();
var zonesArray = new Array();
var SDArray = new Array();
var styles=new Array();
var regionArray = new Array();
var rent_types=new Array();

//locationArray = theString.split(",");
//zipsArray =theStringZips.split(",");
//zonesArray =theStringZones.split(",");
//SDArray=theStringSD.split(",");
//styles=theStringStyle.split(",");
//rent_types=theStringRentType.split(",");
// Build Region Select Box
function buildTestSelect(f_name,sel_name_from,sel_name_to) {
	var obj=eval('document.'+f_name+'.'+sel_name_from);
	var arraystr = new Array();
	if (sel_name_to=='all_towns'){
		arraystr=locationArray;
	}
	if (sel_name_to=='all_zips'){
		arraystr=zipsArray;
	}
	if (sel_name_to=='all_zones'){
		arraystr=zonesArray;
	}
	if (sel_name_to=='all_sd'){
		arraystr=SDArray;
	}
	if (sel_name_to=='all_styles'){
		arraystr=styles;
	}
	if (sel_name_to=='all_rent_types'){
		arraystr=rent_types;
	}
	
	var counter = 0;
	
	for (i=0;i <arraystr.length;i=i+2) {		
		var aString = arraystr[i];
		var aString2 = arraystr[i+1];
	 	if ( aString.search("==") != -1 ) {
			//alert(aString + ' , ' + aString2);
			//alert(sel_name_from);
			var newopt = new Option( aString2, escape( aString.substr(2) ) );
			obj.options[counter] = newopt;
			counter++;
	 	}
	}
	obj.selectedIndex = 0;	
	refreshLocations(f_name,sel_name_from,sel_name_to);
}

// Build Region Select Box
function refreshLocations(f_name,sel_name_from,sel_name_to) {
	var obj_from=eval('document.'+f_name+'.'+sel_name_from);
	var obj_to=eval('document.'+f_name+'.'+sel_name_to);
	var counter = 0;
	var selectedRegionID = obj_from.options[obj_from.selectedIndex].value;
	var theCompare = "==" + selectedRegionID;
	 //alert(theCompare);
	var foundRegion = true;
	obj_to.options.length = 0
	
	var arraystr = new Array();
	if (sel_name_to=='all_towns')
	{	
		arraystr=locationArray;		
	}
	if (sel_name_to=='all_zips'){
		arraystr=zipsArray;
	}
	if (sel_name_to=='all_zones'){
		arraystr=zonesArray;
	}
	if (sel_name_to=='all_sd'){
		arraystr=SDArray;
	}
	if (sel_name_to=='all_styles'){
		arraystr=styles;
	}
	if (sel_name_to=='all_rent_types'){
		arraystr=rent_types;
	}
	for (i=0;i <arraystr.length;i=i+2)
	{
		var aString = arraystr[i];
		var aString2 = arraystr[i+1];	

	 	// alert(aString.search(theCompare));

	 	if (foundRegion == true)
		{
			// alert("Found Region");
			if ( aString.search("==") != -1 )
			{
				foundRegion = false;
			}
			else
			{
			var newopt = new Option( aString2, escape( aString ) );
			obj_to.options[counter] = newopt;
			counter++;
			}
	 	}
		else if ( aString == theCompare )
		{
			foundRegion = true;
		}

	}
}
function refreshLocationstown(f_name,sel_name_to,arraystr) {	
	var obj_to=eval('document.'+f_name+'.'+sel_name_to);	
	alert(obj_to);
	var counter=0;
	
}



var selectversionstring = "Select a version";
var allstring = "All";
var submitcount = 0;

var browserTag = "Unknown";
if ( navigator.appName == "Microsoft Internet Explorer" ) {
    browserTag = "MSIE";
}
else if ( navigator.appName == "Netscape" ) {
    browserTag = "NS";
}
var majorVer = parseInt( navigator.appVersion );

function supportedBrowser() {
    return( ( ( browserTag == "NS"   && majorVer >= 3 )
			  || ( browserTag == "MSIE" && majorVer >= 4 ) ) ? 1 : 0 );
}

function rebuildSelect( object, options ) {
    if ( ! supportedBrowser() ) return( 0 );
	if ( object.options == null ) {
		return( 0 );
	}
    if ( options != null && options.length > 0 ) {
		object.options.length = 0;
		for ( var i = 0; i < options.length; i++ ) {
			var newopt = new Option( options[i], escape( options[i] ) );
			object.options[i] = newopt;
		}
		object.selectedIndex = 0;
    }
    return( 1 );
}

function updateVersions(f_name,sel_name_from,sel_name_to) {
	var obj_from=eval('document.'+f_name+'.'+sel_name_from);
	var obj_to=eval('document.'+f_name+'.'+sel_name_to);
    if ( ! supportedBrowser() ) return( 0 );
    var prodsel	= obj_from;
    var product	= prodsel.options[prodsel.selectedIndex].text;
    var versions = new Array();
    var i = 0;
    for ( var version in r[product] ) {
		// alert("got here");


		versions[i++] = all_towns;
    }
    if ( prodsel.selectedIndex == 0 ) {
		// versions[0] = selectversionstring;
    } else if ( versions.length < 1 ) {
		// versions[0] = allstring;
    }
	rebuildSelect(obj_to, versions );
    return( 1 );
}

function updateProducts(f_name,sel_name_from,sel_name_to) {
	var obj_from=eval('document.'+f_name+'.'+sel_name_from);
	var obj_to=eval('document.'+f_name+'.'+sel_name_to);
    if ( ! supportedBrowser() ) return( 0 );
    var products = new Array();
    var i = 0;
    for ( var prod in r ) {
		products[i++] = prod;
    }
    rebuildSelect( obj_from, products );
	if ( obj_to ) {
	    updateVersions(f_name,sel_name_from,sel_name_to);
	}
    return( 1 );
}


function countProducts(f_name,sel_name) {
	var obj_f=eval('document.'+f_name);
	var obj=eval('document.'+f_name+'.'+sel_name);
    var last = 0;
    for ( var i = 0; i < obj_f.elements.length; i++ ) {
		var n = obj_f.elements[i].name;
		var l = n.length;
		if ( n.substring(0,8) == "product_" && n.substring(l-5) == "_name" ) {
			var num = parseInt( n.substring(8,l-5) );
			if ( num > last ) last = num;
		}
    }
    return( last );
}

function refreshLocation(command,top,f_name,sel_name) {
	var obj=eval('document.'+f_name+'.'+sel_name);
    if ( supportedBrowser() && ( command == "refreshproduct" || command == "refreshversion" ) ) {
		if ( obj && ( command == "refreshproduct" ) ) {
			updateVersions();
		}
    }
    else {
		if ( ! supportedBrowser() ) {
			return( false );
		}
		if ( command == "addproduct" && browserTag == "MSIE" && majorVer == 3 ) {
			var last = countProducts();
			if ( last > 2 ) {
				alert( "Because of limitations in cookie size, you may only have three products in Microsoft Internet Explorer 3.x." );
				return( false );
			}
		}
		var url = buildURL(command);
		if ( top ) {
			window.top.location = url;
		}
		else {
			window.location = url;
		}
	}
    return( false );
}

function additem(fbox,tbox) {	
	
	if(fbox.selectedIndex!=-1){
		move(fbox,tbox);
		removeitembyname(tbox,"Search All Towns")
		SortD(tbox);
	}
}

function additem(fbox,tbox, is_manhattan_office)
{	
		if(fbox.selectedIndex!=-1){
		move(fbox,tbox);
		if (is_manhattan_office)
		{
			removeitembyname(tbox,"Search All Areas")
		}
		else
		{	
			removeitembyname(tbox,"Search All Towns")
		}
		SortD(tbox);
	}
}

function additembyname(tbox, theName) {
	if (tbox.options.length == 0) {
			var no = new Option();
			no.value = theName;
			no.text = theName;
			tbox.options[0] = no;
			tbox.length++;
	}
	BumpUp(tbox);
}

function removeitembyname(fbox, theName) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].value == theName || fbox.options[i].value == "None Selected") {
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		   }
	}
	BumpUp(fbox);
}

function removeitem(fbox) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			// var no = new Option();
			// no.value = fbox.options[i].value;
			// no.text = fbox.options[i].text;
			// tbox.options[tbox.options.length] = no;
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		   }
	}
	BumpUp(fbox);
	additembyname (fbox,"Search All Towns")
}

function removeitem(fbox, is_manhattan_office) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			// var no = new Option();
			// no.value = fbox.options[i].value;
			// no.text = fbox.options[i].text;
			// tbox.options[tbox.options.length] = no;
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		   }
	}
	BumpUp(fbox);
	if (is_manhattan_office)
		additembyname (fbox,"Search All Areas")
	else
		additembyname (fbox,"Search All Towns")
}

function move(fbox,tbox) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			var no = new Option();
			no.value = fbox.options[i].value;
			no.text = fbox.options[i].text;


			// check that this entry doesn't already exist.  If it doesn't, then add it

			var len = tbox.length;
			var found = false;
			for(var count = 0; count < len; count++) {
				if (tbox.options[count] != null) {
					if (no.text == tbox.options[count].text) {
					found = true;
					break;
					}
				}
			}


			if (found != true) {
				tbox.options[tbox.options.length] = no;
				len++;
			}
			// tbox.options[tbox.options.length] = no;
			// fbox.options[i].value = "";
			// fbox.options[i].text = "";
		   }
	}
	// BumpUp(fbox);
	//if (sortitems)
	 SortD(tbox);
}


function BumpUpold(box)  {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value == "")  {
for(var j=i; j<box.options.length-1; j++)  {
box.options[j].value = box.options[j+1].value;
box.options[j].text = box.options[j+1].text;
}
var ln = i;
break;
   }
}
if(ln < box.options.length)  {
box.options.length -= 1;
BumpUp(box);
   }
}

/***********************
Name:			BumpUp
Description:	This function removes empty Option objects from the Select object array
Input:			box, a Select object
Programmer:	Michael Coca
Date:			4.30.02
************************/
function BumpUp(box) {
	var i=0;
	while (i < box.options.length) {
		if (box.options[i].value == "")
			box.options[i] = null;
			// by setting the individual Option object to null, it is removed from the array
			// and the other objects indexes are automatically adjusted
		else
			i++; // otherwise look at the next item
	}
} // end function

function SortD(box)  {
var temp_opts = new Array();
var temp = new Object();
for(var i=0; i<box.options.length; i++)  {
temp_opts[i] = box.options[i];
}
for(var x=0; x<temp_opts.length-1; x++)  {
for(var y=(x+1); y<temp_opts.length; y++)  {
if(temp_opts[x].text > temp_opts[y].text)  {
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
      }
   }
}
for(var i=0; i<box.options.length; i++)  {
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
   }
}

function numberOfSelectedEntries(theSelect) {
	n = 0
	count = 0
	while (n < theSelect.options.length) {
	if(theSelect.options[n].selected && theSelect.options[n].value != "None Selected") {
		count ++ }
	n ++ }
	return count
}

function selectAll(theSelect) {
	for(var i =0;i < theSelect.options.length; i++) {
		theSelect.options[i].selected = true;
	}
}

function popWindow2(theLink) {
	var initialX =	100;
	var initialY = 	100;
	if (navigator.appName == "Netscape") {
		theWindow = open(	theLink,
			"Lookup",
			"width=560,height=400," +
						"screenX=" + initialX + ",screenY=" + initialY +
				",scrollbars");
			theLink.focus();		// make sure our new window is in front
	}
	else {
		theWindow = open(	theLink,
			"Lookup",
			"width=560,height=400" +
			",left=" + initialX + ",top=" + initialY +
				",scrollbars");
	}
	theWindow.focus();
}


// ajax for getting vlaues of cities


var req;
var myDate;
function createRequestObject() {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	return req;
	
}

function areacatOpen2(catid)
{
	if(catid)
	{	
	   myDate = new Date(document.lastModified);
		req=createRequestObject();
		if(req) {
			req.onreadystatechange = emailupdate2;
			url="/getareas.php?catid="+catid+"&mydate="+myDate;
			//alert(url);
			req.open("GET", url, true);
			req.send("");				
		}
	}	
}
function emailupdate2()
{
    // only if req shows "loaded"
    if (req.readyState == 4)
	 {
        // only if "OK"
		//alert(req.status);
        if (req.status == 200)
		 {
            // ...processing statements go here...
			var response = req.responseText;
			//alert(response);			
			var theString=response;
			var arraystr = theString.split("||");
			document.qsearch.all_towns.options.length = 0;
			var counter=0;
			for (i=0;i <arraystr.length;i=i+2)
			{
				var aString = arraystr[i];
				var aString2 = arraystr[i+1]; 	 	
				document.qsearch.all_towns.options[counter] = new Option(aString2,aString);
				counter++;
			}
			
         }
		 else 
		 {
            alert("There was a problem retrieving the data:\n" +
             req.statusText);
         }
    }
}