
/**
 * NameLab namespace
 */
NameLab = {
/* In Win MSIE 6, we need to manually wire up the draggable objects' :hover
	effect, because they are not <a> tags.  Using conditional compilation
	keying off of the JScript version to detect this.
*/
/*@cc_on
/*@if (@_jscript_version < 5.7)
	needManualHover: true,
/*@end
	isIE: true,
@*/
	isSafari: false
};

NameLab.init = function() {
	var savedLogin = NameLab.getCookieValue( "loginemail" );
	if ( savedLogin && savedLogin.length > 0 ) {
		var el = document.getElementById( "loginemail" );
		if ( el ) {
			if ( el.value == "" ) {
				el.value = savedLogin;
			}
		}
	}
	
	isSafari = navigator.userAgent.indexOf("Safari") != -1;
	
	NameLab.wireInputInstructions( "searchinput", "Type first or last name here" );
	/*NameLab.wireInputInstructions( "loginemail", "email address" );
	NameLab.wireInputInstructions( "loginpswd", "password", true );*/
	
	var el = document.getElementById( "cover" );
	if ( el ) {
		el.onmouseover = function() { document.getElementById("cred_ex").style.display = "block"; }
		el.onmouseout = function() { document.getElementById("cred_ex").style.display = "none"; }
		document.getElementById("cred_ex").style.display = "none";
	}
	
	NameLab.preloadImages( '/img/namelab/submit_over.gif', '/img/namelab/logout_over.gif',
			'/img/namelab/pointer_rt_over.gif', '/img/namelab/pointer_lft_over.gif', '/img/namelab/close_over.gif',
			'/img/namelab/email_names_over.gif', '/img/namelab/more_over.gif' );
}


NameLab.wireEnterSubmit = function( id, formname ) {
	var el = document.getElementById( id );
	if ( el ) {
		el.parentFormName = formname;
		el.onkeypress = function( evt ) {
			if ( evt.keyCode == 13 ) {
				if ( document.forms[ this.parentFormName ].onsubmit() )
					document.forms[ this.parentFormName ].submit();
			}
			else
				return true;
		}
	}
}

NameLab.wireInputInstructions = function( id, instruction, isPassword ) {
	var el = document.getElementById( id );
	if ( el ) {
		el.isPassword = isPassword;
		el.instructionText = instruction;
		if ( el.value == "" ) {
			if ( el.isPassword ) {
				if ( NameLab.isIE )
					el.className = "instruct";
				else {
					el.type = 'text';
					el.value = instruction;
				}
			}
			else
				el.value = instruction;
			el.style.color = "#999999";
			el.showingInstruction = true;
		}
		else {
			if ( el.isPassword ) {
				if ( NameLab.isIE )
					el.className = "";
				else {
					el.type = 'password';
				}
			}
			el.style.color = "#444444";
			el.showingInstruction = false;
		}
		el.onfocus = function() {
			if ( this.showingInstruction ) {
				this.value = "";
				this.showingInstruction = false;
				this.style.color = "#444444";
				if ( this.isPassword ) {
					if ( NameLab.isIE )
						this.className = "dummy";
					else {
						this.type = 'password';
						if ( navigator.userAgent.indexOf("Safari") != -1 ) {
							/* changing the input type on Safari screws up the input focus... */
							this.focus();
							this.select();
						}
					}
				}
			}
		}
		el.onblur = function() {
			if ( this.value == "" ) {
				this.showingInstruction = true;
				this.style.color = "#999999";
				if ( this.isPassword ) {
					if ( NameLab.isIE )
						this.className = "instruct";
					else {
						this.type = 'text';
						this.value = this.instructionText;
					}
				}
				else
					this.value = this.instructionText;
			}
		}
	}
}


NameLab.preloadImages = function() {
	if ( document.images ) {
		if ( !NameLab.preload )
			NameLab.preload = new Array();
		var i, j = NameLab.preload.length, args = NameLab.preloadImages.arguments;
		for ( i = 0; i < args.length; i++ ) {
    		if ( args[i].indexOf("#") !=0 ) {
    			NameLab.preload[j] = new Image;
    			NameLab.preload[j++].src = args[i];
    		}
    	}
    }
}


NameLab.getCookieValue = function( cookieName ) {
	var c = "" + document.cookie;
	var ind = c.indexOf( cookieName + "=" );
	if ( ind == -1 || cookieName == "" )
		return "";
	var ind1 = c.indexOf( ';', ind );
	if ( ind1 == -1 )
		ind1 = c.length;
	return unescape( c.substring(ind + cookieName.length + 1, ind1) );
}

NameLab.getCookieValueMulti = function( cookieName, elemName ) {
	var isLogin = NameLab.getCookieValue( cookieName );
	var elem1 = isLogin.split('&');
	for(var i=0;i < elem1.length;i++) {
		var c = elem1[i];
		var n = c.split('=');
		if (n[0] == elemName) {
			return true;
			break;	
		}
	}
}


/**
 * nameSearch handlers for interior pages
 */
NameLab.doSearch = function() {
	if ( NameLab.checkSearch() )
		document.forms.search_f.submit();
}

NameLab.checkSearch = function() {
	var el = document.getElementById( "searchinput" );
	if ( el ) {
		if ( el.showingInstruction || el.value == "" ) {
			alert( "Please enter something to search for" );
			return false;
		}
	}
	return true;
}


/**
 * login handlers for all pages
 */
NameLab.doLogin = function() {
	if ( NameLab.checkLogin() )
		document.forms.login_f.submit();
	return false;
}

