function clickCountry( elem )
{
	var	row_elem,
		child_elem;

	row_elem = find_row_elem( elem );
	child_elem = document.getElementById( "ctry_"+row_elem.getAttribute("obj_id")+"_child" );
	
	if ( row_elem.getAttribute( "expand" )=="false" )
	{
		elem.src='images/expand.png';
		row_elem.setAttribute( "expand", "true" );
		child_elem.style.display = "block";
	}
	else
	{
		elem.src='images/collapse.png';
		row_elem.setAttribute( "expand", "false" );
		child_elem.style.display = "none";
	}
}

function clickState( elem )
{
	var row_elem,
		child_elem;
	
	row_elem = find_row_elem( elem );
	child_elem = document.getElementById( "st_"+row_elem.getAttribute("obj_id")+"_child" );
	
	if ( row_elem.getAttribute( "expand" )=="false" )
	{
		elem.src='images/expand.png';
		row_elem.setAttribute( "expand", "true" );
		child_elem.style.display = "block";
	}
	else
	{
		elem.src='images/collapse.png';
		row_elem.setAttribute( "expand", "false" );
		child_elem.style.display = "none";
	}
}

function checkCountry( elem )
{
	var row_elem,
		child_elem;
	
	row_elem = find_row_elem( elem );
	child_elem = document.getElementById( "ctry_"+row_elem.getAttribute( "obj_id" )+"_child" );
	
	checkItems( child_elem, elem.checked );
}

function checkState( elem )
{
	var row_elem,
		child_elem,
		ctry_elem,
		st_name,
		thresh,
		count;
	
	row_elem = find_row_elem( elem );
	count = updateParentChk( row_elem );
	
	ctry_elem = document.getElementById( row_elem.parentNode.id.slice( 0,-6 ) );
	thresh = parseInt( ctry_elem.getAttribute( "thresh" ) );
	st_name = ctry_elem.getAttribute( "st_name" );
	
	if ( !elem.checked && count[0]>thresh && count[1]==count[0]-1 )
	{
		window.alert( "Selecting "+thresh+" or more "+st_name+"s is the same price as the entire country." );
		elem.checked = true;
		count = updateParentChk( row_elem );
	}
	else
	{
		child_elem = document.getElementById( "st_"+row_elem.getAttribute( "obj_id" )+"_child" );
		if ( child_elem ) checkItems( child_elem, elem.checked );
		
		if ( elem.checked && count[1]==thresh && count[0]>thresh )
		{
			window.alert( "Selecting "+thresh+" or more "+st_name+"s is the same price as the entire country." );
			
			row_elem = document.getElementById( row_elem.parentNode.id.slice( 0, -6 )+"_chk" );
			row_elem.checked = true;
			checkCountry( row_elem );
		}
	}
	//else if ( !elem.checked && count[0]>thresh && count[1]==count[0]-1 )
		//window.alert( "Selecting "+thresh+" or more "+st_name+"s is the same price as the entire country" );
}

function checkCounty( elem )
{
	var row_elem,
		parent_elem,
		st_chk,
		checked,
		count;
	
	row_elem = find_row_elem( elem );
	count = updateParentChk( row_elem );
	
	if ( !elem.checked && count[0]>5 && count[1]==count[0]-1 )
	{
		window.alert( "Selecting 5 or more counties is the same price as the entire state." );
		elem.checked = true;
		count = updateParentChk( row_elem );
	}
	else
	{
		parent_elem = document.getElementById( row_elem.parentNode.id.slice( 0, -6 ) );
		updateParentChk( parent_elem );
	
		if ( elem.checked && count[1]==5 && count[0]>5 )
		{
			window.alert( "Selecting 5 or more counties is the same price as the entire state." );
			
			row_elem = document.getElementById( row_elem.parentNode.id.slice( 0, -6 )+"_chk" );
			row_elem.checked = true;
			checkState( row_elem );
		}
	}
	//else if ( !elem.checked && count[0]>5 && count[1]==count[0]-1 )
		//window.alert( "Selecting 5 or more counties is the same price as the entire state" );
}

