/* ---------------------------- */
/* XMLHTTPRequest Enable 	*/
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();


/* -------------------------- */
/* INSERT 		      */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solves an Internet Explorer cache issue */
var nocache = 0;

function insertFav(x,y,z,q) {
	// Optional: Show a waiting message in the layer with ID login_response
	//document.getElementById('addToFav').innerHTML = "Adding to Favorites..."
	// Set the random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'includes/AddFavs.cfm?OrgID=' + x + '&OppID=' + y + '&UserID=' + z + '&nocache = '+nocache);
	if (x!=0) {OID=x;}
	if (y!=0) {OID=y;}
	if (q==1) {
		rowInTable='Y';
	}
	else {
		rowInTable='N'
	}
	http.onreadystatechange = insertFavReply;
	http.send(null);
}

function insertFavTwo(x,y,z) {
	// Optional: Show a waiting message in the layer with ID login_response
	//document.getElementById('addToFav').innerHTML = "Adding to Favorites..."
	// Set the random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'includes/AddFavs.cfm?OrgID=' + x + '&OppID=' + y + '&UserID=' + z + '&nocache = '+nocache);
	http.send(null);
}

function insertFavReply() {
	if (rowInTable=='Y'){
		if(http.readyState == 4){
			var response = http.responseText;
			if (Find('Added to Favorites',response)) {
				document.getElementById('addToFav' + OID).innerHTML = '<img src="images/inner/btn.addedtofaves.gif" alt="Submit" border="0" style="padding-top: 5px;">';
			}
			if (Find('Already in Favorites',response)) {
				document.getElementById('addToFav' + OID).innerHTML = '<img src="images/inner/btn.alreadyinfavorites.gif" alt="Add to My Faves" width="169" height="21" border="0" />';
			}
			if (!Find('Added to Favorites',response) && !Find('Already in Favorites',response)) {
				document.getElementById('addToFav' + OID).innerHTML = 'Error';
			}
		}
	}
	else {
		if(http.readyState == 4){
			var response = http.responseText;
			if (Find('Added to Favorites',response)) {
				document.getElementById('addToFav').innerHTML = '<img src="images/inner/btn.addedtofaves.gif" alt="Submit" border="0" style="padding-top: 5px;">';
			}
			if (Find('Already in Favorites',response)) {
				document.getElementById('addToFav').innerHTML = '<img src="images/inner/btn.alreadyinfavorites.gif" alt="Add to My Faves" width="169" height="21" border="0" />';
			}
			if (!Find('Added to Favorites',response) && !Find('Already in Favorites',response)) {
				document.getElementById('addToFav').innerHTML = 'Error';
			}
		}
	}
}

function insertFavAll(x,y,z) {
	// Optional: Show a waiting message in the layer with ID login_response
	document.getElementById('addAllToFav').innerHTML = "Adding to Favorites..."
	// Set the random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'includes/AddAllFavs.cfm?OrgIDs=' + x + '&OppIDs=' + y + '&UserID=' + z + '&nocache = '+nocache);
	http.onreadystatechange = insertFavAllReply;
	http.send(null);
}

function insertFavAllReply() {
	if(http.readyState == 4){
		var response = http.responseText;
		if (Find('Added to Favorites',response)) {
			document.getElementById('addAllToFav').innerHTML = '<img src="images/inner/btn.alladdedtofaves_visited.gif" alt="Submit" border="0" style="padding-top: 5px;">';
		}
		if (!Find('Added to Favorites',response)) {
			document.getElementById('addAllToFav').innerHTML = 'Error';
		}
	}
}

// These functions no longer in use. Matching code for EmailAFriend.cfm that template's version 2.
/*function emailAFriend(x,y,z) {
	// Optional: Show a waiting message in the layer with ID login_response
	//document.getElementById('emailAFriend').innerHTML = "Emailing friend..."
	var email='';
	email = prompt("Enter address to send listing to.", email);
	if (email) {
		// Set the random number to add to URL request
		nocache = Math.random();
		// Pass the login variables like URL variable
		http.open('get', 'includes/EmailAFriend.cfm?OrgID=' + x + '&OppID=' + y + '&UserID=' + z + '&EmailAddress=' + email + '&nocache = '+nocache);
		http.onreadystatechange = insertReplyEmail;
		http.send(null);
	}
}

function insertReplyEmail() {
	if(http.readyState == 4){
		var response = http.responseText;
		if (Find('Email Sent',response)) {
			//document.getElementById('emailAFriend').innerHTML = '<img src="images/inner/btn.emailSent.gif" width="96" height="21" border="0" />';
			alert('Email sent.');
		}
		if (!Find('Email Sent',response)) {
			document.getElementById('emailAFriend').innerHTML = 'Error';
		}
	}
}
*/

var OJSID='';
var rowInTable='N';
var PassedUserID='';
var toggleFlagUnflag='';

