﻿// (C) 2008 ONE COMMUNITY INC.

// CAMERAS:
function OpenCam(camUrl,camName,camSettings) {
  camWindow=window.open(camUrl,camName,camSettings);
  camWindow.focus();
}

// SEE COMMENTS IN SOURCE CONTROL:
function scOpenPopup(theName,theUrl) {
	day = new Date();
	id = day.getTime();
	if ( theName == 'chat' ) {
		var openPopup = eval("page" + id + " = window.open(theUrl,theName,'width=600,height=400,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no');");
	}
	else {
		var openPopup = eval("page" + id + " = window.open(theUrl,theName,'width=1050,height=650,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no');");
	}
	openPopup.focus();
}
function scConfirmAction(theText, theAction) {
	if ( confirm(theText) ) {
		eval(theAction);
		return true;
	}
	else {
		return false;
	}
}
function scImgSwap(theName, theSrc, theLink) {
	document.getElementById('a-' + theName).href = theLink;
	var x = document.getElementById(theName)
	// ENCOURAGE OLDER IE TO REDRAW IMAGE:
	//create img in memory; set its onload event to replace web img
	var newIMG = new Image();
	newIMG.onload = function (){ x.src = newIMG.src; }
	// download new image src; when done, its onload event will trigger
	newIMG.src = theSrc;
}
function scInputInsert(obj, text) {
	// INSERT TEXT AT CURSOR LOCATION
	obj = document.getElementById(obj);
	// (C) 2006 http://parentnode.org/javascript/working-with-the-cursor-position/
	if(document.selection) {
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n");
		var range = document.selection.createRange();

		if(range.parentElement() != obj) {
			return false;
		}
		range.text = text;
		var actual = tmp = obj.value.replace(/\r\n/g, "\n");
		for(var diff = 0; diff < orig.length; diff++) {
			if(orig.charAt(diff) != actual.charAt(diff)) break;
		}
		for(var index = 0, start = 0; 
			tmp.match(text) 
				&& (tmp = tmp.replace(text, "")) 
				&& index <= diff; 
			index = start + text.length
		) {
			start = actual.indexOf(text, index);
		}
	} else if(obj.selectionStart) {
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	}
	
	if(start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.value += text;
	}
}
function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) {
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}
