/**
 * Project: 	Web Application Database with Interface
 * Developer: 	Major Web Consulting -- major.webcon@comcast.net
 * Last Updated: 	10/06/2006
 * Description: 
 * This javascript contains common functions that are used by more than one script.
 *
 * Script Usage/Instructions:
 *   n/a
 * 
 * Code Specifications: 
 *   All PHP files used for this site require:
 *   1) /common/site.common.php
 *   2) /common/site.common.js
 *   
 */
function PrintListing(name,type,zip)
{
	var pForm = getElementById( "print_listing" );

	getElementById( "print_name" ).value = name;
	getElementById( "print_type" ).value = type;
	getElementById( "print_zip" ).value = zip;

	pForm.action = "http://www.giftescapes.com/search/business-listing-print.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function BrowseState(state)
{
	var pForm = getElementById( "browse_state" );

	getElementById( "browse_state_state" ).value = state;

	pForm.action = "http://www.giftescapes.com/browse/city.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function BrowseCity(city,state,type)
{
	var pForm = getElementById( "browse_city" );

	getElementById( "browse_city_city" ).value = city;
	getElementById( "browse_city_state" ).value = state;
	getElementById( "browse_city_type" ).value = type;

	pForm.action = "city-browse.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function Browse(type)
{
	var pForm = getElementById( "browse" );

	getElementById( "browse_type" ).value = type;

	pForm.action = "http://www.giftescapes.com/browse/index.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function GetResultsPaginate(page)
{
	var pForm = getElementById( "results" );

	getElementById( "low_limit" ).value = page - 10;

	pForm.action = "http://www.giftescapes.com/search/search-results.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function GetResultsPaginateBrowseCityRegion(page)
{
	var pForm = getElementById( "browse_city" );

	getElementById( "low_limit" ).value = page - 10;

	pForm.action = "http://www.giftescapes.com/browse/city-browse.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function GetResultsPaginateBrowseCity(page)
{
	var pForm = getElementById( "browse_city" );

	getElementById( "low_limit" ).value = page - 10;

	pForm.action = "http://www.giftescapes.com/browse/city.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function GetResultsPaginateBrowse(page)
{
	var pForm = getElementById( "paginate_browse" );

	getElementById( "low_limit" ).value = page - 10;

	pForm.action = "http://www.giftescapes.com/browse/index.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function setCity(city)
{
	var pForm = getElementById( "search" );

	getElementById( "city_nav" ).value = city;
}

var callNumber = 0;
function getCities(state)
{
	if ( typeof(script) != "undefined" && script )
	{
		alert("Sorry, but you have to wait!");
		return false;
	}
	callNumber++;
	script = document.createElement("script");
	script.src = "http://www.giftescapes.com/common/citiesScript.php?state="+state+"&CN=" + callNumber;	
	document.getElementsByTagName("head")[0].appendChild(script);
	return;	
}

function write2Div( cities )
{
	document.getElementById("cities_div").innerHTML = cities;
	setTimeout('removeScript()', 50);
	return;
}

function removeScript()
{
	if ( typeof(script) != "undefined" )
	{
		document.getElementsByTagName("head")[0].removeChild(script);
		script = null;
	}
	return;
}

// Ensure the user of this form has entered the required fields.
function validateData()
{
	var txtEmail		= getElementById( "business_email");
	var txtName		= getElementById( "business_name");
	var txtCity		= getElementById( "city");
	var txtState		= getElementById( "state");
	var txtZip			= getElementById( "zip");
	var txtUsername		= getElementById( "username");
	var txtPassword		= getElementById( "password");
	var txtID			= getElementById( "partner_id");
	var bRequirementsMet	= true;
	var txtFocusField;
			
	if( txtName.value.length == 0 ) {
		txtFocusField = txtName;
		window.alert( "\nPlease enter a business name." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if( bRequirementsMet && ( txtCity.value.length == 0 ) ) {
		txtFocusField = txtCity;
		window.alert( "\nPlease enter a city." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if( bRequirementsMet && ( txtState.value.length == 0 ) ) {
		txtFocusField = txtState;
		window.alert( "\nPlease enter a state." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if(  bRequirementsMet && ( txtZip.value.length == 0 ) ) {
		txtFocusField = txtZip;
		window.alert( "\nPlease enter a zip code." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if(  bRequirementsMet && ( txtEmail.value.length == 0 ) ) {
		txtFocusField = txtEmail;
		window.alert( "\nPlease enter a e-mail address." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if(  bRequirementsMet && ( txtEmail.value.length > 0 ) ) {
		var nAtSymbolIndex = txtEmail.value.indexOf( "@" );
		var sUserName      = "";
		var sServerName    = "";
				
		if( nAtSymbolIndex > 0 && nAtSymbolIndex < txtEmail.value.length - 1 ) {
			sUserName = txtEmail.value.substring( 0, nAtSymbolIndex );
			sServerName = txtEmail.value.substring( nAtSymbolIndex + 1 );
			nAtSymbolIndex = sServerName.indexOf( "." );
		}
		if( nAtSymbolIndex == -1 || sUserName.length == 0 || sServerName.length == 0 ) {
			window.alert( "You have entered an invalid email address. Please enter a correct email address." );
			txtEmail.focus();
			bRequirementsMet = false;
		}
	}
	if( bRequirementsMet && ( txtUsername.value.length == 0 ) ) {
		txtFocusField = txtUsername;
		window.alert( "\nPlease enter a username." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if( bRequirementsMet && ( txtPassword.value.length == 0 ) ) {
		txtFocusField = txtPassword;
		window.alert( "\nPlease enter a password." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if( bRequirementsMet && ( txtID.value.length == 0 ) ) {
		txtFocusField = txtID;
		window.alert( "\nPlease enter a partner ID." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	return bRequirementsMet;
}

// Ensure the user of this form has entered the required fields.
function validatePartnerJoinData()
{
	var txtType		= getElementById( "business_type");
	var txtEmail		= getElementById( "email");
	var txtName		= getElementById( "business_name");
	var txtCity		= getElementById( "city");
	var txtState		= getElementById( "state");
	var txtZip			= getElementById( "zip");
	var txtPhone		= getElementById( "phone");
	var bRequirementsMet	= true;
	var txtFocusField;
	
	if( txtName.value.length == 0 ) {
		txtFocusField = txtName;
		window.alert( "\nPlease enter a business name." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if( bRequirementsMet && ( txtCity.value.length == 0 ) ) {
		txtFocusField = txtCity;
		window.alert( "\nPlease enter a city." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if( bRequirementsMet && ( txtState.value.length == 0 ) ) {
		txtFocusField = txtState;
		window.alert( "\nPlease enter a state." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if(  bRequirementsMet && ( txtZip.value.length == 0 ) ) {
		txtFocusField = txtZip;
		window.alert( "\nPlease enter a zip code." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if(  bRequirementsMet && txtType.value.length == 0 ) {
		txtFocusField = txtType;
		window.alert( "\nPlease enter a type of business." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if(  bRequirementsMet && ( txtPhone.value.length == 0 ) ) {
		txtFocusField = txtPhone;
		window.alert( "\nPlease enter a phone number." );
		txtFocusField.focus();
		bRequirementsMet = false;
	}
	if(  bRequirementsMet && ( txtEmail.value.length > 0 ) ) {
		var nAtSymbolIndex = txtEmail.value.indexOf( "@" );
		var sUserName      = "";
		var sServerName    = "";
				
		if( nAtSymbolIndex > 0 && nAtSymbolIndex < txtEmail.value.length - 1 ) {
			sUserName = txtEmail.value.substring( 0, nAtSymbolIndex );
			sServerName = txtEmail.value.substring( nAtSymbolIndex + 1 );
			nAtSymbolIndex = sServerName.indexOf( "." );
		}
		if( nAtSymbolIndex == -1 || sUserName.length == 0 || sServerName.length == 0 ) {
			window.alert( "You have entered an invalid email address. Please enter a correct email address." );
			txtEmail.focus();
			bRequirementsMet = false;
		}
	}
			
	return bRequirementsMet;
}

function getElementById( id ) {
	if( typeof( document.getElementById ) == "undefined" ) {
		for( var i = 0; i < document.all.length; i++ ) {
			var el = document.all( i );
			if( el.id == id ) {
				return el;
			}
		}
	}
	else {
		return document.getElementById( id );
	}
}

function GetResults(page)
{
	if( validateData() ) {
		var pForm = getElementById( "formRes" );
	
		getElementById( "lRange" ).value = page - 10;
		getElementById( "hRange" ).value = page;

		pForm.action = "search_results.html";
		pForm.method = "post";
		pForm.submit();

		return true;
	}
}

function GetType(type)
{
	var pForm = getElementById( "navSearch" );

	pForm.action = "search_results.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function AddPartner(frm)
{
	var pForm = getElementById( "formMain" );

	pForm.action = "add_partner_sub.html";
	pForm.method = "post";
	pForm.submit();

	return true;
}

function SubmitAddPartner()
{
	if( validateData() ) {
		var pForm = getElementById( "formMain" );

		pForm.action = "add-partner.html"; 
		pForm.method = "post";
		pForm.submit(); 

		return true;
	}
}

function SubmitUpdatePartner()
{
	if( validateData() ) {
		var pForm = getElementById( "formMain" );

		pForm.action = "update-partner.html"; 
		pForm.method = "post";
		pForm.submit(); 

		return true;
	}
}

function SubmitPartnerJoin()
{
	if( validatePartnerJoinData() ) {
		var pForm = getElementById( "formMain" );

		pForm.action = "signup-form.html"; 
		pForm.method = "post";
		pForm.submit(); 

		return true;
	}
}

function PartnerJoin(name,type,tab) 
{
	if (screen) {
		leftPos = (screen.width / 2) - 251
		topPos = (screen.height / 2) - 162
	}
	window.open("partner_join_popup.html?name="+escape(name)+"&type="+type+"&tab="+tab,"Approve","menubar=no,width=400,height=250,left="+leftPos+",top="+topPos+",toolbar=no");
}

