var ChordialBrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
ChordialBrowserDetect.init();




var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   
   function SelectTab(tabName) {
	SetValue("Main","SelectedTab",tabName);
	SubmitEvent("Main","SelectTab");
   } // SelectTab()

	function ViewDetails(strScript, strTable, strFile) {
		SetValue('Main','SelectedScript',strScript);
		SetValue('Main','SelectedTable',strTable);
		SetValue('Main','SelectedFile',strFile);
		SetValue('Main','SDSPageType','Details');
		SubmitEvent('Main','ViewDetails');
	} // ViewDetails()

	function ViewScripts(name) {
		SetValue('Main','SelectedScript',name);
		SetValue('Main','SDSPageType','Script');
		SubmitEvent('Main','Render');
	} // ViewScripts()

	function SaveXML(siteID) {
		if (confirm("Saving XML - Are You Sure?")) {
			SetValue("frmPickaSite","TargetSiteID",siteID)
			SetValue("Banners","SDSGetSiteID",siteID);
			SubmitEvent("Banners","SaveXML");
		}
	} // SaveXML()

	
		function GetCheckboxChecked(formName,name) {
			eval("var checkbox = document." + formName + "." + name + ";");
			return checkbox.checked;
		} // BBSGGetCheckboxChecked()

		function SaveUserValue() {
			if (confirm("Updating Configuration Variable. Are You Sure?")) {
				SubmitEvent("Main","SaveUserValue")
			}
		}//SaveUserValue()
		function SaveSiteValue() {
			if (confirm("Updating Configuration Variable. Are You Sure?")) {
				SubmitEvent("Main","SaveSiteValue")
			}
		}//SaveSiteValue()
		function SaveBPValue() {
			if (confirm("Updating Configuration Variable. Are You Sure?")) {
				SubmitEvent("Main","SaveBPValue")
			}
		}//SaveBPValue()

		function SaveAppValue() {
			if (confirm("Updating Configuration Variable. Are You Sure?")) {
				SubmitEvent("Main","SaveAppValue")
			}
		}//SaveAppValue()


		function SelectCV() {
			SubmitEvent("Main","SelectCV")
		} // SelectCV()

		function SelectCVType() {
			SubmitEvent("Main","SelectCVType")
		} // SelectCVType()


		function Select() {
			if (SDSValidateSelect() == true) {
				SubmitEvent("Main","Select");
			}
		} // Select()
		
		function Save() {
			if (ValidateUpdate() == true) {
				SubmitEvent("Main","Update");
			}
		} // Select()
		

		function FindForm(formName) {
		    return document.forms[0];
			var numForms = document.forms.length;
			var i,form;
			for (i = 1; i <= numForms; i++) {
				form = document.forms[i - 1];
				if (form.name == formName) {
				
					break;
				}
			}
			return form;
		} 
	
	function GetValue(formName,valueName) {

	var formObject = FindForm(formName);
	var value;
	var numElements = formObject.elements.length;
	var i,element;
	for (i = 1; i <= numElements; i++) {
		element = formObject.elements[i - 1];
		if (element.name == valueName) {
			value = element.value;
			break;
		}
	}
	return value;
		} 

		function SetValue(formName,valueName,value) {
			var formObject = FindForm(formName);
			var numElements = formObject.elements.length;
			var i,element;
			for (i = 1; i <= numElements; i++) {
				element = formObject.elements[i - 1];
				if (element.name == valueName) {
					element.value = value;
					break;
				}
			}
		} // SetValue()

function GetRadioValue(formName,name,numButtons) {
	var i, checkedFlag;
	for (i = 0; i <= numButtons - 1; i++) {
		 eval("var radio = document." + formName + "." + name + "[" + i + "];");
		 checkedFlag = radio.checked;
		 if (checkedFlag == true) {
			  return radio.value;
		 }
	}
	return "";
} // GetRadioValue()

function GetRadioChecked(formName,name,ordinal,state) {
	eval("var radio = document." + formName + "." + name + "[" + ordinal + "];");
	radio.checked = state;
} // GetRadioChecked()