function updateParentChk( row_elem )
{
	var checked,
		chk_elem;
	
	row_elem = find_row_elem( row_elem.parentNode );
	
	checked = countItems( row_elem );
	checked = checked.split( "," );
	
	chk_elem = document.getElementById( row_elem.id.slice( 0, -5 )+"chk" );
	
	if ( chk_elem.checked )
	{
		if ( checked[1]<checked[0] ) chk_elem.checked = false;
	}
	else
	{
		if ( checked[1]==checked[0] ) chk_elem.checked = true;
	}
	
	return new Array( checked[0], checked[1] );
}

function checkItems( elem, checked )
{
	var	i=0;
	while( i<elem.childNodes.length )
	{
		if ( elem.childNodes[i].nodeName=="INPUT" )
		{
			if ( elem.childNodes[i].type=="checkbox" )
			{
				elem.childNodes[i].checked = checked;
			}
		}
		else if ( elem.childNodes[i].nodeName=="DIV" )
		{
			if ( elem.childNodes[i].id!="" )	
			{
				checkItems( elem.childNodes[i], checked );
			}
		}
		
		i++;
	}
}

function countItems( elem, count, chk_count )
{
	var	i=0;
	
	if ( !count ) var count = 0;
	if ( !chk_count ) var chk_count = 0;
	
	while( i<elem.childNodes.length )
	{
		if ( elem.childNodes[i].nodeName=="INPUT" )
		{
			if ( elem.childNodes[i].type=="checkbox" )
			{
				count++;
				if ( elem.childNodes[i].checked ) chk_count++;
			}
		}
		else if ( elem.childNodes[i].nodeName=="DIV" )
		{
			if ( elem.childNodes[i].id!="" )
			{
				if ( elem.childNodes[i].id.slice( -5 )!="child" )
				{
					result = countItems( elem.childNodes[i] );
					result = result.split( "," );
			
					count += parseInt( result[0] );
					chk_count += parseInt( result[1] );
				}
			}
		}
		
		i++;
	}
	
	return count+","+chk_count;
}

function find_row_elem( elem )
{
	var row_elem = elem;
	
	while( row_elem && row_elem.nodeName!="DIV" )
	{
		row_elem = row_elem.parentNode;
	}
	
	return row_elem;
}

function get_selection()
{
	var selection = new Array();
	
	find_selection( document.getElementById( "region_list" ), selection );
	
	return selection;
}

function find_selection( parent, selection, path )
{
	var	i=0,
		type,
		new_path;
	
	if ( !path ) var path = "";
	
	while( i<parent.childNodes.length )
	{
		if ( parent.childNodes[i].nodeName=="INPUT" )
		{
			if ( parent.childNodes[i].type=="checkbox" )
			{
				if ( parent.childNodes[i].checked )
				{
					selection.push( path );
					break;
				}
			}
		}
		else if ( parent.childNodes[i].nodeName=="DIV" )
		{
			if ( parent.childNodes[i].id!="" )
			{
				new_path = path;
				if ( parent.childNodes[i].id.slice( -5 )!="child" )
				{
					if ( new_path!="" ) new_path += "_";
					
					type = parent.childNodes[i].getAttribute( "obj_type" );
					new_path += parent.childNodes[i].id.slice( type.length+1 );
				}
				
				find_selection( parent.childNodes[i], selection, new_path );
			}
		}
		 
		i++;
	}
}

function get_quote()
{
	var selection = get_selection();
	var elem;
	
	if ( selection.length>0 )
	{
		selection = "sel="+selection.toString();
		
		elem = document.getElementById("users_num");
		selection += "&users_num="+elem.options[elem.selectedIndex].value;
		
		elem = document.getElementById("prod_id");
		selection += "&prod_id="+elem.options[elem.selectedIndex].value;
		
		elem = document.getElementById("term_id");
		selection += "&term_id="+elem.options[elem.selectedIndex].value;
		
		elem = document.getElementById("license_id");
		selection += "&license_id="+elem.options[elem.selectedIndex].value;
		
		selection += "&mode=detail"
		AjaxRequest( "php/quote.ajax.php", "POST", selection, ACTION_PROC, get_quote_callback );
	}
	else window.alert( "Please make a selection and click 'Get Quote'." );
}

