function findPos(obj) {
	var position = new Object();

	position.curleft = 0;
	position.curtop = 0;
	if (obj.offsetParent) {
		position.curleft = obj.offsetLeft;
		position.curtop = obj.offsetTop;

		while (obj = obj.offsetParent) {
			position.curleft += obj.offsetLeft;
			position.curtop += obj.offsetTop;
			
		}
	}
	return position;
}

function calculateImagePopupLeft(imageLeft, imageWidth) {
	
	var defaultCaptionWidth = 600;
	
	var newLeft = imageLeft + (imageWidth/2) - (defaultCaptionWidth/2);
	if(newLeft < 25) {
		return 12;
	} else if((newLeft + defaultCaptionWidth + 20) > document.body.clientWidth) {
		return document.body.clientWidth - (defaultCaptionWidth + 30);
	}
	return newLeft;
	
}

function calculateImagePopupLeftInFilmStrip(imageLeft, imageWidth) {
	
	var defaultCaptionWidth = 600;
    var filmStrip = 'filmstripDiv';
    if(document.getElementById('filmstripDiv') != null) {
       filmStrip = 'filmstripDiv';
    }
    else {
    filmStrip = 'normalVariantsFilmstripDiv';
    }
	if(document.getElementById(filmStrip).scrollLeft){
		var newLeft = imageLeft + imageWidth -  document.getElementById(filmStrip).scrollLeft - (defaultCaptionWidth)/2;
	}else{
		var newLeft = imageLeft + imageWidth - (defaultCaptionWidth)/2;
	}
    
	
	if(newLeft < document.getElementById(filmStrip).style.left) {
		return 0;
	} else if((newLeft + defaultCaptionWidth + 20) > document.body.clientWidth) {
		return document.body.clientWidth - (defaultCaptionWidth + 30);
	}
	return newLeft;
	
}

function calculateFilmstripDivId(image)
{
	var imageId = image.id;
	var indexOfDashInImageId = imageId.indexOf('-');
	var filmstripDivSuffix = '';
	
    if (indexOfDashInImageId > -1)
		{
			filmstripDivSuffix = '_' + imageId.substring(0, indexOfDashInImageId);
		}
	
	return 'filmstripDiv' + filmstripDivSuffix
}

function isArray(a) {
    return isObject(a) && typeof(a.length)!= "undefined";
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return a === null;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
}  

function isBlank(str) {
    return trim(str) == '';
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

// Validate email address and display or undisplay error div
function validateEmailAddress (emailAddress, errorDiv) {

	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	var valid = regex.test(emailAddress);
	if(valid) {
		//valid
		errorDiv.style.display="none";
	} else {
		//invalid
		errorDiv.style.display="block";
	}
	return valid;
	
}

function validateFormField (field, len, errorDiv) {
	if(field.length >= len) {
		//valid
		errorDiv.style.display="none";
		return true;
	} else {
		//invalid
		errorDiv.style.display="block";
		return false;
	}
}

// Validate Radcon user lastname
function validateLastname (lastname, errorDiv) {
	return validateFormField(trim(lastname), 1, errorDiv);
}


// Validate Radcon user streetAddress
function validateStreetAddress1(streetAddress1, errorDiv) {
    return validateFormField(trim(streetAddress1), 1, errorDiv);
}


// Validate Radcon user city
function validateCity (city, errorDiv) {
    return validateFormField(trim(city), 1, errorDiv);
}

// Validate Radcon user ZipCode
function validateZipCode (zipCode, errorDiv) {
    return validateFormField(trim(zipCode), 1, errorDiv);
}

function doesElementExistInPage(elementName) {
	if (document.getElementById(elementName)) {
		return true;
	} else {
		return false;
	}
}

function positionOutlineElements() {
	if(isObject(document.getElementById('chapter_contents'))) {
		var chapterContentTop = document.getElementById('chapter_contents').offsetTop;
		document.getElementById('chapter_outline').style.top = chapterContentTop + 'px';
   		document.getElementById('chapter_outline').style.display = 'block';
	}	
	if(isObject(document.getElementById('topic_contents'))) {
		var topicContentTop = document.getElementById('topic_contents').offsetTop;
		document.getElementById('topic_outline').style.top = topicContentTop + 'px';
   		document.getElementById('topic_outline').style.display = 'block';     
	}	
}