function GetSelectOptionValue(name) {
	eval("var selectionList = document.forms[0]." + name + ";");
	var selectedIndex = selectionList.selectedIndex;
	var options = selectionList.options;
	return options[selectedIndex].value;
} // GetSelectOptionValue()

function GetSelectOptionText(name) {
	eval("var selectionList = document.forms[0]." + name + ";");
	var selectedIndex = selectionList.selectedIndex;
	var options = selectionList.options;
	return options[selectedIndex].text;
} // GetSelectOptionText()

function GetSelectOptionTextByID(id) {
	var selectionList = document.getElementById(id);
	var selectedIndex = selectionList.selectedIndex;
	var options = selectionList.options;
	return options[selectedIndex].text;
} // GetSelectOptionText()

function GetSelectOptionValueByID(id) {
	var selectionList = document.getElementById(id);
	var selectedIndex = selectionList.selectedIndex;
	var options = selectionList.options;
	return options[selectedIndex].value;
} // GetSelectOptionValue()

function GetValue(formName,valueName) {
	var formObject = FindForm(formName);
	var value;
	var numElements = formObject.elements.length;
	var i,element;
	for (i = 1; i <= numElements; i++) {
		 element = formObject.elements[i - 1];
		 if (element.name == valueName) {
			  value = element.value;
			  break;
		 }
	}
	return value;
} // GetValue()

function SetSelectOption(formName,name,value) {
	eval("var selectionList = document." + formName + "." + name + ";");
	if (selectionList != null) {
		var options = selectionList.options;
		var i,optionValue;
		for (i = 1; i <= options.length; i++) {
			optionValue = options[i - 1].text;
			if (optionValue == value) {
				selectionList.selectedIndex = i - 1;
				break;
			}
		}
	}
} // SetSelectOption()

function SetSelectOptionByID(id,value) {
    var selectionList = document.getElementById(id);
	if (selectionList != null) {
		var options = selectionList.options;
		var i,optionValue;
		for (i = 1; i <= options.length; i++) {
			optionValue = options[i - 1].text;
			if (optionValue == value) {
				selectionList.selectedIndex = i - 1;
				break;
			}
		}
	}
} // SetSelectOption()

function SetSelectOptionByDropDown(selectionList,value) {
	if (selectionList != null) {
		var options = selectionList.options;
		var i,optionValue;
		for (i = 1; i <= options.length; i++) {
			optionValue = options[i - 1].text;
			if (optionValue == value) {
				selectionList.selectedIndex = i - 1;
				break;
			}
		}
	}
} // SetSelectOption()

function SetSelectOptionByValue(formName,name,value) {
	eval("var selectionList = document." + formName + "." + name + ";");
	if (selectionList != null) {
		var options = selectionList.options;
		var i,optionValue;
		for (i = 1; i <= options.length; i++) {
			optionValue = options[i - 1].value;
			if (optionValue == value) {
				selectionList.selectedIndex = i - 1;
				break;
			}
		}
	}
} // SetSelectOption()

function SetSelectOptionByIDValue(id,value) {
    var selectionList = document.getElementById(id);
	if (selectionList != null) {
		var options = selectionList.options;
		var i,optionValue;
		for (i = 1; i <= options.length; i++) {
			optionValue = options[i - 1].value;
			if (optionValue == value) {
				selectionList.selectedIndex = i - 1;
				break;
			}
		}
	}
} // SetSelectOption()

//Pass integer month and year and the function returns the # of days in the month
function getDays(month, year) 
{
	// Test for leap year when February is selected.
	if (1 == month)
		return ((0 == year % 4) && (0 != (year % 100))) ||
				(0 == year % 400) ? 29 : 28;
	else
		return daysInMonth[month];
}

function GridSort(formName, sortBy, SDSEventType) {
	var sortDir = 'ASC';
	if (sortBy!=null) {
		eval("currSortBy=document.forms[0].SortBy.value")
		//if already the current sort column, reverse the sort order
		if (currSortBy==sortBy) {
			eval("currDir=document.forms[0].SortDir.value")
			if (currDir=="ASC") sortDir = 'DESC'
		}
		eval("document.forms[0].SortDir.value='" + sortDir + "'");
		eval("document.forms[0].SortBy.value='" + sortBy + "'");
	}
	SubmitEvent(formName, SDSEventType);
} //GridSort()

