function init(showEditor) {
	preloadImages();
	if (showEditor == 1) {
		initEditor();
	}
}

function initEditor() {
	var config = new HTMLArea.Config();
	config.toolbar = [["bold", "italic", "underline", "separator", "strikethrough", "subscript", "superscript", "separator", "insertorderedlist", "insertunorderedlist", "separator", "inserthorizontalrule", "createlink", "separator", "htmlmode"]];
	config.statusBar = false;
	HTMLArea.replace('content', config);
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		nav_photos_down = newImage("images/nav_photos-down.gif");
		nav_photos_over = newImage("images/nav_photos-over.gif");
		nav_baby_blog_down = newImage("images/nav_baby_blog-down.gif");
		nav_baby_blog_over = newImage("images/nav_baby_blog-over.gif");
		nav_movies_down = newImage("images/nav_movies-down.gif");
		nav_movies_over = newImage("images/nav_movies-over.gif");
		nav_family_down = newImage("images/nav_family-down.gif");
		nav_family_over = newImage("images/nav_family-over.gif");
		nav_emails_down = newImage("images/nav_emails-down.gif");
		nav_emails_over = newImage("images/nav_emails-over.gif");
		nav_themes_down = newImage("images/nav_themes-down.gif");
		nav_themes_over = newImage("images/nav_themes-over.gif");
		nav_settings_down = newImage("images/nav_settings-down.gif");
		nav_settings_over = newImage("images/nav_settings-over.gif");
		nav_account_down = newImage("images/nav_account-down.gif");
		nav_account_over = newImage("images/nav_account-over.gif");
		preloadFlag = true;
	}
}

function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
}

function toggleCheck(theCheckbox, theId) {

	var isChecked = document.forms["theForm"][theCheckbox].checked;

	if (isChecked) {
		document.forms["theForm"][theCheckbox].checked = false;
		$("pg"+theId).style.border = "none";
	} else {
		document.forms["theForm"][theCheckbox].checked = true;
		$("pg"+theId).style.border = "1px solid #FF0000;";
	}
}

function selectOne(myForm,myBox,myTotal){
	var gn = myForm.announce;
	if (myTotal > 1) {
		for (j=0; j<myTotal; j++) {
			if(myBox!=j){
				gn[j].checked = false;
			}
		}
   	}		
}	

function resetChecks(myCheck) {
	if (myCheck.checked != true)	{
		var gn = $("theForm").announce_ids;
		for (j=0; j < gn.length; j++) {
			gn[j].checked = false;
		}
	}
}

function selectAllOptions(form_name,field_name) {
	var selectItems = document.forms[form_name].elements[field_name];
	var countItems = selectItems.length;
	for(var i=0; i<countItems; i++) {
		if (countItems) {
			selectItems[i].selected = 1;
		}
	}
}

function switchBabyForm(theChoice) {
	
	theForm.location.value = "";
	theForm.city.value = "";
	theForm.state.selectedIndex = 0;
	theForm.province.value = "";

	if (theChoice.options[theChoice.selectedIndex].value == "Adoption") {
		changeDiv("a1", "display", "");
		changeDiv("b1", "display", "none");
		changeDiv("b2", "display", "none");
		changeDiv("b3", "display", "none");
	} else {
		changeDiv("a1", "display", "none");
		changeDiv("b1", "display", "");
		changeDiv("b2", "display", "");
		changeDiv("b3", "display", "");
	}
}

function changeDiv(the_div,the_style,the_change) {
	var the_style = getStyleObject(the_div);
	if (the_style != false) {
		the_style.display = the_change;
	}
}

function hideAll() {
	changeDiv("album_link","display","none");
	changeDiv("blog_link","display","none");
	changeDiv("movie_link","display","none");
}

function wipeAll() {
	$('subject').innerHTML = "";
	$('body').innerHTML = "";
}

function getStyleObject(objectId) {
	return $(objectId).style;
}

function insertEmail(id,website,site_title,owner) {
	hideAll();
	wipeAll();
	switch (id)	{
		case "0":
			return;
			break;
		case "1": // Website Invite
			insertContent('invite','','',website,site_title,owner);
			break;
		case "2": // New Photos
			changeDiv('album_link','display','block');
			insertContent('photos',theForm.album_link_id[0].value,theForm.album_link_id[0].text,website,site_title,owner);
			break;
		case "3": // New Movies
			changeDiv('movie_link','display','block');
			insertContent('movie',theForm.movie_link_id[0].value,theForm.movie_link_id[0].text,website,site_title,owner);
			break;
		case "4": // New Blog Entry
			changeDiv('blog_link','display','block');
			insertContent('blog',theForm.blog_link_id[0].value,theForm.blog_link_id[0].text,website,site_title,owner);
			break;
	};
	changeDiv('step2','display','block');
}

