// JavaScript Document

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///														    	///
///	  Copyright 2006 Josh Schwartzman / The Design Site LLC		///
///	  		josh@thedesignsite.com								///
///			Some portions of this file by 						///
///			Max Masnick & Matthew Cieplak						///
///	  Created for Self Portrait (selfportrait.net)				///
///   Last Modified:  July 25, 2006								///
///																///
///																///
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////


function redirect(url){
	document.location.href = url;
}

function submitOnEnter(myfield, e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
	   {
	   myfield.form.submit();
	   return false;
	   }
	else
	   return true;	
}

	// changes the style, and selects the hidden input
function selectStyle(styleId){
	var styleDIV = document.getElementById(styleId);
	var styleInput = document.getElementById(styleId+'Input');
	if(styleDIV && styleInput){
		if(styleInput.value == 'selected'){ /// deselect
			styleDIV.setAttribute('class', 'style');
			styleDIV.setAttribute('className', 'style');
			styleInput.value = '';
		}else{ //select
			styleDIV.setAttribute('class', 'styleSelected');
			styleDIV.setAttribute('className', 'styleSelected');
			styleInput.value = 'selected';
		}
	}
}


function showDays(monthElementId, dayElementId){ // fill a select with the correct number of days for the selected month
	var monthSelect = document.getElementById(monthElementId);
	var daySelect = document.getElementById(dayElementId);
	if(monthSelect && daySelect){
		var month = monthSelect.selectedIndex;
		var selectOptions = '<option value=""></option>';
		if(month != ''){ // only add options if a month is selected
			var days = daysInMonth(month); // get array of days for this month
			for(i=1; i <= days.length; i++){
				selectOptions += '<option value='+i+'>'+i+'</option>';
			}
		}
		daySelect.innerHTML = selectOptions;
	}
}

	// return an array of the correct days for a month
function daysInMonth(month){
	var num_days;
	var day_array = new Array();
	// figure out how many days are in the month
	if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
		num_days = 31;
	}
	else if(month == 2){
		num_days = 28;	
	}
	else{ num_days = 30; }
	for(i=0; i < num_days; i++){ // make an array with the correct number of days
		day_array.push(i);	
	}
	return day_array;
}

	// redirect to a location with a get variable -- grab from a select option
	// getVarId - the id tag on the select box
function redirectSelectToGet(getVarId){
	var getVarSelect, getValue;
	getVarSelect = document.getElementById(getVarId);
	getValue = getVarSelect.options[getVarSelect.selectedIndex].value;
	document.location = getValue;
}

function hideAndShow(elementId, visibility){
	var element = document.getElementById(elementId);
	var elementStatus = document.getElementById(elementId+'_status');
	if(element && elementStatus && (visibility != elementStatus.value)){
		if(visibility == 'hidden' && elementStatus.value == 'visible'){
			new Effect.Fade(elementId, { duration: 0.5 });
			elementStatus.value = 'hidden';
		}
		else if(visibility == 'visible' && elementStatus.value == 'hidden'){
			new Effect.Appear(elementId, { duration: 0.5 });
			elementStatus.value = 'visible';
		}
	}
}

/*   MESSAGING   */

function areYouSureLink(linkID, spanID, path){ // activates an <A href> tag - that has an id elementId, with the path provided
	var alink = document.getElementById(linkID);
	var aspan = document.getElementById(spanID);
	if(alink && aspan){
		alink.innerHTML = "";
		aspan.innerHTML = "<strong style=\"color: red;\">are you sure?</strong> <a id=\""+linkID+"\" href=\""+path+"\">yes</a>/<a href=\"#\" onclick=\"notSureLink('"+spanID+"', '"+path+"'); return false;\">no</a>";
	}
}

function notSureLink(spanID, path)
{
	var aspan = document.getElementById(spanID);
	if(aspan){
		aspan.innerHTML = "<a id=\"deletelink\" href=\""+path+"\" onclick=\"areYouSureLink('deletelink', 'deletespan', '"+path+"'); return false;\">delete</a>";
	}
}