function ShowEditItemsForm(formName,pageno) {
	if (pageno!=null) {
		eval("document.forms[0].PageNo.value=" + pageno);
	}
	SubmitEvent(formName, "ShowEditItemsForm");
} //ShowEditItemsForm()

function SDSValidate() 
{
    return true;
}

function SubmitEvent(formName, eventType) 
{
    SetValue(formName,'SDSEventType',eventType);

    if (SDSValidate() == true) 
    {
        eval("document.forms[0].submit();");
    }
}

function DigitsOnly(event) {
	// Call from the onKeyPress event to limit text entry to only digits 0-9, delete, and backspace.
	if (window.Event)  // handle Netscape style 
		return (event.which >= 48 && event.which <= 57 || event.which == 8 || event.which == 0);
	else  // handle IE style (ignore Mac IE since key events are not supported)
		if (navigator.userAgent.indexOf("Mac") == -1) 
			event.returnValue = (event.keyCode >= 48 && event.keyCode <= 57);
} //DigitsOnly

function MoneyOnly(event) {
	// Call from the onKeyPress event to limit text entry to only digits 0-9, period, delete, and backspace.
	if (window.Event)  // handle Netscape style 
		return (event.which >= 48 && event.which <= 57 || event.which == 8 || event.which == 0 || event.which == 46);
	else  // handle IE style (ignore Mac IE since key events are not supported)
		if (navigator.userAgent.indexOf("Mac") == -1) 
			event.returnValue = ((event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 46);
} //MoneyOnly

function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function SetPromptValues(strPrefix, strPromptValues) {
    var objHiddenValues = document.getElementById(strPrefix + "_PromptValues");
    objHiddenValues.value = strPromptValues;
}
    
function GetPromptValue(strPrefix, position) {
    var objHidden = document.getElementById(strPrefix + "_PromptValues");
    var strPromptValues = objHidden.value;
    var arrValues = strPromptValues.split(",");
    
    return arrValues[position];
}

function StringStartsWith(str, prefix) {
    return str.indexOf(prefix) === 0;
}

String.prototype.trim = function() { return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "")) }
String.prototype.startsWith = function(str) { return (this.match("^" + str) == str) }
String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }

Array.prototype.Contains = function(mxd, strict) {
    for (i in this) {
        if (i == mxd && !strict) return true;
        else if (i === mxd) return true;
    }
    return false;
}

function AddTrailer(inString, trailer) {
    var outString = inString;
    if (inString.endsWith(trailer) == false) {
        outString += trailer;
    }
    return outString;
}

function chordialFireEvent(obj, evt) {
    var fireOnThis = obj;
    if (document.createEvent) {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent(evt, true, false);
        fireOnThis.dispatchEvent(evObj);
    } else if (document.createEventObject) {
        fireOnThis.fireEvent('on' + evt);
    }
}

function clickLink(linkobj) {
    var onclickHandler = linkobj.getAttribute('onclick')
    if (onclickHandler == null) document.location = linkobj.getAttribute('href');
    else eval(onclickHandler(linkobj));
}

function ChordialGetScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

//DO NOT USE THIS:  IT DOES NOT WORK CROSS BROWSER!!!!!!
//I am leaving it here, but you should NOT use it!!
function SimulateLinkClick(link) {
    if (ChordialBrowserDetect.browser == "Explorer") {
        link.click();
    }
    else {
        try {
            var myEvt = document.createEvent('MouseEvents');
            myEvt.initMouseEvent('click', false, false, window, 1, 0, 0, 0, 0, false, false, false, false, 1, null);
            link.dispatchEvent(myEvt);
        } catch (ex) {
            myEvt = document.createEventObject();
            myEvt.detail = 0;
            myEvt.screenX = 0;
            myEvt.screenY = 0;
            myEvt.clientX = 0;
            myEvt.clientY = 0;
            myEvt.ctrlKey = false;
            myEvt.altKey = false;
            myEvt.shiftKey = true;
            myEvt.metaKey = false;
            myEvt.button = 1;
            myEvt.relatedTarget = null;
            link.fireEvent('onclick', myEvt);
        }
    }
}


