function changeImage(name,folder,filename,langid,extension) {
	
	//alert(name);
	if(langid!='')
		document[name].src = 'Images/' + folder + '/' + filename + '_' + langid + '.' + extension;
	else
		document[name].src = 'Images/' + folder + '/' + filename + '.' + extension;
}

function gbl_OpenNewWindow(strWinURL, strWinName, strOptions){
	var win;
	if(strOptions == ''){
		strOptions = 'scrollbars=yes,location=yes,toolbar=yes,resizable=yes,menubar=yes,status=yes'
	}
	win = window.open(strWinURL, strWinName, strOptions);
	win.focus();
}

//In javascript, for some reason, the base tag is not used.
//This function addresses this problem in a limited way.
function gbl_FixRelativeURL(strRelativeURL){	
	if(document.all && document.all.tags("base") && document.all.tags("base")[0])
		return document.all.tags("base")[0].href + strRelativeURL;
	else
		return strRelativeURL;
}

function gbl_GetFormElementValue(objFormElement){	

	//recursively handle form element group (such as radio buttons)	
	if(objFormElement.length && !objFormElement.type){
		var strValue;
		for(var i=0; i<objFormElement.length; i++){
			strValue=gbl_GetFormElementValue(objFormElement[i]);
			if (strValue !=''){return strValue;}
		}
		return '';
	}
	if(objFormElement.type){
		if(objFormElement.type=="select-one"||objFormElement.type=="select-multiple"){
			if(objFormElement.options&&objFormElement.selectedIndex>=0){
				if(objFormElement.options[objFormElement.selectedIndex].value!='')
				   return objFormElement.options[objFormElement.selectedIndex].value;
				else
				   return objFormElement.options[objFormElement.selectedIndex].text;
			} else{
				return '';
			}
		} else if(objFormElement.type=="checkbox"||objFormElement.type=="radio"){
			if(objFormElement.checked==true)
				return objFormElement.value;
			else
				return '';
		}
	}
	//else
	return objFormElement.value;
}


/* ****************************************************************************
	****************************************************************************
	The following is used for checking that there are enough spots in a course
	that is being registered for.
	****************************************************************************
	**************************************************************************** */
	
function CheckSessionCount() {
	var ErrorExists = false;
	var ErrorMessage = '';
	
	//check if session has enough room for everyone being registered
	for(i = 0; i < Sessions.length; i++) {
		var CurrentCount = SessionUserCount[i];
		
		//minus current sessions from current count
		for(j = 0; j < UserCurrentSession.length; j++)
			if (UserCurrentSession[j] == Sessions[i])
				CurrentCount -= 1;
				
		//add new sessions
		for(j = 0; j < UserNewSession.length; j++)
			if (UserNewSession[j] == Sessions[i])
				CurrentCount += 1;
		
		// Add error message that current session is over-booked
		if (CurrentCount > SessionMaxUsers[i]) {
			ErrorExists = true;
			ErrorMessage += 'To many people being registered for course ' + SessionTypeName[i] + ' in ' + SessionCityName[i] + ' on ' + SessionDate[i] + '\n';
		}
	}
	if (ErrorExists) {
		alert(ErrorMessage);
		return false;
	} else
		return true;
}

function ChangeUserSession(SelectObj) {
	var UserID = GetUserID(gbl_GetFormElementValue(SelectObj));
	var CurrentSessionID = GetCurrentSessionID(gbl_GetFormElementValue(SelectObj));
	var NewSessionID = GetNewSessionID(gbl_GetFormElementValue(SelectObj));
	
	var UserNewSessionID = GetUserNewSessionID(UserID);
	
	// Decrement UserCount by 1 for the session the user is leaving, if applicable
	if (UserNewSessionID != 0) {
		var oldsession = document.getElementById('SessionUserCount' + UserNewSessionID);
		var count = oldsession.innerHTML;
		count *= 1;
		count -= 1;
		oldsession.innerHTML = count;
		if (GetSessionMaxUsers(UserNewSessionID) >= count)
			oldsession.style.color = '#000000';
	}
	
	// Increment UserCount by 1 for the session the user is entering, if applicable
	if (NewSessionID != 0) {
		var newsession = document.getElementById('SessionUserCount' + NewSessionID);
		var count = newsession.innerHTML;
		count *= 1;
		count += 1;
		newsession.innerHTML = count;
		if (GetSessionMaxUsers(NewSessionID) < count)
			newsession.style.color = '#ff0000';
	}
	
	// Update internal array for checking on form submit	
	for(i = 0; i < Users.length; i++)
		if (Users[i] == UserID)
			UserNewSession[i] = NewSessionID;
}

function GetSessionMaxUsers(SessionID) {
	return SessionMaxUsers[GetSessionIndex(SessionID)];
}

function GetSessionIndex(SessionID) {
	for(i = 0; i < Sessions.length; i++)
		if (Sessions[i] == SessionID)
			return i;
}

function GetUserCurrentSessionID(UserID) {
	return UserCurrentSession[GetUserIndex(UserID)];
}

function GetUserNewSessionID(UserID) {
	return UserNewSession[GetUserIndex(UserID)];
}

function GetUserIndex(UserID) {
	for(i = 0; i < Users.length; i++)
		if (Users[i] == UserID)
			return i;
}

function GetUserID(UserSession) {
	return GetID(UserSession, 1);
}

function GetCurrentSessionID(UserSession) {
	return GetID(UserSession, 2);
}

function GetNewSessionID(UserSession) {
	return GetID(UserSession, 3);
}

function GetID(UserSession, Pos) {
	var pattern = /(\d+);(\d+):(\d+)/;
	var re = new RegExp(pattern);
	return parseInt(re.exec(UserSession)[Pos]);
}


	/* ****************************************************************************
	****************************************************************************
	****************************************************************************
	**************************************************************************** */
function PrintConfirmation() {
	gbl_OpenNewWindow(gbl_FixRelativeURL('Home/PrintSession.aspx?popup=1'),'','scrollbars=yes,location=no,toolbar=no,resizable=yes,menubar=no,status=yes,height=500,width=670');
}

function OpenSessionView(SessionID) {
	gbl_OpenNewWindow(gbl_FixRelativeURL('Admin/ViewSession.aspx?popup=1&amp;SessionID=' + SessionID),'','scrollbars=yes,location=no,toolbar=no,resizable=yes,menubar=no,status=yes,height=350,width=670');
}
