// 2/26/2009 
// This calls the old cgi form.
// Used by www.jeffersonhospital.org gateway files for dept phys listing
// loading from www.jeffersonhospital.org/jeffnowappt/apptfrmloc.js
// Used by www.frankfordhospitals.org
// loading from apps.jeffersonhospital.org/jeffnowappt/apptfrmloc.js

var apptformwin = null;

// vars for jeffersonhospital.org and jeffnow
if (location.href.indexOf('.dev.') != -1) {			// on development
	formURL = 'http://www.dev.jeffersonhospital.org/cgi-bin/jeffnowappt/apptfrmloc.cgi?'
} else if (location.href.indexOf('.test.') != -1) {
	formURL = 'https://www.test.jeffersonhospital.org/cgi-bin/jeffnowappt/apptfrmloc.cgi?'
} else {
	formURL = 'https://www.jeffersonhospital.org/cgi-bin/jeffnowappt/apptfrmloc.cgi?' 
}
var hosp = '';				// tjuh or mlh
var prefHosp = '';		// preferred hospital, pull down menu text
var specialty = '';
var physname = '';
var ohp = 0;

// substring conversion
function convertSubstr(s, ss1, ss2) {
	i = s.indexOf(ss1);
	while (i != -1) {
		s = s.substring(0,i) + ss2 + s.substring(i+1);
		i = s.indexOf(ss1)
	}
	return s;
}

// delete spaces before and after a string
function clearSpaces(s) {
	while (s[0] == ' ') {
		s = s.slice(1);
	}
	
	while (s[s.length - 1] == ' ') {
		s = s.slice(0, -1);
	}
	
	return s;
}

// depending on the current page's url, open the correct appt form
function openFormWindow() {
	theurl = location.href;
	
	winCompare = false;
	if (theurl.indexOf('http://www.jeffersonhospital.org') == 0 || theurl.indexOf('hos=0') > 10 ||
		theurl.indexOf('http://www.dev.jeffersonhospital.org') == 0 ||
		theurl.indexOf('http://www.test.jeffersonhospital.org') == 0) {
		hosp = 'tjuh'
	} else if (theurl.indexOf('http://www.mainlinehealth.org') == 0) {	// mailline not using this anymore.
		hosp = 'mlh'
	} else if (theurl.indexOf('http://www.frankfordhospitals.org') == 0 ||
			   theurl.indexOf('http://jhsweb01.tju.edu') == 0 || theurl.indexOf('hos=1') > 10) {
		hosp = 'fhs'
	} else {
		winCompare = true;
	}
	
	// the form url
	gotoURL = formURL + hosp + '+' + escape(prefHosp) + '+' + escape(specialty) + '+' + escape(physname) + '+' + ohp;
	//alert(gotoURL); return;
	
	if (winCompare) {		// from comparison window
		location = gotoURL;
		return self;
	} else {
		if (navigator.appVersion.substring(0,1) < 4) {
			apptwinObj = window.open(gotoURL, 'apptwin', 'height=460,width=590,directories=0,menubar=0,resizable=1,scrollbars=1,status=1,location=0,toolbar=1');
		} else {
			apptwinObj = window.open(gotoURL, 'apptwin', 'height=460,width=590,directories=0,menubar=0,resizable=1,scrollbars=1,status=1,location=0,toolbar=0');
		}
		return apptwinObj;
	}
}

// main function, NN6 has some problem with focus() and reopen the window when closed
function openApptForm() {
	if (apptformwin == null || apptformwin.closed) {
		apptformwin = openFormWindow();
	} else {
		apptformwin.focus();
	}
}