NameLab.checkLogin = function() {
	var el = document.getElementById( "loginemail" );
	if ( el ) {
		if ( el.showingInstruction || el.value == "" ) {
			alert( "Please enter your email address to login" );
			return false;
		}
	}
	var el = document.getElementById( "loginpswd" );
	if ( el ) {
		if ( el.showingInstruction || el.value == "" ) {
			alert( "Please enter your password to login" );
			return false;
		}
	}
	return true;
}


/**
 * logout handler for all pages
 */
NameLab.doLogout = function() {
	document.forms.logout_f.submit();
}


/**
 * logout handler for all pages
 */
NameLab.emailThis = function( url ) {
	var loggedin = NameLab.getCookieValueMulti( "FeRegU", "member_id" );
	var url_addr = location.hostname;
	if ( loggedin && loggedin == true ) {
		if ( url == undefined )
			url = 'http://'+window.location.hostname+'/';
		window.open( "/email?p=" + escape(url), "_blank", "width=600,height=600,scrollbars=yes" );
	}
	else {
		alert( "Please log in to email this page." );
	}
	return false;
}
//NameLab.emailThis(location.pathname);

/**
 * save a name handler for all pages
 */
NameLab.addToSaved = function( name ) {
	var loggedin = NameLab.getCookieValueMulti( "FeRegU", "member_id" );
	if ( loggedin && loggedin == true ) {
	  window.location = window.location.protocol+"//"+window.location.host.replace(/genealogy\./,"baby-names.")+"/savednames?save=" + escape(name);
	}
	else {
		alert( "Please log in to save a name." );
	}
	return false;
}


/**
 * save a name handler for all pages
 */
NameLab.doRename = function( name ) {
	if ( document.forms.attr_f ) {
		var n = 0;
		var str = "";
		while ( document.forms.attr_f["c"+n] ) {
			if ( document.forms.attr_f["c"+n].checked ) {
				if ( str.length > 0 )
					str += ",";
				str += document.forms.attr_f["c"+n].value;
			}
			n++;
		}
		
		if ( str.length > 0 ) {
			var r = '';
			if ( document.forms.attr_f.r[0].checked )
				r = document.forms.attr_f.r[0].value;
			else if ( document.forms.attr_f.r[1].checked )
				r = document.forms.attr_f.r[1].value;
				
			var url = "renamer?attr=" + str + "&r=" + r;
			window.location = url;
			//document.forms.attr_f.submit();
		}
		else {
			alert( "Check off some personality traits first." );
		}
	}
}


/**
 * logout handler for all pages
 */
NameLab.printThis = function() {
	window.print();
	return false;
}

/**
 * selects all saved names
 */
NameLab.selectAllSaved = function() {
	var n = 0;
	while ( document.forms.savednamelist_f["sn"+n] ) {
		document.forms.savednamelist_f["sn"+n].checked = true;
		n++;
	}
	return false;
}

/**
 * unselects all saved names
 */
NameLab.unselectAllSaved = function() {
	var n = 0;
	while ( document.forms.savednamelist_f["sn"+n] ) {
		document.forms.savednamelist_f["sn"+n].checked = false;
		n++;
	}
	return false;
}

/**
 * deletes all selected names
 */
NameLab.deleteSelected = function() {
	var ids = [];
	var n = 0;
	while ( document.forms.savednamelist_f["sn"+n] ) {
		if ( document.forms.savednamelist_f["sn"+n].checked )
			ids.push( document.forms.savednamelist_f["sn"+n].value );
		n++;
	}
	if ( ids.length > 0 ) {
		var msg = "Are you sure you want to delete the selected name";
		if ( ids.length != 1 )
			msg += "s";
		msg += "?";
		if ( confirm(msg) == 1 ) {
			document.forms.savednamelist_f.del.value = ids.join( "," );
			document.forms.savednamelist_f.submit();
		}
	}
	else {
		alert( "Please select the names you wish to delete by using the checkboxes in the Saved Names list." );
	}
	return false;
}

/**
 * emails selected names
 */
NameLab.emailSelected = function() {
	var ids = [];
	var n = 0;
	while ( document.forms.savednamelist_f["sn"+n] ) {
		if ( document.forms.savednamelist_f["sn"+n].checked )
			ids.push( document.forms.savednamelist_f["sn"+n].value );
		n++;
	}
	if ( ids.length > 0 ) {
		window.open( "/email?n=" + escape(ids.join(",")), "_blank", "width=600,height=600" );
	}
	else {
		alert( "Please select the names you wish to email by using the checkboxes in the Saved Names list." );
	}
	return false;
}

/**
 * shows / hides comment entry form
 */
NameLab.enterComment = function() {
	var loggedin = NameLab.getCookieValue( "loggedin" );
	if ( loggedin && loggedin == "true" ) {
		var el = document.getElementById( "commententry" );
		if ( el ) {
			if ( el.style.display == "block" )
				el.style.display = "none";
			else
				el.style.display = "block";
		}
		else {
			alert( "Please log in to enable comments." );
		}
	}
	else {
		alert( "Please log in to enable comments." );
	}
}

/**
 * submits comment
 */
NameLab.checkCommentEntry = function() {
	if ( document.forms.comment_f.cmnt.value == "" ) {
		alert( "Please enter a comment." );
		return false;
	}
	return true;
}

/**
 * submits comment
 */
NameLab.doSubmitComment = function() {
	if ( NameLab.checkCommentEntry() ) {
		document.forms.comment_f.submit();
	}
}

NameLab.showNameList = function() {
	if ( document.forms.list_f.f.value != "" ) {
		document.forms.list_f.submit();
	}
}

/**
 * checks the form for the name history page
 */
NameLab.checkSurname = function() {
	var el = document.getElementById( "surfield" );
	if ( el ) {
		if ( el.showingInstruction || el.value == "" ) {
			alert( "Please enter a name." );
			return false;
		}
	}
	return true;
}
