ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
ns6 = (document.getElementById)? true:false;
var r = new Object();

var selectversionstring = "Select a version";
var allstring = "All";
var submitcount = 0;

var browserTag = "Unknown";

function checkAllTowns(){
	thisField = document.custom.version
	selectAll(thisField);
	additem(thisField,document.custom.selectedVersion)
}

function removeAllTowns(){
	thisField = document.custom.selectedVersion
	selectAll(thisField);
	removeitem(thisField)
}

function fillSelectedBox(){
	if (document.custom.cityTempList.value != ""){
		var townList = document.custom.cityTempList.value
		var locationArraySelected = new Array();
		var regionArraySelected = new Array();
		var nameGuidPair = new Array()
		
		locationArraySelected = townList.split("|");
		
		var counter = 0;
		for (i=0;i <locationArraySelected.length;i++) {
			if (locationArraySelected[i] != "") {
				nameGuidPair = locationArraySelected[i].split(":");
				var guid = nameGuidPair[0];
				var city = nameGuidPair[1];		
		 		var newopt = new Option(city, guid);
				document.custom.selectedVersion.options[counter] = newopt;
				counter++;
			}
		}
	}
}

function fillTempList(){
	var tList = "";
	if (document.custom.version != '') {
		a = document.custom.selectedVersion.options.length;
		for (var i=0; i<a; i++) {
			tList = tList + document.custom.selectedVersion.options[i].value + ":" + document.custom.selectedVersion.options[i].text + "|";
		}
		document.custom.cityTempList.value = tList;
	}
}

function showObject(obj) {
	if (ie4) obj.visibility = "visible"
	else if (ns6) obj.style.visibility = "visible"
	else obj.visibility = "show"
}

function hideObject(obj) {
	if (ie4) obj.visibility = "hidden"
	else if (ns6) obj.style.visibility = "hidden"
	else obj.visibility = "hide"
}

function regular()
{
	//showObject(block);
	//hideObject(block2)
}

function advanced()
{
	//showObject(block2);
	//hideObject(block)
}

function selectRegionStart(regidx){
	
	document.custom.product.selectedIndex = regidx - 1;
	refreshLocations();
}


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], options[i]  );
			object.options[i] = newopt;
		}
		object.selectedIndex = 0;
    }
    return( 1 );
}

function updateVersions() {
    if ( ! supportedBrowser() ) return( 0 );
    var prodsel	= document.custom.product;
    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++] = version;
    }
    if ( prodsel.selectedIndex == 0 ) {
		// versions[0] = selectversionstring;
    } else if ( versions.length < 1 ) {
		// versions[0] = allstring;
    }
	rebuildSelect( document.custom.version, versions );
    return( 1 );
}

function updateProducts() {
	if ( ! supportedBrowser() ) return( 0 );
    var products = new Array();
    var i = 0;
    for ( var prod in r ) {
		products[i++] = prod;
    }
    rebuildSelect( document.custom.product, products );
	if ( document.custom.version ) {
	    updateVersions();
	}
    return( 1 );
}

function countProducts() {
    var last = 0;
    for ( var i = 0; i < document.custom.elements.length; i++ ) {
		var n = document.custom.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) {
    if ( supportedBrowser() && ( command == "refreshproduct" || command == "refreshversion" ) ) {
		if ( document.custom.version && ( 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 );
}

sortitems = 1;  // Automatically sort items within lists? (1 or 0)

function additem(fbox,tbox) {
	move(fbox,tbox);
	removeitembyname(tbox,"None Selected")
	if (sortitems) 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 = "";
			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,"None Selected")
}

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 BumpUp(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);
   }
}

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 checkAndGetNumber(aField, aName)
	{
	var checkOK = "0123456789-,";
	var checkStr = aField.value;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
				if (ch != ",")
			{
				allNum += ch;
			}
		}

	if (!allValid)
	{	// alert("Please enter only numbers for the " + aName);
		// aField.value = "";
		// aField.focus();
		return false
	}
	return true
}


// Check for a blank field
function isFieldBlank(theField) {
    if(theField.value == "")
        return true;
    else
        return false;
}



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;
	}
}


// Check for a valid email address (Does it contain a "@")


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();
}


function checkFunction(){
if (document.custom.newlisting.checked == true){
	document.custom.pricereduced.checked = false
	}
}
function checkFunction2(){
if (document.custom.pricereduced.checked == true){
	document.custom.newlisting.checked = false
	}
}
