var popWin = null;
function popUp() {

	if(popWin != null) {
		popWin.close();
	}
	popWin =  window.open("/retrievewait.html", "emmapwin", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=710");
	popWin.document.write( "<html><head><link type='text/css' href='/css/default.css' rel='stylesheet'></head><body><div class='mapwait'>We are retrieving your map. This could take a few seconds.</div></body></html>" );
}


function getRequestForm( frameRef ) {
	var frm = null;
	if ( frameRef != null ) {
		var targetFrame = null;
		if ( typeof( frameRef ) == "string" ) {
			targetFrame = top.frames[frameRef];
		} else {
			targetFrame = frameRef;
		}
		//alert ( targetFrame );
		if ( targetFrame != null ) {
			//we have a target frame
			frm = targetFrame.document.getElementById( 'clientForm' );
		}
	}
	return frm;
}

function initSession( frameRef, memberid, planid ) {
	var frm = getRequestForm( frameRef );
	if ( frm != null ) {
		if ( frm.memberid != null ) {
			frm.memberid.value = memberid;
		}
		if ( frm.planid != null ) {
			frm.planid.value = planid;
		}
		//alert( frm.planid.value );
		submitRequest( frameRef, 'initsession' );
	}
	return true;
}

function submitRequest( frameRef, actionRequest ) {
	var frm = getRequestForm( frameRef );
	//alert ( frm.name );
	if ( frm != null ) {
		//we found our form to submit...update the action and submit it
		if ( frm.clientAction != null ) {
			frm.clientAction.value = actionRequest;
			frm.submit( );
		}
	}
	return true;
}

function locatorMap( pos, ri ) {
	//retrieve a map for the given index
	var frm = document.forms['locatorNav'];
	if ( frm != null ) {
		if ( frm.routeidx != null ) {
			frm.routeidx.value = pos;
		}
        if ( frm.routeidy != null ) {
            frm.routeidy.value = ri;
        }
        frm.choice.value = 'route';
		popUp();
		frm.target = "emmapwin";
		frm.submit();
	}
}

function locatorNavigate( pos ) {
	var frm = document.forms['locatorNav'];
	if ( frm != null ) {
		if ( frm.locPos != null ) {
			frm.locPos.value = pos;
		}
		frm.target = window.name;
		frm.choice.value = "nav";
		//submit the form
		frm.submit( );
	}
	return true;
}

function locatorRestart( frameRef, formName ) {
  if ( frameRef != null && formName != null ) {
	  var frm = frameRef.document.getElementById( formName );
	  if ( frm != null ) {
		  frm.choice.value = "restart";
			frm.target = window.name;
		  frm.submit();
	  }
  }
  return true;
}

/** represents the parsed query string  **/
function QueryString(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}

function getQueryString( w, key ) {
	if ( w != null ) {
    var page = new QueryString( w.location.search ); 
    return unescape( page.getValue( key ) ); 
	} else {
		return null;
	}
}

function signIn( ) {
	var init = getQueryString( parent, 'initsession' );
	//alert( init );
	if ( init != "false" ) {
		var frm = document.clientForm;
		if ( frm != null ) {
			frm.memberid.value = getQueryString( parent, 'memberid' );
			frm.planid.value = getQueryString( parent, 'planid' );
			frm.clientAction.value = 'initsession';
			alert( frm.clientAction.value );
			frm.submit();
		}
	}
}