function flagFollowup(x,y,z,f) {
	// Optional: Show a waiting message in the layer with ID login_response
	//document.getElementById('flagFollowup' + x).innerHTML = "Flagging for followup..."
	// Set the random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'includes/FlagFollowup.cfm?OJSID=' + x + '&UserID=' + y + '&Flag=' + f + '&nocache = '+nocache);
	OJSID=x;
	PassedUserID=y;
	if (f==1) {
		toggleFlagUnflag=0;
	}
	else {
		toggleFlagUnflag=1;
	}	
	if (z==1) {
		rowInTable='Y';
	}
	else {
		rowInTable='N'
	}
	http.onreadystatechange = insertReplyFollowup;
	http.send(null);
}

function insertReplyFollowup() {
	if (rowInTable=='Y'){
		if(http.readyState == 4){
			var response = http.responseText;
			if (Find('Flagged',response)) {				
				if (toggleFlagUnflag==0) {
					document.getElementById('flagFollowup' + OJSID).innerHTML = '<a href="javascript:flagFollowup(' + OJSID  + ',' + PassedUserID + ',1,0,0);"><img src="images/inner/btn.flaggedforfollowup.gif" width="193" height="21" border="0"></a>';
					document.getElementById('OJSRow' + OJSID).className = 'flagged';
				}
				else {
					document.getElementById('flagFollowup' + OJSID).innerHTML = '<a href="javascript:flagFollowup(' + OJSID  + ',' + PassedUserID + ',1,1,0);"><img src="images/inner/btn.flagforfollowup.gif" width="193" height="21" border="0"></a>';
					document.getElementById('OJSRow' + OJSID).className = '';
				}				
			}
			if (!Find('Flagged',response)) {
				document.getElementById('flagFollowup' + OJSID).innerHTML = 'Error';
			}
		}
	}
	else {	
		if(http.readyState == 4){
			var response = http.responseText;
			if (Find('Already Flagged',response)) {
				document.getElementById('flagFollowup' + OJSID).innerHTML = '<img src="images/inner/btn.flaggedforfollowup.gif" width="193" height="21" border="0">';
			}
			if (Find('Followup Flagged',response)) {
				document.getElementById('flagFollowup' + OJSID).innerHTML = '<img src="images/inner/btn.flaggedforfollowup.gif" width="193" height="21" border="0">';
			}
			if (!Find('Already Flagged',response) && !Find('Followup Flagged',response)) {
				document.getElementById('flagFollowup' + OJSID).innerHTML = 'Error';
			}
		}
	}
	
}

function removeFav(x,y) {
	if (confirm('Are you sure you would like to remove this favorite from your list?')) {
		// Set the random number to add to URL request
		nocache = Math.random();
		// Pass the login variables like URL variable
		http.open('get', 'includes/RemoveFavs.cfm?FavoriteID=' + x + '&UserID=' + y + '&nocache = '+nocache);
		FID=x;
		http.onreadystatechange = removeFavReply;
		http.send(null);
	}
}

function removeFavTwo(p,u,r) {
	// Set the random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'includes/RemoveFavsTwo.cfm?OrgID=' + r + '&OpportunityID=' + p + '&UserID=' + u + '&nocache = '+nocache);
	http.send(null);
}

function removeFavReply() {
	if(http.readyState == 4){
		var response = http.responseText;
		if (Find('Removed from Favorites',response)) {
			document.getElementById('Fav' + FID).style.display = 'none';
			document.getElementById('Fav' + FID).style.visibility = 'hidden';
		}
		if (!Find('Removed from Favorites',response)) {
			document.getElementById('FavRemove' + FID).innerHTML = 'Error';
		}
	}
}

function removeAppl(x,y) {
	if (confirm('This will not withdraw an application.\nThis will only remove the application record from being displayed here.\nAre you sure you would like to remove this application from your list? ')) {
		// Set the random number to add to URL request
		nocache = Math.random();
		// Pass the login variables like URL variable
		http.open('get', 'includes/RemoveAppl.cfm?OJSID=' + x + '&UserID=' + y + '&nocache = '+nocache);
		OJSID=x;
		http.onreadystatechange = removeApplReply;
		http.send(null);
	}
}

function removeApplReply() {
	if(http.readyState == 4){
		var response = http.responseText;
		if (Find('Removed from Applications',response)) {
			document.getElementById('Appl' + OJSID).style.display = 'none';
			document.getElementById('Appl' + OJSID).style.visibility = 'hidden';
		}
		if (!Find('Removed from Applications',response)) {
			document.getElementById('ApplRemove' + OJSID).innerHTML = 'Error';
		}
	}
}

function Find(sb,s){
	 return s.toString().indexOf(sb) + 1;
}

function queryMetroAreas(zip) {
	nocache = Math.random();
	http.open('get', '/includes/CheckCSA.cfm?Zip=' + zip + '&nocache = '+nocache);
	http.onreadystatechange = processZipReply;
	http.send(null);
}

function processZipReply() {
	if(http.readyState == 4){
		var response = http.responseText;
		if (!Find('Zip Not Found',response) && !Find('Multiple Results Found',response) && !Find('Error: please provide ZIP code.',response)) {
			for (i=0; i <= document.f1.MetroRegionID.options.length - 1; i++) {
				if (document.f1.MetroRegionID.options[i].value == trim(response)) {
					document.f1.MetroRegionID.selectedIndex = i;
					break;
				}
			}
		}
		else {
			document.f1.MetroRegionID.selectedIndex = 0;
		}
	}
}