function insertContent(type,id,id_label,website,site_title,owner) {

	var objBody = $('body');
	var objSubject = $('subject');

	objBody.innerHTML = "Hello,<br /><br />";

	switch (type)	{
		case "":
			return;
			break;
		case "invite": // Website Invite
			objSubject.innerHTML = "MyBabyPlace: " + site_title;
			objBody.innerHTML += owner + " just created a new MyBabyPlace website in order to share baby photos, movies and stories with you. To view the website, please visit the following link: <br /><br />"+website+" <br /><br />When you visit, please be sure to sign the guest book to let " + owner + " know you've stopped by.  Enjoy!";
			break;
		case "photos": // New Photos
			var url = website+"/album.php?id="+id;
			objSubject.innerHTML = site_title+": New Photos!";
			objBody.innerHTML += owner + "'s MyBabyPlace website features new photos in the \""+id_label+"\" album. To view the new photos, please visit the following link:<br /><br /> " + url;
			break;
		case "movie": // New Movies
			var url = website+"/movies.php#m"+id;
			objSubject.innerHTML = site_title+": New Movie!";
			objBody.innerHTML += owner + "'s MyBabyPlace website has been updated with a new movie called \""+id_label+"\". To view the new movie, please visit the following link:<br /><br /> " + url;
			break;
		case "blog": // New Blog Entry
			var url = website+"/blog_entry.php?entry="+id;
			objSubject.innerHTML = site_title+": New Blog Entry!";
			objBody.innerHTML += owner + "'s MyBabyPlace website has been updated with a new blog entry called \""+id_label+"\". Read all about it at the following link:<br /><br /> " + url;
			break;
	};
	objBody.innerHTML += " <br /><br />Sincerely,<br />The MyBabyPlace Team";
	changeDiv('step3','display','block');
	changeDiv('step4','display','block');

}

function submitEmailForm() {
	theForm.fSubject.value = $('subject').innerHTML;
	theForm.fBody.value = $('body').innerHTML;
	theForm.submitBtn.enabled = false;
	theForm.submit();
}

function parentLink(link) {
	window.opener.location=link;
	window.close();
}

function toggleRows(hide, show) {
	$(hide).style.display = "none";
	$(show).style.display = "inline";
	return false;
}

function updateImage(direction,photo_id,desaturate) {
	var rotation = parseInt(theForm.rotation.value);
	if (desaturate == -1)	{
		var bw = parseInt(theForm.desaturate.value);
	} else {
		if (doConfirm('WARNING! After changing your photo to black and white, saving that change can not be undone.  If you save a photo as black and white and later decide you want it in color, you will need to re-upload that photo. You can still preview the picture without saving the change. Click OK to do so.')) {
			var bw = desaturate;
		} else {
			var bw = parseInt(theForm.desaturate.value);
		}
	}		
	document.theForm.desaturate.value = bw;
	rotation+=direction*90;
	if (Math.abs(rotation)==360) {
		rotation = 0;
	}
	document.theForm.rotation.value = rotation;
	var newImage = '/manage/photos_display.php?photo='+photo_id+'&rotation='+rotation+'&desaturate='+bw;
	document.theImage.src = newImage;
}

function setRadio(form_name, field_name, element_number) {
	var objRadio = document.forms[form_name].elements[field_name];
	objRadio[element_number].checked = 1;
}

function setCheckBoxes(form_name, field_name, check_value) {

	var theFrame = $("photos");
	if (theFrame) {
		var theDoc = theFrame.contentWindow.document;
	} else {
		var theDoc = document;
	}

	var objCheckBoxes = theDoc.forms[form_name].elements[field_name];
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes) {
		objCheckBoxes.checked = check_value;
	} else {
		for(var i = 0; i < countCheckBoxes; i++) {
			objCheckBoxes[i].checked = check_value;
		}
	}
}

function selectAllPhotos(theState) {

	var theFrame = $("photos");
	var theDoc = theFrame.contentWindow.document
	var objCheckBoxes = theDoc.forms["theForm"].elements["selected_rows[]"];
	
	for (i=0; i<objCheckBoxes.length; i++) {
		objCheckBoxes[i].checked = theState;
		togglePhotoSelect('theForm', 'selected_rows[]', i, true, false);
	}
}

