//javascript functions


//test for flash or no flash and reload current page
function testFlash(pageURL) {
	//test for flash version

	if (pageURL) {
		var player = new MM_FlashInfo();
		var requireFlashVersion = 7;

		if (player.version >= requireFlashVersion) {
			window.location = pageURL + "?flash=true";
		} else {
			window.location = pageURL + "?flash=false";
		}
	}
}

//swap image with a new image URL
function swapImg(imgName, imgURL) {
	imgNum = getImgNum(imgName);

	if (imgNum > -1) {document.images[imgNum].src = imgURL;}
}

//find image number using older method for compatibility
function getImgNum(imgName) {
	for (i=0; i<document.images.length; i++) {
		if (document.images[i].name == imgName) {return i;}
	}

	return -1;
}

//go to the pulldown url
function gotoURL(thisPulldown) {
	if (thisPulldown) {
		window.location = thisPulldown.options[thisPulldown.selectedIndex].value;
		return true;
	} else {
		return false;
	}
}

//open popup window
function openWindow(url, name, width, height) {
	if (!url) {return false;}

	if (!name) {name = "popupwin";}
	if (!width) {width = 640;}
	if (!height) {height = 700;}

	properties = 'toolbar=0';
	properties += ', scrollbars=1';
	properties += ', location=0';
	properties += ', statusbar=1';
	properties += ', menubar=0';
	properties += ', resizable=1';
	properties += ', width=' + width;
	properties += ', height=' + height;
	properties += ', left=50';
	properties += ', top=50';

	window.open(url, name, properties);
}

//close window and return to the previous window
function refreshParent() {
	opener.document.location.reload();
	self.close();
}

//get div object
function getObj(thisName) {
	if (document.getElementById) {
		var thisObj = document.getElementById(thisName);
	} else if (document.all) {
		var thisObj = document.all[thisName];
	} else if (document.layers) {
		var thisObj = document.layers[thisName];
	}

	return thisObj;
}

//called when a source field is changed
function sourceFieldChanged(source_el, target_field_name, function_name) {
	if (typeof(source_el.form) == 'undefined') {
		var form = document.forms[0];
	} else {
		var form = source_el.form;
	}
	var target_field = form[target_field_name];
	if (
		!target_field.value.length ||
		((!target_field_edited[target_field_name] && edit_is_new_object) || target_field.value == target_field_set_value[target_field_name])
	) {
		var target_value = source_el.value;
		if (function_name) {
			target_value = eval(function_name+'(target_value);');
		}
		target_field.value = target_value;
		target_field_set_value[target_field_name] = target_value;
	}
}

// validates without worrying about whether it has already been changed
function validateField(source_el, target_field_name, function_name) {
	if (typeof(source_el.form) == 'undefined') {
		var form = document.forms[0];
	} else {
		var form = source_el.form;
	}
	var target_field = form[target_field_name];
	var target_value = target_field.value;
	if (function_name) {
		target_field.value = eval(function_name+'(target_value);');
	}
}

function targetFieldChanged(target_el) {
	if (target_el.value != target_field_set_value[target_field_name]) {
		target_field_edited[target_el.name] = true;
	}
}

function titleToFileName(value) {
	value = value.replace(/[^a-z0-9.\s_-]/gi,'');
	value = value.replace(/[\s]/gi,'_');
	value = value.replace(/\.php/gi,'');
	return value.toLowerCase()+'.php';
}

function convertFolderName(value) {
	value = value.replace(/[^a-z0-9.\s_-]/gi,'');
	value = value.replace(/[\s]/gi,'_');
	return value.toLowerCase();
}

function convertLowercase(value) {
	value = value.replace(/[^a-z0-9.\s_-]/gi,'');
	value = value.replace(/[\s]/gi,'');
	return value.toLowerCase();
}

function convertUppercase(value) {
	value = value.replace(/[^a-z0-9.\s_-]/gi,'');
	value = value.replace(/[\s]/gi,'');
	return value.toUpperCase();
}

function makeUsername(source_el, firstname_field_name, lastname_field_name) {
	if (typeof(source_el.form) == 'undefined') {
		var form = document.forms[0];
	} else {
		var form = source_el.form;
	}

	var firstname_field = form[firstname_field_name];
	var firstname_value = firstname_field.value;

	var lastname_field = form[lastname_field_name];
	var lastname_value = lastname_field.value;

	source_el.value = convertLowercase(firstname_value + lastname_value); //.charAt(0)
}

//check if textarea if beyond max length
function checkMaxLength(source_el, maxLen) {
	if(source_el.value.length > maxLen) {
		source_el.value = source_el.value.substring(0, maxLen);
	}
}


//called to update the sample color box
function updateColor(source_el, target_name) {
	if (typeof(source_el.form) == 'undefined') {
		var form = document.forms[0];
	} else {
		var form = source_el.form;
	}

	var source_field = source_el;
	var source_value = source_field.value;

	updateBox(target_name, source_value);
}

function updateColorByID(source_el, target_name) {
	if (typeof(source_el.form) == 'undefined') {
		var form = document.forms[0];
	} else {
		var form = source_el.form;
	}

	var source_field = source_el;
	var source_value = source_field.value;

	if (masterColors) {
		var newColor = (masterColors[source_value]) ? masterColors[source_value] : "#ffffff";
		updateBox(target_name, newColor);
	}
}

function drawBox(target_name, target_color) {
	var newHTML = "";

	newHTML += '<span id="' + target_name + '" style="width:14px; height:14px; background-color:' + target_color + '; border: 1px solid #555;">';
	newHTML += '<img src="/shared/images/spacer.gif" width="14" height="14" border="0">';
	newHTML += '</span>' + "\n";

	document.write(newHTML);
}

function updateBox(target_name, target_color) {
	var target_obj = eval(target_name);

	target_obj.style.backgroundColor = target_color;
}