function checkBrowser() {
	app=navigator.appName

	if ((app.indexOf('Netscape') != -1) && (parseInt(navigator.appVersion.charAt(0)) >= 5)) {
  		return 'netscape6';
  	} else if (app.indexOf('Netscape') != -1) {
  		return 'netscape';
  	} else if (app.indexOf('Microsoft') != -1) {
  		return 'explorer';
  	} else {
  		return 'other';
  	}
}

//will popup an alert if message is not an empty string
function reportError(message, isError){
	if (reportError.arguments.length < 2){
		isError = false;
	}
	if (isError){
		dialogType = 1;
	} else {
		dialogType = 4;
	}
	if (message.length > 0){
		newAlert('CardScan.Net', message, dialogType, 1);
	}
}

//replaces ampersands in strings with a custom escape character
function replaceUrlSafe(theString){
	parsedString = theString;
	while (parsedString.indexOf(' ') != -1){
		parsedString = parsedString.replace(' ', '+');
	}
	while (parsedString.indexOf('&') != -1){
		parsedString = parsedString.replace('&', '::amp::');
	}
	while (parsedString.indexOf('"') != -1){
		parsedString = parsedString.replace('"', '::quote::');
	}
	while (parsedString.indexOf('#') != -1){
		parsedString = parsedString.replace('#', '::pound::');
	}
	while (parsedString.indexOf('*') != -1){
		parsedString = parsedString.replace('*', '::asterisk::');
	}
	return parsedString;
}

//replaces ampersands in strings with a custom escape character
function restoreUrlSafe(theString){
	parsedString = theString;
	while (parsedString.indexOf('+') != -1){
		parsedString = parsedString.replace('+', ' ');
	}
	while (parsedString.indexOf('::amp::') != -1){
		parsedString = parsedString.replace('::amp::', '&');
	}
	while (parsedString.indexOf('::quote::') != -1){
		parsedString = parsedString.replace('::quote::', '"');
	}
	while (parsedString.indexOf('::pound::') != -1){
		parsedString = parsedString.replace('::pound::', '#');
	}
	while (parsedString.indexOf('::asterisk::') != -1){
		parsedString = parsedString.replace('::asterisk::', '*');
	}
	return parsedString;
}

//submits forms
function submitForm(FormName, Url){
	document.forms[FormName].action = Url;
	document.forms[FormName].submit();
}	

//given a form object, will submit it from any other window
function submitRemoteForm(FormObj, Url){
	theForm = eval(FormObj);
	theForm.action = Url;
	theForm.submit();
}

//will center a specific window after it has been created
//currently not used
function centerWindow(windowName, windowHeight, windowWidth){
	screenHeight = screen.availHeight;
	screenWidth = screen.availWidth;
	newX = (screenWidth / 2) - (windowWidth / 2);
	newY = (screenHeight / 2) - (windowHeight / 2);
	windowName.moveTo(newX,newY);
	//windowName.focus();
	return false;
}

//given the window width and height, will return the coordinates at which the window should be placed to center it
function windowCenterCoordinate(windowWidth, windowHeight, axis){
	screenHeight = screen.availHeight;
	screenWidth = screen.availWidth;
	
	newX = (screenWidth / 2) - (windowWidth / 2);
	newY = (screenHeight / 2) - (windowHeight / 2);
	if (axis == 'x'){
		return newX;
	} else {
		return newY;
	}
}

//generates a string which can be used within the style definition of a window
//compensates for browser descrepancies
function generateWindowString(width, height){
	xPos = windowCenterCoordinate(width, height, 'x');
	yPos = windowCenterCoordinate(width, height, 'y');
	windowString = 'width=' + width + ',height=' + height + ',screenX=' + xPos + ',screenY=' + yPos + ',top=' + yPos + ',left=' + xPos;
	return windowString;
}

//simple search and replace function for strings
function replaceString(searchString, findString, replaceWithString){
	var newString = searchString;
	while (newString.indexOf(findString) != -1){
		startPos = newString.indexOf(findString);
		endPos = findString.length + startPos;
		newString = newString.substr(0, (startPos)) + replaceWithString + newString.substr(endPos, newString.length);
	}
	return newString;
}

function ltrim(inputString){
	var parsedString = inputString;
	while (parsedString.charAt(0) == ' '){
		parsedString = parsedString.substr(1);
	}
	return parsedString;
}

function rtrim(inputString){
	var parsedString = inputString;
	while (parsedString.charAt(parsedString.length-1) == ' '){
		parsedString = parsedString.substr(0, parsedString.length - 1);
	}
	return parsedString;
}


//returns the selected option's value 
function evaluateRadio(radioObj){
	theReturnValue = null;
	if (radioObj){
		if (radioObj.length){
			for(i=0;i<radioObj.length;i++){
				if (radioObj[i].checked){
					theReturnValue = radioObj[i].value;
					break;
				}
			}
		} else {
			if (radioObj.checked){
				theReturnValue = radioObj.value;
			}
		}
	}
	return theReturnValue;
}

//global that indicates whether any form field has been altered-
var hasChanged = false;