function get_quote_callback( data )
{
	//window.alert( data );
	document.getElementById( "quote_result" ).innerHTML = data;
}

function get_quote_pdf()
{
	var selection = get_selection();
	var elem;
	
	if ( selection.length>0 )
	{
		document.forms['quote_form'].elements['sel'].value = selection.toString();
		document.forms['quote_form'].submit();
	}
	else window.alert( "Please make a selection and click 'Get Quote'." );
}

function click_tab_nav(show_elem, hide_elem)
{
	/*document.getElementById("depot_form_container").style.height = document.getElementById("ctl_"+show_elem).getAttribute("theight")+"px";*/
	document.getElementById("tab_"+show_elem).style.display = 'block';
	document.getElementById("ctl_"+show_elem).className = 'depot_tab_label depot_tab_selected';
	document.getElementById("tab_"+hide_elem).style.display = 'none';
	document.getElementById("ctl_"+hide_elem).className = 'depot_tab_label';
}

function info_click_next( from_loc )
{
	var data;
	
	if ( document.getElementById( "signup_first" ).value=="") window.alert( "Please enter your first name." );
	else if ( document.getElementById( "signup_comp" ).value=="") window.alert( "Please enter your company name." );
	else if ( document.getElementById( "signup_last" ).value=="") window.alert( "Please enter your last name." );
	else if ( document.getElementById( "signup_addr" ).value=="") window.alert( "Please enter your physical address." );
	else if ( document.getElementById( "signup_city" ).value=="") window.alert( "Please enter your city." );
	else if ( document.getElementById( "signup_state" ).value=="") window.alert( "Please enter your state." );
	else if ( document.getElementById( "signup_zip" ).value=="") window.alert( "Please enter your zip code." );
	else if ( document.getElementById( "signup_email" ).value=="") window.alert( "Please enter your email address." );
	else if ( !validate_email( document.getElementById( "signup_email" ).value ) ) window.alert( "Invalid email address" );
	else if ( document.getElementById( "signup_vemail" ).value=="") window.alert( "Please verify your email address." );
	else if ( document.getElementById( "signup_email" ).value!=document.getElementById( "signup_vemail" ).value) window.alert( "Email and verify fields do not match." );
	else if ( document.getElementById( "signup_areacode" ).value=="") window.alert( "Phone invalid." );
	else if ( document.getElementById( "signup_prefix" ).value=="") window.alert( "Phone invalid." );
	else if ( document.getElementById( "signup_phone" ).value=="") window.alert( "Phone invalid." );
	else if ( document.getElementById( "signup_fax_areacode" ).value=="") window.alert( "Fax invalid." );
	else if ( document.getElementById( "signup_fax_prefix" ).value=="") window.alert( "Fax invalid." );
	else if ( document.getElementById( "signup_fax_phone" ).value=="") window.alert( "Fax invalid." );
	else
	{
		data  = "npa="+document.getElementById( "signup_areacode" ).value;
		data += "&nxx="+document.getElementById( "signup_prefix" ).value;
		data += "&from_loc="+from_loc;
		
		AjaxRequest( "php/verify_npa_nxx.ajax.php", "GET", data, ACTION_PROC, info_click_next_cb );
	}
}