function togglePhotoSelect(theForm, theChecks, theCheck, doSwap, inFrame) {

	var theDoc;
	var objParentForm;

	if (!inFrame) {
		theDoc = $("photos").contentWindow.document;
		objParentForm = document.forms["theForm"];
	} else {
		theDoc = document;
		objParentForm = this.parent.document.forms["theForm"];
	}

	var objCheckBoxes = theDoc.forms[theForm].elements[theChecks];
	var objDiv = theDoc.getElementById('pg'+theCheck);
	
	if(!objCheckBoxes.length) {
		if (objCheckBoxes.checked == 1) {
			if (doSwap) {
				objCheckBoxes.checked = 0;
				objDiv.className = "pg_normal";
			} else {
				objDiv.className = "pg_selected";
			}
		} else {
			if (doSwap) {
				objCheckBoxes.checked = 1;
				objDiv.className = "pg_selected";
			} else {
				objDiv.className = "pg_normal";
			}
		}
	} else {
		if (objCheckBoxes[theCheck].checked == 1) {
			if (doSwap) {
				objCheckBoxes[theCheck].checked = 0;
				objDiv.className = "pg_normal";
			} else {
				objDiv.className = "pg_selected";
			}
		} else {
			if (doSwap) {
				objCheckBoxes[theCheck].checked = 1;
				objDiv.className = "pg_selected";
			} else {
				objDiv.className = "pg_normal";
			}
		}
	}

	var intChecked = 0;
	for (j=0; j<objCheckBoxes.length; j++) {
		if (objCheckBoxes[j].checked) {
			intChecked++;
		}
	}

	if (intChecked > 1) {
		objParentForm.submit_cover.disabled = true;
		objParentForm.submit_edit.disabled = true;
	} else {
		objParentForm.elements["submit_cover"].disabled = false;
		objParentForm.elements["submit_edit"].disabled = false;
	}

}

function photoRollOver(objPhoto) {
	objPhoto.style.border = "1px solid #000000;";
}

function photoRollOut(objPhoto) {
	objPhoto.style.border = "none;";
}

function doConfirm(msg) {
	return confirm(msg);
}

function setAction(theForm, theAction, theWarning, theItem) {

	var theFrame = $("photos");
	if (theFrame) {
		var theDoc = theFrame.contentWindow.document;
		var objCheckBoxes = theDoc.forms[theForm].elements["selected_rows[]"];
		var aryValues = new Array(0);
		if (objCheckBoxes.length) {  
			for (k=0; k<objCheckBoxes.length; k++) {
				if (objCheckBoxes[k].checked) {
					aryValues.push(objCheckBoxes[k].value);
				}
			}
		} else {
			aryValues.push(objCheckBoxes.value);
		}
		document.forms[theForm].elements["selected_rows"].value = aryValues.join(',');
	} else {
		var theDoc = document;
	}	

	if (theItem) {
		document.forms[theForm].submit_item.value=theItem;
	}
	if (theAction) {
		document.forms[theForm].submit_action.value=theAction;
	}

	submitWarn(theForm, theWarning);

}

function submitWarn(theForm,theWarning) {
	if (theWarning) {
		if (doConfirm(theWarning)) {
			document.forms[theForm].submit();
		}
	} else {
		document.forms[theForm].submit();
	}
}

/***********/
/* Updated */
/***********/

function selectPhoto(account_id, photo_id, photo_name, orient, uid) {
	
	var w = (orient) ? 140 : 105;
	var h = (orient) ? 105 : 140;
	
	p = window.parent.document;
	p["selected_image" + uid].src = '/members/' + account_id + '/photos/small/' + photo_name;
	p["selected_image" + uid].width = w;
	p["selected_image" + uid].height = h;
	p.theForm["photo_id" + uid].value = photo_id;
	p.theForm["photo_name" + uid].value = photo_name;
	
	hideModal();
}

function removePhoto() {
	document.selected_image.width = 140;
	document.selected_image.height = 105;
	document.selected_image.src = '/templates/common/images/noimage.jpg';
	document.theForm.photo_id.value = null;
	document.theForm.photo_name.value = null;
}

function uploadFile(u, w, h, t, objForm) {
	var objFncOnLoad = {
		doSubmit: function() {
			objForm.submit();
		}
	};
	var objFncOnClose = {
		doStop: function() {
			if (document.all) {	
				document.execCommand('Stop');
			} else {
				window.stop();
			}
		}
	};
	showModal(u, w, h, t, objFncOnLoad.doSubmit, objFncOnClose.doStop);
}

function openLibrary(u, w, h, t) {
	showModal(u, w, h, t, null, null);	
}

function openHelp(u, w, h, t) {
	showModal(u, w, h, t, null, null);	
}

function insertNames(n) {
	if (n != '') {
		document.theForm.title.value = n;
	} else {
		alert("At least one baby profile needs to be created in order to automatically create your website title.");
	}
}

function toggleItems(show, hide) {
	$(show).style.display = "block";
	$(hide).style.display = "none";
}