//if a form field has been altered, then offer to let the user save the document
function alertChanged(url, thisPage){

	//if we are creating a live card pages and they haven't saved
	if (window.location.toString().indexOf('my_AccuCard_edit.asp') != -1){ //AccuCard code here-
		alertChangedAccuCard(url);
	
	//if we are in livebook prefs
	} else if (window.location.toString().indexOf('AccuCard_service.asp') != -1){
			if (hasChanged){
				if(newConfirm('Save Changes?', 'You have made changes to your preferences. Would you like to save them?', 1, 1, 0)){
					//the parameter passed below it the FORWARDING URL- see the submitThisForm function
					submitThisForm(url);
				} else {
					if (validateFilesEnabled(url)){
						window.location = url;
					}
				}
			} else {
				if (validateFilesEnabled(url)){
					window.location = url;
				}
			}
			
	} else {

		if (hasChanged) {
			//if we are in edit view
			if (window.location.toString().indexOf('edit_view.asp') != -1){ //edit view uses its own code- call the function here-
				alertChangedEditView(url);
			//if we are in summary view
			} else if (window.location.toString().indexOf('summary_view.asp') != -1){
				window.location = url;
				

			
			//if we are in AccuCard split view
			} else if (window.location.toString().indexOf('split_view.asp') != -1){
				if(newConfirm('Save Changes?', 'You have made changes to this contact. Would you like to save them?', 1, 1, 0)){
				//if (confirm('You have made changes to this contact. Would you like to save them?')){
					
					newURL = '/' + thisPage + '?save=true&forward=' + url;
					//alert(newURL);
					submitForm('form1', newURL);
				} else {
					window.location = url;
				}
			
			//if we are in preferences or AccuCard preferences
			} else if ((window.location.toString().indexOf('prefs_main.asp') != -1) || (window.location.toString().indexOf('prefs_personal_info.asp') != -1) ||(window.location.toString().indexOf('AccuCard_service.asp') != -1)){
				if (newConfirm('Save Changes?', 'You have made changes to your preferences. Would you like to save them?', 1, 1, 0)){
					newURL = thisPage + '?update=all&forward=' + url;
					//alert(newURL);
					submitForm('form1', newURL);
				} else {
					window.location = url;
				}
			}
		} else {
			window.location = url;
		}
	}
}

var helpWin

//open the help window
function openHelpWin(theUrl, winName, features) {
	if (!helpWin || helpWin.closed) {
		helpWin = window.open(theUrl, winName, features);
		helpWin.focus()
	} else {
		// bring existing window to focus
		helpWin.focus()
	}
}


//opens a new window
var myWind
function openNewWin(theUrl, winName, features) {
	if (!myWind || myWind.closed) {
		myWind = window.open(theUrl, winName, features);
	} else {
		// bring existing window to focus
		myWind.focus()
	}
}

//returns an anchor object given its name as a string
function getAnchorObj(theAnchorName){
	for (i=0;i<document.anchors.length;i++){
		if (document.anchors[i].name == theAnchorName){
			return document.anchors[i];
		}
	}
	return false;
}

//given an object and a cursor type as a string, this sets the cursor type to the object
function setCursor(theObj, theCursor){
	if ((checkBrowser() == 'explorer') || (checkBrowser() == 'netscape6')){
		theObj.style.cursor = theCursor;
	}
}

//given an object and a color as a string, this sets the color to the object
function setColor(theObj, theColor){
	if ((checkBrowser() == 'explorer') || (checkBrowser() == 'netscape6')){
		theObj.style.color = theColor;
	}
}

//TODO- update this
//given an object, sets the cursor type to waiting
function setWaiting(theObj){
	if ((checkBrowser() == 'explorer') || (checkBrowser() == 'netscape6')){
		theObj.style.cursor = 'wait';
	}
}

//retuns querystrings in JavaScript just like in asp
function getQueryString(theKey){
	qs = document.location.search;
	//start each pair with an ampersand
	qs = '&' + qs.slice(1, qs.length);
	//return an empty string if there is no qs
	if (qs.indexOf('=') == -1){
		return '';
	}
	found=false;
	//loop through each of the pairs to find the key
	while (qs.indexOf('=') != -1){
		//trim the ampersand
		qs = qs.slice(1, qs.length);
		//find the next ampersand, this is the end of the pair
		nextAmp = qs.indexOf('&');
		if (nextAmp == -1) {
			nextAmp = qs.length+1;
		}
		//get the current pair
		thePair = qs.slice(0, nextAmp);
		//slice the key
		key = thePair.slice(0, qs.indexOf('='));
		theValue = thePair.slice(qs.indexOf('=')+1);
		//find out if this is the key we are looking for
		if (key.toLowerCase() == theKey.toLowerCase()){
			return replaceString(theValue, '+', ' ');
			found=true;
			break;
		}
		
		qs = qs.slice(nextAmp);
	}
	if (!found) {
		return '';
	}
}

//utility function- alerts the value of the given select
function alertValue(theSelect){
	if (theSelect.selectedIndex != -1){
		for (i=0;i<theSelect.options.length;i++){
			if (theSelect.options[i].selected) {
				alert('value: ' + theSelect.options[i].value + '\ntext: ' + theSelect.options[i].text);
			}
		}
	} else {
		alert('nothing selected');
	}	
}

function cleanWindows(){
	if (cleanWindows.arguments.length > 0){
		for (i=0;i<cleanWindows.arguments.length;i++){
			winObj = eval(cleanWindows.arguments[i]);
			if (winObj){
				winObj.close();
			}
		}
	}
}

function validateBadCharArray(theString, badCharArray){
	validated = true;
	badCharArray = badCharArray.split(',');
	for (i=0;i<badCharArray.length;i++){
		if (theString.indexOf(badCharArray[i]) != -1) {
			validated = false;
		}
	}
	return validated;
}

//TODO- working on a way to kill session variables when a user closes their window
//if they dont click logout
function logout(){
	//return false;
	//url = 'logout_popup.asp'
	//window.open(url,'logout','scrollbars=no,resizable=no,width=10,height=10');
}