/**
 * Get IP to Geolocation / Region from Google API
 * Matches a sampling of surrounding regions and cities
 * author: mcn 10/27/2009, NC
 * copyright: page1solutions.com
 * 
 * REQUIREMENTS GIVEN: https://na2.salesforce.com/a0740000005yIkV
 *   Guys, 
 *   The below page needs to have a geo-targeted page created for North Carolina. 
 *   http://www.cochranfirm.com/verdicts.html 
 *   Below are the requirements The Bar has stated. For the time being we just need to create a "under Construction" Verdicts and Settlements page.
 *   Please let me know any questions you have.
*/

var debug = true;
//var debug = false;

var clientCity  = ""; 
var clientState = ""; 

google.load("maps", "2");

function initialize() {
	
	if (google.loader.ClientLocation && google.loader.ClientLocation.address.city && google.loader.ClientLocation.address.region) {
		clientCity  = google.loader.ClientLocation.address.city.toUpperCase();
		clientState = google.loader.ClientLocation.address.region.toUpperCase();
	}
}

initialize();

var terms       = [clientCity,clientState];
var locations   = {'NC':['NC','NORTH CAROLINA']};

//
// Match a term in array against the given array of words, 
// 	Return true if found, false otherwise.
//                  
function match_within (terms, words) {

	var matched = false;
	var wsize   = words.length;
	var tsize   = terms.length;

	if (wsize == 0 || tsize == 0) {
		return false;
	}
		
	for ( var i = 0; i < wsize; i++ ) {
	
		for ( var j = 0; j < tsize; j++ ) {
			var pat = new RegExp( words[i] );
			matched = pat.test( terms[j] );
			
			if ( debug ) {
				//alert("match word pattern: " + words[i] + " with term: " + terms[j] + " result: " + matched); //IE
				//console.log( "match word pattern: " + words[i] + " with term: " + terms[j] + " result: " + matched );
			}
			if ( matched == true ) return true;
		}
	}
	return false;
}

//
// If a geoIP term is matched within the words for a webclient's city/state, 
//	redirect the client to the appropriate page
//
switch ( true ) {

	case ( match_within( terms, locations['NC'] ) ) :
		window.location.replace("http://www.cochranfirm.com/verdicts-nc.html"); 
		break;
	
	default:
}