function info_click_next_cb( data )
{
	var	phone,fax,from_loc;
	
	if ( data=="abandon" ) data = new Array( 0, 0, "abandon" );
	else data = data.split( String.fromCharCode(1) );
	
	if ( data[0]>0 ) window.alert( "a server error occurred" );
	else if ( data[1]>0 ) window.alert( "Invalid areacode/prefix." );
	else
	{
		from_loc = data[2];
		
		phone  = document.getElementById( "signup_areacode" ).value;
		phone += document.getElementById( "signup_prefix" ).value;
		phone += document.getElementById( "signup_phone" ).value;
		
		fax  = document.getElementById( "signup_fax_areacode" ).value;
		fax += document.getElementById( "signup_fax_prefix" ).value;
		fax += document.getElementById( "signup_fax_phone" ).value;
		
		data  = "company="+document.getElementById( "signup_comp" ).value;
		data += "&first="+document.getElementById( "signup_first" ).value;
		data += "&last="+document.getElementById( "signup_last" ).value;
		data += "&address="+document.getElementById( "signup_addr" ).value;
		data += "&city="+document.getElementById( "signup_city" ).value;
		data += "&state="+document.getElementById( "signup_state" ).value;
		data += "&zip="+document.getElementById( "signup_zip" ).value;
		data += "&phone="+phone;
		data += "&fax="+fax;
		data += "&email="+document.getElementById( "signup_email" ).value;
		data += "&from_loc="+from_loc;
		data += "&activated=";
		if ( from_loc=="abandon" ) data += "0";
		else data += "1";
		
		AjaxRequest( "php/signup.ajax.php", "GET", data, ACTION_PROC, info_click_next_cb2 );
	}
}

function info_click_next_cb2( data )
{
	var send_data;
	
	data = data.split( String.fromCharCode(1) );
	
	if ( data[0]>0 )
		window.alert( "a server error occurred" );
	else
	{
		if ( data[2]!="abandon")
		{
			if ( data[2]=="download" )
			{
				document.forms['down_form'].submit();
				
				//send_data  = "signup_id="+data[3];
				//send_data += "&file_name="+document.forms['down_form'].elements['down_file_name_go'].value;
				
				//AjaxRequest( "php/download.ajax.php", "GET", send_data, ACTION_PROC, downloadCB );
			}
			else
			{
				click_tab_nav( 'use', 'info' );
				document.getElementById( "tab_info" ).innerHTML = data[1];
			}
		}
	}
}

/*function downloadCB( data )
{
	data = data.split( String.fromCharCode(1) );
	
	if ( data[0]>0 )
		window.alert( "a server error occurred" );
	else
		
}*/

function info_change()
{
	AjaxRequest( "php/signout.ajax.php", "GET", null, ACTION_PROC, info_change_cb );
}

function info_change_cb()
{
	document.location.href = "quote.php";
}

function use_case_click_next()
{
	var val = get_use_case_radio();
	if ( val==0 )
		click_tab_nav( 'prod', 'use' );
	else if ( val<0 )
		window.alert( 'A Use Case is the planned use of the NAVTEQ data.  Please select the use case that best suits your needs.' );
	else
	{
		if ( window.confirm( 'At present, Quote Builder is available for GIS use cases only.  For all other cases, please click "OK" to contact a NAVmart Data Specialist or "Cancel" to select a different use case.' ) )
			document.location.href = "contact.php";
		else
		{
			//select GIS
		}
	}
}

/*function change_use_case_radio( elem )
{
	var i,this_ix = parseInt( elem.id.slice( -1 ) );
	
	i=0;
	while( i<6 )
	{
		if ( i!=this_ix ) document.getElementById( "usecase"+i ).checked = false;
		i++;
	}
}*/

function get_use_case_radio()
{
	var i;
	
	i=0;
	while( i<6 )
	{
		if ( document.getElementById( "usecase"+i ).checked ) return i;
		i++;
	}
	
	return -1;
}

function validate_email( email )
{
	var	atIx,dotIx;
	
	atIx = email.indexOf( "@", 0 );
	if ( atIx>0 )
	{
		dotIx = email.indexOf( ".", atIx );
		if ( dotIx>atIx+1 && dotIx<email.length-1 ) return true;
	}
	
	return false;
}